任务7
【任务7.1】对MenuDriver类增加异常处理,以防止用户输入不法的信息
程序设计
package com.qst.dms.dos; import java.util.InputMismatchException; import java.util.Scanner; public class MenuDriver { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int choice; do { try { System.out.println("*************************"); System.out.println("* 1、数据采集 2、数据匹配 *"); System.out.println("* 3、数据保存 4、数据显示(匹配数据) *"); System.out.println("* 5、数据发送 6、数据显示(原始数据) *"); System.out.println("* 0、退出应用 *"); System.out.println("*************************"); System.out.print("请输入菜单项(0-6):"); choice = scanner.nextInt(); switch (choice) { case 1: System.out.println("数据采集"); break; case 2: System.out.println("数据匹配"); break; case 3: System.out.println("数据保存"); break; case 4: System.out.println("数据显示(匹配数据)"); break; case 5: System.out.println("数据发送"); break; case 6: System.out.println("数据显示(原始数据)"); break; case 0: System.out.println("退出应用"); break; default: System.out.println("请输入正确的菜单项(0-6)!"); break; } } catch (InputMismatchException e) { System.out.println("请输入正确的菜单项(0-6)!"); scanner.nextLine(); // 清空输入缓冲区 choice = -1; // 设置一个无效的菜单项,使循环继续 } } while (choice != 0); scanner.close(); } }
测试
【任务7.2.1】对LogRecService类增加异常处理,以防止用户输入不法的信息
程序设计
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() { 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(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); } } }
测试
【任务7.2.2】对TransportService类增加异常处理,以防止用户输入不法的信息
程序设计
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() { int transportType; int id,type; Date nowDate; String address,handler,reciver; 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("请输入收货人:"); reciver = 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, 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); } } }
测试
【任务7.3】自定义数据分析异常类DataAnalyseException,数据分析处理过程中抛出自定义异常
空构造方法
带参构造方法
程序设计
package com.qst.dms.exception; public class DataAnalyseException extends Exception { public DataAnalyseException() { super(); } public DataAnalyseException(String message) { super(message); } }
【任务7.4】对LogRecAnalyse类增加异常处理,没有匹配的日志数据时进行DataAnalyseException异常处理
程序设计
package com.qst.dms.gather; import com.qst.dms.entity.DataBase; import com.qst.dms.entity.LogRec; import com.qst.dms.entity.MatchedLogRec; import com.qst.dms.exception.DataAnalyseException; public class LogRecAnalyse extends DataFilter implements IDataAnalyse { // “登录”集合 private LogRec[] logIns; // “登出”集合 private LogRec[] logOuts; public LogRecAnalyse(LogRec[] datas) { super(datas); } // 实现DataFilter抽象类中的过滤抽象方法 @Override public void doFilter() { // 获取数据集合 LogRec[] datas = (LogRec[]) getDatas(); // 根据日志登录状态统计不同状态的日志个数 int numIn = 0; int numOut = 0; // 遍历统计 for (LogRec data : datas) { if (data.getLogType() == LogRec.LOG_IN) { numIn++; } else if (data.getLogType() == LogRec.LOG_OUT) { numOut++; } } // 创建登录、登出数组 logIns = new LogRec[numIn]; logOuts = new LogRec[numOut]; // 数组下标记录 int indexIn = 0; int indexOut = 0; // 遍历,对日志数据进行过滤,根据日志登录状态分别放在不同的数组中 for (LogRec data : datas) { if (data.getLogType() == LogRec.LOG_IN) { logIns[indexIn++] = data; } else if (data.getLogType() == LogRec.LOG_OUT) { logOuts[indexOut++] = data; } } } // 实现IDataAnalyse接口中数据分析方法 @Override public Object[] matchData() throws DataAnalyseException { if (logIns.length == 0 || logOuts.length == 0) { throw new DataAnalyseException("没有匹配的日志数据"); } // 创建日志匹配数组 MatchedLogRec[] matchLogs = new MatchedLogRec[logIns.length]; // 日志匹配数组下标记录 int index = 0; // 数据匹配分析 for (LogRec in : logIns) { for (LogRec out : logOuts) { if ((in.getUser().equals(out.getUser())) && (in.getIp().equals(out.getIp())) && out.getType() != DataBase.MATHCH) { // 修改in和out日志状态类型为“匹配” in.setType(DataBase.MATHCH); out.setType(DataBase.MATHCH); // 添加到匹配数组中 matchLogs[index++] = new MatchedLogRec(in, out); } } } if(index==0){ throw new DataAnalyseException("没有匹配的日志数据"); } return matchLogs; } }
测试
【任务7.5】对TransportAnalyse类增加异常处理,没有匹配的物流数据时进行DataAnalyseException异常处理
程序设计
package com.qst.dms.gather; // 导入需要的类 import com.qst.dms.entity.DataBase; import com.qst.dms.entity.MatchedTransport; import com.qst.dms.entity.Transport; import com.qst.dms.exception.DataAnalyseException; public class TransportAnalyse extends DataFilter implements IDataAnalyse { // 成员属性 private Transport[] transSends; private Transport[] transIngs; private Transport[] transRecs; // 构造方法 public TransportAnalyse(Transport[] trans) { super(trans); } // 实现DataFilter抽象类中的过滤抽象方法 @Override public void doFilter() { // 获取数据集合 Transport[] trans = (Transport[]) getDatas(); // 根据物流状态统计不同状态的物流个数 int numSend = 0; int numTran = 0; int numRec = 0; // 遍历统计 for (Transport tran : trans) { if (tran.getTransportType() == Transport.SENDING) { numSend++; } else if (tran.getTransportType() == Transport.TRANSPORTING) { numTran++; } else if (tran.getTransportType() == Transport.RECEIVED) { numRec++; } } // 创建不同状态的物流数组 transSends = new Transport[numSend]; transIngs = new Transport[numTran]; transRecs = new Transport[numRec]; // 数组下标记录 int indexSend = 0; int indexTran = 0; int indexRec = 0; // 遍历,对物流数据进行过滤,根据物流状态分别放在不同的数组中 for (Transport tran : trans) { if (tran.getTransportType() == Transport.SENDING) { transSends[indexSend++] = tran; } else if (tran.getTransportType() == Transport.TRANSPORTING) { transIngs[indexTran++] = tran; } else if (tran.getTransportType() == Transport.RECEIVED) { transRecs[indexRec++] = tran; } } } // 实现IDataAnalyse接口中数据分析方法 @Override public MatchedTransport[] matchData() throws DataAnalyseException { if (transSends.length == 0 || transIngs.length == 0 || transRecs.length==0) { throw new DataAnalyseException("没有匹配的物流数据"); } // 创建物流匹配数组 MatchedTransport[] matchTrans = new MatchedTransport[transSends.length]; // 日志匹配数组下标记录 int index = 0; // 数据匹配分析 for (Transport send : transSends) { for (Transport tran : transIngs) { for (Transport rec : transRecs) { if (send.getReciver().equals(tran.getReciver()) && send.getReciver().equals(rec.getReciver()) && tran.getType() != DataBase.MATHCH && rec.getType() != DataBase.MATHCH) { // 修改物流状态类型为“匹配” send.setType(DataBase.MATHCH); tran.setType(DataBase.MATHCH); rec.setType(DataBase.MATHCH); // 添加到匹配数组中 matchTrans[index++] = new MatchedTransport(send, tran, rec); } } } } if(index==0){ throw new DataAnalyseException("没有匹配的物流数据"); } return matchTrans; } }