#用户表
create table employee (
id int primary key auto_increment, #自增
empId varchar(50) not null default '',#员工号
pwd char(32) not null default '',#密码md5
name 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 '',#账单号可以按照自己规则生成 UUID
menuId 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;
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 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
一个类就对应一张表,如果是多表查询,那就可以新建一个类来存储多表查询的信息‘
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;
}
}
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;
}
@Override
public String toString() {
return id +"\t\t\t"+state;
}
}
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;
}
@Override
public String toString() {
return id+"\t\t\t"+name+"\t\t"+type+"\t\t"+price;
}
}
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;
}
@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;
}
}
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表的name
private 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;
}
@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;
}
}
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 { //泛型指定具体类型
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 queryMulti(String sql, Class clazz, Object... parameters) {
Connection connection = null;
try {
connection = JDBCUtilsByDruid.getConnection();
return qr.query(connection, sql, new BeanListHandler(clazz), parameters);
} catch (SQLException e) {
throw new RuntimeException(e); //将编译异常->运行异常 ,抛出
} finally {
JDBCUtilsByDruid.close(null, null, connection);
}
}
//查询单行结果 的通用方法
public T querySingle(String sql, Class clazz, Object... parameters) {
Connection connection = null;
try {
connection = JDBCUtilsByDruid.getConnection();
return qr.query(connection, sql, new BeanHandler(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);
}
}
}
package com.hspedu.mhl.dao;
import com.hspedu.mhl.domain.Bill;
/**
* @author 林然
* @version 1.0
*/
public class BillDAO extends BasicDAO {
}
package com.hspedu.mhl.dao;
import com.hspedu.mhl.domain.DiningTable;
/**
* @author 林然
* @version 1.0
*/
public class DiningTableDAO extends BasicDAO {
//这里还可以写特有的操作
}
package com.hspedu.mhl.dao;
import com.hspedu.mhl.domain.Employee;
/**
* @author 林然
* @version 1.0
*/
public class EmployeeDAO extends BasicDAO{
//这里还可以写特有的操作
}
package com.hspedu.mhl.dao;
import com.hspedu.mhl.domain.Menu;
/**
* @author 林然
* @version 1.0
*/
public class MenuDAO extends BasicDAO
package com.hspedu.mhl.dao;
import com.hspedu.mhl.domain.MultiTableBean;
/**
* @author 林然
* @version 1.0
*/
public class MultiTableDAO extends BasicDAO {
}
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();
//定义DiningService
private DiningService diningService=new DiningService();
//定义MultiTableDAO类
private MultiTableDAO multiTableDAO=new MultiTableDAO();
//编写点餐方法
//1 生成账单
//2 更新对应餐桌状态
public boolean orderMenu(int menuId,int nums,int diningTableId){
//生成一个账单号,UUId
String 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 list(){
return billDAO.queryMulti("select * from bill",Bill.class);
}
//返回所有账单列表
public List 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;
}
}
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 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;
}
}
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返回一个对象‘
//如果查询不到,就返回null
public 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;
}
}
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
jdbc项目实战