3. 交易明细文本文件内容如下:(20分) 有一个TransRecord.txt文件,保存的是交易记录明细。一行是一条交易明细,每行分7列。 请编码,实现如下功能: 3.1、设计一个交易记录类TransRecord (字段数据类型自己决定)。(5分) 3.2、解析文件(IO流),将文件中数据,将每条交易明细封装为1个TransRecord对象。所有TransRecord对象,添加到一个集合中,并且打印到控制台;(就是将对象添加到集合并打印集合)(10分) 提示: 1.BufferedReader 方法readLine() 2.String类里面 split(“\\s+”) 可以分割数据 3.3、完成一个功能(方法):输入客户号,查询交易明细记录并打印出来,封装成一个方法,通过传入客户号作为参数(5分)java实现
时间: 2024-02-21 22:01:28 浏览: 162
3.1 交易记录类 TransRecord 的代码如下:
```java
public class TransRecord {
private String date;
private String time;
private String tradeType;
private String clientID;
private String tradeID;
private double tradeAmount;
private double balance;
// 构造器
public TransRecord(String date, String time, String tradeType, String clientID, String tradeID, double tradeAmount, double balance) {
this.date = date;
this.time = time;
this.tradeType = tradeType;
this.clientID = clientID;
this.tradeID = tradeID;
this.tradeAmount = tradeAmount;
this.balance = balance;
}
// getter 和 setter 方法
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getTradeType() {
return tradeType;
}
public void setTradeType(String tradeType) {
this.tradeType = tradeType;
}
public String getClientID() {
return clientID;
}
public void setClientID(String clientID) {
this.clientID = clientID;
}
public String getTradeID() {
return tradeID;
}
public void setTradeID(String tradeID) {
this.tradeID = tradeID;
}
public double getTradeAmount() {
return tradeAmount;
}
public void setTradeAmount(double tradeAmount) {
this.tradeAmount = tradeAmount;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
// toString 方法
@Override
public String toString() {
return "TransRecord{" +
"date='" + date + '\'' +
", time='" + time + '\'' +
", tradeType='" + tradeType + '\'' +
", clientID='" + clientID + '\'' +
", tradeID='" + tradeID + '\'' +
", tradeAmount=" + tradeAmount +
", balance=" + balance +
'}';
}
}
```
3.2 解析文件并将交易明细封装为 TransRecord 对象,添加到集合中,并打印到控制台的代码如下:
```java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class TransRecordParser {
public static void main(String[] args) {
List<TransRecord> transRecordList = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new FileReader("TransRecord.txt"))) {
String line;
while ((line = br.readLine()) != null) {
String[] fields = line.split("\\s+");
TransRecord transRecord = new TransRecord(
fields[0], fields[1], fields[2], fields[3], fields[4],
Double.parseDouble(fields[5]), Double.parseDouble(fields[6])
);
transRecordList.add(transRecord);
}
} catch (IOException e) {
e.printStackTrace();
}
// 打印交易明细集合
for (TransRecord transRecord : transRecordList) {
System.out.println(transRecord);
}
}
}
```
3.3 查询交易明细记录并打印出来的方法如下:
```java
public static void searchTransRecord(String clientID, List<TransRecord> transRecordList) {
for (TransRecord transRecord : transRecordList) {
if (transRecord.getClientID().equals(clientID)) {
System.out.println(transRecord);
}
}
}
```
其中 `clientID` 为要查询的客户号,`transRecordList` 为交易明细记录的集合。
阅读全文