标签:insert 菜单 nts 开始 rgba 程序 用户输入 png span
快递实体类:
1 package bean; 2 3 import java.io.Serializable; 4 5 /** 6 * 快递类 7 * 8 * @author Administrator 9 */ 10 public class Express implements Serializable { 11 private String order; 12 private String company; 13 private int code, target; 14 15 public Express() { 16 17 } 18 @Override 19 public String toString() { 20 return "快递单号:" + order + "\t快递公司:" + company + "\t取件码:" + code + "\t"; 21 } 22 23 24 public String getOrder() { 25 return order; 26 } 27 28 public void setOrder(String order) { 29 this.order = order; 30 } 31 32 public void setCompany(String company) { 33 this.company = company; 34 } 35 36 public int getCode() { 37 return code; 38 } 39 40 public void setCode(int code) { 41 this.code = code; 42 } 43 44 public int getTarget() { 45 return target; 46 } 47 48 public void setTarget(int target) { 49 this.target = target; 50 } 51 }
视图层:
1 package view; 2 3 import bean.Express; 4 5 import java.util.Scanner; 6 7 /** 8 * @author Administrator 9 */ 10 public class View { 11 private final Scanner scanner = new Scanner(System.in); 12 13 public void welcome() { 14 System.out.println("=====欢迎使用新职课快递柜====="); 15 } 16 17 public void exit() { 18 System.out.println("=====欢迎下次使用====="); 19 } 20 21 /** 22 * 选择身份界面 23 */ 24 public int menuMain() { 25 System.out.println("请输入您的身份:1-快递员,2-用户,0-退出程序"); 26 //全局使用nextLine(),不会因为输入产生冲突,且可以更好的接收各种类型的数据 27 String input = scanner.nextLine(); 28 int num = -1; 29 // 判断用户输入是否正确 30 try { 31 num = Integer.parseInt(input); 32 } catch (NumberFormatException e) { 33 System.out.println(e.getMessage()); 34 } 35 if (num < 0 || num > 2) { 36 System.out.println("输入有误,请重新输入"); 37 return menuMain(); 38 } 39 return num; 40 } 41 42 /** 43 * 快递员功能界面 44 */ 45 public int menuDeliveryMan() { 46 System.out.println("请选择操作:1-快递录入,2-快递删除,3-快递修改,4-查看所有快递,5-返回上级目录,0-退出"); 47 String input = scanner.nextLine(); 48 int num = -1; 49 // 判断用户输入是否正确 50 try { 51 num = Integer.parseInt(input); 52 } catch (NumberFormatException e) { 53 System.out.println(e.getMessage()); 54 } 55 if (num < 0 || num > 5) { 56 System.out.println("输入有误,请重新输入"); 57 return menuDeliveryMan(); 58 } 59 return num; 60 } 61 62 /** 63 * 快递员录入快递 64 * 65 * @return 快递单号及快递公司 66 */ 67 public Express insert() { 68 System.out.println("请输入快递单号:"); 69 String order = scanner.nextLine(); 70 System.out.println("请输入快递公司:"); 71 String company = scanner.nextLine(); 72 Express express = new Express(); 73 express.setOrder(order); 74 express.setCompany(company); 75 return express; 76 } 77 78 /** 79 * 用于查找快递单号 80 * 81 * @return 快递单号 82 */ 83 public String findByOrder() { 84 System.out.println("请输入要操作的快递单号:"); 85 return scanner.nextLine(); 86 } 87 88 /** 89 * 打印快递信息 90 */ 91 public void printExpress(Express express) { 92 System.out.println(express.toString()); 93 } 94 95 /** 96 * 修改快递信息 97 */ 98 public Express updateExpress() { 99 System.out.println("请输入新的快递单号:"); 100 String order = scanner.nextLine(); 101 System.out.println("请输入新的快递公司:"); 102 String company = scanner.nextLine(); 103 Express express = new Express(); 104 express.setOrder(order); 105 express.setCompany(company); 106 return express; 107 } 108 109 /** 110 * 询问是否删除 111 * 112 * @return 1确认删除,2取消并退出 113 */ 114 public int deleteExpress() { 115 System.out.println("是否确认删除:1-确认删除,其他-取消操作"); 116 String input = scanner.nextLine(); 117 int num = -1; 118 // 判断用户输入是否正确 119 try { 120 num = Integer.parseInt(input); 121 } catch (NumberFormatException e) { 122 System.out.println(e.getMessage()); 123 } 124 return num; 125 } 126 127 /** 128 * 用户菜单界面 129 */ 130 public int menuUser() { 131 System.out.println("请输入取件码:"); 132 String code = scanner.nextLine(); 133 int num = -1; 134 // 判断用户输入是否正确 135 try { 136 num = Integer.parseInt(code); 137 } catch (NumberFormatException e) { 138 System.out.println(e.getMessage()); 139 } 140 if (num < 100000 || num > 999999) { 141 System.out.println("输入有误,请重新输入"); 142 return menuUser(); 143 } 144 return num; 145 } 146 147 /** 148 * 提示快递已存在 149 */ 150 public void expressExist() { 151 System.out.println("此单号在快递柜中已存在,请检查是否正确"); 152 } 153 154 /** 155 * 打印空信息,提示快递不存在 156 */ 157 public void printNull() { 158 System.out.println("该快递不存在,请检查输入是否正确!"); 159 } 160 161 /** 162 * 打印操作成功信息 163 */ 164 public void printSuccess() { 165 System.out.println("操作成功!"); 166 } 167 168 /** 169 * 打印空取件码,提示取件码错误 170 */ 171 public void printCodeNull() { 172 System.out.println("取件码错误或快递已取出,请检查!"); 173 } 174 175 public void printFail() { 176 System.out.println("操作失败!"); 177 } 178 179 public void isNull() { 180 System.out.println("无快递信息!"); 181 } 182 }
数据操作层:
接口:
1 package dao; 2 3 import bean.Express; 4 5 import java.io.IOException; 6 import java.util.ArrayList; 7 8 /** 9 * @author Administrator 10 */ 11 public interface ExpressDao { 12 /** 13 *添加快递 14 * @param express 快递 15 * @return ee 16 */ 17 Express add(Express express); 18 19 /** 20 * 删除快递 21 * @param order 顺序 22 * @return ww 23 */ 24 boolean delete(String order); 25 26 /** 27 * 修改快递 28 * @param oldExp 开始存的快递单号 29 * @param newExp 后面改的快递单号 30 * @return rr 31 */ 32 Express update(Express oldExp, Express newExp); 33 34 /** 35 * 查询所有快递信息 36 * @return ee 37 */ 38 ArrayList<Express> getAll(); 39 40 /** 41 * 取快递 42 * @param code 取件码 43 * @return ee 44 */ 45 boolean getExpress(int code); 46 47 /** 48 * 数组大小 49 * @return ee 50 */ 51 int size(); 52 53 /** 54 *根据快递号查询快递信息 55 * @param order 顺序 56 * @return ee 57 */ 58 Express findByOrder(String order); 59 60 /** 61 * 根据取件查询快递信息 62 * @param order 顺序 63 * @return ee 64 */ 65 Express findByCode(int order); 66 67 /** 68 * 从文件中读取快递信息(反序列化) 69 */ 70 void readFromFile(); 71 72 /** 73 * 将信息写入文件中(序列化) 74 * @throws IOException io异常 75 */ 76 void writeToFile() throws IOException; 77 }
实现类:
1 package dao; 2 3 import bean.Express; 4 5 import java.io.*; 6 import java.util.ArrayList; 7 import java.util.Random; 8 9 /** 10 * @author Administrator 11 */ 12 public class ExpressDaoImpl implements ExpressDao { 13 private final File file = new File("Express.txt"); 14 private ArrayList<Express> list; 15 private final Random random = new Random(); 16 17 /** 18 * 最多存储数量 19 */ 20 private final int max = 100; 21 22 /** 23 * 反序列化获得快递柜中存放的对象HashMap<Integer, Express> data 24 */ 25 @Override 26 public void readFromFile() { 27 try (FileInputStream fis = new FileInputStream(file)) { 28 ObjectInputStream ois = new ObjectInputStream(fis); 29 // 反序列化读取对象 30 list = (ArrayList<Express>) ois.readObject(); 31 // 关闭输入流 32 ois.close(); 33 } catch (IOException | ClassNotFoundException e) { 34 // 打开文件异常时 将expresses初始为空 35 list = new ArrayList<>(); 36 } 37 } 38 39 @Override 40 public void writeToFile() throws IOException { 41 ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file)); 42 // 序列化对象 43 oos.writeObject(list); 44 oos.close(); 45 } 46 47 /** 48 * 随机生成6位数取件码 49 * 50 * @return 6位数取件码 51 */ 52 public int randomCode() { 53 while (true) { 54 int code = random.nextInt(900000) + 100000; 55 Express e = findByCode(code); 56 if (e == null) { 57 return code; 58 } 59 } 60 61 } 62 63 /** 64 * 初始化快递柜位置 65 */ 66 public int randomTarget() { 67 int target; 68 do { 69 target = random.nextInt(max) + 1; 70 } while (findByTarget(target) != null); 71 return target; 72 } 73 74 /** 75 * 判断快递柜是否占用 76 */ 77 private Express findByTarget(int target) { 78 for (Express express : list) { 79 if (express.getTarget() == target) { 80 return express; 81 } 82 } 83 return null; 84 } 85 86 /** 87 * 根据取件码查询快递 88 * 89 * @param code 取件码 90 * @return 查询的结果,没有则返回null 91 */ 92 @Override 93 public Express findByCode(int code) { 94 for (Express express : list) { 95 if (express.getCode() == code) { 96 return express; 97 } 98 } 99 return null; 100 } 101 102 /** 103 * 根据单号查询 104 */ 105 @Override 106 public Express findByOrder(String order) { 107 for (Express e : list) { 108 if (e.getOrder().equals(order)) { 109 return e; 110 } 111 } 112 return null; 113 } 114 115 @Override 116 public synchronized Express add(Express express) { 117 if (size() >= max) { 118 return null; 119 } else { 120 express.setCode(randomCode()); 121 express.setTarget(randomTarget()); 122 list.add(express); 123 return express; 124 } 125 } 126 127 @Override 128 public synchronized boolean delete(String order) { 129 Express byOrder = findByOrder(order); 130 if (byOrder == null) { 131 return false; 132 } 133 list.remove(byOrder); 134 return true; 135 } 136 137 @Override 138 public synchronized Express update(Express oldExp, Express newExp) { 139 Express byOrder = findByOrder(oldExp.getOrder()); 140 if (byOrder != null) { 141 list.remove(oldExp); 142 newExp.setCode(randomCode()); 143 newExp.setTarget(randomTarget()); 144 list.add(newExp); 145 return newExp; 146 } 147 return oldExp; 148 } 149 150 @Override 151 public synchronized ArrayList<Express> getAll() { 152 return list; 153 } 154 155 @Override 156 public synchronized boolean getExpress(int code) { 157 Express express = findByCode(code); 158 if (express != null) { 159 list.remove(express); 160 return true; 161 } 162 return false; 163 } 164 165 @Override 166 public int size() { 167 return list.size(); 168 } 169 }
服务端:
1 package server; 2 3 import bean.Express; 4 import dao.ExpressDao; 5 import dao.ExpressDaoImpl; 6 7 import java.io.*; 8 import java.net.ServerSocket; 9 import java.net.Socket; 10 import java.util.ArrayList; 11 12 /** 13 * @author Administrator 14 */ 15 public class Server { 16 private ServerSocket serverSocket; 17 private static final ExpressDao EXPRESS_DAO = new ExpressDaoImpl(); 18 private int numOfClient = 0; 19 20 public static void main(String[] args) { 21 Server service = new Server(); 22 service.start(); 23 } 24 25 public void start() { 26 try { 27 serverSocket = new ServerSocket(7063); 28 System.out.println("服务器已启动"); 29 EXPRESS_DAO.readFromFile(); 30 do { 31 Socket socket = serverSocket.accept(); 32 System.out.println("第" + (++numOfClient) + "个客户端连接了"); 33 new Thread(() -> { 34 try { 35 // 准备连接 进入主功能模块 36 receive(socket); 37 } catch (IOException e) { 38 e.printStackTrace(); 39 } 40 }).start(); 41 } while (true); 42 } catch (IOException e) { 43 e.printStackTrace(); 44 } finally { 45 try { 46 if (serverSocket != null) { 47 serverSocket.close(); 48 } 49 } catch (IOException e) { 50 e.printStackTrace(); 51 } 52 } 53 } 54 55 public void receive(Socket socket) throws IOException { 56 InputStream is; 57 OutputStream os; 58 ObjectInputStream ois; 59 ObjectOutputStream oos; 60 try { 61 is = socket.getInputStream(); 62 os = socket.getOutputStream(); 63 ois = new ObjectInputStream(is); 64 oos = new ObjectOutputStream(os); 65 o: 66 while (true) { 67 System.out.println("进入主菜单"); 68 switch (ois.readInt()) { 69 case 0: 70 System.out.println("客户端退出"); 71 EXPRESS_DAO.writeToFile(); 72 break o; 73 case 1: 74 deliveryManClient(ois, oos); 75 break; 76 case 2: 77 userClient(ois, oos); 78 break; 79 default: 80 break; 81 } 82 } 83 84 } catch (IOException | ClassNotFoundException e) { 85 e.printStackTrace(); 86 } 87 } 88 89 /** 90 * 管理员客户端 91 * 92 * @param oos 发送数据给服务区 93 * @param ois 从服务器接收数据 94 */ 95 private void deliveryManClient(ObjectInputStream ois, ObjectOutputStream oos) throws IOException, ClassNotFoundException { 96 System.out.println("进入快递员界面"); 97 switch (ois.readInt()) { 98 case 0: 99 EXPRESS_DAO.writeToFile(); 100 break; 101 case 1: 102 System.out.println("添加快递"); 103 addExpress(ois, oos); 104 break; 105 case 2: 106 System.out.println("删除快递"); 107 deleteExpress(ois, oos); 108 break; 109 case 3: 110 System.out.println("修改快递"); 111 updateExpress(ois, oos); 112 break; 113 case 4: 114 System.out.println("查看快递"); 115 seleteAllExpress(oos); 116 break; 117 default: 118 } 119 } 120 121 /** 122 * 用户 123 * 124 * @param oos 发送数据给服务器 125 * @param ois 从服务器接收数据 126 */ 127 private void userClient(ObjectInputStream ois, ObjectOutputStream oos) throws IOException, ClassNotFoundException { 128 System.out.println("用户取快递界面"); 129 // 根据客户端传过来的取件码 查找快递对象 130 Express e = EXPRESS_DAO.findByCode(ois.readInt()); 131 // 向客户端传送查找的对象 132 oos.writeObject(e); 133 oos.flush(); 134 if (e != null) { 135 //取出快递 136 EXPRESS_DAO.writeToFile(); 137 oos.writeBoolean(EXPRESS_DAO.getExpress(e.getCode())); 138 oos.flush(); 139 } 140 } 141 142 /** 143 * 存储快递 144 */ 145 private void addExpress(ObjectInputStream ois, ObjectOutputStream oos) throws IOException, ClassNotFoundException { 146 //1.从客户端接收快递数据 147 Express addExpress = (Express) ois.readObject(); 148 System.out.println(addExpress.toString()); 149 //2.根据快递单号判断对应快递是否已存在 150 Express byOrder = EXPRESS_DAO.findByOrder(addExpress.getOrder()); 151 //判断快递是否存在,不存在则添加快递,为空则代表不存在,添加快递。 152 if (byOrder == null) { 153 byOrder = EXPRESS_DAO.add(addExpress); 154 oos.writeObject(byOrder); 155 } else { 156 oos.writeObject(null); 157 } 158 oos.flush(); 159 } 160 161 /** 162 * 删除快递 163 */ 164 private void deleteExpress(ObjectInputStream ois, ObjectOutputStream oos) throws IOException, ClassNotFoundException { 165 //获取到客户端输入的单号 166 String deleteOrder = (String) ois.readObject(); 167 //判断是否存在 168 Express byOrder =EXPRESS_DAO.findByOrder(deleteOrder); 169 //发送快递信息至客户端 170 oos.writeObject(byOrder); 171 oos.flush(); 172 //如果快递存在 173 if (byOrder != null) { 174 //接收用户确认是否删除 175 if (ois.readInt() == 1) { 176 oos.writeBoolean(EXPRESS_DAO.delete(deleteOrder)); 177 oos.flush(); 178 } 179 } 180 181 } 182 183 /** 184 * 修改快递 185 */ 186 private void updateExpress(ObjectInputStream ois, ObjectOutputStream oos) throws IOException, ClassNotFoundException { 187 //得到需要修改的单号 188 String updateOrder = (String) ois.readObject(); 189 //根据单号查找内容,将查找到的数据发送客户端 190 Express oldExpress = EXPRESS_DAO.findByOrder(updateOrder); 191 oos.writeObject(oldExpress); 192 oos.flush(); 193 if (oldExpress != null) { 194 //接收客户端回传的更改内容 195 Express newExpress = (Express) ois.readObject(); 196 //根据新快递内容去库中查找是否存在,存在就代表库中有相同的数据 197 Express isExit = EXPRESS_DAO.findByOrder(newExpress.getOrder()); 198 if (isExit != null) { 199 oos.writeBoolean(false); 200 } else { 201 Express update = EXPRESS_DAO.update(oldExpress, newExpress); 202 //修改成功 203 oos.writeBoolean(true); 204 //将修改的值返回客户端 205 oos.writeObject(update); 206 } 207 oos.flush(); 208 } 209 } 210 211 212 /** 213 * 查看所有快递 214 */ 215 private void seleteAllExpress(ObjectOutputStream oos) throws IOException { 216 ArrayList<Express> list = EXPRESS_DAO.getAll(); 217 Express[] expresses = new Express[list.size()]; 218 list.toArray(expresses); 219 oos.writeObject(expresses); 220 oos.flush(); 221 } 222 }
客户端:
1 package client; 2 3 import bean.Express; 4 import view.View; 5 6 import java.io.*; 7 import java.net.Socket; 8 import java.util.Arrays; 9 import java.util.List; 10 11 /** 12 * @author Administrator 13 */ 14 public class Client { 15 /** 16 * 初始化视图 17 */ 18 private static final View VIEW = new View(); 19 /** 20 * 创建视图 21 */ 22 private static Socket socket; 23 /** 24 * 状态码 25 */ 26 private boolean status = true; 27 28 public static void main(String[] args) throws IOException { 29 Client ec = new Client(); 30 ec.init(); 31 } 32 33 34 private void init() throws IOException { 35 OutputStream os; 36 InputStream is; 37 ObjectOutputStream oos = null; 38 ObjectInputStream ois = null; 39 //1.欢迎 40 VIEW.welcome(); 41 try { 42 socket = new Socket("127.0.0.1", 7063); 43 os = socket.getOutputStream(); 44 is = socket.getInputStream(); 45 // 由于服务器是先ois后oos 为了保证配对 这里需要顺序调换 46 oos = new ObjectOutputStream(os); 47 ois = new ObjectInputStream(is); 48 m: 49 while (status) { 50 int menu = VIEW.menuMain(); 51 oos.writeInt(menu); 52 oos.flush(); 53 switch (menu) { 54 case 0: 55 break m; 56 case 1: 57 deliveryManClient(oos, ois); 58 break; 59 case 2: 60 userClient(oos, ois); 61 break; 62 default: 63 break; 64 } 65 } 66 oos.writeInt(0); 67 oos.flush(); 68 VIEW.exit(); 69 } catch (IOException | ClassNotFoundException e) { 70 e.printStackTrace(); 71 } finally { 72 if (ois != null) { 73 ois.close(); 74 } 75 if (oos != null) { 76 oos.close(); 77 } 78 if (socket != null) { 79 socket.close(); 80 } 81 } 82 83 84 } 85 86 /** 87 * 管理员客户端 88 * 89 * @param oos 发送数据给服务区 90 * @param ois 从服务器接收数据 91 */ 92 private void deliveryManClient(ObjectOutputStream oos, ObjectInputStream ois) throws IOException, ClassNotFoundException { 93 int menu = VIEW.menuDeliveryMan(); 94 oos.writeInt(menu); 95 oos.flush(); 96 switch (menu) { 97 case 0: 98 status = false; 99 break; 100 case 1: 101 addExpress(oos, ois); 102 break; 103 case 2: 104 deleteExpress(oos, ois); 105 break; 106 case 3: 107 updateExpress(oos, ois); 108 break; 109 case 4: 110 seleteAllExpress(ois); 111 break; 112 default: 113 break; 114 } 115 } 116 117 /** 118 * 用户 119 * 120 * @param oos 发送数据给服务器 121 * @param ois 从服务器接收数据 122 */ 123 private void userClient(ObjectOutputStream oos, ObjectInputStream ois) throws IOException, ClassNotFoundException { 124 //获取用户输入的取件码 125 int code = VIEW.menuUser(); 126 oos.writeInt(code); 127 oos.flush(); 128 //根据取件码查询快递 129 Express byCode = (Express) ois.readObject(); 130 if (byCode == null) { 131 //不存在,提示用户取件码错误或快递不存在 132 VIEW.printCodeNull(); 133 } else { 134 //提示用户操作成功,打印快递信息 135 VIEW.printSuccess(); 136 VIEW.printExpress(byCode); 137 //从快递柜删除该快递 138 if (!ois.readBoolean()) { 139 VIEW.printFail(); 140 } 141 } 142 } 143 144 /** 145 * 存储快递 146 */ 147 private void addExpress(ObjectOutputStream oos, ObjectInputStream ois) throws IOException, ClassNotFoundException { 148 //1.提示输入快递信息 149 Express addExpress = VIEW.insert(); 150 oos.writeObject(addExpress); 151 oos.flush(); 152 //2.判断快递是否已存储 153 Express add = (Express) ois.readObject(); 154 if (add != null) { 155 VIEW.printExpress(add); 156 } else { 157 //提示快递已存在 158 VIEW.expressExist(); 159 } 160 } 161 162 /** 163 * 删除快递 164 */ 165 private void deleteExpress(ObjectOutputStream oos, ObjectInputStream ois) throws IOException, ClassNotFoundException { 166 //获取到用户输入的单号 167 String deleteOrder = VIEW.findByOrder(); 168 //传给服务器 169 oos.writeObject(deleteOrder); 170 //刷新 171 oos.flush(); 172 //查询单号是否存在 173 Express i = (Express) ois.readObject(); 174 if (i == null) { 175 VIEW.printNull(); 176 } else { 177 //向用户确认是否删除 178 int menu = VIEW.deleteExpress(); 179 //传给服务器 180 oos.writeInt(menu); 181 oos.flush(); 182 if (menu == 1) { 183 if (ois.readBoolean()) { 184 VIEW.printSuccess(); 185 } else { 186 VIEW.printFail(); 187 } 188 } 189 } 190 191 } 192 193 /** 194 * 修改快递 195 */ 196 private void updateExpress(ObjectOutputStream oos, ObjectInputStream ois) throws IOException, ClassNotFoundException { 197 //得到需要修改的单号 198 String updateOrder = VIEW.findByOrder(); 199 oos.writeObject(updateOrder); 200 oos.flush(); 201 //得到服务端回传的数据 202 Express oldExpress = (Express) ois.readObject(); 203 //判断是否存在 204 if (oldExpress != null) { 205 Express newExpress = VIEW.updateExpress(); 206 //将修改的内容发送至服务器 207 oos.writeObject(newExpress); 208 oos.flush(); 209 if (!ois.readBoolean()) { 210 VIEW.expressExist(); 211 VIEW.printExpress(oldExpress); 212 } else { 213 //得到修改后的值,打印输出 214 Express update = (Express) ois.readObject(); 215 VIEW.printSuccess(); 216 VIEW.printExpress(update); 217 } 218 } else { 219 VIEW.printNull(); 220 } 221 } 222 223 224 /** 225 * 查看所有快递 226 */ 227 private void seleteAllExpress(ObjectInputStream ois) throws IOException, ClassNotFoundException { 228 Express[] es = (Express[]) ois.readObject(); 229 List<Express> expresses = Arrays.asList(es); 230 if (expresses.size() != 0) { 231 for (Express ex : expresses) { 232 VIEW.printExpress(ex); 233 234 } 235 } else { 236 VIEW.isNull(); 237 } 238 } 239 }
标签:insert 菜单 nts 开始 rgba 程序 用户输入 png span
原文地址:https://www.cnblogs.com/baiyangshu/p/14940839.html