day02-显示所有菜品&点餐功能

饭宝  金牌会员 | 2022-10-20 20:52:29 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 907|帖子 907|积分 2721

满汉楼02

4.功能实现04

4.6显示所有菜品

4.6.1思路分析

创建一个菜单表menu,在Domain层创建与菜单表对应的Javabean-Menu类,在DAO层创建MenuDAO,完成对menu表的增删改查,在Service层创建一个和menu表相关的service类,service类提供给界面层使用
4.6.2代码实现

1.创建menu表
  1. -- 创建menu表(id,name,type,price)
  2. CREATE TABLE menu(
  3.         id INT PRIMARY KEY AUTO_INCREMENT,#自增主键,作为菜谱编号(唯一)
  4.         NAME VARCHAR(50) NOT NULL DEFAULT '',#菜品名称
  5.         TYPE VARCHAR(50) NOT NULL DEFAULT '',#菜品种类
  6.         price DOUBLE NOT NULL DEFAULT 0        #价格
  7. )CHARSET=utf8
  8. -- 添加测试数据
  9. INSERT INTO menu VALUES(NULL,'八宝饭','主食',10);
  10. INSERT INTO menu VALUES(NULL,'叉烧包','主食',20);
  11. INSERT INTO menu VALUES(NULL,'宫保鸡丁','热菜',30);
  12. INSERT INTO menu VALUES(NULL,'山药拨鱼','凉菜',14);
  13. INSERT INTO menu VALUES(NULL,'银丝卷','甜食',9);
  14. INSERT INTO menu VALUES(NULL,'水煮鱼','热菜',26);
  15. INSERT INTO menu VALUES(NULL,'甲鱼汤','汤菜',100);
  16. INSERT INTO menu VALUES(NULL,'鸡蛋汤','汤菜',16);
复制代码
2.创建Menu类
  1. package com.li.mhl.domain;
  2. /**
  3. * @author 李
  4. * @version 1.0
  5. * 该类和menu表对应
  6. */
  7. public class Menu {
  8.     /**
  9.      * Field   Type         Null    Key     Default  Extra
  10.      * ------  -----------  ------  ------  -------  ----------------
  11.      * id      int(11)      NO      PRI     (NULL)   auto_increment
  12.      * name    varchar(50)  NO
  13.      * type    varchar(50)  NO
  14.      * price   double       NO              0
  15.      */
  16.     private Integer id;
  17.     private String name;
  18.     private String type;
  19.     private Double price;
  20.     public Menu() {
  21.     }
  22.     public Menu(Integer id, String name, String type, Double price) {
  23.         this.id = id;
  24.         this.name = name;
  25.         this.type = type;
  26.         this.price = price;
  27.     }
  28.     public Integer getId() {
  29.         return id;
  30.     }
  31.     public void setId(Integer id) {
  32.         this.id = id;
  33.     }
  34.     public String getName() {
  35.         return name;
  36.     }
  37.     public void setName(String name) {
  38.         this.name = name;
  39.     }
  40.     public String getType() {
  41.         return type;
  42.     }
  43.     public void setType(String type) {
  44.         this.type = type;
  45.     }
  46.     public Double getPrice() {
  47.         return price;
  48.     }
  49.     public void setPrice(Double price) {
  50.         this.price = price;
  51.     }
  52.     @Override
  53.     public String toString() {
  54.         return id + "\t\t\t" + name + "\t\t" + type + "\t\t" + price;
  55.     }
  56. }
复制代码
3.创建MenuDAO类
  1. package com.li.mhl.dao;
  2. import com.li.mhl.domain.Menu;
  3. /**
  4. * @author 李
  5. * @version 1.0
  6. */
  7. public class MenuDAO extends BasicDAO<Menu>{
  8. }
复制代码
4.创建MenuService类
  1. package com.li.mhl.service;
  2. import com.li.mhl.dao.MenuDAO;
  3. import com.li.mhl.domain.Menu;
  4. import java.util.List;
  5. /**
  6. * @author 李
  7. * @version 1.0
  8. * 完成对menu表的各种操作(通过调用MenuDAO)
  9. */
  10. public class MenuService {
  11.     //定义MenuDAO属性
  12.     private MenuDAO menuDAO = new MenuDAO();
  13.     //编写方法,查询所有菜品(返回所有的菜品给界面使用)
  14.     public List<Menu> list() {
  15.         return menuDAO.queryMulti("select * from menu", Menu.class);
  16.     }
  17. }
