一、界面设计
1. 需求说明
2 界面设计
2.1 用户登录
2.2 显示餐桌状态
2.3 预订
2.4 显示菜品
2.5 点餐
2.6 查看账单
2.7 结账
3 分层设计
二、功能实现
1 工具类
1.1 构建如下的项目结构
1.2 导入相关的工具类和jar包
- 相关的工具类和jar包会放在评论区的百度网盘的链接中
1.3 整体项目结构
1.4 相关sql语句,也就是要建立的表
#用户表
create table employee (id int primary key auto_increment, #自增empId varchar(50) not null default '',#员工号pwd char(32) not null default '',#密码md5name varchar(50) not null default '',#姓名job varchar(50) not null default '' #岗位
)charset=utf8; 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, '666', md5('123456'), '老韩', '经理');#餐桌表
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, '空','','');#菜谱
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);#账单流水, 考虑可以分开结账, 并考虑将来分别统计各个不同菜品的销售情况
create table bill (id int primary key auto_increment, #自增主键billId varchar(50) not null default '',#账单号可以按照自己规则生成 UUIDmenuId int not null default 0,#菜品的编号, 也可以使用外键nums SMALLINT 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;
2 界面层
package com.hspedu.mhl.view;import com.hspedu.mhl.domain.*;
import com.hspedu.mhl.service.BillService;
import com.hspedu.mhl.service.DiningService;
import com.hspedu.mhl.service.EmployeeService;
import com.hspedu.mhl.service.MenuService;
import com.hspedu.mhl.utils.Utility;import java.util.List;/*** @author 林然* @version 1.0*/
public class MHLView {//控制是否退出菜单private boolean loop =true;private String key="";//接收用户输入//定义一个employeeService对象private EmployeeService employeeService=new EmployeeService();//定义一个DiningTableService的属性DiningService diningService=new DiningService();//定义一个MenuService属性private MenuService menuService=new MenuService();//定义一个BillService属性BillService billService=new BillService();public static void main(String[] args) {new MHLView().mainMenue();}//显示所有餐桌状态public void listDiningTable(){List<DiningTable> list= diningService.list();System.out.println("\n餐桌编号\t\t 餐桌状态");for(DiningTable diningTable:list){System.out.println(diningTable);}System.out.println("显示完毕");}//预订餐桌public void book_table(int id){DiningTable diningTable=diningService.getDiningTableById(id);if (diningTable==null){System.out.println("餐桌号不存在,请重新预定");}else {if ("空".equals(diningTable.getState())){System.out.print("请确定是否预定y/n");String flag=Utility.readString(1);if("y".equals(flag)){System.out.print("请输入预订人姓名");String name=Utility.readString(10);System.out.print("请输入预定人电话");String tel=Utility.readString(20);if (diningService.orderDiningTable(id,name,tel)){System.out.println(id+"餐桌预定成功");}else {System.out.println(id+"餐桌预定失败");}}else {System.out.println("已取消预定");}}else {System.out.println("该餐桌已被预定,请重新预定");}}}//显示所有菜品public void show_menu(){List<Menu> list= menuService.list();for (Menu menu:list){System.out.println(menu);}}//完成点餐public void ordermenu(){System.out.println("=============点餐服务==============");System.out.print("请输入点餐桌号(-1退出):");int orderDiningId=Utility.readInt();//如果orderDiningId为-1,那么取消if(orderDiningId==-1){System.out.println("=============取消点餐==============");return;}System.out.print("请输入点餐菜品号(-1退出):");int menuId=Utility.readInt();//如果menuId为-1,那么取消if(menuId==-1){System.out.println("=============取消点餐==============");return;}System.out.print("请输入点餐菜品量(-1退出):");int ordernums=Utility.readInt();//如果ordernums为-1,那么取消if(ordernums==-1){System.out.println("=============取消点餐==============");return;}//验证餐桌号是否存在DiningTable diningTable=diningService.getDiningTableById(orderDiningId);if(diningTable==null){System.out.println("=============餐桌号不存在==============");return;}//验证菜品Menu menu=menuService.getMenuById(menuId);if(menu==null){System.out.println("=============菜品不存在==============");return;}//点餐if(billService.orderMenu(menuId,ordernums,orderDiningId)){System.out.println("=============点餐成功==============");}else {System.out.println("=============点餐失败==============");}}//显示所有账单信息public void listBill(){System.out.println("\n编号\t\t菜品号\t\t菜品量\t\t金额\t\t桌号\t\t\t日期\t\t\t\t\t\t状态\t\t菜品名");List<MultiTableBean> Bills=billService.list2();for (MultiTableBean bill :Bills){System.out.println(bill);}System.out.println("=============显示完毕==============");}//结账public void paybill(){System.out.println("=============结账服务==============");System.out.print("请选择要结账的餐桌编号(-1退出):");int diningTableId=Utility.readInt();if (diningTableId==-1){System.out.println("=============取消结账==============");return;}DiningTable diningTable=diningService.getDiningTableById(diningTableId);if(diningTable==null){System.out.println("=============结账餐桌不存在==============");return;}if(!billService.hasPayBillByDiningTableId(diningTableId)){System.out.println("=============该餐桌没有未结账账单==============");return;}System.out.print("结账方式(现金/微信/支付宝)表示退出:");String payMode=Utility.readString(10,"");//如果回车,就是返回""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("=============取消结账==============");}}public void mainMenue(){while (loop){System.out.println("=============满汉楼==============");System.out.println("\t\t 1 登录满汉楼");System.out.println("\t\t 2 退出满汉楼");System.out.print("请输入你的选择");key= Utility.readString(1);switch (key){case "1":System.out.println("请输入员工号:");String empId=Utility.readString(50);System.out.println("请输入密码");String pwd=Utility.readString(50);Employee employee=employeeService.getEmployeeByIdAndPwd(empId,pwd);if(employee!=null){//说明存在用户System.out.println("============登录成功【" +employee.getName()+"】================");//显示二级菜单,这里二级菜单是循环操作while (loop){System.out.println("=============满汉楼(二级菜单)==============");System.out.println("\t\t 1 显示餐桌状态");System.out.println("\t\t 2 预订餐桌");System.out.println("\t\t 3 显示所有菜品");System.out.println("\t\t 4 点餐服务");System.out.println("\t\t 5 查看账单");System.out.println("\t\t 6 结账");System.out.println("\t\t 9 退出满汉楼");System.out.print("请输入你的选择");key=Utility.readString(1);switch (key){case "1":listDiningTable();break;case "2":System.out.print("请输入你要预定的餐桌号");int id=Utility.readInt();book_table(id);break;case"3":System.out.println("菜品编号\t\t菜品名\t\t类别\t\t\t价格");show_menu();System.out.print("显示完毕");break;case"4":ordermenu();break;case "5":listBill();break;case"6":paybill();break;case"9":loop=false;break;default:System.out.println("你的输入有误吗,请重新输入");}}}else {System.out.println("=============登录失败===========");}break;case "2":System.out.println("退出满汉楼");loop=false;break;default:System.out.println("你的输入有误,请重新输入");}}}
}
3 domain层
一个类就对应一张表,如果是多表查询,那就可以新建一个类来存储多表查询的信息‘
3.1 Employee
package com.hspedu.mhl.domain;/*** @author 林然* @version 1.0* 这是一个javbean和employ对应*/
public class Employee {private Integer id;private String empId;private String pwd;private String name;private String job;public Employee() {//无参构造器,底层apache-dbutils反射需要}public Employee(Integer id, String empId, String pwd, String name, String job) {this.id = id;this.empId = empId;this.pwd = pwd;this.name = name;this.job = job;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getEmpId() {return empId;}public void setEmpId(String empId) {this.empId = empId;}public String getPwd() {return pwd;}public void setPwd(String pwd) {this.pwd = pwd;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getJob() {return job;}public void setJob(String job) {this.job = job;}
}
3.2 DiningTable
package com.hspedu.mhl.domain;/*** @author 林然* @version 1.0* 这是一个javabean 和 diningTable对应*/
public class DiningTable {private Integer id;private String state;private String name;private String orderTel;public DiningTable() {}public DiningTable(Integer id, String state, String name, String orderTel) {this.id = id;this.state = state;this.name = name;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 getName() {return name;}public void setName(String name) {this.name = name;}public String getOrderTel() {return orderTel;}public void setOrderTel(String orderTel) {this.orderTel = orderTel;}@Overridepublic String toString() {return id +"\t\t\t"+state;}
}
3.3 Menu
package com.hspedu.mhl.domain;/*** @author 林然* @version 1.0*/
public class Menu {private Integer id;private String name;private String type;private Double price;public Menu() {}public Menu(Integer id, String name, String type, Double price) {this.id = id;this.name = name;this.type = type;this.price = price;}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;}@Overridepublic String toString() {return id+"\t\t\t"+name+"\t\t"+type+"\t\t"+price;}
}
3.4 Bill
package com.hspedu.mhl.domain;import java.util.Date;/*** @author 林然* @version 1.0* Bill是一个javabean 和bill对应*/
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;}@Overridepublic String toString() {return id+"\t\t"+menuId+"\t\t\t"+nums+"\t\t\t"+money+"\t"+diningTableId+"\t\t"+billDate+"\t\t"+state;}
}
3.5 MultiTableBean
package com.hspedu.mhl.domain;import java.util.Date;/*** @author 林然* @version 1.0* 这是一个javabean,可以和多张表进行对应*/
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表的nameprivate String name;//思考:这里的属性名是否要和列名保持一致--可以不一致,但是建议保持一致// -但是这里如果多表查询有相同名字--可以使用别名来解决public MultiTableBean() {}public MultiTableBean(Integer id, String billId, Integer menuId, Integer nums, Double money, Integer diningTableId, Date billDate, String state, String name) {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;}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;}@Overridepublic 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;}
}
4 DAO层
4.1 BasicDAO
package com.hspedu.mhl.dao;import com.hspedu.mhl.utils.JDBCUtilsByDruid;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;/*** @author 韩顺平* @version 1.0* 开发BasicDAO , 是其他DAO的父类*/
public class BasicDAO<T> { //泛型指定具体类型private QueryRunner qr = new QueryRunner();//开发通用的dml方法, 针对任意的表public int update(String sql, Object... parameters) {Connection connection = null;try {connection = JDBCUtilsByDruid.getConnection();int update = qr.update(connection, sql, parameters);return update;} catch (SQLException e) {throw new RuntimeException(e); //将编译异常->运行异常 ,抛出} finally {JDBCUtilsByDruid.close(null, null, connection);}}//返回多个对象(即查询的结果是多行), 针对任意表/**** @param sql sql 语句,可以有 ?* @param clazz 传入一个类的Class对象 比如 Actor.class* @param parameters 传入 ? 的具体的值,可以是多个* @return 根据Actor.class 返回对应的 ArrayList 集合*/public List<T> queryMulti(String sql, Class<T> clazz, Object... parameters) {Connection connection = null;try {connection = JDBCUtilsByDruid.getConnection();return qr.query(connection, sql, new BeanListHandler<T>(clazz), parameters);} catch (SQLException e) {throw new RuntimeException(e); //将编译异常->运行异常 ,抛出} finally {JDBCUtilsByDruid.close(null, null, connection);}}//查询单行结果 的通用方法public T querySingle(String sql, Class<T> clazz, Object... parameters) {Connection connection = null;try {connection = JDBCUtilsByDruid.getConnection();return qr.query(connection, sql, new BeanHandler<T>(clazz), parameters);} catch (SQLException e) {throw new RuntimeException(e); //将编译异常->运行异常 ,抛出} finally {JDBCUtilsByDruid.close(null, null, connection);}}//查询单行单列的方法,即返回单值的方法public Object queryScalar(String sql, Object... parameters) {Connection connection = null;try {connection = JDBCUtilsByDruid.getConnection();return qr.query(connection, sql, new ScalarHandler(), parameters);} catch (SQLException e) {throw new RuntimeException(e); //将编译异常->运行异常 ,抛出} finally {JDBCUtilsByDruid.close(null, null, connection);}}}
4.2 BillDAO
package com.hspedu.mhl.dao;import com.hspedu.mhl.domain.Bill;/*** @author 林然* @version 1.0*/
public class BillDAO extends BasicDAO<Bill> {
}
4.3 DiningTableDAO
package com.hspedu.mhl.dao;import com.hspedu.mhl.domain.DiningTable;/*** @author 林然* @version 1.0*/
public class DiningTableDAO extends BasicDAO<DiningTable> {//这里还可以写特有的操作
}
4.4 EmployeeDAO
package com.hspedu.mhl.dao;import com.hspedu.mhl.domain.Employee;/*** @author 林然* @version 1.0*/
public class EmployeeDAO extends BasicDAO<Employee>{//这里还可以写特有的操作}
4.5 MenuDAO
package com.hspedu.mhl.dao;import com.hspedu.mhl.domain.Menu;/*** @author 林然* @version 1.0*/
public class MenuDAO extends BasicDAO<Menu> {//这里还可以写特有的操作
}
4.6 MultiTableDAO
package com.hspedu.mhl.dao;import com.hspedu.mhl.domain.MultiTableBean;/*** @author 林然* @version 1.0*/
public class MultiTableDAO extends BasicDAO<MultiTableBean> {
}
5 Service层
5.1 BillService
package com.hspedu.mhl.service;import com.hspedu.mhl.dao.BasicDAO;
import com.hspedu.mhl.dao.BillDAO;
import com.hspedu.mhl.dao.MultiTableDAO;
import com.hspedu.mhl.domain.Bill;
import com.hspedu.mhl.domain.MultiTableBean;import java.util.List;
import java.util.UUID;/*** @author 林然* @version 1.0* 处理和订单相关的业务逻辑*/
public class BillService {//定义BillDAO属性private BillDAO billDAO=new BillDAO();//定义MenuSerVice属性private MenuService menuService=new MenuService();//定义DiningServiceprivate DiningService diningService=new DiningService();//定义MultiTableDAO类private MultiTableDAO multiTableDAO=new MultiTableDAO();//编写点餐方法//1 生成账单//2 更新对应餐桌状态public boolean orderMenu(int menuId,int nums,int diningTableId){//生成一个账单号,UUIdString billID = UUID.randomUUID().toString();//将账单生成到bill表//要求计算账单金额,需要menuService来返回double price =menuService.getMenuById(menuId).getPrice();double money=price*nums;String sql ="insert into bill values(null,?,?,?,?,?,now(),'未结账')";int row= billDAO.update(sql,billID,menuId,nums,money,diningTableId);if(row<=0){return false;}//需要更新餐桌状态return diningService.updateDiningTableState(diningTableId,"就餐中");}//返回所有账单列表public List<Bill> list(){return billDAO.queryMulti("select * from bill",Bill.class);}//返回所有账单列表public List<MultiTableBean> list2(){String sql="select bill.*,name from bill,menu where bill.menuId=menu.id";return multiTableDAO.queryMulti(sql,MultiTableBean.class);}//查看某个餐桌是否有未结账的菜单public boolean hasPayBillByDiningTableId(int diningTableId){String sql="select * from bill where diningTableId=? and state='未结账' limit 0,1";return billDAO.querySingle(sql,Bill.class,diningTableId)!=null;}//完成结账【如果餐桌存在,并且该餐桌有未结账的账单public boolean payBill(int diningTableId,String payMode ){//修改bill表String sql="update bill set state=? where diningTableId=? and state='未结账'";int row=billDAO.update(sql,payMode,diningTableId);if(row<=0)return false;//修改diningTable表// 注意:不要在这里直接操作,而应该在diningService进行操作if(!diningService.updateDiningTableStateToFree(diningTableId)){return false;}return true;}
}
5.2 DiningService
package com.hspedu.mhl.service;import com.hspedu.mhl.dao.DiningTableDAO;
import com.hspedu.mhl.domain.DiningTable;import java.util.List;/*** @author 林然* @version 1.0*/
public class DiningService {//定义一个DiningTableDAO对象private DiningTableDAO diningTableDAO=new DiningTableDAO();//返回所有餐桌信息public List<DiningTable> list(){String sql="select id,state from diningTable";return diningTableDAO.queryMulti(sql,DiningTable.class);}//订座 根据id查询对应的餐桌 DiningTable对象,如果返回空表示id编号对应的餐桌不存在public DiningTable getDiningTableById(int id){String sql ="select * from diningTable where id=?";DiningTable diningTable=diningTableDAO.querySingle(sql,DiningTable.class,id);return diningTable;}//如果餐桌可以预订,调用方法对其进行更新(包括预定人的名字和电话)public boolean orderDiningTable(int id,String name,String orderTel){String sql="update diningTable set state='已经预定',orderName=?,orderTel=? where id=?";int row= diningTableDAO.update(sql,name,orderTel,id);return row>0;}//需要提供一个更新餐桌状态的方法public boolean updateDiningTableState(int Id,String state){String sql="update diningTable set state=? where id=? ";int row= diningTableDAO.update(sql,state,Id);return row>0;}//提供方法,将指定的的餐桌设置为空闲状态public boolean updateDiningTableStateToFree(int Id){String sql="update diningTable set state='空',orderName='',orderTel='' where id=? ";int row= diningTableDAO.update(sql,Id);return row>0;}}
5.3 EmployeeService
package com.hspedu.mhl.service;import com.hspedu.mhl.dao.EmployeeDAO;
import com.hspedu.mhl.domain.Employee;/*** @author 林然* @version 1.0* 该类对employee表的各种操作(通过调用EmployeeDAO对象完成)*/
public class EmployeeService {//定义一个 EmployeeDAO 属性private EmployeeDAO employeeDAO=new EmployeeDAO();//方法,根据empId和pwd返回一个对象‘//如果查询不到,就返回nullpublic Employee getEmployeeByIdAndPwd(String empId,String pwd){String sql="select * from employee where empId=? and pwd=md5(?)";Employee employee=employeeDAO.querySingle(sql,Employee.class,empId,pwd);return employee;}
}
5.4 MenuService
package com.hspedu.mhl.service;import com.hspedu.mhl.dao.MenuDAO;
import com.hspedu.mhl.domain.Menu;import java.util.List;/*** @author 林然* @version 1.0*/
public class MenuService {//定义一个 MenuDAO 属性private MenuDAO menuDAO=new MenuDAO();//显示所有菜品public List<Menu> list(){String sql="select * from menu";return menuDAO.queryMulti(sql,Menu.class);}public Menu getMenuById(int id){String sql="select * from menu where id=?";return menuDAO.querySingle(sql,Menu.class,id);}
}
三、演示视频
jdbc项目实战