util练习,处理文件,实现特定功能
还不知道如何处理流
交易明细文件内容如下例:
---------------------------------------------------------------------------
#客户号 姓名 所述机构号 性别 帐号 发生时间 发生额
000001|刘恭亮|0000|1|4155990188888888|20060720200005|300.00
000201|白晓龙|0002|1|4155990199999999|20060720200005|500.00
000101|黄戈华|0012|1|4155990100000000|20060720200005|1000.50
000101|张东|0012|1|4155990155555555|20060720200005|600.99
000301|关婧媛|0013|0|4155990111111111|20060722201005|5000.00
000001|刘恭亮|0000|1|4155990188888888|20060725200005|200.00
---------------------------------------------------------------------------
一行是一条交易明细,每行分6列,列间用|分隔。#为注释符号。
类TransRecord存储一条明细(金额字段数据类型定为BigDecimal)。
解析文件数据至TransRecord[]。
实现如下功能:
public class TransRecordManager{
/**
* 记录数组
*/
private TransRecord[] records;
/**
* 加载数据
* @param in - 数据流
* @return
* @throws - 解析过程中IO错误
*/
public void load(InputStream in) throws IOException;
/**
* 加载数据
* @param fileName - 包含记录数据的文件名
* @return
* @throws - 解析过程中IO错误
*/
public void load(String fileName) throws IOException;
/**
* 取所有记录
* @return 所有记录数组或null
*/
public TransRecord[] getAll();
/**
* 按客户号查询记录
* @param customerNumber - 客户号
* @return 符合条件的记录数组或null
*/
public TransRecord[] findByCustomerNumber(String customerNumber);
/**
* 按日期段查询记录
* @param start - 开始日期
* @param end - 结束日期
* @return 符合条件的记录数组或null
*/
public TransRecord[] findByDate(String start, String end);
/**
* 取得总金额
* @return 总金额
*/
public BigDecimal totalAmount();
/**
* 按金额排序
* @return 按金额升序排序的结果
*/
public TransRecord[] sortByAmount();
/**
* 打印
* @param out - 输出流
*/
public void print(OutputStream out);
}
将类中的TransRecord[]全部换成List再实现一次。
提示:排序、查询采用java.util.Arrays和java.util.Collections中的方法
1 package test.util; 2 3 import java.io.BufferedReader; 4 import java.io.File; 5 import java.io.FileNotFoundException; 6 import java.io.FileReader; 7 import java.io.IOException; 8 import java.io.InputStream; 9 import java.io.OutputStream; 10 import java.math.BigDecimal; 11 12 /* 13 #客户号 姓名 所属机构号 性别 帐号 发生时间 发生额 14 000001,刘恭亮,0000,1,4155990188888888,20060720200005,300.00 15 000201,白晓龙,0002,1,4155990199999999,20060720200005,500.00 16 000101,黄戈华,0012,1,4155990100000000,20060720200005,1000.50 17 000101,张 东,0012,1,4155990155555555,20060720200005,600.99 18 000301,关婧媛,0013,0,4155990111111111,20060722201005,5000.00 19 000001,刘恭亮,0000,1,4155990188888888,20060725200005,200.00 20 */ 21 class TransRecord { 22 23 private String customerNumber; 24 private String customerName; 25 private String jiGou; 26 private int sex; 27 private String accountNumber; 28 private String happenTime; 29 private BigDecimal amount; 30 private static final String[] SEX = {"女","男"}; 31 32 public TransRecord(String[] message,int n){ 33 if(7 == n){ 34 this.customerNumber = message[0]; 35 this.customerName = message[1]; 36 this.jiGou = message[2]; 37 this.sex = Integer.parseInt(message[3]); 38 this.accountNumber = message[4]; 39 this.happenTime = message[5]; 40 this.amount = new BigDecimal(message[6]); 41 } 42 else{ 43 System.out.println("对象创建失败!"); 44 System.exit(1);//发生异常,终止程序 45 } 46 } 47 48 public String getCustomerNumber(){ 49 return customerNumber; 50 } 51 52 public String getCustomerName(){ 53 return customerName; 54 } 55 56 public String getJiGou(){ 57 return jiGou; 58 } 59 60 public String getSex(){ 61 return SEX[sex]; 62 } 63 64 public String getAccountNumber(){ 65 return accountNumber; 66 } 67 68 public String getHappenTime(){ 69 return happenTime; 70 } 71 72 public BigDecimal getAmount(){ 73 return amount; 74 } 75 76 //重写toString方法 77 public String toString(){ 78 return ("客户号:"+getCustomerNumber()+" 姓名:"+getCustomerName() 79 +" 所属机构号:"+getJiGou()+" 性别:"+getSex() 80 +" 帐号:"+getAccountNumber()+" 发生时间:"+getHappenTime() 81 +" 账户金额:"+getAmount()); 82 } 83 } 84 85 public class TransRecordManager{ 86 public static final String FILENAME = System.getProperty("user.dir") 87 + "\\src\\test\\util\\data.txt"; 88 public static final int LINES_OF_FILENAME= 6; 89 90 /** 91 * 记录数组 92 */ 93 private TransRecord[] records = getAll(); 94 95 /** 96 * 加载数据 97 * @param in - 数据流 98 * @return 99 * @throws - 解析过程中IO错误 100 */ 101 public void load(InputStream in) throws IOException{ 102 103 } 104 105 /** 106 * 加载数据 107 * @param fileName - 包含记录数据的文件名 108 * @return 109 * @throws - 解析过程中IO错误 110 */ 111 public void load(String fileName) throws IOException{ 112 113 } 114 115 /** 116 * 取所有记录 117 * @return 所有记录数组或null 118 */ 119 public TransRecord[] getAll(){// 读取文本内容,并生成对象存放在list数组中 120 BufferedReader br = null; 121 int n = -2; 122 String str = new String(); 123 TransRecord[] list = new TransRecord[LINES_OF_FILENAME]; 124 try{ 125 br = new BufferedReader(new FileReader(new File(FILENAME))); 126 } 127 catch(FileNotFoundException e){ 128 e.printStackTrace(); 129 } 130 try{ 131 while((str=br.readLine())!= null){ 132 n++; 133 if(-1 == n) 134 continue; 135 String[] tempStr = str.split(","); 136 list[n]= new TransRecord(tempStr,tempStr.length); 137 } 138 return list; 139 } 140 catch(IOException e){ 141 e.printStackTrace(); 142 return null; 143 } 144 finally{ 145 try { 146 br.close(); 147 } 148 catch (IOException e) { 149 e.printStackTrace(); 150 } 151 } 152 } 153 154 /** 155 * 按客户号查询记录 156 * @param customerNumber - 客户号 157 * @return 符合条件的记录数组或null 158 */ 159 public TransRecord[] findByCustomerNumber(String customerNumber){ 160 TransRecord[] relist = records; 161 //用来保存符合条件的对象 162 TransRecord[] result = new TransRecord[LINES_OF_FILENAME]; 163 for(int i=0, j=0;i<relist.length;i++){ 164 if(relist[i].getCustomerNumber().equals(customerNumber)){ 165 result[j++] = relist[i]; 166 } 167 } 168 return result; 169 } 170 171 /** 172 * 按日期段查询记录 173 * @param start - 开始日期 174 * @param end - 结束日期 175 * @return 符合条件的记录数组或null 176 */ 177 public TransRecord[] findByDate(String start, String end){ 178 TransRecord[] temp = records; 179 TransRecord[] result = new TransRecord[LINES_OF_FILENAME]; 180 //把输入时间的格式转换为14位 181 for(int i = start.length(), j = end.length(); i < 14 && j < 14; i++, j++){ 182 start += "0"; 183 end += "0"; 184 } 185 //利用BigDecimal对象进行精确比较 186 BigDecimal bigStart = new BigDecimal(start); 187 BigDecimal bigEnd = new BigDecimal(end); 188 BigDecimal bigTemp = null; 189 for(int i = 0,j = 0; i < temp.length; i++){ 190 bigTemp = new BigDecimal(temp[i].getHappenTime()); 191 if ((bigTemp.compareTo(bigStart) > 0) && (bigTemp.compareTo(bigEnd) < 0)){ 192 result[j++] = temp[i]; 193 } 194 } 195 return result; 196 } 197 198 /** 199 * 取得总金额 200 * @return 总金额 201 */ 202 public BigDecimal totalAmount(){ 203 TransRecord[] temp = records; 204 BigDecimal total = new BigDecimal("0"); 205 for(int i = 0; i < temp.length; i++){ 206 total = total.add(temp[i].getAmount());//累加amount 207 } 208 return total; 209 } 210 211 /* 212 * 按金额排序 213 * @return 按金额升序排序的结果 214 */ 215 public TransRecord[] sortByAmount(){ 216 TransRecord[] temp = records;//读取数据 217 //利用选择排序 218 for(int i = 0; i < temp.length; i++){ 219 int k = i; 220 for(int j = i+1; j < temp.length; j++){ 221 if(temp[j].getAmount().compareTo(temp[i].getAmount()) <= 0) 222 k = j; 223 } 224 if(k != i){ 225 TransRecord result = temp[i]; 226 temp[i] = temp[k]; 227 temp[k] = result; 228 } 229 } 230 return temp; 231 } 232 233 /** 234 * 打印 235 * @param out - 输出流 236 */ 237 public void print(OutputStream out){ 238 239 } 240 241 public static void main(String[] args){ 242 TransRecordManager trandRecMan = new TransRecordManager(); 243 TransRecord[] findCustNum =trandRecMan.findByCustomerNumber("000001"); 244 TransRecord[] findDate =trandRecMan.findByDate("20060721","20060723"); 245 TransRecord[] sortAccount = trandRecMan.sortByAmount(); 246 247 System.out.println("总金额="+trandRecMan.totalAmount()); 248 249 System.out.println(); 250 System.out.println("按时间查找后的结果"); 251 for(int i = 0; i < findDate.length; i++){ 252 if(findDate[i] != null){ 253 System.out.println(findDate[i]); 254 } 255 } 256 257 System.out.println(); 258 System.out.println("按客户号查找后的结果"); 259 for(int i = 0; i < findCustNum.length; i++){ 260 if(findCustNum[i] != null){ 261 System.out.println(findCustNum[i]); 262 } 263 } 264 265 //打印出按照amount排序后的结果 266 System.out.println(); 267 System.out.println("按照账户金额升序排列后的结果"); 268 for(int i = 0; i < sortAccount.length; i++){ 269 System.out.println(sortAccount[i]); 270 } 271 } 272 }