最近有空,整理并完善刚进公司时的面试题:
1、读取文件,把每行中交易成功的记录(最后是002001AAAAAAA的)读出。
2、按照机构号排序,机构号相同的按照柜员号排序。
3、把读出的符合条件排序后的记录输入result.txt文件中。
txt文档名为tranflow.txt 内容如下:
机构号,柜员号,柜员流水号,交易码,交易名称,账号,交易金额,成功标志
110101,112129,A07003201406110646442915987201,100003,对私活期存款-有卡折,6228851029519393,700000.00,002001AAAAAAA
110101,112137,B07003201406141009472916581401,100004,对私活期取款,402230080000002131,11.00,002001VCR0007
020501,021547,A07003201407310921112914445801,110006,对公活期销户,0205010120010019890,9994.40,002001ACR0103
110101,112137,B07003201406141130332916643101,160001,移动金融对私客户注册,6400050002452,20.00,0060051
020501,021570,A07003201407301045282913884701,110003,对公活期现金存入,0205010120010019890,4900.00,002001ACR0904
110101,112129,B07003201406111002522916014201,100104,定期储蓄到期销户(卡、折),402230080323867442,141023.24,002001AAAAAAA
150101,153672,B07003201406170406452916147801,720033,对私基金产品赎回,6228671137889078,50000.00,021001AAAAAAA
150101,153857,B07003201406170522452916219101,720033,对私基金产品赎回,6228671137889029,120000.00,0210011337
020501,021515,B07003201406270142552916784701,230001,受理票据贴现,0205010115700010365,30000.00,011001AAAAAAA
150101,153672,B07003201406170456542916138201,720033,对私基金产品赎回,402230080274465675,20000.00,021001AAAAAAA
020501,021515,B07003201406270144432916785201,230001,受理票据贴现,0205010120010019817,200000.00,011001FFFFFFF
110101,112129,B07003201406111009482916018501,100104,定期储蓄到期销户(卡、折),402230080323867442,140741.48,002001AAAAAAA
020501,021549,A07003201407230356052913910701,010001,现金入库,0205011341660299991,50000.00,002001AAAAAAA
020501,021547,A07003201407310923432914450501,110006,对公活期销户,0205010120010019890,9994.40,002001AAAAAAA
针对第一题,代码比较简单,当时的思路是:利用IO读取文件,一次读取一行,根据','拆分,判断最后一个字符串是否和002001AAAAAAA一致即可,再看第二第三题,似乎都比较简单,题目意思就是把机构号和柜员号进行拼接,排序即可,实现起来也比较简单,再检查交易流水号符不符合需求,即可实现第三题。
看起来,我们似乎需要写三个方法,每个都去读文件,然后对字符串进行相应处理即可,但是,问题来了,怎么凸显面试者的代码能力?
所以,我们可以提取一个方法,专门负责读取txt文件,返回一个String数组的集合,随后分别对该集合进行处理,全部代码如下:
package com.test;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class FileOper {
/**
* 读取文件
* @param fileName 文件名
**/
public static List<String[]> readTxt(String fileName){
FileReader fr = null;
BufferedReader br = null;
List<String[]> result = new ArrayList<String[]>();
try{
File file = new File(fileName);
if(file.exists()){
fr = new FileReader(new File(fileName));
InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "UTF-8");
br = new BufferedReader(isr);
int index = 0;
while(true){
//读取一行数据,读不到时退出循环
String read = br.readLine();
if(read == null){
break;
}
//跳过第一行
if(index == 0){
index = 1;
continue;
}
//数据拆分
String[] str = read.split(",");
result.add(str);
}
}
}catch(IOException e){
e.printStackTrace();
}finally{
if(br != null){
try{
br.close();
}catch(IOException e){
e.printStackTrace();
}
}
if(fr != null){
try{
fr.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
System.out.println("读取数据文件完成");
return result;
}
// 过滤不合规格的流水
public static Map<String,String[]> JiGouHao(List<String[]> str){
Map<String,String[]> result = new HashMap<String,String[]>();
//数据不合规格或非成功流水时读取下一条
Iterator it = str.iterator();
while(it.hasNext()){
String[] o =(String[]) it.next();
if(o.length == 8 && o[7].trim().endsWith("AAAAAAA")){
result.put(o[0], o);
}else{
continue;
}
}
return result;
}
/**
* 进行升序排序,将柜员号与流水号进行拼接然后排序
* @param list
* @return
*/
public static List<String[]> sort(List<String[]> list){
List<String[]> result = new ArrayList<String[]>();
//排序方法
String[] sortArray = new String[list.size()];
Map<String,String[]> map = new HashMap<String,String[]>();
for(int i = 0;i < list.size();i++){
String[] data = list.get(i);
String key = data[1] + data[2];
map.put(key, data);
sortArray[i] = key;
}
Arrays.sort(sortArray);
for(int i = 0 ;i < sortArray.length;i++){
result.add(map.get(sortArray[i]));
}
return result;
}
public static void main(String[] agrs){
//单元测试
List<String[]> str = readTxt("E:\\tts9Eclipse\\workspace\\TestWork\\src\\tranflow.txt");
Map<String,String[]> map = JiGouHao(str);
// 遍历key
for (String key : map.keySet()) {
System.out.println("Key = " + key);
}
//遍历map中的值
for (String[] value : map.values()) {
System.out.println("Value = " + Arrays.toString(value));
}
System.out.println("第一题OK");
List<String[]> sac = sort(str);
Iterator it = sac.iterator();
while(it.hasNext()){
String[] o = (String[]) it.next();
System.out.println(Arrays.toString(o));
}
System.out.println("第二题OK");
Map<String,String[]> mmp = JiGouHao(sac);
// 遍历key
for (String key : mmp.keySet()) {
System.out.println("Key = " + key);
}
//遍历map中的值
for (String[] value : mmp.values()) {
System.out.println("Value = " + Arrays.toString(value));
}
System.out.println("第三题OK");
}
}