最近去深交所进行了一次面试,结果还有机试题,感觉还有点技术含量的,所以回来把自己实现的代码逻辑整理到博客上来
机试题1:现在本地C盘中存有股票数据的文件stock_data.txt文件(我把次文件改放在D盘中的mysql5.6文件夹中),有一系列股价在前 日期在后的原始数据如下(数据是自己编的)
3.79 2018-10-03
3.86 2018-10-04
3.65 2018-10-02
4.22 2018-10-01
3.72 2018-10-05
5.25 2018-10-10
4.75 2018-10-09
4.15 2018-10-08
3.45 2018-10-06
3.00 2018-10-07
现要求你实现去读该文件中的数据,并按日期前后顺序重新排列好并打印出来,效果如下所示
2018-10-01 4.22
2018-10-02 3.65
2018-10-03 3.79
2018-10-04 3.86
2018-10-05 3.72
2018-10-06 3.45
2018-10-07 3.0
2018-10-08 4.15
2018-10-09 4.75
2018-10-10 5.25
机试题2:在第1题实现股票价格按日期升序排列后的基础上找出其中股票价格处于单调递增区间的项目,并一数组的形式将其打印出来。如2018-10-02~2018-10-04日的股价处理单调递增区间,打印出来的格式如下
[2018-10-02 3.65,2018-10-03 3.79,2018-10-04 3.86]
本人解决这两道机试题时主要用到了java.io包和java.util包下的File、FileReadder、BufferedReader、Date和List、Map等工具类
采用IntelliJ IDEA开发工具新建一个java project, 工程结构如下
贴上代码如下
测试类及实体类代码
package com.company;
import com.company.com.company.model.StockItemTO;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.io.*;
public class Main {
private static List<StockItemTO> stockItemList = new ArrayList<>();
private static List<String> datePriceList = new ArrayList<>();
private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
public static void main(String[] args) {
readStockFileAndSort("D:\\mysql5.6\\stock_data.txt");
// selectIncreaseItems(stockItemList);
}
/**
* 实现读取本地文件D:\mysql5.6\stock_data.txt中的股价数据并实现按日期排序
* @param filePath
*/
public static void readStockFileAndSort(String filePath){
File file = new File(filePath);
try{
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String str = null;
StockItemTO stockTO = null;
String[] datePriceArrr = null;
while((str=bufferedReader.readLine())!=null){
datePriceArrr = str.split(" ");
stockTO = new StockItemTO();
stockTO.setPrice(Float.parseFloat(datePriceArrr[0]));
stockTO.setDate(parseDate(datePriceArrr[1]));
stockItemList.add(stockTO);
}
Collections.sort(stockItemList, new Comparator<StockItemTO>() {
@Override
public int compare(StockItemTO o1, StockItemTO o2) {
return o1.getDate().compareTo(o2.getDate());
}
});
String itemStr = null;
for(StockItemTO stockItemTO:stockItemList){
itemStr = parseDateStr(stockItemTO.getDate())+" "+String.valueOf(stockItemTO.getPrice());
datePriceList.add(itemStr);
System.out.println(itemStr);
}
}catch(FileNotFoundException e1){
e1.printStackTrace();
}catch(IOException e2){
e2.printStackTrace();
}
}
/**
* 找出股票上涨区间的数据,并按日期 股价的格式(如[2018-10-02 3.65,2018-10-03 3.79,2018-10-04 3.86])
* 打印出来
* @param sortedStockItems
*/
public static void selectIncreaseItems(List<StockItemTO> sortedStockItems){
//是否需要new 一个List标识
boolean needNewFlag = false;
int count = 0;
boolean hasFirstNew = false;
Map<String,List<StockItemTO>> stockItemsMap = new HashMap<String,List<StockItemTO>>();
List<StockItemTO> increList = new ArrayList<>();
StockItemTO compareTO = sortedStockItems.get(0);
for(int i=1;i<sortedStockItems.size();i++){
if(i==1 && sortedStockItems.get(i).getPrice()>compareTO.getPrice()){
increList.add(sortedStockItems.get(0));
increList.add(sortedStockItems.get(1));
hasFirstNew = true;
}else if(i>1 && i!=sortedStockItems.size()-1 && sortedStockItems.get(i).getPrice()>compareTO.getPrice()){
if(!hasFirstNew){
increList.add(sortedStockItems.get(i-1));
hasFirstNew = true;
}
if(!needNewFlag){
increList.add(sortedStockItems.get(i));
}else{
stockItemsMap.put(String.valueOf(count),increList);
increList = new ArrayList<>();
increList.add(sortedStockItems.get(i-1));
increList.add(sortedStockItems.get(i));
}
}else if(i>1 && i!=sortedStockItems.size()-1 && sortedStockItems.get(i).getPrice()<compareTO.getPrice()){
//遇到连续递减区间时,只需在第一次开始递减时将increList放到stockItemsList中,并设置needNewFlag = true
if(compareTO.getPrice()>sortedStockItems.get(i-2).getPrice()){
stockItemsMap.put(String.valueOf(count),increList);
needNewFlag = true;
count ++;
}
}else if(i==sortedStockItems.size()-1){
if(sortedStockItems.get(i).getPrice()>compareTO.getPrice()){
increList.add(sortedStockItems.get(i));
stockItemsMap.put(String.valueOf(count),increList);
}else{
stockItemsMap.put(String.valueOf(count),increList);
}
}
compareTO = (StockItemTO) sortedStockItems.get(i);
}
if(!stockItemsMap.isEmpty()){
List<List<String>> stockStrList = new ArrayList<List<String>>();
List<String> itemStrList = null;
List<StockItemTO> increItemList = null;
StockItemTO increItemTO = null;
for(Map.Entry<String,List<StockItemTO>> entry:stockItemsMap.entrySet()){
itemStrList = new ArrayList<>();
increItemList = entry.getValue();
itemStrList = new ArrayList<>();
for(int k=0;k<increItemList.size();k++){
increItemTO = increItemList.get(k);
itemStrList.add(parseDateStr(increItemTO.getDate())+" "+increItemTO.getPrice());
}
stockStrList.add(itemStrList);
}
for(int i=0;i<stockStrList.size();i++){
System.out.println(stockStrList.get(i).toString());
}
}
}
public static Date parseDate(String dateStr){
Date date = null;
try{
date = sdf.parse(dateStr);
}catch (ParseException e){
e.printStackTrace();
}
return date;
}
public static String parseDateStr(Date date){
return sdf.format(date);
}
}
package com.company.com.company.model;
import java.util.Date;
public class StockItemTO {
private Date date;
private float price;
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
}
3. 解决机试题1时在Main函数中调用静态方法
readStockFileAndSort("D:\\mysql5.6\\stock_data.txt");
运行后控制台打印内容如下:
打印内容表明代码实现了机试题1中要求的功能
4. 解决机试题2时屏蔽
readStockFileAndSort(String filePath){}静态函数中打印信息的代码,第51行:System.out.println(itemStr);
同时调用静态方法readStockFileAndSort("D:\\mysql5.6\\stock_data.txt");和
selectIncreaseItems(stockItemList);
运行后控制台打印内容如下,表明代码实现了机试题2要求的功能