复制代码
5.修改MHLView
修改处1:定义属性
  1. //定义MenuService属性
  2. private MenuService menuService = new MenuService();
复制代码
修改处2:编写方法
  1. //显示所有菜品
  2. public void listMenu() {
  3.     List<Menu> list = menuService.list();
  4.     System.out.println("\n菜品编号\t\t菜品名\t\t类别\t\t价格");
  5.     for (Menu menu: list) {
  6.         System.out.println(menu);
  7.     }
  8.     System.out.println("============显示完毕============");
  9. }
复制代码
修改处3:在内层循环的二级菜单中调用该方法
4.6.3测试

测试通过
4.7点餐功能

4.7.1功能说明

4.7.2思路分析

功能如上图所示,分析如下:

  • 在输入点餐桌号和菜品编号时都需要对其进行合理性校验,如果不合理,给出提示信息
  • 当点餐成功之后,餐桌的状态state应该发生变化,由已经预定变为进餐中
  • 一旦点餐,就应该生成账单
根据上述的分析,整体的框架应该如下:
4.7.3代码实现

1.创建bill表
  1. -- 创建bill账单表(id,billId,menuId,nums,billDate,money,state,diningTableId)
  2. #账单流水, 考虑可以分开结账, 并考虑将来分别统计各个不同菜品的销售情况
  3. CREATE TABLE bill (
  4.         id INT PRIMARY KEY AUTO_INCREMENT, #自增主键
  5.         billId VARCHAR(50) NOT NULL DEFAULT '',#账单号可以按照自己规则生成 UUID
  6.         menuId INT NOT NULL DEFAULT 0,#菜品的编号, 也可以使用外键
  7.         nums SMALLINT NOT NULL DEFAULT 0,#份数
  8.         money DOUBLE NOT NULL DEFAULT 0, #金额
  9.         diningTableId INT NOT NULL DEFAULT 0, #餐桌
  10.         billDate DATETIME NOT NULL ,#订单日期
  11.         state VARCHAR(50) NOT NULL DEFAULT '' # 状态 '未结账' , '已经结账-现金/支付宝', '挂单'
  12. )CHARSET=utf8;
复制代码
2.创建Bill类
  1. package com.li.mhl.domain;
  2. import java.util.Date;
  3. /**
  4. * @author 李
  5. * @version 1.0
  6. * 该类和 bill表对应
  7. */
  8. public class Bill {
  9.     /**
  10.      * Field          Type         Null    Key     Default  Extra
  11.      * -------------  -----------  ------  ------  -------  ----------------
  12.      * id             int(11)      NO      PRI     (NULL)   auto_increment
  13.      * billId         varchar(50)  NO
  14.      * menuId         int(11)      NO              0
  15.      * nums           smallint(6)  NO              0
  16.      * money          double       NO              0
  17.      * diningTableId  int(11)      NO              0
  18.      * billDate       datetime     NO              (NULL)
  19.      * state          varchar(50)  NO
  20.      */
  21.     private Integer id;
  22.     private String billId;
  23.     private Integer menuId;
  24.     private Integer nums;
  25.     private Double money;
  26.     private Integer diningTableId;
  27.     private Date billDate;
  28.     private String state;
  29.     public Bill() {
  30.     }
  31.     public Bill(Integer id, String billId, Integer menuId, Integer nums, Double money, Integer diningTableId, Date billDate, String state) {
  32.         this.id = id;
  33.         this.billId = billId;
  34.         this.menuId = menuId;
  35.         this.nums = nums;
  36.         this.money = money;
  37.         this.diningTableId = diningTableId;
  38.         this.billDate = billDate;
  39.         this.state = state;
  40.     }
  41.     public Integer getId() {
  42.         return id;
  43.     }
  44.     public void setId(Integer id) {
  45.         this.id = id;
  46.     }
  47.     public String getBillId() {
  48.         return billId;
  49.     }
  50.     public void setBillId(String billId) {
  51.         this.billId = billId;
  52.     }
  53.     public Integer getMenuId() {
  54.         return menuId;
  55.     }
  56.     public void setMenuId(Integer menuId) {
  57.         this.menuId = menuId;
  58.     }
  59.     public Integer getNums() {
  60.         return nums;
  61.     }
  62.     public void setNums(Integer nums) {
  63.         this.nums = nums;
  64.     }
  65.     public Double getMoney() {
  66.         return money;
  67.     }
  68.     public void setMoney(Double money) {
  69.         this.money = money;
  70.     }
  71.     public Integer getDiningTableId() {
  72.         return diningTableId;
  73.     }
  74.     public void setDiningTableId(Integer diningTableId) {
  75.         this.diningTableId = diningTableId;
  76.     }
  77.     public Date getBillDate() {
  78.         return billDate;
  79.     }
  80.     public void setBillDate(Date billDate) {
  81.         this.billDate = billDate;
  82.     }
  83.     public String getState() {
  84.         return state;
  85.     }
  86.     public void setState(String state) {
  87.         this.state = state;
  88.     }
  89. }
