文章目录
- 任务9
- 【任务9.1.1】升级DataBase类为可序列化的类,以便在文件保存或网络中传递
- 【任务9.1.2】升级LogRec类为可序列化的类,以便在文件保存或网络中传递
- 【任务9.1.3】升级MatchedLogRec类为可序列化的类,以便在文件保存或网络中传递
- 【任务9.1.4】升级Transport类为可序列化的类,以便在文件保存或网络中传递
- 【任务9.1.5】升级MatchedTransport类为可序列化的类,以便在文件保存或网络中传递
- 【任务9.2】修改LogRecService类,在该类中增加三个方法,分别用于保存匹配的日志信息,读取匹配的日志信息和读取匹配日志信息并返回日志实体集合,实现匹配日志的保存和读取功能
- 【任务9.3】修改TransportService类,在该类中增加三个方法,分别用于保存匹配的物流信息,和读取匹配的物流信息,实现匹配物流信息的保存和读取功能.
- 【任务9.4】在com.qst.dms.dos下创建测试类FileDemo,测试匹配的日志、物流信息的保存和读写功能
- 【任务9.5】 以上的文件的保存,由于使用ObjectOutputStream,在多次进行打开—写入---关闭一个文件时,会出现异常,无法读取所有数据,可以利用下面的方法解决:
- 【任务9.6】 将数据保存功能,集成到MenuDriver,并进行测试。
任务9
【任务9.1.1】升级DataBase类为可序列化的类,以便在文件保存或网络中传递
程序设计
package com.qst.dms.entity;import java.io.Serializable;
import java.util.Date;public class DataBase implements Serializable {public static final int IN = 1;public static final int OUT = 0;// ID标识private int id;// 时间private Date time;// 地点private String address;// 状态private int type;// 状态常量public static final int GATHER = 1;//"采集"public static final int MATHCH = 2;//"匹配";public static final int RECORD = 3;//"记录";public static final int SEND = 4;//"发送";public static final int RECIVE = 5;//"接收";public static final int WRITE = 6;//"归档";public static final int SAVE = 7;//"保存";public static final String DataType[]=new String[]{null,"GATHER","MATHCH","RECORD","SEND","RECIVE","WRITE","SAVE"};public DataBase(int id, Date time, String address, int type) {this.id = id;this.time = time;this.address = address;this.type = type;}public DataBase() {}public int getId() {return id;}public void setId(int id) {this.id = id;}public Date getTime() {return time;}public void setTime(Date time) {this.time = time;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}public int getType() {return type;}public void setType(int type) {this.type = type;}@Overridepublic String toString() {return "\nid=" + id +"\ntime=" + time +"\naddress=" + address +"\ntype=" + DataType[type];}
}
【任务9.1.2】升级LogRec类为可序列化的类,以便在文件保存或网络中传递
程序设计
package com.qst.dms.entity;import java.io.Serializable;
import java.util.Date;public class LogRec extends DataBase implements Serializable {/*** 登录用户名*/private String user;/*** 登录用户主机IP地址*/private String ip;/*** 登录状态:登录、登出*/private int logType;/*** 登录常量LOG_IN、登出常量常量LOG_OUT*/public static final int LOG_IN=1;public static final int LOG_OUT=0;public static final String LogTypeArray[]=new String[]{"LogOut","LogIn"};public LogRec(int id, Date time, String address, int type, String user, String ip, int logType) {super(id, time, address, type);this.user = user;this.ip = ip;this.logType = logType;}public LogRec() {super();}public String getUser() {return user;}public void setUser(String user) {this.user = user;}public String getIp() {return ip;}public void setIp(String ip) {this.ip = ip;}public int getLogType() {return logType;}public void setLogType(int logType) {this.logType = logType;}@Overridepublic String toString() {return "LogRec: " +super.toString() +"\nuser=" + user +"\nip=" + ip +"\nlogType=" + LogTypeArray[logType] +"\n";}public char[] toArray() {String data = this.getId() + "," + this.getTime().getTime() + "," + this.getAddress() + "," + this.getType() + "," + this.getUser() + "," + this.getIp() + "," + LogTypeArray[this.getLogType()];return data.toCharArray();}
}
【任务9.1.3】升级MatchedLogRec类为可序列化的类,以便在文件保存或网络中传递
程序设计
package com.qst.dms.entity;import java.io.Serializable;
import java.util.Date;public class MatchedLogRec extends MatchedDataBase implements Serializable {private LogRec login;private LogRec logout;// user用户登录名public String getUser() {return login.getUser();}// 登入时刻public Date getLogInTime() {return login.getTime();}// 登出时刻public Date getLogoutTime() {return logout.getTime();}// 登入记录public LogRec getLogin() {return login;}// 登出记录public LogRec getLogout() {return logout;}public MatchedLogRec() {}public MatchedLogRec(LogRec login, LogRec logout) {this.login = login;this.logout = logout;}@Overridepublic String toString() {return login.toString() + " | " + logout.toString();}
}
【任务9.1.4】升级Transport类为可序列化的类,以便在文件保存或网络中传递
程序设计
package com.qst.dms.entity;import java.io.Serializable;
import java.util.Date;public class Transport extends DataBase implements Serializable {/*** 经手人*/private String handler;/*** 收货人*/private String reciver;/*** 物流状态*/private int transportType;/*** 物流状态常量:发货中, 送货中, 已签收*/public static final int SENDING = 1;// 发货中public static final int TRANSPORTING = 2;// 送货中public static final int RECEIVED = 3;// 已签收public static final String TransportArray[]=new String[]{null,"SENDDING","TRANSPORTING","RECIEVED"};public Transport(int id, Date time, String address, int type, String handler, String reciver, int transportType) {super(id, time, address, type);this.handler = handler;this.reciver = reciver;this.transportType = transportType;}public String getHandler() {return handler;}public void setHandler(String handler) {this.handler = handler;}public String getReciver() {return reciver;}public void setReciver(String reciver) {this.reciver = reciver;}public int getTransportType() {return transportType;}public void setTransportType(int transportType) {this.transportType = transportType;}@Overridepublic String toString() {return "Transport: " +super.toString() +"\nhandler=" + handler +"\nreciver=" + reciver +"\ntransportType=" + TransportArray[transportType] +"\n";}public char[] toArray() {String data = this.getId() + "," + this.getTime().getTime() + "," + this.getAddress() + "," + this.getType() + "," + this.getHandler() + "," + this.getReciver() + "," + TransportArray[this.getTransportType()];return data.toCharArray();}
}
【任务9.1.5】升级MatchedTransport类为可序列化的类,以便在文件保存或网络中传递
程序设计
package com.qst.dms.entity;import java.io.Serializable;public class MatchedTransport extends MatchedDataBase implements Serializable {private Transport send;private Transport trans;private Transport receive;public MatchedTransport(Transport send, Transport tran, Transport rec) {this.send=send;this.trans=tran;this.receive=rec;}public MatchedTransport() {}public Transport getSend() {return send;}public void setSend(Transport send) {this.send = send;}public Transport getTrans() {return trans;}public void setTrans(Transport trans) {this.trans = trans;}public Transport getReceive() {return receive;}public void setReceive(Transport receive) {this.receive = receive;}@Overridepublic String toString() {return send.toString() + " | " + trans.toString() + " | " + receive.toString();}
}
【任务9.2】修改LogRecService类,在该类中增加三个方法,分别用于保存匹配的日志信息,读取匹配的日志信息和读取匹配日志信息并返回日志实体集合,实现匹配日志的保存和读取功能
首先给类中,增加一个属性:
private static final String saveFile = "MatchedLogRec.dat";
//存储本地存储文件的文件名。// 匹配日志信息保存,参数是集合
public void saveMatchLogRec(List<MatchedLogRec> matchLogs) {
// 创建一个ObjectOutputStream对象输出流,并连接文件输出流
// 以可追加的方式创建文件输出流,数据保存到MatchedLogRec.dat文件中}// 读匹配日志信息保存,参数是集合
public List<MatchedLogRec> readMatchLogRec(){
List<MatchedLogRec> matchLogs = new ArrayList<>();
// 创建一个ObjectInputStream对象输入流,并连接文件输入流,读MatchedLogRec.dat文件中
//读取文件之前,首先判断文件是否存在,如果不存在,则创建一个空文件
//读取文件中的所有对象时,可以使用异常处理和EOFException异常处理读取结束//读取数据结束后,关闭文件流。
}// 读匹配日志信息保存,参数是集合
public List<LogRec> readLogRec (){
List<LogRec> matchLogs = new ArrayList<>();
// 创建一个ObjectInputStream对象输入流,并连接文件输入流,读MatchedLogRec.dat文件中
//读取文件之前,首先判断文件是否存在,如果不存在,则创建一个空文件
//读取文件中的所有对象时,可以使用异常处理和EOFException异常处理读取结束//读取数据结束后,关闭文件流。
}
程序设计
package com.qst.dms.service;import com.qst.dms.entity.LogRec;
import com.qst.dms.entity.MatchedLogRec;
import com.qst.dms.exception.DataAnalyseException;
import com.qst.dms.gather.LogRecAnalyse;
import com.qst.dms.util.AppendObjectOutputStream;import java.io.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;public class LogRecService {private static final String saveFile = "MatchedLogRec.dat";private Scanner scanner;public LogRecService() {scanner = new Scanner(System.in);}public LogRec inputLog() {int id, type, logType;Date nowDate;String address;String user;String ip;while (true) {try {System.out.println("请输入ID标识:");id = scanner.nextInt();nowDate = new Date();System.out.println("请输入地址:");address = scanner.next();type = LogRec.GATHER;System.out.println("请输入登录用户名:");user = scanner.next();System.out.println("请输入主机IP:");ip = scanner.next();System.out.println("请输入登录状态(1表示登录,0表示登出):");logType = scanner.nextInt();if (logType == 0 || logType == 1) {break;} else {throw new IllegalArgumentException("非法的登录状态");}} catch (Exception e) {System.out.println("输入错误,请重新输入");scanner.nextLine();}}return new LogRec(id, nowDate, address, type, user, ip, logType);}public void showLog(List<LogRec> logRecs) {System.out.println("日志信息:");for (LogRec logRec : logRecs) {System.out.println(logRec);}}// 匹配日志信息输出,参数是集合public void showMatchLog(List<MatchedLogRec> matchLogs) {System.out.println("匹配日志信息:");for (MatchedLogRec matchLog : matchLogs) {System.out.println(matchLog);}}// 保存public static void saveLogRec(List<LogRec> matchLogs) {try {AppendObjectOutputStream.setFile(new File(saveFile));File file = AppendObjectOutputStream.getFile();FileOutputStream fileOut = new FileOutputStream(file, true);AppendObjectOutputStream objOut = new AppendObjectOutputStream(file);for (LogRec matchLog : matchLogs) {objOut.writeObject(matchLog);}objOut.close();fileOut.close();System.out.println("匹配日志信息保存成功\n");} catch (IOException e) {System.out.println("保存匹配日志信息发生异常:" + e.getMessage()+"\n");}}//匹配public static List<MatchedLogRec> readMatchLogRec() {List<MatchedLogRec> matchedLogs = new ArrayList<>();List<LogRec> logs = new ArrayList<>();try {AppendObjectOutputStream.setFile(new File(saveFile));File file = AppendObjectOutputStream.getFile();if (!file.exists()) {file.createNewFile();}FileInputStream fileIn = new FileInputStream(file);// 创建一个ObjectInputStream对象输入流,并连接文件输入流ObjectInputStream objIn = new ObjectInputStream(fileIn);// 使用异常处理和EOFException异常处理读取结束try {while (true) {LogRec log = (LogRec) objIn.readObject();logs.add(log);}} catch (EOFException e) {// 读取结束,不做任何操作}// 创建日志数据分析对象LogRecAnalyse logAnalyse = new LogRecAnalyse(logs);// 日志数据过滤logAnalyse.doFilter();// 日志数据匹配分析try {List<MatchedLogRec> objs = logAnalyse.matchData(); // 进行数据匹配// 处理匹配的日志数据// 判断objs集合是否是配置日志集合if (objs instanceof List<?>) {// 将集合强制类型转换成配置日志集合matchedLogs = (List<MatchedLogRec>) objs;// 输出匹配的日志信息System.out.println("匹配日志信息如下:");for (MatchedLogRec log : matchedLogs) {if (log != null) {System.out.println(log);}}System.out.println("\n");}} catch (DataAnalyseException e) {System.out.println(e.getMessage());}objIn.close();fileIn.close();System.out.println("匹配日志信息读取完成\n");} catch (IOException | ClassNotFoundException e) {System.out.println("读取匹配日志信息发生异常:" + e.getMessage()+"\n");}return matchedLogs;}//显示public static List<LogRec> readLogRec() {List<LogRec> logs = new ArrayList<>();try {AppendObjectOutputStream.setFile(new File(saveFile));File file = AppendObjectOutputStream.getFile();if (!file.exists()) {file.createNewFile();}FileInputStream fileIn = new FileInputStream(file);// 创建一个ObjectInputStream对象输入流,并连接文件输入流ObjectInputStream objIn = new ObjectInputStream(fileIn);// 使用异常处理和EOFException异常处理读取结束try {while (true) {LogRec log = (LogRec) objIn.readObject();logs.add(log);}} catch (EOFException e) {// 读取结束,不做任何操作}// 输出的日志信息System.out.println("日志信息如下:");for (LogRec log : logs) {if (log != null) {System.out.println(log);}}objIn.close();fileIn.close();System.out.println("日志信息读取完成\n");} catch (IOException | ClassNotFoundException e) {System.out.println("读取日志信息发生异常:" + e.getMessage() +"\n");}return logs;}
}
【任务9.3】修改TransportService类,在该类中增加三个方法,分别用于保存匹配的物流信息,和读取匹配的物流信息,实现匹配物流信息的保存和读取功能.
// 存储物流数据的文件名
private static final String saveFile = "MatchedTransport.dat";
// 匹配物流信息保存,参数是集合
public void saveMatchedTransport(List<MatchedTransport> matchTrans) {
// 创建一个ObjectOutputStream对象输出流,并连接文件输出流
// 以可追加的方式创建文件输出流,数据保存到MatchedTransport.dat文件中}// 读匹配物流信息保存,参数是集合
public List<MatchedTransport> readMatchedTransport(){
List<MatchedTransport> matchTrans = new ArrayList<>();
// 创建一个ObjectInputStream对象输入流,并连接文件输入流,读MatchedTransport.dat文件中//读取文件之前,首先判断文件是否存在,如果不存在,则创建一个空文件
//读取文件中的所有对象时,可以使用异常处理和EOFException异常处理读取结束//读取数据结束后,关闭文件流。
}// 读匹配物流信息保存,参数是集合
public List<Transport> readTransport(){
List<Transport> matchTrans = new ArrayList<>();
// 创建一个ObjectInputStream对象输入流,并连接文件输入流,读MatchedTransport.dat文件中//读取文件之前,首先判断文件是否存在,如果不存在,则创建一个空文件
//读取文件中的所有对象时,可以使用异常处理和EOFException异常处理读取结束//读取数据结束后,关闭文件流。
}
程序设计
package com.qst.dms.service;import com.qst.dms.entity.MatchedTransport;
import com.qst.dms.entity.Transport;
import com.qst.dms.exception.DataAnalyseException;
import com.qst.dms.gather.TransportAnalyse;
import com.qst.dms.util.AppendObjectOutputStream;import java.io.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;public class TransportService {private static final String saveFile = "MatchedTransport.dat";private Scanner scanner;public TransportService() {scanner = new Scanner(System.in);}public Transport inputTransport() {int transportType;int id, type;Date nowDate;String address, handler, receiver;while (true) {try {System.out.println("请输入ID标识:");id = scanner.nextInt();nowDate = new Date();System.out.println("请输入地址:");address = scanner.next();type = Transport.GATHER;System.out.println("请输入货物经手人:");handler = scanner.next();System.out.println("请输入收货人:");receiver = scanner.next();System.out.println("请输入物流状态(1表示发货中,2表示送货中,3表示已签收):");transportType = scanner.nextInt();if (transportType == 1 || transportType == 2 || transportType == 3) {break;} else {throw new IllegalArgumentException("非法的物流状态");}} catch (Exception e) {System.out.println("输入错误,请重新输入");scanner.nextLine();}}return new Transport(id, nowDate, address, type, handler, receiver, transportType);}public void showTransport(List<Transport> transports) {System.out.println("物流信息:");for (Transport transport : transports) {System.out.println(transport);}}// 匹配物流信息输出,参数是集合public void showMatchTransport(List<MatchedTransport> matchTrans) {System.out.println("匹配物流信息:");for (MatchedTransport matchTran : matchTrans) {System.out.println(matchTran);}}// 保存public static void saveTransport(List<Transport> transports) {try {AppendObjectOutputStream.setFile(new File(saveFile));File file = AppendObjectOutputStream.getFile();FileOutputStream fileOut = new FileOutputStream(file, true);AppendObjectOutputStream objOut = new AppendObjectOutputStream(file);for (Transport transport : transports) {objOut.writeObject(transport);}objOut.close();fileOut.close();System.out.println("匹配物流信息保存成功\n");} catch (IOException e) {System.out.println("保存匹配物流信息发生异常:" + e.getMessage()+"\n");}}//匹配public static List<MatchedTransport> readMatchTransport() {List<MatchedTransport> matchedTransports = new ArrayList<>();List<Transport> transports = new ArrayList<>();try {AppendObjectOutputStream.setFile(new File(saveFile));File file = AppendObjectOutputStream.getFile();if (!file.exists()) {file.createNewFile();}FileInputStream fileIn = new FileInputStream(file);// 创建一个ObjectInputStream对象输入流,并连接文件输入流ObjectInputStream objIn = new ObjectInputStream(fileIn);// 创建物流数据分析对象TransportAnalyse ta = new TransportAnalyse(transports);// 物流数据过滤ta.doFilter();try {// 物流数据分析List<MatchedTransport> objs = ta.matchData();// 判断objs集合是否是匹配物流集合if (objs instanceof List<?>) {// 将集合强制类型转换成匹配物流集合matchedTransports = (List<MatchedTransport>) objs;// 输出匹配的物流信息System.out.println("匹配物流信息如下:");for (MatchedTransport transport : matchedTransports) {if (transport != null) {System.out.println(transport);}}System.out.println("\n");}} catch (DataAnalyseException e) {System.out.println(e.getMessage());}objIn.close();fileIn.close();System.out.println("匹配物流信息读取完成\n");} catch (IOException e) {System.out.println("读取匹配物流信息发生异常:" + e.getMessage() +"\n");}return matchedTransports;}//显示public static List<Transport> readTransport() {List<Transport> transports = new ArrayList<>();try {AppendObjectOutputStream.setFile(new File(saveFile));File file = AppendObjectOutputStream.getFile();if (!file.exists()) {file.createNewFile();}FileInputStream fileIn = new FileInputStream(file);// 创建一个ObjectInputStream对象输入流,并连接文件输入流ObjectInputStream objIn = new ObjectInputStream(fileIn);// 使用异常处理和EOFException异常处理读取结束try {while (true) {Transport transport = (Transport) objIn.readObject();transports.add(transport);}} catch (EOFException e) {// 读取结束,不做任何操作}objIn.close();fileIn.close();// 输出的物流信息System.out.println("物流信息如下:");for (Transport transport : transports) {if (transport != null) {System.out.println(transport);}}System.out.println("物流信息读取完成\n");} catch (IOException | ClassNotFoundException e) {System.out.println("读取物流信息发生异常:" + e.getMessage() +"\n");}return transports;}
}
【任务9.4】在com.qst.dms.dos下创建测试类FileDemo,测试匹配的日志、物流信息的保存和读写功能
测试数据如下:
new LogRec(1001, new Date(), "青島",DataBase.GATHER, "zhangsan", "192.168.1.1", 1),
new LogRec(1002, new Date(), "青島", DataBase.GATHER, "zhangsan", "192.168.1.1", 0)));
new LogRec(1003, new Date(), "北京",DataBase.GATHER, "lisi", "192.168.1.6", 1),
new LogRec(1004, new Date(), "北京", DataBase.GATHER, "lisi", "192.168.1.6", 0)));
new LogRec(1005, new Date(), "济南",DataBase.GATHER, "wangwu", "192.168.1.89", 1),
new LogRec(1006, new Date(), "济南", DataBase.GATHER, "wangwu", "192.168.1.89", 0)));new Transport(2001, new Date(), "青島",DataBase.GATHER,"zhangsan","zhaokel",1),
new Transport(2002, new Date(), "北京",DataBase.GATHER,"lisi","zhaokel",2),
new Transport(2003, new Date(), "北京",DataBase.GATHER,"wangwu","zhaokel",3)));
new Transport(2004, new Date(), "青島",DataBase.GATHER,"maliu","zhaokel",1),
new Transport(2005, new Date(), "北京",DataBase.GATHER,"sunqi","zhaokel",2),
new Transport(2006, new Date(), "北京",DataBase.GATHER,"fengba","zhaokel",3)))
程序设计
package com.qst.dms.dos;import com.qst.dms.entity.*;
import com.qst.dms.service.LogRecService;
import com.qst.dms.service.TransportService;import java.util.*;public class FileDemo {public static void main(String[] args) {List<LogRec> logs = new ArrayList<>();List<Transport> transports = new ArrayList<>();//日志信息logs.add(new LogRec(1001, new Date(), "青島", DataBase.GATHER, "zhangsan", "192.168.1.1", 1));logs.add(new LogRec(1002, new Date(), "青島", DataBase.GATHER, "zhangsan", "192.168.1.1", 0));logs.add(new LogRec(1003, new Date(), "北京", DataBase.GATHER, "lisi", "192.168.1.6", 1));logs.add(new LogRec(1004, new Date(), "北京", DataBase.GATHER, "lisi", "192.168.1.6", 0));logs.add(new LogRec(1005, new Date(), "济南", DataBase.GATHER, "wangwu", "192.168.1.89", 1));logs.add(new LogRec(1006, new Date(), "济南", DataBase.GATHER, "wangwu", "192.168.1.89", 0));//物流信息transports.add(new Transport(2001, new Date(), "青島",DataBase.GATHER,"zhangsan","zhaokel",1));transports.add(new Transport(2002, new Date(), "北京",DataBase.GATHER,"lisi","zhaokel",2));transports.add(new Transport(2003, new Date(), "北京",DataBase.GATHER,"wangwu","zhaokel",3));transports.add(new Transport(2004, new Date(), "青島",DataBase.GATHER,"maliu","zhaokel",1));transports.add(new Transport(2005, new Date(), "北京",DataBase.GATHER,"sunqi","zhaokel",2));transports.add(new Transport(2006, new Date(), "北京",DataBase.GATHER,"fengba","zhaokel",3));//日志测试LogRecService.saveLogRec(logs);LogRecService.readMatchLogRec();LogRecService.readLogRec();//物流测试TransportService.saveTransport(transports);TransportService.readMatchTransport();TransportService.readTransport();}
}
【任务9.5】 以上的文件的保存,由于使用ObjectOutputStream,在多次进行打开—写入—关闭一个文件时,会出现异常,无法读取所有数据,可以利用下面的方法解决:
下面类主要解决使用ObjectOutputStream进行多次写入数据时,流信息重复写入的问题。
使用AppendObjectOutputStream类,修改上面的文件保存方法
创建了AppendObjectOutputStream的流操作对象收,必修执行
设置数据文件,否则在处理流信息写入时会出现错误,无法完成正确的数据存储功能。
程序设计
package com.qst.dms.util;import java.io.*;public class AppendObjectOutputStream extends ObjectOutputStream {private static File file = null;public static File getFile(){return file;}public static void setFile(File file){AppendObjectOutputStream.file = file;}public AppendObjectOutputStream() throws IOException,SecurityException{super();}public AppendObjectOutputStream(File file) throws IOException,SecurityException{super(new FileOutputStream(file,true));}public void writeStreamHeader() throws IOException{if(file==null||(file.exists()&&file.length()==0)){super.writeStreamHeader();}else{this.reset();}}}
【任务9.6】 将数据保存功能,集成到MenuDriver,并进行测试。
程序设计
package com.qst.dms.dos;import com.qst.dms.entity.LogRec;
import com.qst.dms.entity.MatchedLogRec;
import com.qst.dms.entity.MatchedTransport;
import com.qst.dms.entity.Transport;
import com.qst.dms.exception.DataAnalyseException;
import com.qst.dms.gather.LogRecAnalyse;
import com.qst.dms.gather.TransportAnalyse;
import com.qst.dms.service.LogRecService;
import com.qst.dms.service.TransportService;import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;public class MenuDriver {public static void main(String[] args) {// 建立一个从键盘接收数据的扫描器Scanner scanner = new Scanner(System.in);// 创建一个泛型ArrayList集合存储日志数据List<LogRec> logRecList = new ArrayList<>();// 创建一个泛型ArrrayList集合存储物流数据List<Transport> transportList = new ArrayList<>();// 创建一个日志业务类LogRecService logService = new LogRecService();// 创建一个物流业务类TransportService tranService = new TransportService();// 日志数据匹配集合List<MatchedLogRec> matchedLogs = null;// 物流数据匹配集合List<MatchedTransport> matchedTrans = null;// 运行菜单选择while (true) {System.out.println("请选择操作:");System.out.println("1. 采集数据");System.out.println("2. 数据匹配");System.out.println("3. 显示数据");System.out.println("4. 退出");// 接收键盘输入的选项try {int choice = scanner.nextInt();int type;switch (choice) {case 1:System.out.println("请输入采集数据类型:1.日志 2.物流");// 接收键盘输入的选项type = scanner.nextInt();if (type == 1) {System.out.println("正在采集日志数据,请输入正确信息,确保数据的正常采集!");// 采集日志数据LogRec log = logService.inputLog();// 将采集的日志数据添加到logRecList集合中logRecList.add(log);LogRecService.saveLogRec(logRecList);} else if (type == 2) {System.out.println("正在采集物流数据,请输入正确信息,确保数据的正常采集!");// 采集物流数据Transport tran = tranService.inputTransport();// 将采集的物流数据添加到transportList集合中transportList.add(tran);TransportService.saveTransport(transportList);}break;case 2:System.out.println("请输入匹配数据类型:1.日志 2.物流");// 接收键盘输入的选项type = scanner.nextInt();if (type == 1) {System.out.println("匹配日志数据如下:");// 数据匹配LogRecService.readMatchLogRec();} else if (type == 2) {System.out.println("匹配物流数据如下:");// 数据匹配TransportService.readMatchTransport();}break;case 3:System.out.println("请输入显示原始数据类型:1.日志 2.物流");// 接收键盘输入的选项type = scanner.nextInt();if (type == 1) {// 显示日志数据LogRecService.readLogRec();} else if (type == 2) {// 显示物流数据TransportService.readTransport();}break;case 4:System.out.println("谢谢使用!");return;default:System.out.println("无效选项,请重新选择!");}} catch (Exception e) {System.out.println("非法输入,请重新输入!");scanner.nextLine();}}}
}
测试