【Java学习笔记】72 - 满汉楼 - 餐饮管理系统
项目代码
https://github.com/yinhai1114/Java_Learning_Code/tree/main/IDEA_Chapter26
一、需求说明
满汉楼项目功能多,界面复杂,涉及到复杂的awt和swing技术和事件编程,做如下调整:
1.去掉界面和事件处理(工作中使用很少),使用控制台界面
2.完成满汉楼项目的登录、订座、点餐和结账、查看账单等功能.
3.在实际工作中,独立完成项目新功能非常重要,这是锻炼编程能力和思想的重要途径
1.用户登录
2.显示餐桌的状态
3.预定
4.显示菜品
5.点餐
6.查看账单
7.结账
二、项目设计
三、功能实现
1.显示主菜单、二级菜单和退出系统功能
public void mainMenu(){
while (loop){
System.out.println("==============满汉楼===============");
System.out.println("\t\t1.登录满汉楼");
System.out.println("\t\t2.退出满汉楼");
System.out.println("请输入你的选择");
key = Utility.readString(1);
switch (key){
case "1":
System.out.println("输入员工号:");
String id = Utility.readString(50);
System.out.println("输入密 码");
String pwd = Utility.readString(50);
//到数据库去判断
if("123".equals(pwd)){
System.out.println("===========登录成功===========");
while(loop){
System.out.println("=========满汉楼(二级菜单)==========");
System.out.println("\t\t1 显示餐桌状态");
System.out.println("\t\t2 预定餐桌状态");
System.out.println("\t\t3 显示所有菜品");
System.out.println("\t\t4 点餐服务");
System.out.println("\t\t5 查看账单");
System.out.println("\t\t6 结账");
System.out.println("\t\t9 退出满汉楼");
key = Utility.readString(1);
switch (key){
case "1":
System.out.println("显示餐桌状态");
break;
case "2":
System.out.println("预定餐桌");
break;
case "3":
System.out.println("显示所有菜品");
break;
case "4":
System.out.println("点餐服务");
break;
case "5":
System.out.println("查看账单");
break;
case "6":
System.out.println("结账");
break;
case "9":
loop = false;
break;
default:
System.out.println("你的输入有误 请重新输入");
}
}
}else{
System.out.println("===========登录失败===========");
}
break;
case "2":
System.out.println("2.退出满汉楼");
loop = false;
break;
default:
System.out.println("输入错误错误");
}
}
System.out.println("退出满汉楼");
}
2.用户登录
CREATE DATABASE mhl
#用户表
CREATE TABLE employee (
id INT PRIMARY KEY AUTO_INCREMENT, #自增
empId VARCHAR(50) UNIQUE NOT NULL DEFAULT '',#员工号
pwd CHAR(32) NOT NULL DEFAULT '',#密码md5
NAME VARCHAR(50) NOT NULL DEFAULT '',#姓名
job VARCHAR(50) NOT NULL DEFAULT '' #岗位
)CHARSET=utf8;
DROP TABLE employee
#添加测试数据
INSERT INTO employee VALUES(NULL, '6668612', MD5('123456'), '张三丰', '经理');
INSERT INTO employee VALUES(NULL, '6668622', MD5('123456'),'小龙女', '服务员');
INSERT INTO employee VALUES(NULL, '6668633', MD5('123456'), '张无忌', '收银员');
INSERT INTO employee VALUES(NULL, '666666', MD5('123456'), '老韩', '经理');
SELECT * FROM employee
/**
* @author 银小海
* @version 1.0
* @email yinhai14@qq.com
* 该类调用EmployeeDAO对象完成对表的各种操作
*/
public class EmployeeService {
//定义一个EmployeeDAO属性
private EmployeeDAO employeeDAO = new EmployeeDAO();
//方法 根据empId和pwd返回一个Employee对象
public Employee getEmployeeByIdAndPwd(String empId,String pwd){
return employeeDAO.querySingle("select * from employee where empId=? and pwd=md5(?)", Employee.class, empId, pwd);
}
}
该服务并不直接修改数据库,而是通过DAO调用JDBCDruid修改数据库
if(employee != null){
System.out.println("===========登录成功(" + employee.getName() + ")===========");
while(loop){
3.显示餐桌状态
1.创建新的餐位表
2.domain里设计一个类对应该表
3.DAO类对表crud
4.service调用DAO修改
-- 创建diningTable 表(id, state , orderName, orderTel ....)
CREATE TABLE diningTable (
id INT PRIMARY KEY AUTO_INCREMENT, #自增, 表示餐桌编号
state VARCHAR(20) NOT NULL DEFAULT '',#餐桌的状态
orderName VARCHAR(50) NOT NULL DEFAULT '',#预订人的名字
orderTel VARCHAR(20) NOT NULL DEFAULT ''
)CHARSET=utf8;
#测试数据
INSERT INTO diningTable VALUES(NULL, '空','','');
INSERT INTO diningTable VALUES(NULL, '空','','');
INSERT INTO diningTable VALUES(NULL, '空','','');
SELECT * FROM diningTable
2.设计一个类对应 该表
/**
* @author 银小海
* @version 1.0
* @email yinhai14@qq.com
* 一个javabean 和diningTable表对应
* id INT PRIMARY KEY AUTO_INCREMENT, #自增, 表示餐桌编号
* state VARCHAR(20) NOT NULL DEFAULT '',#餐桌的状态
* orderName VARCHAR(50) NOT NULL DEFAULT '',#预订人的名字
* orderTel VARCHAR(20) NOT NULL DEFAULT ''
*/
public class DiningTable {
private Integer id;
private String state;
private String orderName;
private String orderTel;
public DiningTable() {
}
public DiningTable(Integer id, String state, String orderName, String orderTel) {
this.id = id;
this.state = state;
this.orderName = orderName;
this.orderTel = orderTel;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getOrderName() {
return orderName;
}
public void setOrderName(String orderName) {
this.orderName = orderName;
}
public String getOrderTel() {
return orderTel;
}
public void setOrderTel(String orderTel) {
this.orderTel = orderTel;
}
@Override
public String toString() {
return id + "\t\t\t" + state;
}
}
4.
public class DiningTableService {
//定义一个DiningTableDAO对象
private DiningTableDAO diningTableDAO = new DiningTableDAO();
//返回所有餐桌的信息
public List<DiningTable> list(){
return diningTableDAO.queryMulti("select id,state from diningTable", DiningTable.class);
}
}
4.订座
功能说明如果该餐桌处于已经预定或者就餐状态,给出提示
1.在DiningTableService内添加对应方法
//根据id 查询对应的DiningTable对象 如果返回null 表示id编号对应餐桌不存在
public DiningTable getDiningTableById(int id){
//把sql语句放到查询分析器去测试一下
return diningTableDAO.querySingle("select * from diningTable where id = ?",DiningTable.class,id);
}
//如果餐桌可以预定 调用方法对其状态进行更新
public boolean orderDiningTable(int id,String orderName,String orderTel) {
int dml =
diningTableDAO.dml("update diningTable set state = '已经" +
"预定',orderName = ?,orderTel=? where id = ?", orderName, orderTel, id);
return dml > 0;
}
2.在view内添加判断用户输入方法,并在对应阶段调用该方法即可
//预定餐桌
public void orderDiningTable(){
System.out.println("=========预定餐桌==========");
System.out.println("请选择要预定的餐桌的编号(-1退出)");
int orderId = Utility.readInt();
if(orderId == -1){
return;
}
DiningTable diningTable = diningTableService.getDiningTableById(orderId);
if(diningTable == null){ //说明不存在
System.out.println("==========该餐桌不存在=========");
return;
}
char key = Utility.readConfirmSelection();//输入Y/N
if (key == 'Y'){
//要预定就调用方法 如果为null该对象不存在
if(!("空".equals(diningTable.getState()))){
System.out.println("==========该餐桌已被预定或就餐中=========");
return;
}
System.out.println("预订人的名字:");
String orderName = Utility.readString(50);
System.out.println("预订人的电话:");
String orderTel = Utility.readString(50);
if(diningTableService.orderDiningTable(orderId,orderName,orderTel)){
System.out.println("预定餐桌成功");
}else{
System.out.println("预定餐桌失败");
}
//过关斩将
}else{
System.out.println("=========取消预定餐桌==========");
}
}
5.显示菜品
1.需要创建一个新的菜单表
2.需要在java内建立domain类
3.需要创建菜单的DAO类
4.需要service组织sql并调用DAO
-- 创建menu表(id, name, type, price)
#菜谱
CREATE TABLE menu (
id INT PRIMARY KEY AUTO_INCREMENT, #自增主键,作为菜谱编号(唯一)
NAME VARCHAR(50) NOT NULL DEFAULT '',#菜品名称
TYPE VARCHAR(50) NOT NULL DEFAULT '', #菜品种类
price DOUBLE NOT NULL DEFAULT 0#价格
)CHARSET=utf8;
#测试数据
INSERT INTO menu VALUES(NULL, '八宝饭', '主食', 10);
INSERT INTO menu VALUES(NULL, '叉烧包', '主食', 20);
INSERT INTO menu VALUES(NULL, '宫保鸡丁', '热菜', 30);
INSERT INTO menu VALUES(NULL, '山药拨鱼', '凉菜', 14);
INSERT INTO menu VALUES(NULL, '银丝卷', '甜食', 9);
INSERT INTO menu VALUES(NULL, '水煮鱼', '热菜', 26);
INSERT INTO menu VALUES(NULL, '甲鱼汤', '汤类', 100);
INSERT INTO menu VALUES(NULL, '鸡蛋汤', '汤类', 16);
2.在java内创建domain的Menu类 对应表的对象
/**
* @author 银小海
* @version 1.0
* @email yinhai14@qq.com
* 该javabean和 menu 表对应
* id INT PRIMARY KEY AUTO_INCREMENT, #自增, 表示餐桌编号
* state VARCHAR(20) NOT NULL DEFAULT '',#餐桌的状态
* orderName VARCHAR(50) NOT NULL DEFAULT '',#预订人的名字
* orderTel VARCHAR(20) NOT NULL DEFAULT ''
*/
public class Menu {
private Integer id;
private String name;
private String type;
private Double price;
public Menu(){//无参构造器
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public Menu(Integer id, String name, String type, Double price) {
this.id = id;
this.name = name;
this.type = type;
this.price = price;
}
}
3.DAO继承BasicDAO
4.service类 组织sql调用DAO
public class MenuService {
private MenuDAO menuDAO = new MenuDAO();
//返回所有的菜品
public List<Menu> list(){
return menuDAO.queryMulti("select * from menu",Menu.class);
}
}
在view内编写方法调用service类的方法
public void listMenu(){
List<Menu> list = menuService.list();
System.out.println("\n菜品编号\t\t菜品名\t\t类别\t\t价格");
for (Menu menu : list) {
System.out.println(menu);
}
System.out.println("=========显示完毕==========");
}
6.点餐
功能说明要求对餐桌号,菜品编号,做合理性校验,如果不合理,给出提示信息
餐桌号 菜品号 检验是否合理,点餐成功,需要修改餐桌状态,生成账单
1.需要创建一个新的账单表
2.需要在java内建立domain类
3.需要创建菜单的DAO类
4.需要service组织sql并调用DAO
1.新建账单表
#增加表 bill 账单表(id, billId, menuId, nums, billDate, money, state, diningTableId )
#账单流水, 考虑可以分开结账, 并考虑将来分别统计各个不同菜品的销售情况
CREATE TABLE bill (
id INT PRIMARY KEY AUTO_INCREMENT, #自增主键
billId VARCHAR(50) NOT NULL DEFAULT '',#账单号可以按照自己规则生成 UUID
menuId INT NOT NULL DEFAULT 0,#菜品的编号, 也可以使用外键
nums INT NOT NULL DEFAULT 0,#份数
money DOUBLE NOT NULL DEFAULT 0, #金额
diningTableId INT NOT NULL DEFAULT 0, #餐桌
billDate DATETIME NOT NULL ,#订单日期
state VARCHAR(50) NOT NULL DEFAULT '' # 状态 '未结账' , '已经结账', '挂单','现金','支付宝','坏账'
)CHARSET=utf8;
SELECT * FROM bill;
2.创建domain类 javabean
/**
* @author 银小海
* @version 1.0
* @email yinhai14@qq.com
* id INT PRIMARY KEY AUTO_INCREMENT, #自增主键
* billId VARCHAR(50) NOT NULL DEFAULT '',#账单号可以按照自己规则生成 UUID
* menuId INT NOT NULL DEFAULT 0,#菜品的编号, 也可以使用外键
* nums INT NOT NULL DEFAULT 0,#份数
* money DOUBLE NOT NULL DEFAULT 0, #金额
* diningTableId INT NOT NULL DEFAULT 0, #餐桌
* billDate DATETIME NOT NULL ,#订单日期
* state VARCHAR(50) NOT NULL DEFAULT '' # 状态 '未结账' , '已经结账', '挂单','现
*/
public class Bill {
private Integer id;
private String billId;
private Integer menuId;
private Integer nums;
private Double money;
private Integer diningTableId;
private Date billDate;
private String state;
public Bill() {
}
public Bill(Integer id, String billId, Integer menuId, Integer nums, Double money, Integer diningTableId, Date billDate, String state) {
this.id = id;
this.billId = billId;
this.menuId = menuId;
this.nums = nums;
this.money = money;
this.diningTableId = diningTableId;
this.billDate = billDate;
this.state = state;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getBillId() {
return billId;
}
public void setBillId(String billId) {
this.billId = billId;
}
public Integer getMenuId() {
return menuId;
}
public void setMenuId(Integer menuId) {
this.menuId = menuId;
}
public Integer getNums() {
return nums;
}
public void setNums(Integer nums) {
this.nums = nums;
}
public Double getMoney() {
return money;
}
public void setMoney(Double money) {
this.money = money;
}
public Integer getDiningTableId() {
return diningTableId;
}
public void setDiningTableId(Integer diningTableId) {
this.diningTableId = diningTableId;
}
public Date getBillDate() {
return billDate;
}
public void setBillDate(Date billDate) {
this.billDate = billDate;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
@Override
public String toString() {
return id +
"\t\t" + menuId +
"\t\t\t" + nums +
"\t\t\t" + money +
"\t" + diningTableId +
"\t\t" + billDate +
"\t\t" + state ;
}
}
3.编写账单服务类 该方法还需要其他服务类辅助 因此也需要再其他服务类内增加方法
public class BillService {
//定义BillDAO属性
private BillDAO billDAO = new BillDAO();
//需要定义另一个service获得菜单用来计算金额
private MenuService menuService = new MenuService();
//需要定义另一个service更改餐桌的状态
private DiningTableService diningTableService = new DiningTableService();
//编写点餐的方法
//1.生成账单 2. 需要更新对应餐桌状态 3.如果成功返回true 否则返回false
public boolean orderMenu(int menuId,int nums,int diningTableId){
//生成一个账单号UUID
String billUUID = UUID.randomUUID().toString();
//将这个账单生成到bill表 要直接计算账单的金额 也就是获得menu对象
int dml = billDAO.dml("insert into bill values" +
"(null,?,?,?,?,?,now(),'未结账')", billUUID, menuId, nums, menuService.getMenuId(menuId).getPrice() * nums, diningTableId);
if(dml <= 0){
return false;
}
//需要更新餐桌状态
return diningTableService.updateDiningTableState(diningTableId,"就餐中");
}
}
4.在view调用该服务类
//完成点餐
public void orderMenu() {
System.out.println("==============点餐服务============");
System.out.print("请输入点餐的桌号(-1退出): ");
int orderDiningTableId = Utility.readInt();
if (orderDiningTableId == -1) {
System.out.println("==============取消点餐============");
return;
}
System.out.print("请输入点餐的菜品号(-1退出): ");
int orderMenuId = Utility.readInt();
if (orderMenuId == -1) {
System.out.println("==============取消点餐============");
return;
}
System.out.print("请输入点餐的菜品量(-1退出): ");
int orderNums = Utility.readInt();
if (orderNums == -1) {
System.out.println("==============取消点餐============");
return;
}
//验证餐桌号是否存在.
DiningTable diningTable = diningTableService.getDiningTableById(orderDiningTableId);
if (diningTable == null) {
System.out.println("==============餐桌号不存在============");
return;
}
//验证菜品编号
Menu menu = menuService.getMenuId(orderMenuId);
if (menu == null) {
System.out.println("==============菜品号不存在============");
return;
}
//点餐
if (billService.orderMenu(orderMenuId, orderNums, orderDiningTableId)) {
System.out.println("==============点餐成功============");
} else {
System.out.println("==============点餐失败============");
}
}
7.查看账单
1.billDao获得bill对象
2.service编写方法调用billDAO获得bill对象
3.view调用该service方法
2.service调用DAO获得list数组
public List<Bill> list(){
return billDAO.queryMulti("select * from bill",Bill.class);
}
3.view显示账单
public void listBill() {
List<Bill> bills = billService.list();
System.out.println("\n编号\t\t菜品号\t\t菜品量\t\t金额\t\t桌号\t\t日期\t\t\t\t\t\t\t状态");
for (Bill bill : bills) {
System.out.println(bill);
}
System.out.println("==============显示完毕============");
}
8.结账
(1)对餐桌号进行校验 (2)修改bill表的state (3)修改diningTable信息 (4)不需要增加表和类
1.在 service类内查看是否有未结账 或者餐桌是否存在 (校验)
//查看某个产值是否有未结账的账单
public boolean hasPayBillByDiningTableId(int diningTableId) {
Bill bill =
billDAO.querySingle("SELECT * FROM bill WHERE diningTableId=? AND state = '未结账' LIMIT 0, 1", Bill.class, diningTableId);
return bill != null;
}
//完成结账[如果餐桌存在,并且该餐桌有未结账的账单]
//如果成功,返回true, 失败返回 false
public boolean payBill(int diningTableId, String payMode) {
//如果这里使用事务的话,需要用ThreadLocal来解决 , 框架中比如mybatis 提供了事务支持
//1. 修改bill表
int update = billDAO.dml("update bill set state=? where diningTableId=? and state='未结账'", payMode, diningTableId);
if(update <= 0) { //如果更新没有成功,则表示失败...
return false;
}
//2. 修改diningTable表
//注意:不要直接在这里操作,而应该调用DiningTableService 方法,完成更新,体现各司其职
if(!diningTableService.updateDiningTableToFree(diningTableId, "空")) {
return false;
}
return true;
}
2.修改diningTable为空闲
//需要提供一个更新餐桌状态为空闲的方法
public boolean updateDiningTableToFree(int id,String state){
int dml = diningTableDAO.dml("update diningTable set state=?,orderName = '',orderTel = '' where id=?", state, id);
return dml > 0;
}
3.在view内定义方法调用service方法
//完成结账
public void payBill() {
System.out.println("==============结账服务============");
System.out.print("请选择要结账的餐桌编号(-1退出): ");
int diningTableId = Utility.readInt();
if (diningTableId == -1) {
System.out.println("=============取消结账============");
return;
}
//验证餐桌是否存在
DiningTable diningTable = diningTableService.getDiningTableById(diningTableId);
if (diningTable == null) {
System.out.println("=============结账的餐桌不存在============");
return;
}
//验证餐桌是否有需要结账的账单
if (!billService.hasPayBillByDiningTableId(diningTableId)) {
System.out.println("=============该餐位没有未结账账单============");
return;
}
System.out.print("结账方式(现金/支付宝/微信)回车表示退出: ");
String payMode = Utility.readString(20, "");//说明如果回车,就是返回 ""
if ("".equals(payMode)) {
System.out.println("=============取消结账============");
return;
}
char key = Utility.readConfirmSelection();
if (key == 'Y') { //结账
//调用我们写的方法
if (billService.payBill(diningTableId, payMode)) {
System.out.println("=============完成结账============");
} else {
System.out.println("=============结账失败============");
}
} else {
System.out.println("=============取消结账============");
}
}
四、进行代码拓展
1.多表查询的需求
我们可以再增加一个MultableBean,跟多张表进行映射,所以要有一个MultableBeanDAO,然后调用该DAO
另一个方案
DBUtils数据库连接池多表连接查询_利用数据池连接数据库多个表-CSDN博客
1.创建bean表
public class MultiTableBean {
private Integer id;
private String billId;
private Integer menuId;
private Integer nums;
private Double money;
private Integer diningTableId;
private Date billDate;
private String state;
//增加一个来自menu表的列 name
//思考 这里的属性名是否一定要和表的列名保持一致.
//答: 可以不一致,但是需要sql做相应的修改, 规范需要保持一致.
private String name;
//增加来自menu表的列 price
private Double price;//默认值 nulll
public MultiTableBean() {
System.out.println("反射调用....");
}
// public MultiTableBean(Integer id, String billId, Integer menuId, Integer nums, Double money, Integer diningTableId, Date billDate, String state, String name, Double price) {
// this.id = id;
// this.billId = billId;
// this.menuId = menuId;
// this.nums = nums;
// this.money = money;
// this.diningTableId = diningTableId;
// this.billDate = billDate;
// this.state = state;
// this.name = name;
// this.price = price;
// }
//给price生成setter 和 getter
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
//给name生成setter 和 getter
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getBillId() {
return billId;
}
public void setBillId(String billId) {
this.billId = billId;
}
public Integer getMenuId() {
return menuId;
}
public void setMenuId(Integer menuId) {
this.menuId = menuId;
}
public Integer getNums() {
return nums;
}
public void setNums(Integer nums) {
this.nums = nums;
}
public Double getMoney() {
return money;
}
public void setMoney(Double money) {
this.money = money;
}
public Integer getDiningTableId() {
return diningTableId;
}
public void setDiningTableId(Integer diningTableId) {
this.diningTableId = diningTableId;
}
public Date getBillDate() {
return billDate;
}
public void setBillDate(Date billDate) {
this.billDate = billDate;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
@Override
public String toString() {
return id +
"\t\t" + menuId +
"\t\t\t" + nums +
"\t\t\t" + money +
"\t" + diningTableId +
"\t\t" + billDate +
"\t\t" + state +
"\t\t" + name +
"\t\t" + price;
}
}
2.创建dao类
3. view中调用
public void listBill() {
/*//单表查询
List<Bill> bills = billService.list();
System.out.println("\n编号\t\t菜品号\t\t菜品量\t\t金额\t\t桌号\t\t日期\t\t\t\t\t\t\t状态");
for (Bill bill : bills) {
System.out.println(bill);
}
System.out.println("==============显示完毕============");
*/
//多表查询
List<MultiTableBean> multiTableBeans = billService.list2();
System.out.println("\n编号\t\t菜品号\t\t菜品量\t\t金额\t\t桌号\t\t日期\t\t\t\t\t\t\t状态\t\t菜品名\t\t价格");
for (MultiTableBean bill : multiTableBeans) {
System.out.println(bill);
}
System.out.println("==============显示完毕============");
}
根据需要增加需要的字段
当将来的多表查询次数越来越多可以考虑拆分
2.javaBean和表的参数名一致性的问题
//增加一个来自menu表的列 name
//思考 这里的属性名是否一定要和表的列名保持一致.
//答: 可以不一致,但是需要sql做相应的修改(例如 name as name2), 规范需要保持一致.
3.对于员工多字段的设置
4.完成更多功能
可以完成登录管理和人事管理