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