标签:add over view 序列 反序列化 tput while 代码块 shc
1.实体类:express
1 package Express_IO.ExpressJiHe; 2 3 import java.io.Serializable; 4 import java.util.Objects; 5 6 public class Express implements Serializable { 7 /** 8 * position:位置 9 * code:取件码 10 * company:公司 11 * number:单号 12 */ 13 private String number; 14 private int position; 15 private String code; 16 private String company; 17 18 public Express() { 19 } 20 21 public Express(int position, String code, String company) { 22 this.position = position; 23 this.code = code; 24 this.company = company; 25 } 26 27 @Override 28 public String toString() { 29 return "快递单号为:" + number + 30 ", 快递公司为:" + company + 31 " , 快递位置在:" + position + 32 "柜 , 取件码为:" + code + 33 ‘。‘; 34 } 35 36 @Override 37 public boolean equals(Object o) { 38 if (this == o) { 39 return true; 40 } 41 if (o == null || getClass() != o.getClass()) { 42 return false; 43 } 44 Express express = (Express) o; 45 return Objects.equals(number, express.number); 46 } 47 48 @Override 49 public int hashCode() { 50 return Objects.hash(number); 51 } 52 53 public String getNumber() { 54 return number; 55 } 56 57 public void setNumber(String number) { 58 this.number = number; 59 } 60 61 public int getPosition() { 62 return position; 63 } 64 65 public void setPosition(int position) { 66 this.position = position; 67 } 68 69 public String getCode() { 70 return code; 71 } 72 73 public void setCode(String code) { 74 this.code = code; 75 } 76 77 public String getCompany() { 78 return company; 79 } 80 81 public void setCompany(String company) { 82 this.company = company; 83 } 84 }
2.视图层:
1 package Express_IO.ExpressJiHe; 2 3 import java.util.HashMap; 4 import java.util.Map; 5 import java.util.Scanner; 6 7 /** 8 * view 视图层 9 * 10 * @author sunzihan 11 */ 12 public class View { 13 private Scanner input = new Scanner(System.in); 14 15 /** 16 * 欢迎 17 */ 18 public void welCome() { 19 System.out.println("欢迎使用!"); 20 } 21 22 /** 23 * 结束 24 */ 25 public void bye() { 26 System.out.println("感谢使用!"); 27 } 28 29 /** 30 * 开始菜单 31 */ 32 public int menu() { 33 System.out.println("请按照提示完成操作:"); 34 System.out.println("请输入您的身份:1-快递员 2-用户 0-退出"); 35 String s = input.nextLine(); 36 int number = -1; 37 try { 38 number = Integer.parseInt(s); 39 } catch (NumberFormatException e) { 40 41 } 42 if (number < 0 || number > 2) { 43 System.out.println("您的输入有误,请重新输入!"); 44 return menu(); 45 } 46 return number; 47 } 48 49 /** 50 * 快递员菜单 51 * 52 * @return 53 */ 54 public int courierMenu() { 55 System.out.println("请选择你的功能"); 56 System.out.println("1-存快递"); 57 System.out.println("2-修改快递"); 58 System.out.println("3-删除快递"); 59 System.out.println("4-查询所有快递"); 60 System.out.println("0-返回上一级"); 61 String s = input.nextLine(); 62 int number = -1; 63 try { 64 number = Integer.parseInt(s); 65 } catch (NumberFormatException e) { 66 67 } 68 if (number < 0 || number > 4) { 69 System.out.println("您的输入有误,请重新输入!"); 70 return courierMenu(); 71 } 72 return number; 73 } 74 75 /** 76 * 用户界面 77 * 78 * @return 79 */ 80 public String userMenu() { 81 System.out.println("请按照提示完成操作:"); 82 System.out.println("请输入6位取件码:"); 83 String s = input.nextLine(); 84 int number = -1; 85 try { 86 number = Integer.parseInt(s); 87 } catch (NumberFormatException e) { 88 89 } 90 if (number < 100000 || number > 999999) { 91 System.out.println("您的输入有误,请重新输入!"); 92 return userMenu(); 93 } 94 return s; 95 } 96 97 /** 98 * 添加快递单号 99 */ 100 public String addNumber() { 101 System.out.println("请按照提示完成操作:"); 102 System.out.println("请输入快递单号:"); 103 String number = input.nextLine(); 104 return number; 105 } 106 107 108 /** 109 * 添加快递其他信息 110 */ 111 public Express addElse() { 112 System.out.println("请输入快递公司:"); 113 String company = input.nextLine(); 114 Express express = new Express(); 115 express.setCompany(company); 116 return express; 117 } 118 119 /** 120 * 操作快递单号 121 */ 122 public String findNumber() { 123 System.out.println("请按照提示完成操作:"); 124 System.out.println("请输入要操作的快递单号:"); 125 String number = input.nextLine(); 126 127 return number; 128 } 129 130 /** 131 * 修改快递信息 132 */ 133 public Express update() { 134 System.out.println("请输入新的快递单号:"); 135 String number = input.nextLine(); 136 System.out.println("请输入新的快递公司:"); 137 String company = input.nextLine(); 138 Express express = new Express(); 139 express.setNumber(number); 140 express.setCompany(company); 141 return express; 142 } 143 144 /** 145 * 删除快递信息 146 */ 147 public int delete() { 148 System.out.println("是否要删除此快递:1-是 2-否"); 149 String s = input.nextLine(); 150 int num = -1; 151 try { 152 num = Integer.parseInt(s); 153 } catch (NumberFormatException e) { 154 155 } 156 if (num < 1 || num > 2) { 157 System.out.println("您的输入有误,请重新输入!"); 158 return delete(); 159 } 160 return num; 161 } 162 163 /** 164 * 打印所有信息 165 */ 166 public void printAll(HashMap<String, Express> map) { 167 if (map.size() != 0) { 168 for (Map.Entry<String, Express> me : map.entrySet()) { 169 System.out.println(me.getValue()); 170 } 171 } else { 172 System.out.println("快递柜无快递!!!"); 173 } 174 175 } 176 }
3.数据操作层:
1 package Express_IO.ExpressJiHe; 2 3 import Library_IO.Library.IOUtil; 4 5 import java.io.File; 6 import java.io.IOException; 7 import java.util.ArrayList; 8 import java.util.HashMap; 9 import java.util.Random; 10 11 /** 12 * @author sunzihan 13 */ 14 @SuppressWarnings("all") 15 public class ExpressDao { 16 static Random random = new Random(); 17 18 static HashMap<String, Express> map = new HashMap<>(); 19 static HashMap<String, String> map1 = new HashMap<>(); 20 21 private static final String FILE_NAME = "express.txt"; 22 23 static{ 24 System.out.println("静态代码块执行了"); 25 File file = new File(FILE_NAME); 26 if(!file.exists()){ 27 try { 28 if(file.createNewFile()){ 29 System.out.println("这是第一次创建文件,创建成功"); 30 31 } 32 } catch (IOException e) { 33 e.printStackTrace(); 34 } 35 }else{ 36 System.out.println("初始化数据执行了"); 37 //初始化数据,从文件中读取数据 38 try { 39 Object obj= null; 40 try { 41 obj = IOUtil.readFile("express.txt"); 42 if(obj!=null && obj instanceof HashMap){ 43 map = (HashMap<String, Express>) obj; 44 } 45 } catch (IOException e) { 46 System.out.println(e.getMessage()); 47 } 48 } catch (ClassNotFoundException e) { 49 System.out.println(e.getMessage()); 50 51 } 52 } 53 } 54 55 56 57 /** 58 * 快递录入 59 */ 60 public static boolean add(String number, Express express) throws IOException { 61 if (map.containsKey(number)) { 62 return false; 63 } 64 //添加单号信息到Express 65 express.setNumber(number); 66 //添加随机6为取件码 67 String code = codeRandom(); 68 express.setCode(code); 69 //添加快递柜位置信息 70 map.put(number, express); 71 map1.put(code, number); 72 int position = positionRandom(number); 73 express.setPosition(position); 74 Express_IO.ExpressJiHe.IOUtil.writeFile(map,"express.txt"); 75 return true; 76 } 77 78 /** 79 * 通过单号查询快递是否存在 80 * @param number:单号 81 */ 82 public static boolean find(String number) { 83 //根据单号判断单号关键字是否存在 84 if (map.containsKey(number)) { 85 return true; 86 } 87 return false; 88 } 89 /** 90 * 通过取件码查询快递是否存在 91 * @param code:取件码 92 */ 93 public static boolean findCode(String code) { 94 //根据取件码判断取件码关键字是否存在 95 if (map1.containsKey(code)) { 96 return true; 97 } 98 return false; 99 } 100 /** 101 * 修改快递信息 102 * number为单号 103 */ 104 public static boolean update(String number, Express newExpress) throws IOException { 105 if (find(number)) { 106 Express oldExpress = map.get(number); 107 newExpress.setCode(oldExpress.getCode()); 108 newExpress.setPosition(oldExpress.getPosition()); 109 map.remove(oldExpress.getNumber()); 110 map.put(newExpress.getNumber(), newExpress); 111 Express_IO.ExpressJiHe.IOUtil.writeFile(map,"express.txt"); 112 return true; 113 } 114 return false; 115 } 116 /** 117 * 删除快递信息 118 */ 119 public static boolean delete(String nummer) throws IOException { 120 if (find(nummer)) { 121 map.remove(nummer); 122 Express_IO.ExpressJiHe.IOUtil.writeFile(map,"express.txt"); 123 return true; 124 } 125 return false; 126 } 127 /** 128 * 删除快递信息 129 */ 130 public static boolean deletecode(String code) throws IOException { 131 if (findCode(code)) { 132 map.remove(map1.get(code)); 133 map1.remove(code); 134 Express_IO.ExpressJiHe.IOUtil.writeFile(map,"express.txt"); 135 return true; 136 } 137 return false; 138 } 139 /** 140 * 生成随机取件码 141 */ 142 public static String codeRandom() { 143 while (true) { 144 int code1 = random.nextInt(900000) + 100000; 145 String code = Integer.toString(code1); 146 if (!map1.containsKey(code)) { 147 return code; 148 } 149 } 150 } 151 /** 152 * 生成随机柜子位置 153 */ 154 public static int positionRandom(String number) throws IOException { 155 Express express = map.get(number); 156 while (true) { 157 int position = random.nextInt(200); 158 if (express.getPosition() != position) { 159 return position; 160 } 161 } 162 } 163 /** 164 * 打印 165 */ 166 public static HashMap<String, Express> print() { 167 return map; 168 } 169 170 }
4.序列化和反序列化的工具类:
1 package Express_IO.ExpressJiHe; 2 3 import java.io.*; 4 5 /** 6 * 文件的读取类:工具类中的方法都采用静态的方法 7 */ 8 public class IOUtil { 9 10 /** 11 * 从指定的文件中去读取文件 12 * @param fileName 13 * @return 14 */ 15 public static Object readFile(String fileName) throws IOException, ClassNotFoundException { 16 //创建输入流 17 FileInputStream fileInputStream = new FileInputStream(fileName); 18 //创建反序列化对象 19 ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream); 20 //读数据 21 Object o = objectInputStream.readObject(); 22 return o; 23 } 24 25 public static void writeFile(Object obj,String fileName) throws IOException { 26 //创建文件输出流对象 27 FileOutputStream fileOutputStream = new FileOutputStream(fileName); 28 //创建文件序列化对象 29 ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream); 30 //将内容写入文件 31 objectOutputStream.writeObject(obj); 32 } 33 } 34 35 /** 36 * 我只是对将对象写入文件和从文件读出来的步骤说明: 37 * 1.写一个写一个文件写入和读出的工具类 38 * 2.对于要写入文件的实体类,因该让他实现serializable接口 39 * 3.操作数据操作类 40 * 1)为文件取一个最终的名字 private static final String FILE_NAME = "express.txt"; 41 * 2)在无参构造方法中初始化数据,就是从文件中读取数据 42 * 3)为了保证我们的文件存在,我们在数据操作类写一个静态代码块,在代码块中创建文件,以及写一些判断语句 43 * 4)对于集合中的增删改方法,我们都在后面添加写入文件的操作 44 * 5) 45 */
5.测试类:
1 package Express_IO.ExpressJiHe; 2 3 import java.io.IOException; 4 import java.util.HashMap; 5 6 @SuppressWarnings("all") 7 public class Main { 8 9 static View view = new View(); 10 static ExpressDao dao = new ExpressDao(); 11 12 public static void main(String[] args) throws IOException { 13 14 15 //欢迎 16 view.welCome(); 17 18 while (true) { 19 int menu = view.menu(); 20 switch (menu) { 21 case 1: 22 courier(); 23 break; 24 case 2: 25 userOprate(); 26 break; 27 case 0: 28 view.bye(); 29 return; 30 } 31 } 32 } 33 34 public static void userOprate() throws IOException { 35 String code = view.userMenu(); 36 boolean b = ExpressDao.findCode(code); 37 if (b) { 38 ExpressDao.deletecode(code); 39 System.out.println("取件成功"); 40 } else { 41 System.out.println("取件码不存在,请重新输入"); 42 43 } 44 45 } 46 47 48 /** 49 * Courier页面 50 */ 51 public static void courier() throws IOException { 52 int num = 1; 53 while (num != 0) { 54 int courierMenu = view.courierMenu(); 55 switch (courierMenu) { 56 case 1: 57 addOperate(); 58 break; 59 case 2: 60 updateOprate(); 61 break; 62 case 3: 63 deleteOprate(); 64 break; 65 case 4: 66 HashMap<String, Express> print = ExpressDao.print(); 67 view.printAll(print); 68 break; 69 case 0: 70 num = 0; 71 break; 72 73 74 } 75 } 76 77 } 78 79 80 /** 81 * 添加信息集成 82 */ 83 public static void addOperate() throws IOException { 84 String number = view.addNumber(); 85 Express express = view.addElse(); 86 boolean add = ExpressDao.add(number, express); 87 if (add) { 88 System.out.println("添加成功"); 89 } else { 90 System.out.println("添加失败,请从输入"); 91 } 92 } 93 94 /** 95 * 修改信息集成 96 */ 97 public static void updateOprate() throws IOException { 98 String number = view.findNumber(); 99 boolean b = ExpressDao.find(number); 100 if (b) { 101 Express newExpress = view.update(); 102 ExpressDao.update(number, newExpress); 103 System.out.println("修改成功"); 104 } else { 105 System.out.println("修改失败,请从输入"); 106 } 107 } 108 109 110 /** 111 * 删除信息集成 112 */ 113 public static void deleteOprate() throws IOException { 114 String number = view.findNumber(); 115 boolean b = ExpressDao.find(number); 116 if (b) { 117 int delete = view.delete(); 118 if (delete == 1) { 119 System.out.println("删除成功"); 120 ExpressDao.delete(number); 121 } else if (delete == 2) { 122 System.out.println("取消删除"); 123 return; 124 } 125 } else { 126 System.out.println("单号不存在,请从输入"); 127 } 128 } 129 130 }
总结:还是来引用老师的一句话:
标签:add over view 序列 反序列化 tput while 代码块 shc
原文地址:https://www.cnblogs.com/baiyangshu/p/14906633.html