任务5
【任务5.3】创建基础匹配实体类
在com.qst.dms.entity下创建MatchedDataBase.java
该类中暂时不用定义任何方法
程序设计
package com.qst.dms.entity; public class MatchedDataBase { }
【任务5.3.1】日志数据匹配类,对日志实体数据进行匹配,对同一个用户在同一个IP地址登录登出信息进行匹配。
在com.qst.dms.entity 下创建MatchedLogRec.java,继承MatchedDataBase类
功能要求:
- 成员属性及相关方法如下
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) { }
- 构造方法
toString(): login.toString() + " | " + logout.toString()
程序设计
package com.qst.dms.entity; import java.util.Date; public class MatchedLogRec extends MatchedDataBase { 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; } @Override public String toString() { return login.toString() + " | " + logout.toString(); } }
【任务5.3.2】修改LogRecService类,在该类中增加显示匹配日志信息的showMatchLog方法。
// 匹配日志信息输出 public void showMatchLog(MatchedLogRec... matchLogs) { …… }
程序设计
package com.qst.dms.service; import com.qst.dms.entity.LogRec; import com.qst.dms.entity.MatchedLogRec; import java.util.Date; import java.util.Scanner; public class LogRecService { private Scanner scanner; public LogRecService() { scanner = new Scanner(System.in); } public LogRec inputLog() { System.out.println("请输入ID标识:"); int id = scanner.nextInt(); Date nowDate = new Date(); System.out.println("请输入地址:"); String address = scanner.next(); int type = LogRec.GATHER; System.out.println("请输入登录用户名:"); String user = scanner.next(); System.out.println("请输入主机IP:"); String ip = scanner.next(); int logType; while (true) { try { 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(LogRec... logRecs) { System.out.println("日志信息:"); for (LogRec logRec : logRecs) { System.out.println(logRec); } } // 匹配日志信息输出 public void showMatchLog(MatchedLogRec... matchLogs) { System.out.println("匹配日志信息:"); for (MatchedLogRec matchLog : matchLogs) { System.out.println(matchLog); } } }
【任务5.4.1】物流数据匹配类,对物流实体数据进行匹配,对发货、送货、签收物流信息进行匹配。
在com.qst.dms.entity 下创建MatchedTransport.java,继承MatchedDataBase类
功能要求:
- 成员属性及相关方法如下
private Transport send; private Transport trans; private Transport receive; 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;
- 构造方法
toString(): send.toString()+"|"+trans.toString()+"|"+receive;
程序设计
package com.qst.dms.entity; public class MatchedTransport extends MatchedDataBase { 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; } @Override public String toString() { return send.toString() + " | " + trans.toString() + " | " + receive.toString(); } }
【任务5.4.2】修改TransportService类,在该类中增加显示匹配日志信息的showMatchTransport方法。
// 匹配的物流信息输出 public void showMatchTransport(MatchedTransport... matchTrans) {
程序设计
package com.qst.dms.service; import com.qst.dms.entity.MatchedLogRec; import com.qst.dms.entity.MatchedTransport; import com.qst.dms.entity.Transport; import java.util.Date; import java.util.Scanner; public class TransportService { private Scanner scanner; public TransportService() { scanner = new Scanner(System.in); } public Transport inputTransport() { System.out.println("请输入ID标识:"); int id = scanner.nextInt(); Date nowDate = new Date(); System.out.println("请输入地址:"); String address = scanner.next(); int type = Transport.GATHER; System.out.println("请输入货物经手人:"); String handler = scanner.next(); System.out.println("请输入收货人:"); String reciver = scanner.next(); int transportType; while (true) { try { 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, reciver, transportType); } public void showTransport(Transport... transports) { System.out.println("物流信息:"); for (Transport transport : transports) { System.out.println(transport); } } // 匹配物流信息输出 public void showMatchTransport(MatchedTransport... matchTrans) { System.out.println("匹配物流信息:"); for (MatchedTransport matchTran : matchTrans) { System.out.println(matchTran); } } }
【任务5.5】创建测试类,测试任务5.3中的程序,演示日志信息的匹配及打印输出
在com.qst.dms.dos 下创建MatchLogDemo.java
测试要求:
- LogRec类数据
id, time, address, type, user, ip,logType
133,2020-7-1,address1,3,lee,10.12.12.124, 1
129,2020-6-29,address1,3,lee,10.12.12.124, 0
127,2020-6-21,address1,3,lee,10.12.12.124, 1
125,2020-6-19,address1,3,lee,10.12.12.124, 0
123,2020-5-1,address1,3,lee,10.12.12.124, 1
- 通过LogRec类数据创建MatchedLogRec数据数组
- 打印输出MatchedLogRec数据
程序设计
package com.qst.dms.dos; import com.qst.dms.entity.LogRec; import com.qst.dms.entity.MatchedLogRec; import com.qst.dms.service.LogRecService; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class MatchLogDemo { public static void main(String[] args) { // 创建LogRec数据数组 LogRec[] logRecs = new LogRec[5]; try { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); logRecs[0] = new LogRec(133, dateFormat.parse("2020-07-01"), "address1", 3, "lee", "10.12.12.124", 1); logRecs[1] = new LogRec(129, dateFormat.parse("2020-06-29"), "address1", 3, "lee", "10.12.12.124", 0); logRecs[2] = new LogRec(127, dateFormat.parse("2020-06-21"), "address1", 3, "lee", "10.12.12.124", 1); logRecs[3] = new LogRec(125, dateFormat.parse("2020-06-19"), "address1", 3, "lee", "10.12.12.124", 0); logRecs[4] = new LogRec(123, dateFormat.parse("2020-05-01"), "address1", 3, "lee", "10.12.12.124", 1); } catch (ParseException e) { e.printStackTrace(); } // 创建MatchedLogRec数据数组 MatchedLogRec[] matchedLogRecs = new MatchedLogRec[2]; matchedLogRecs[0] = new MatchedLogRec(logRecs[0], logRecs[1]); matchedLogRecs[1] = new MatchedLogRec(logRecs[2], logRecs[3]); // 创建LogRecService对象并打印输出MatchedLogRec数据 LogRecService logRecService = new LogRecService(); logRecService.showMatchLog(matchedLogRecs); } }
测试
【任务5.6】创建测试类,测试任务5.4中的程序,演示物流信息的匹配及打印输出
在com.qst.dms.dos 下创建MatchTransportDemo.java
测试要求:
- Transport类数据数组
id, time, address, type,handler, reciver,transportType
233,2020-7-1,suzhou,3,lee,wang, 1
229,2020-6-29,hangzhou,2,lee,wang, 1
227,2020-6-21,nanjing,4,lee,sun, 2
225,2020-6-19,suzhou,5,lee,liu, 2
223,2020-5-1,nanjing,6,lee,zhang, 3
- 通过Transport类数据创建MatchedTransport数据数组
- 打印输出匹配的MatchedTransport数据
程序设计
package com.qst.dms.dos; import com.qst.dms.entity.MatchedTransport; import com.qst.dms.entity.Transport; import com.qst.dms.service.TransportService; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class MatchTransportDemo { public static void main(String[] args) { // 创建Transport数据数组 Transport[] transports = new Transport[5]; try { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); transports[0] = new Transport(233, dateFormat.parse("2020-07-01"), "suzhou", 3, "lee", "wang", 1); transports[1] = new Transport(229, dateFormat.parse("2020-06-29"), "hangzhou", 2, "lee", "wang", 1); transports[2] = new Transport(227, dateFormat.parse("2020-06-21"), "nanjing", 4, "lee", "sun", 2); transports[3] = new Transport(225, dateFormat.parse("2020-06-19"), "suzhou", 5, "lee", "liu", 2); transports[4] = new Transport(223, dateFormat.parse("2020-05-01"), "nanjing", 6, "lee", "zhang", 3); } catch (ParseException e) { e.printStackTrace(); } // 创建MatchedTransport数据数组 MatchedTransport[] matchedTransports = new MatchedTransport[2]; // 创建TransportService对象并打印输出匹配的MatchedTransport数据 TransportService transportService = new TransportService(); transportService.showMatchTransport(matchedTransports); } }
测试