复制代码
3.创建BillDAO类
  1. package com.li.mhl.dao;
  2. import com.li.mhl.domain.Bill;
  3. /**
  4. * @author 李
  5. * @version 1.0
  6. */
  7. public class BillDAO extends BasicDAO<Bill>{
  8. }
复制代码
4.创建BillService类
  1. package com.li.mhl.service;
  2. import com.li.mhl.dao.BillDAO;
  3. import java.util.UUID;
  4. /**
  5. * @author 李
  6. * @version 1.0
  7. * 处理和账单表bill相关的业务逻辑
  8. */
  9. public class BillService {
  10.     //定义BillDAO属性
  11.     private BillDAO billDAO = new BillDAO();
  12.     //定义MenuService属性
  13.     private MenuService menuService = new MenuService();
  14.     //定义DiningTableService属性
  15.     private DiningTableService diningTableService = new DiningTableService();
  16.     //编写点餐的方法
  17.     /**
  18.      * 1.生成账单
  19.      * 2.需要更新对应的餐桌的状态
  20.      * 3.如成功返回true,失败返回false
  21.      */
  22.     public boolean orderMenu(int menuId, int nums, int diningTableId) {
  23.         //使用UUID生成一个账单号
  24.         String billID = UUID.randomUUID().toString();
  25.         //将账单生成到bill表()
  26.         //这里的金额money = 由menuId(菜品编号)查询出来的单价 * nums
  27.         //因此,在MenuService类中编写方法getMenuById()查询菜品单价
  28.         int update = billDAO.update("insert into bill values(null,?,?,?,?,?,now(),'未结账')",
  29.                 billID, menuId, nums, menuService.getMenuById(menuId).getPrice() * nums, diningTableId);
  30.         if (update <= 0) {
  31.             return false;
  32.         }
  33.         //需要更新对应的餐桌的状态
  34.         //在DiningTableService类中编写方法updateDiningTableState()更新对应的餐桌的状态
  35.         return diningTableService.updateDiningTableState(diningTableId, "就餐中");
  36.     }
  37. }
复制代码
7.修改MHLView

修改处1:在该类中定义BillService属性
  1. //根据菜品id,返回Menu对象
  2. public Menu getMenuById(int id) {
  3.     return menuDAO.querySingle("select * from menu where id=?", Menu.class, id);
  4. }
复制代码
修改处2:在该类中增加方法
[code]//完成点餐操作public void orderMenu() {    System.out.println("============点餐服务============");        System.out.print("请输入点餐的桌号(-1退出): ");    int orderDiningTableId = Utility.readInt();    if (orderDiningTableId == -1) {        System.out.println("============取消点餐============");        return;    }    //验证餐桌号是否存在    DiningTable diningTableById = diningTableService.getDiningTableById(orderDiningTableId);    if (diningTableById == null) {        System.out.println("============餐桌号不存在============");        return;    }        System.out.print("请输入菜品编号(-1退出): ");    int orderMenuId = Utility.readInt();    if (orderMenuId == -1) {        System.out.println("============取消点餐============");        return;    }    //验证菜品编号是否存在    Menu menuById = menuService.getMenuById(orderMenuId);    if (menuById == null) {        System.out.println("============菜品不存在============");        return;    }        System.out.print("请输入菜品数量(0~99)(-1退出): ");    int orderNums = Utility.readInt();    if (orderNums == -1) {        System.out.println("============取消点餐============");        return;    }    if (orderNums

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

饭宝

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表