一、项目介绍
1、有哪些需求需要连接后台完成功能?
前台传给后台 后台返回给前台
注册: 用户名 + 密码 操作是否成功
登录: 用户名 + 密码 操作是否成功
下单: 用户名 + 菜名 操作是否成功
Request : 前端发送给后台的所有数据的载体
Response : 后台返回给前台的所有数据的载体
2、各部件
角色:
1.大堂经理==迎宾接待 EtoakServer.java
它的职责 只是接待客人进门
不涉及点菜不涉及炒菜 不涉及采购菜品
然后将客人转交给一个负责点菜的服务员
告诉服务员开始干活 它继续站在门口 等下一个客人
2.服务员 EtoakThread.java
它的职责
a> 接过客人手里的"请求表Request==点菜的菜单" 接 : Request
b> 将做菜的过程外包给 厨子部门 外包的化
c> 将厨子部门做好装好盘的菜 给用户端上去 发 : Response
3.厨子总监 EtoakController.java
它的职责
根据用户在菜单上的选项 决定具体给提供什么样的餐品
取前端放入的askNo 取做某件事对应的数据
安排自己的小弟 去炒菜 但是由他装盘
a> 从请求当中获取做这项业务需要的数据
b> 安排小弟去干活(真正的业务!!!) 还得拿到小弟返回的结果
c> 找个空盘子 把菜放进去 返回
4.厨子小弟 Etoakservice.java
它的职责
根据Controller传过来的参数
传回一个int值代表正确与否
二、代码
1、EtoakServer.java
import java.net.*;
import java.util.concurrent.*;public class EtoakServer{public static void main(String[] args)throws Exception{ExecutorService es = Executors.newFixedThreadPool(5);ServerSocket server = new ServerSocket(17888);boolean isRunning = true;while(isRunning){Socket skt = server.accept();EtoakThread et = new EtoakThread(skt);es.submit(et);}server.close();es.shutdown();}
}
2、EtoakController.java
import java.util.*;public class EtoakController{static EtoakService service = new EtoakService();public Response work(Request req){int askNo = req.getAksNo();Response res = null ;if(askNo == 1001){res = register(req);}else if(askNo == 1002){res = login(req);}else if(askNo == 1003){res = order(req);}return res;}public Response register(Request req){String username = req.getParameter("username");String password = req.getParameter("password");Response res = new Response();int rr = service.register(username,password);res.setResult(rr);return res;}public Response login(Request req){String username = req.getParameter("username");String password = req.getParameter("password");Response res = new Response();EtoakFAO fao = new EtoakFAO();int rr = service.login(username,password);res.setResult(rr);return res;}public Response order(Request req){String username = req.getParameter("username");String foods = req.getParameter("foods");Response res = new Response();int rr = service.order(username,foods);res.setResult(rr);return res;}
}
3、EtoakThread.java
import java.net.*;
import java.io.*;
public class EtoakThread implements Runnable{Socket skt;public EtoakThread(Socket skt){this.skt = skt;}@Overridepublic void run(){try{InputStream is = skt.getInputStream();ObjectInputStream ois = new ObjectInputStream(is);Request req = (Request)ois.readObject();EtoakController controller = new EtoakController();Response res = controller.work(req);OutputStream os = skt.getOutputStream();ObjectOutputStream oos = new ObjectOutputStream(os);oos.writeObject(res);oos.close();ois.close();skt.close();}catch(Exception e){e.printStackTrace();}}
}
4、Etoakservice.java
import java.util.*;
public class EtoakService{EtoakFAO fao = new EtoakFAO();public int register(String username,String password){Map<String,String> userMap = fao.load();if(userMap.containsKey(username))return 1;userMap.put(username,password);fao.save(userMap);return 0;}public int login(String username,String password){Map<String,String> userMap = fao.load();if(!userMap.containsKey(username))return 1;String realPassword = userMap.get(username);if(realPassword.equals(password)){return 0;}else{return 2;}}public int order(String username,String foods){if(username.contains("Jay")){return 1;//点餐失败 太胖了}if(foods.contains("野生")){return 2;//点餐失败 你很刑}String[] data = foods.split(",");if(data.length < 5){return 3;//数量不够}return 0;}}
5、Test.java
import java.util.*;
import java.util.concurrent.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
public class Test{public static void main(String[] args)throws Exception{Request req = new Request();req.setAskno(1003);req.setParameter("username","Jim");req.setParameter("password","123123");req.setParameter("foods","水煮鱼");//r.getAksNo();Socket skt = new Socket("192.168.17.185",17888);ObjectOutputStream oos = new ObjectOutputStream(skt.getOutputStream());oos.writeObject(req);ObjectInputStream ois = new ObjectInputStream(skt.getInputStream());Response res = (Response)ois.readObject();ois.close();oos.close();skt.close();int result = res.getResult();System.out.println(result);}}
6、Request.java
import java.util.*;
import java.util.concurrent.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;public class Request implements Serializable{private int askNo;public void setAskno(int askNo){this.askNo = askNo;}public int getAksNo(){return askNo;}private Map<String,String> params = new LinkedHashMap<>();public void setParameter(String key,String value){params.put(key,value);}public String getParameter(String key){return params.get(key);}}
7、Response.java
public class Response implements java.io.Serializable{private int result = -1;public void setResult(int result){this.result = result;}public int getResult(){return result;}
}
8、EtoakFAO.java
import java.io.*;
import java.util.*;public class EtoakFAO{public void main(String[] args){Map<String,String> map = new HashMap<>();map.put("Jim","123123");try(ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("users.data"))){oos.writeObject(map);oos.close();}catch(Exception e){e.printStackTrace();}}public void save(Map<String,String> map){try(ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("users.data"))){oos.writeObject(map);oos.close();}catch(Exception e){e.printStackTrace();}}public Map<String,String> load(){Map<String,String> map = new HashMap<>();try(ObjectInputStream ois = new ObjectInputStream(new FileInputStream("users.data"))){map = (Map<String,String>)ois.readObject();ois.close();}catch(Exception e){e.printStackTrace();}return map;}}
三、小结
这次是前后端一起进行的练习项目,这个结构是需要我多熟悉的,我在写第一遍的过程中直接忽略了service的作用根本没写这个部分,直接在Controller中实现了功能,这是不规范的,应当注意,先写框架,在进行功能编辑,不然容易在功能编辑的过程中忘记一些部分
四、赠品
这个小项目是实现一个点餐的页面的部分,主要的知识是在于,如何给菜单项添加监听器,然后整合一些给其他按钮增加监听器的各种功能,然后集合而成的一个小型的项目。
这里面需要一些基础的资源,代码只能参考,没有资源是无法正常的实现功能的。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
public class OrderFrame{JFrame frame;JPanel east,south,center,cc,cs;JLabel title,total,pic;JButton enter,submit,reset;java.awt.List list;Map<String,Integer> nameAndPrice = new HashMap<>();String current = "海鲜煲";int sum;public OrderFrame(){frame = new JFrame("点餐窗体");createMenu();east = new JPanel(new BorderLayout());south = new JPanel(new FlowLayout());center = new JPanel(new BorderLayout());cc = new JPanel(new BorderLayout());cs = new JPanel(new FlowLayout());title = new JLabel("海鲜煲 : 560 元");total = new JLabel("当前总共 : 0 元");pic = new JLabel(new ImageIcon("img/海鲜煲.jpg"));enter = new JButton("我要点这道菜");enter.addActionListener((ae) -> {list.add(current);int price = nameAndPrice.get(current);sum += price;total.setText("当前总共 : "+sum+" 元");});submit = new JButton("提交菜单");submit.addActionListener((ae) -> {String[] data = list.getItems();//得到列表中的项目们if(data.length < 5){JOptionPane.showMessageDialog(frame,"太磕碜了吧?!");return;}if(sum < 500){JOptionPane.showMessageDialog(frame,"太寒碜了吧?!");return;}System.out.println("验证通过 准备连接服务器 但是 下午的活咯");});reset = new JButton("全部重置");reset.addActionListener((ae) -> {sum = 0;list.removeAll();total.setText("当前总共 : 0 元");});list = new java.awt.List();list.setFont(new Font("宋体",1,18));east.add(list);south.add(enter);south.add(submit);south.add(reset);south.add(total);cs.add(title);cc.add(pic);center.add(cs,"South");center.add(cc,"Center");frame.add(center,"Center");frame.add(east,"East");frame.add(south,"South");frame.setSize(800,400);frame.setVisible(true);frame.setDefaultCloseOperation(3);}public void createMenu(){FoodListener fl = new FoodListener();JMenuBar bar = new JMenuBar();Properties menuPros = new Properties();try(InputStreamReader r = new InputStreamReader(new FileInputStream("cfg/config.properties"),"GBK")){menuPros.load(r);}catch(Exception e){e.printStackTrace();}Set<String> menuNames = menuPros.stringPropertyNames();for(String menuName : menuNames){JMenu menu = new JMenu(menuName);String fileName = menuPros.getProperty(menuName);Properties itemPros = new Properties();try(InputStreamReader r = new InputStreamReader(new FileInputStream("cfg/"+fileName),"GBK")){itemPros.load(r);}catch(Exception e){e.printStackTrace();}Set<String> itemNames = itemPros.stringPropertyNames();for(String itemName : itemNames){String str = itemPros.getProperty(itemName);int price = Integer.parseInt(str);nameAndPrice.put(itemName,price);//偷偷存 名字和价格JMenuItem mi = new JMenuItem(itemName);mi.addActionListener(fl);menu.add(mi);}bar.add(menu);}frame.setJMenuBar(bar);}public static void main(String[] args){new OrderFrame();}class FoodListener implements ActionListener{@Overridepublic void actionPerformed(ActionEvent ae){String cmd = ae.getActionCommand();//菜名pic.setIcon(new ImageIcon("img/"+cmd+".jpg"));title.setText(cmd + " : "+nameAndPrice.get(cmd)+" 元");current = cmd;}}
}