Java小项目--满汉楼

Java小项目–满汉楼

项目需求

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

项目实现

1.实现对工具包的编写

在这里插入图片描述
先创建libs包完成对jar包的拷贝和添加入库
德鲁伊工具包

package com.wantian.mhl.utils;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class JDBCUtilsDruid {
    private static DataSource ds;
    static {
        Properties properties = new Properties();
        try {
            properties.load(new FileInputStream("src\\druid.properties"));
            ds = DruidDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    public static Connection getConnection() throws SQLException {
        return ds.getConnection();
    }

    public static void close(ResultSet set, Statement statement,Connection connection){
        try {
            if (set != null) {
                set.close();
            }
            if (statement != null) {
                statement.close();
            }
            if (connection != null) {
                connection.close();
            }
        }catch (SQLException e){
            throw new RuntimeException();
        }
    }
}

工具包

package com.wantian.mhl.utils;


/**
	工具类的作用:
	处理各种情况的用户输入,并且能够按照程序员的需求,得到用户的控制台输入。
*/

import java.util.*;
/**

	
*/
public class Utility {
	//静态属性。。。
    private static Scanner scanner = new Scanner(System.in);

    
    /**
     * 功能:读取键盘输入的一个菜单选项,值:1——5的范围
     * @return 1——5
     */
	public static char readMenuSelection() {
        char c;
        for (; ; ) {
            String str = readKeyBoard(1, false);//包含一个字符的字符串
            c = str.charAt(0);//将字符串转换成字符char类型
            if (c != '1' && c != '2' && 
                c != '3' && c != '4' && c != '5') {
                System.out.print("选择错误,请重新输入:");
            } else break;
        }
        return c;
    }

	/**
	 * 功能:读取键盘输入的一个字符
	 * @return 一个字符
	 */
    public static char readChar() {
        String str = readKeyBoard(1, false);//就是一个字符
        return str.charAt(0);
    }
    /**
     * 功能:读取键盘输入的一个字符,如果直接按回车,则返回指定的默认值;否则返回输入的那个字符
     * @param defaultValue 指定的默认值
     * @return 默认值或输入的字符
     */
    
    public static char readChar(char defaultValue) {
        String str = readKeyBoard(1, true);//要么是空字符串,要么是一个字符
        return (str.length() == 0) ? defaultValue : str.charAt(0);
    }
	
    /**
     * 功能:读取键盘输入的整型,长度小于2位
     * @return 整数
     */
    public static int readInt() {
        int n;
        for (; ; ) {
            String str = readKeyBoard(2, false);//一个整数,长度<=2位
            try {
                n = Integer.parseInt(str);//将字符串转换成整数
                break;
            } catch (NumberFormatException e) {
                System.out.print("数字输入错误,请重新输入:");
            }
        }
        return n;
    }
    /**
     * 功能:读取键盘输入的 整数或默认值,如果直接回车,则返回默认值,否则返回输入的整数
     * @param defaultValue 指定的默认值
     * @return 整数或默认值
     */
    public static int readInt(int defaultValue) {
        int n;
        for (; ; ) {
            String str = readKeyBoard(10, true);
            if (str.equals("")) {
                return defaultValue;
            }
			
			//异常处理...
            try {
                n = Integer.parseInt(str);
                break;
            } catch (NumberFormatException e) {
                System.out.print("数字输入错误,请重新输入:");
            }
        }
        return n;
    }

    /**
     * 功能:读取键盘输入的指定长度的字符串
     * @param limit 限制的长度
     * @return 指定长度的字符串
     */

    public static String readString(int limit) {
        return readKeyBoard(limit, false);
    }

    /**
     * 功能:读取键盘输入的指定长度的字符串或默认值,如果直接回车,返回默认值,否则返回字符串
     * @param limit 限制的长度
     * @param defaultValue 指定的默认值
     * @return 指定长度的字符串
     */
	
    public static String readString(int limit, String defaultValue) {
        String str = readKeyBoard(limit, true);
        return str.equals("")? defaultValue : str;
    }


	/**
	 * 功能:读取键盘输入的确认选项,Y或N
	 * 将小的功能,封装到一个方法中.
	 * @return Y或N
	 */
    public static char readConfirmSelection() {
        System.out.println("请输入你的选择(Y/N)");
        char c;
        for (; ; ) {//无限循环
        	//在这里,将接受到字符,转成了大写字母
        	//y => Y n=>N
            String str = readKeyBoard(1, false).toUpperCase();
            c = str.charAt(0);
            if (c == 'Y' || c == 'N') {
                break;
            } else {
                System.out.print("选择错误,请重新输入:");
            }
        }
        return c;
    }

    /**
     * 功能: 读取一个字符串
     * @param limit 读取的长度
     * @param blankReturn 如果为true ,表示 可以读空字符串。 
     * 					  如果为false表示 不能读空字符串。
     * 			
	 *	如果输入为空,或者输入大于limit的长度,就会提示重新输入。
     * @return
     */
    private static String readKeyBoard(int limit, boolean blankReturn) {
        
		//定义了字符串
		String line = "";

		//scanner.hasNextLine() 判断有没有下一行
        while (scanner.hasNextLine()) {
            line = scanner.nextLine();//读取这一行
           
			//如果line.length=0, 即用户没有输入任何内容,直接回车
			if (line.length() == 0) {
                if (blankReturn) return line;//如果blankReturn=true,可以返回空串
                else continue; //如果blankReturn=false,不接受空串,必须输入内容
            }

			//如果用户输入的内容大于了 limit,就提示重写输入  
			//如果用户如的内容 >0 <= limit ,我就接受
            if (line.length() < 1 || line.length() > limit) {
                System.out.print("输入长度(不能大于" + limit + ")错误,请重新输入:");
                continue;
            }
            break;
        }

        return line;
    }
}

2.初步编写界面

package com.wantian.mhl.view;

import com.wantian.mhl.utils.Utility;

public class MHLView {
    // 控制是否退出菜单
    private boolean loop = true;
    private String key = "";

    public static void main(String[] args) {
        new MHLView().mainMenu();
    }

    //显示主菜单
    public void mainMenu() {
        while (loop) {
            System.out.println("==============满汉楼===============");
            System.out.println("\t\t 1 登录满汉楼");
            System.out.println("\t\t 2 退出满汉楼");
            System.out.println("请输入你的选择: ");
            key = Utility.readString(1);
            switch (key){
                case "1":
                    System.out.print("请输入员工号: ");
                    String id = Utility.readString(50);
                    System.out.print("请输入密  码: ");
                    String pwd = Utility.readString(50);
                    //到数据库去判断
                    if (pwd.equals("123")){
                        System.out.println("================登录成功===============\n");
                        //显示二级菜单
                        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.println("请输入你的选择");
                            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("================登录失败===============\n");
                    }
                    break;
                case "2":
                    System.out.println("退出满汉楼");
                    loop = false;
                    break;
                default:
                    System.out.println("你输入有误,请重新输入");
            }
        }
    }
}

3.创建Employee表,domain(类),EmployeeDAO和BasicDAO

创建Employee表

-- 创建满汉楼的表--
CREATE DATABASE mhl;
#用户表emplyee表(主键id,empId,name,pwd,job等)
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; 

-- 添加测试数据
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'), '老韩', '经理');

创建Employee类

package com.wantian.mhl.domain;

/**
 * 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 '' #岗位
 */
public class Employee {
    private Integer id;
    private String empId;
    private String pwd;
    private String name;
    private String job;

    public Employee() {
    }

    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;
    }
}

创建BasicDAO表

package com.wantian.mhl.dao;

import com.wantian.mhl.utils.JDBCUtilsDruid;
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;

public class BasicDAO <T>{
    private QueryRunner qr = new QueryRunner();
    private int update(String sql,Object...parameters){
        Connection connection = null;
        try {
            connection= JDBCUtilsDruid.getConnection();
            int update = qr.update(sql, parameters);
            return update;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            JDBCUtilsDruid.close(null,null,connection);
        }
    }

    public List<T> queryMultiply(String sql,Class<T> clazz,Object...parameters){
        Connection connection = null;
        try {
            connection = JDBCUtilsDruid.getConnection();
            List<T> query = qr.query(connection, sql, new BeanListHandler<T>(clazz), parameters);
            return query;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            JDBCUtilsDruid.close(null,null,connection);
        }
    }
    public T querySingle(String sql,Class<T> clazz,Object...parameters){
        Connection connection = null;
        try {
            connection = JDBCUtilsDruid.getConnection();
            T query = qr.query(connection, sql, new BeanHandler<T>(clazz), parameters);
            return query;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            JDBCUtilsDruid.close(null,null,connection);
        }
    }

    public Object queryScalar(String sql,Object...parameters){
        Connection connection = null;
        try {
            connection = JDBCUtilsDruid.getConnection();
            Object query = qr.query(connection, sql, new ScalarHandler(), parameters);
            return query;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            JDBCUtilsDruid.close(null,null,connection);
        }
    }
}

创建EmployeeDAO

package com.wantian.mhl.dao;

import com.wantian.mhl.domain.Employee;

public class EmployeeDAO extends BasicDAO<Employee>{
    //这里可以写特有的操作
}

4.创建一个EmployeeService

package com.wantian.mhl.service;

import com.wantian.mhl.dao.EmployeeDAO;
import com.wantian.mhl.domain.Employee;

/**
 * 该类完成对employee表的各种业务操作
 */
public class EmployeeService {
    //定义一个Employee属性
    private EmployeeDAO employeeDAO = new EmployeeDAO();

    //方法,根据 empId 和 pwd 返回一个Employee对象
    //如果查询不到,就返回一个null
    public Employee getEmployeeByIdAndPwd(String empId,String pwd){
        Employee employee =
                employeeDAO.querySingle("select * from employee where empId = ? and pwd = md5(?)", Employee.class, empId, pwd);
        return employee;
    }
}

然后将其添加到菜单界面进行判定
这个位置

Employee employee = employeeService.getEmployeeByIdAndPwd(empId, pwd);
                    //到数据库去判断
                    if (employee != null){
                        System.out.println("================登录成功["+ employee.getName() + "]===============\n");

5.创建Dining表,domain(类DiningTable),DiningTableDAO,DingTableService

创建dining表

-- 创建dining表(id,state,orderName等)
#餐桌表
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, '空','','');

创建DiningTable

package com.wantian.mhl.domain;

/**
 * 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 ;
    }
}

创建DiningTableDAO

package com.wantian.mhl.dao;

public class DiningTableDAO extends BasicDAO<DiningTable>{
    //如果有特殊操作可以添加
}

创建DiningTableService

package com.wantian.mhl.service;

import com.wantian.mhl.dao.DiningTableDAO;
import com.wantian.mhl.domain.DiningTable;

import java.util.List;

public class DiningTableService {
    // 定义一个DiningTableDAO对象
    private DiningTableDAO diningTableDAO = new DiningTableDAO();
    public List<DiningTable> list() {
        List<DiningTable> diningTables = diningTableDAO.queryMultiply("select id, state from diningTable", DiningTable.class);
        return diningTables;
    }

6.完成对餐桌的预订

检测餐桌是否存在和餐桌的状态

//根据id,查询对应的餐桌DiningTable 对象
    //如果返回null,表示id编号对应的餐桌不存在
    public DiningTable getDiningTableById(int id){
        return diningTableDAO.querySingle("select * from diningTable where id = ?",DiningTable.class,id);
    }
    //如果餐桌可以预订,调用方法,对其状态进行更新(包括预订人的名字和电话)
    public boolean orderDiningTable(int id,String orderName,String orderTel){
        int update = diningTableDAO.update("update diningTable set state = '已经预订',orderName = ?,orderTel = ? where id = ?", orderName, orderTel, id);
        return update > 0;
    }

然后编写进入界面

public void orderDiningTable(){
        System.out.println("=============预订餐桌==============");
        System.out.println("请选择要预订餐桌的编号(-1 表示退出预订)");
        int orderId = Utility.readInt();
        if (orderId == -1){
            System.out.println("==============取消预订餐桌================");
            return;
        }
        char key = Utility.readConfirmSelection();
        if (key == 'Y'){
            // 根据orderId,返回对应DiningTable对象,如果为null,说明该对象不存在
            DiningTable diningTable = diningTableService.getDiningTableById(orderId);
            if (diningTable == null){
                System.out.println("餐桌不存在");
                return;
            }
            //判断该餐桌状态是否为“空”
            if(!("空".equals(diningTable.getState()))){
                System.out.println("==============餐桌已被预订==============");
                return;
            }
            //这时,得到相关信息并完成预订
            System.out.print("请输入预订人的名字");
            String orderName = Utility.readString(50);
            System.out.print("请输入预订人的电话:");
            String orderTel = Utility.readString(50);
            if(diningTableService.orderDiningTable(orderId,orderName,orderTel)) {
                System.out.println("===============预订餐桌成功==============");
            }else {
                System.out.println("===============预订餐桌失败==============");
            }
        }else {
            System.out.println("==================取消预订餐桌===============");
        }
    }

7.创建menu表,domain类,和MenuDAO,MenuService

创建menu表

#菜谱
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);

Menu类

package com.wantian.mhl.domain;

/**
 * id int primary key auto_increment, #自增主键,作为菜谱编号(唯一)
 * 	name varchar(50) not null default '',#菜品名称
 * 	type varchar(50) not null default '', #菜品种类
 * 	price double not null default 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;
    }
}

创建MenuDAO

public class MenuDAO extends BasicDAO<Menu>{
    //这里可以创建特殊方法
}

创建MenuService

package com.wantian.mhl.service;

import com.wantian.mhl.dao.MenuDAO;
import com.wantian.mhl.domain.Menu;

import java.util.List;

public class MenuService {

    //定义MenuDAO
    private MenuDAO menuDAO = new MenuDAO();

    //返回所有的菜品
    public List<Menu> list(){
        return menuDAO.queryMultiply("select * from menu",Menu.class);
    }
 }

创建bill表,bill类,BillDAO,BillService

创建bill表

#账单流水, 考虑可以分开结账, 并考虑将来分别统计各个不同菜品的销售情况
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;

创建bill类

package com.wantian.mhl.domain;

import java.util.Date;

/**
 *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 '' # 状态 '未结账' , '已经结账', '挂单'
 */
public class Bill {
    private Integer id;
    private String billId;
    private Integer menuId;
    private Integer numbers;
    private Double money;
    private Integer diningId;
    private Date billDate;
    private String state;

    public Bill() {
    }

    public Bill(Integer id, String billId, Integer menuId, Integer numbers, Double money, Integer diningId, Date billDate, String state) {
        this.id = id;
        this.billId = billId;
        this.menuId = menuId;
        this.numbers = numbers;
        this.money = money;
        this.diningId = diningId;
        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 getNumbers() {
        return numbers;
    }

    public void setNumbers(Integer numbers) {
        this.numbers = numbers;
    }

    public Double getMoney() {
        return money;
    }

    public void setMoney(Double money) {
        this.money = money;
    }

    public Integer getDiningId() {
        return diningId;
    }

    public void setDiningId(Integer diningId) {
        this.diningId = diningId;
    }

    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;
    }
}

创建BillDAO

package com.wantian.mhl.dao;

import com.wantian.mhl.domain.Bill;

public class BillDAO extends BasicDAO<Bill>{
    //这里可以进行特殊方法的添加
}

进行BillService编写

package com.wantian.mhl.service;

import com.wantian.mhl.dao.BasicDAO;
import com.wantian.mhl.dao.BillDAO;
import com.wantian.mhl.domain.Menu;

import java.util.UUID;

public class BillService {
    // 定义BillDAO属性
    private BillDAO billDAO = new BillDAO();

    // 定义MenuService
    private MenuService menuService = new MenuService();

    // 定义DiningTableService
    private DiningTableService diningTableService = new DiningTableService();
    // 编写点餐的方法
    // 1.生成菜单
    // 2.需要更新餐桌的状态
    public boolean orderMenu(int menuId,int numbers,int diningTableId){
        // 生成一个账单号,UUID
        String billId = UUID.randomUUID().toString();

        // 将账单生成到bill表
        // 需要计算账单金额
        int update = billDAO.update("insert into bill values(null,?,?,?,?,?,now(),'未结账')", billId, menuId, numbers, menuService.getMenuById(menuId).getPrice() * numbers, diningTableId);
        if(update <= 0){
            return false;
        }
        return diningTableService.updateDiningTableState(diningTableId,"就餐中");
    }

}

这里在其它的如DiningTableService里,增加了特殊方法,来设置状态

public boolean updateDiningTableState(int id,String state){
        int update = diningTableDAO.update("update diningTable set state = ? where id = ?", state, id);
        return update > 0;
    }

编写界面和判断数据是否合理

public void orderMenu(){
        System.out.println("================点餐服务==============");
        System.out.print("请输入点餐的桌号(-1退出):");
        int orderDiningTableId = Utility.readInt();
        if (orderDiningTableId == -1){
            System.out.println("=============取消点餐=============");
            return;
        }
        System.out.println("请输入点餐的菜品号(-1退出): ");
        int orderMenuId = Utility.readInt();
        if (orderMenuId == -1){
            System.out.println("=============取消点餐=============");
            return;
        }
        System.out.println("请输入点餐的菜品量(-1退出): ");
        int orderNumbers = Utility.readInt();
        if (orderNumbers == -1){
            System.out.println("=============取消点餐=============");
            return;
        }

        // 验证数据是否存在(餐桌)
        DiningTable diningTable = diningTableService.getDiningTableById(orderDiningTableId);
        if (diningTable == null){
            System.out.println("===============餐桌不存在==============");
            return;
        }

        // 验证菜品编号
        Menu menu = menuService.getMenuById(orderMenuId);
        if (menu == null){
            System.out.println("===============菜品不存在===============");
            return;
        }
        if(billService.orderMenu(orderMenuId,orderNumbers,orderDiningTableId)){
            System.out.println("===============点餐成功=================");
        }else {
            System.out.println("===============点餐失败=================");
        }
    }

返回查询账单

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("===============显示完毕===============");
    }

实现结账

编写设置状态的方法

public boolean updateDiningTableToFree(int id,String state){
        int update = diningTableDAO.update("update diningTable set state = ? ,orderName = ' ', orderTel = ' ',where id = ?", state, id);
        return update > 0;
    }

编写提交账单方法

public boolean payBill(int diningTableId,String payMode) {
        // 1. 修改bill表
        int update = billDAO.update("update bill set state = ? where diningTableId = ? and state = '未结账'", payMode, diningTableId);
        if (update <= 0){
            return false;
        }//如果更新没有成功,表示失败

        // 2. 修改diningTable表
        if (!diningTableService.updateDiningTableToFree(diningTableId,"空")){
            return false;
        }
        return true;
    }

界面编写结账

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("================取消结账===============");
        }
    }

完成一个多表映射

完成多表账单的编写

package com.wantian.mhl.domain;

import java.util.Date;

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 getNumbers() {
        return nums;
    }

    public void setNumbers(Integer numbers) {
        this.nums = numbers;
    }

    public Double getMoney() {
        return money;
    }

    public void setMoney(Double money) {
        this.money = money;
    }

    public Integer getDiningId() {
        return diningTableId;
    }

    public void setDiningId(Integer diningId) {
        this.diningTableId = diningId;
    }

    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;
    }

    public Integer getNums() {
        return nums;
    }

    public void setNums(Integer nums) {
        this.nums = nums;
    }

    public Integer getDiningTableId() {
        return diningTableId;
    }

    public void setDiningTableId(Integer diningTableId) {
        this.diningTableId = diningTableId;
    }

    @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;
    }
}

完成对返回账单的升级(多表查询)

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菜品名");
        for (MultiTableBean multiTableBean : multiTableBeans) {
            System.out.println(multiTableBean);
        }
    }

view

package com.wantian.mhl.view;

import com.wantian.mhl.domain.*;
import com.wantian.mhl.service.BillService;
import com.wantian.mhl.service.DiningTableService;
import com.wantian.mhl.service.EmployeeService;
import com.wantian.mhl.service.MenuService;
import com.wantian.mhl.utils.Utility;

import java.util.List;

public class MHLView {
    // 控制是否退出菜单
    private boolean loop = true;
    private String key = "";
    private EmployeeService employeeService = new EmployeeService();
    private DiningTableService diningTableService = new DiningTableService();
    private MenuService menuService = new MenuService();
    private BillService billService = new BillService();
    public static void main(String[] args) {
        new MHLView().mainMenu();
    }

    //显示主菜单
    public void mainMenu() {
        while (loop) {
            System.out.println("==============满汉楼===============");
            System.out.println("\t\t 1 登录满汉楼");
            System.out.println("\t\t 2 退出满汉楼");
            System.out.println("请输入你的选择: ");
            key = Utility.readString(1);
            switch (key){
                case "1":
                    System.out.print("请输入员工号: ");
                    String empId = Utility.readString(50);
                    System.out.print("请输入密  码: ");
                    String pwd = Utility.readString(50);
                    Employee employee = employeeService.getEmployeeByIdAndPwd(empId, pwd);
                    //到数据库去判断
                    if (employee != null){
                        System.out.println("================登录成功["+ employee.getName() + "]===============\n");
                        //显示二级菜单
                        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.println("请输入你的选择");
                            key = Utility.readString(1);
                            switch (key){
                                case "1":
                                    listDiningTable();
                                    break;
                                case "2":
                                    orderDiningTable();
                                    break;
                                case "3":
                                    listMenu();
                                    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("================登录失败===============\n");
                    }
                    break;
                case "2":
                    System.out.println("退出满汉楼");
                    loop = false;
                    break;
                default:
                    System.out.println("你输入有误,请重新输入");
            }
        }
    }

    //显示所有菜品
    public void listMenu(){
        List<Menu> list = menuService.list();
        System.out.println("\n菜品编号\t\t菜品名\t\t类别\t\t\t价格");
        for (Menu menu : list) {
            System.out.println(menu);
        }
        System.out.println("================显示完毕===============");
    }

    // 显示所有餐桌状态
    public void listDiningTable(){
        List<DiningTable> list = diningTableService.list();
        System.out.println("\n餐桌编号\t\t餐桌状态");
        for (DiningTable diningTable : list) {
            System.out.println(diningTable);
        }
        System.out.println("==============显示完毕==============");
    }

    // 完成预订餐桌
    public void orderDiningTable(){
        System.out.println("=============预订餐桌==============");
        System.out.println("请选择要预订餐桌的编号(-1 表示退出预订)");
        int orderId = Utility.readInt();
        if (orderId == -1){
            System.out.println("==============取消预订餐桌================");
            return;
        }
        char key = Utility.readConfirmSelection();
        if (key == 'Y'){
            // 根据orderId,返回对应DiningTable对象,如果为null,说明该对象不存在
            DiningTable diningTable = diningTableService.getDiningTableById(orderId);
            if (diningTable == null){
                System.out.println("餐桌不存在");
                return;
            }
            //判断该餐桌状态是否为“空”
            if(!("空".equals(diningTable.getState()))){
                System.out.println("==============餐桌已被预订==============");
                return;
            }
            //这时,得到相关信息并完成预订
            System.out.print("请输入预订人的名字");
            String orderName = Utility.readString(50);
            System.out.print("请输入预订人的电话:");
            String orderTel = Utility.readString(50);
            if(diningTableService.orderDiningTable(orderId,orderName,orderTel)) {
                System.out.println("===============预订餐桌成功==============");
            }else {
                System.out.println("===============预订餐桌失败==============");
            }
        }else {
            System.out.println("==================取消预订餐桌===============");
        }
    }
    // 完成点餐
    public void orderMenu(){
        System.out.println("================点餐服务==============");
        System.out.print("请输入点餐的桌号(-1退出):");
        int orderDiningTableId = Utility.readInt();
        if (orderDiningTableId == -1){
            System.out.println("=============取消点餐=============");
            return;
        }
        System.out.println("请输入点餐的菜品号(-1退出): ");
        int orderMenuId = Utility.readInt();
        if (orderMenuId == -1){
            System.out.println("=============取消点餐=============");
            return;
        }
        System.out.println("请输入点餐的菜品量(-1退出): ");
        int orderNumbers = Utility.readInt();
        if (orderNumbers == -1){
            System.out.println("=============取消点餐=============");
            return;
        }

        // 验证数据是否存在(餐桌)
        DiningTable diningTable = diningTableService.getDiningTableById(orderDiningTableId);
        if (diningTable == null){
            System.out.println("===============餐桌不存在==============");
            return;
        }

        // 验证菜品编号
        Menu menu = menuService.getMenuById(orderMenuId);
        if (menu == null){
            System.out.println("===============菜品不存在===============");
            return;
        }
        if(billService.orderMenu(orderMenuId,orderNumbers,orderDiningTableId)){
            System.out.println("===============点餐成功=================");
        }else {
            System.out.println("===============点餐失败=================");
        }
    }

    // 显示账单信息
    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 multiTableBean : multiTableBeans) {
            System.out.println(multiTableBean);
        }
    }
    // 完成结账
    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("================取消结账===============");
        }
    }
}

Utils

Utility

package com.wantian.mhl.utils;


/**
	工具类的作用:
	处理各种情况的用户输入,并且能够按照程序员的需求,得到用户的控制台输入。
*/

import java.util.*;
/**

	
*/
public class Utility {
	//静态属性。。。
    private static Scanner scanner = new Scanner(System.in);

    
    /**
     * 功能:读取键盘输入的一个菜单选项,值:1——5的范围
     * @return 1——5
     */
	public static char readMenuSelection() {
        char c;
        for (; ; ) {
            String str = readKeyBoard(1, false);//包含一个字符的字符串
            c = str.charAt(0);//将字符串转换成字符char类型
            if (c != '1' && c != '2' && 
                c != '3' && c != '4' && c != '5') {
                System.out.print("选择错误,请重新输入:");
            } else break;
        }
        return c;
    }

	/**
	 * 功能:读取键盘输入的一个字符
	 * @return 一个字符
	 */
    public static char readChar() {
        String str = readKeyBoard(1, false);//就是一个字符
        return str.charAt(0);
    }
    /**
     * 功能:读取键盘输入的一个字符,如果直接按回车,则返回指定的默认值;否则返回输入的那个字符
     * @param defaultValue 指定的默认值
     * @return 默认值或输入的字符
     */
    
    public static char readChar(char defaultValue) {
        String str = readKeyBoard(1, true);//要么是空字符串,要么是一个字符
        return (str.length() == 0) ? defaultValue : str.charAt(0);
    }
	
    /**
     * 功能:读取键盘输入的整型,长度小于2位
     * @return 整数
     */
    public static int readInt() {
        int n;
        for (; ; ) {
            String str = readKeyBoard(2, false);//一个整数,长度<=2位
            try {
                n = Integer.parseInt(str);//将字符串转换成整数
                break;
            } catch (NumberFormatException e) {
                System.out.print("数字输入错误,请重新输入:");
            }
        }
        return n;
    }
    /**
     * 功能:读取键盘输入的 整数或默认值,如果直接回车,则返回默认值,否则返回输入的整数
     * @param defaultValue 指定的默认值
     * @return 整数或默认值
     */
    public static int readInt(int defaultValue) {
        int n;
        for (; ; ) {
            String str = readKeyBoard(10, true);
            if (str.equals("")) {
                return defaultValue;
            }
			
			//异常处理...
            try {
                n = Integer.parseInt(str);
                break;
            } catch (NumberFormatException e) {
                System.out.print("数字输入错误,请重新输入:");
            }
        }
        return n;
    }

    /**
     * 功能:读取键盘输入的指定长度的字符串
     * @param limit 限制的长度
     * @return 指定长度的字符串
     */

    public static String readString(int limit) {
        return readKeyBoard(limit, false);
    }

    /**
     * 功能:读取键盘输入的指定长度的字符串或默认值,如果直接回车,返回默认值,否则返回字符串
     * @param limit 限制的长度
     * @param defaultValue 指定的默认值
     * @return 指定长度的字符串
     */
	
    public static String readString(int limit, String defaultValue) {
        String str = readKeyBoard(limit, true);
        return str.equals("")? defaultValue : str;
    }


	/**
	 * 功能:读取键盘输入的确认选项,Y或N
	 * 将小的功能,封装到一个方法中.
	 * @return Y或N
	 */
    public static char readConfirmSelection() {
        System.out.println("请输入你的选择(Y/N)");
        char c;
        for (; ; ) {//无限循环
        	//在这里,将接受到字符,转成了大写字母
        	//y => Y n=>N
            String str = readKeyBoard(1, false).toUpperCase();
            c = str.charAt(0);
            if (c == 'Y' || c == 'N') {
                break;
            } else {
                System.out.print("选择错误,请重新输入:");
            }
        }
        return c;
    }

    /**
     * 功能: 读取一个字符串
     * @param limit 读取的长度
     * @param blankReturn 如果为true ,表示 可以读空字符串。 
     * 					  如果为false表示 不能读空字符串。
     * 			
	 *	如果输入为空,或者输入大于limit的长度,就会提示重新输入。
     * @return
     */
    private static String readKeyBoard(int limit, boolean blankReturn) {
        
		//定义了字符串
		String line = "";

		//scanner.hasNextLine() 判断有没有下一行
        while (scanner.hasNextLine()) {
            line = scanner.nextLine();//读取这一行
           
			//如果line.length=0, 即用户没有输入任何内容,直接回车
			if (line.length() == 0) {
                if (blankReturn) return line;//如果blankReturn=true,可以返回空串
                else continue; //如果blankReturn=false,不接受空串,必须输入内容
            }

			//如果用户输入的内容大于了 limit,就提示重写输入  
			//如果用户如的内容 >0 <= limit ,我就接受
            if (line.length() < 1 || line.length() > limit) {
                System.out.print("输入长度(不能大于" + limit + ")错误,请重新输入:");
                continue;
            }
            break;
        }

        return line;
    }
}

JDBCUtilsDruid

package com.wantian.mhl.utils;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class JDBCUtilsDruid {
    private static DataSource ds;
    static {
        Properties properties = new Properties();
        try {
            properties.load(new FileInputStream("src\\druid.properties"));
            ds = DruidDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    public static Connection getConnection() throws SQLException {
        return ds.getConnection();
    }

    public static void close(ResultSet set, Statement statement,Connection connection){
        try {
            if (set != null) {
                set.close();
            }
            if (statement != null) {
                statement.close();
            }
            if (connection != null) {
                connection.close();
            }
        }catch (SQLException e){
            throw new RuntimeException();
        }
    }
}

Service

BillService

package com.wantian.mhl.service;

import com.wantian.mhl.dao.BasicDAO;
import com.wantian.mhl.dao.BillDAO;
import com.wantian.mhl.dao.MultiTableDAO;
import com.wantian.mhl.domain.Bill;
import com.wantian.mhl.domain.Menu;
import com.wantian.mhl.domain.MultiTableBean;

import java.util.List;
import java.util.UUID;

public class BillService {
    // 定义BillDAO属性
    private BillDAO billDAO = new BillDAO();

    // 定义MenuService
    private MenuService menuService = new MenuService();

    private MultiTableDAO multiTableDAO = new MultiTableDAO();
    // 定义DiningTableService
    private DiningTableService diningTableService = new DiningTableService();
    // 编写点餐的方法
    // 1.生成菜单
    // 2.需要更新餐桌的状态
    public boolean orderMenu(int menuId,int numbers,int diningTableId){
        // 生成一个账单号,UUID
        String billId = UUID.randomUUID().toString();

        // 将账单生成到bill表
        // 需要计算账单金额
        int update = billDAO.update("insert into bill values(null,?,?,?,?,?,now(),'未结账')", billId, menuId, numbers, menuService.getMenuById(menuId).getPrice() * numbers, diningTableId);
        if(update <= 0){
            return false;
        }
        return diningTableService.updateDiningTableState(diningTableId,"就餐中");
    }

    // 返回所有的账单,提供给View调用
    public List<Bill> list(){
        return billDAO.queryMultiply("select * from bill",Bill.class);
    }

    public List<MultiTableBean> list2(){
        return multiTableDAO.queryMultiply("select bill.*,`name` ,price from bill,menu where bill.menuId = menu.id",
                MultiTableBean.class);
    }// 返回所有的账单,并带有菜品名

    // 查看某张餐桌是否有未结账的账单
    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;
    }

    // 如果餐桌存在,且未结账,就完成结账
    public boolean payBill(int diningTableId,String payMode) {
        // 1. 修改bill表
        int update = billDAO.update("update bill set state = ? where diningTableId = ? and state = '未结账'", payMode, diningTableId);
        if (update <= 0){
            return false;
        }//如果更新没有成功,表示失败

        // 2. 修改diningTable表
        if (!diningTableService.updateDiningTableToFree(diningTableId,"空")){
            return false;
        }
        return true;
    }
}

DiningTableService

package com.wantian.mhl.service;

import com.wantian.mhl.dao.DiningTableDAO;
import com.wantian.mhl.domain.DiningTable;

import java.util.List;

public class DiningTableService {
    // 定义一个DiningTableDAO对象
    private DiningTableDAO diningTableDAO = new DiningTableDAO();
    public List<DiningTable> list() {
        List<DiningTable> diningTables = diningTableDAO.queryMultiply("select id, state from diningTable", DiningTable.class);
        return diningTables;
    }

    //根据id,查询对应的餐桌DiningTable 对象
    //如果返回null,表示id编号对应的餐桌不存在
    public DiningTable getDiningTableById(int id){
        return diningTableDAO.querySingle("select * from diningTable where id = ?",DiningTable.class,id);
    }
    //如果餐桌可以预订,调用方法,对其状态进行更新(包括预订人的名字和电话)
    public boolean orderDiningTable(int id,String orderName,String orderTel){
        int update = diningTableDAO.update("update diningTable set state = '已经预订',orderName = ?,orderTel = ? where id = ?", orderName, orderTel, id);
        return update > 0;
    }
    // 需要一个更新餐桌状态的方法
    public boolean updateDiningTableState(int id,String state){
        int update = diningTableDAO.update("update diningTable set state = ? where id = ?", state, id);
        return update > 0;
    }

    public boolean updateDiningTableToFree(int id,String state){
        int update = diningTableDAO.update("update diningTable set state = ? ,orderName = ' ', orderTel = ' ' where id = ?", state, id);
        return update > 0;
    }
}

EmployeeService

package com.wantian.mhl.service;

import com.wantian.mhl.dao.EmployeeDAO;
import com.wantian.mhl.domain.Employee;

/**
 * 该类完成对employee表的各种业务操作
 */
public class EmployeeService {
    //定义一个Employee属性
    private EmployeeDAO employeeDAO = new EmployeeDAO();

    //方法,根据 empId 和 pwd 返回一个Employee对象
    //如果查询不到,就返回一个null
    public Employee getEmployeeByIdAndPwd(String empId,String pwd){
        Employee employee =
                employeeDAO.querySingle("select * from employee where empId = ? and pwd = md5(?)", Employee.class, empId, pwd);
        return employee;
    }
}

MenuService

package com.wantian.mhl.service;

import com.wantian.mhl.dao.MenuDAO;
import com.wantian.mhl.domain.Menu;

import java.util.List;

public class MenuService {

    //定义MenuDAO
    private MenuDAO menuDAO = new MenuDAO();

    //返回所有的菜品
    public List<Menu> list(){
        return menuDAO.queryMultiply("select * from menu",Menu.class);
    }

    // 需要方法,根据id返回Menu对象
    public Menu getMenuById(int id){
        return menuDAO.querySingle("select * from menu where id = ?", Menu.class,id);
    }
 }

domain

Bill

package com.wantian.mhl.domain;

import java.util.Date;

/**
 *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 '' # 状态 '未结账' , '已经结账', '挂单'
 */
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 numbers, Double money, Integer diningId, Date billDate, String state) {
        this.id = id;
        this.billId = billId;
        this.menuId = menuId;
        this.nums = numbers;
        this.money = money;
        this.diningTableId = diningId;
        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 getNumbers() {
        return nums;
    }

    public void setNumbers(Integer numbers) {
        this.nums = numbers;
    }

    public Double getMoney() {
        return money;
    }

    public void setMoney(Double money) {
        this.money = money;
    }

    public Integer getDiningId() {
        return diningTableId;
    }

    public void setDiningId(Integer diningId) {
        this.diningTableId = diningId;
    }

    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;
    }

    public Integer getNums() {
        return nums;
    }

    public void setNums(Integer nums) {
        this.nums = nums;
    }

    public Integer getDiningTableId() {
        return diningTableId;
    }

    public void setDiningTableId(Integer diningTableId) {
        this.diningTableId = diningTableId;
    }

    @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;
    }
}

DiningTable

package com.wantian.mhl.domain;

/**
 * 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 ;
    }

Employee

package com.wantian.mhl.domain;

/**
 * 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 '' #岗位
 */
public class Employee {
    private Integer id;
    private String empId;
    private String pwd;
    private String name;
    private String job;

    public Employee() {
    }

    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;
    }
}

Menu

package com.wantian.mhl.domain;

/**
 * id int primary key auto_increment, #自增主键,作为菜谱编号(唯一)
 * 	name varchar(50) not null default '',#菜品名称
 * 	type varchar(50) not null default '', #菜品种类
 * 	price double not null default 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;
    }
}

MultiTableBean

package com.wantian.mhl.domain;

import java.util.Date;

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;
    private Double price;

    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;
    }

    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 Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    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 getNumbers() {
        return nums;
    }

    public void setNumbers(Integer numbers) {
        this.nums = numbers;
    }

    public Double getMoney() {
        return money;
    }

    public void setMoney(Double money) {
        this.money = money;
    }

    public Integer getDiningId() {
        return diningTableId;
    }

    public void setDiningId(Integer diningId) {
        this.diningTableId = diningId;
    }

    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;
    }

    public Integer getNums() {
        return nums;
    }

    public void setNums(Integer nums) {
        this.nums = nums;
    }

    public Integer getDiningTableId() {
        return diningTableId;
    }

    public void setDiningTableId(Integer diningTableId) {
        this.diningTableId = diningTableId;
    }

    @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;
    }
}

DAO

BasicDAO

package com.wantian.mhl.dao;

import com.wantian.mhl.domain.DiningTable;
import com.wantian.mhl.utils.JDBCUtilsDruid;
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;

public class BasicDAO <T>{
    private QueryRunner qr = new QueryRunner();
    public int update(String sql,Object...parameters){
        Connection connection = null;
        try {
            connection= JDBCUtilsDruid.getConnection();
            int update = qr.update(connection,sql, parameters);
            return update;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            JDBCUtilsDruid.close(null,null,connection);
        }
    }

    public List<T> queryMultiply(String sql, Class<T> clazz, Object...parameters){
        Connection connection = null;
        try {
            connection = JDBCUtilsDruid.getConnection();
            List<T> query = qr.query(connection, sql, new BeanListHandler<T>(clazz), parameters);
            return query;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            JDBCUtilsDruid.close(null,null,connection);
        }
    }
    public T querySingle(String sql,Class<T> clazz,Object...parameters){
        Connection connection = null;
        try {
            connection = JDBCUtilsDruid.getConnection();
            T query = qr.query(connection, sql, new BeanHandler<T>(clazz), parameters);
            return query;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            JDBCUtilsDruid.close(null,null,connection);
        }
    }

    public Object queryScalar(String sql,Object...parameters){
        Connection connection = null;
        try {
            connection = JDBCUtilsDruid.getConnection();
            Object query = qr.query(connection, sql, new ScalarHandler(), parameters);
            return query;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            JDBCUtilsDruid.close(null,null,connection);
        }
    }
}

BillDAO

package com.wantian.mhl.dao;

import com.wantian.mhl.domain.Bill;

public class BillDAO extends BasicDAO<Bill>{
    //这里可以进行特殊方法的添加
}

DiningTableDAO

package com.wantian.mhl.dao;

import com.wantian.mhl.domain.DiningTable;

public class DiningTableDAO extends BasicDAO<DiningTable>{
    //如果有特殊操作可以添加

}

EmployeeDAO

package com.wantian.mhl.dao;

import com.wantian.mhl.domain.Employee;

public class EmployeeDAO extends BasicDAO<Employee>{
    //这里可以写特有的操作
}

MenuDAO

package com.wantian.mhl.dao;

import com.wantian.mhl.domain.Menu;

public class MenuDAO extends BasicDAO<Menu>{
    //这里可以创建特殊方法
}

MuItiTableDAO

package com.wantian.mhl.dao;

import com.wantian.mhl.domain.MultiTableBean;

public class MultiTableDAO extends BasicDAO<MultiTableBean>{

}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:/a/475512.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

Java基础--集合

集合 1.可以动态的保存任意多个对象&#xff0c;使用比较方便。 2.提供了一系列方便的操作对象的方法&#xff1a;add&#xff0c;remove&#xff0c;set&#xff0c;get等。 3.使用集合添加&#xff0c;删除新元素的示意代码&#xff0c;简介明了。 集合主要是两种&#xff0…

c语言扫雷改进版

目录 文章目录 主体 整体架构流程 技术名词解释 技术细节 测试情况 文章目录 概要整体架构流程技术名词解释技术细节测试情况 主体 主体包括菜单&#xff0c;游戏规则简绍&#xff0c;选择进行与否 int main() {int input;srand((unsigned int)time(NULL));do{ menu()…

谷歌地图TMS地图服务地址收集2024,测试可用

对于普通的开发者或者GIS从业者来说&#xff0c;免费的底图影像服务&#xff0c;太重要了。之前写过一篇谷歌地图的TMS地址收集的博文&#xff0c;由于谷歌网站关闭已经不能用。最近又发现了谷歌在国内开放了其他地址&#xff0c;在这里给大家分享一下。 https://gac-geo.googl…

springboot Thymeleaf模版引擎使用

1.引入依赖 <!--thymeleaf视图引擎--> <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> html中要声明约束&#xff0c;这样就可以使用themelraf视…

EMC Unity存储系统(包含VNXe)常用检查命令

DELL EMC的Unity存储系统&#xff0c;包括VNXe存储系统的OS已经完全和Clariion 的VNX不同了&#xff0c;近期遇到很多关于EMC unity存储系统故障的一些初步检查需求&#xff0c;下面是一些对于DELL EMC Unity存储系统的最常用的底层检查命令&#xff0c;可以对系统故障有个初步…

小程序商城如何接入和设置支付宝支付功能?

众所周知&#xff0c;移动支付已成为电商平台不可或缺的核心组件。为了提升用户体验并拓宽支付渠道&#xff0c;许多小程序商城系统纷纷引入了支付宝作为支付选项。那么&#xff0c;如何在小程序商城系统中成功接入和设置支付宝支付功能呢&#xff1f;这似乎是大家当前所面临的…

电子台账:账页数据溯源

目录 1 前言 2 打开数据溯源面板 3 溯源面板操作 1 前言 账页中让人眼花缭乱的大堆数据来自哪里&#xff1f;从企业数据源表格中自动抓取数据后&#xff0c;如果感觉数据不对&#xff0c;就需要进行核对、排错&#xff0c;怎样确定程序到底抓取的哪些单元格&#xff1f;取数…

Visual Studio 2022 出现:E1696无法打开 源 文件 “stdafx.h“;E0020未定义标识符 “_TCHAR“错误解决办法

今天在用自己电脑上的visual studio2022写并行计算实验报告时出现了这种错误&#xff0c;但我在机房电脑vs2010版本运行时没有任何问题&#xff0c;于是我在网上找了解决办法&#xff0c;现总结如下。 1.报错E1696无法打开 源 文件 "stdafx.h"&#xff0c;解决办法&a…

思维线索(Thread of Thought)-ThoT梳理混乱的语境

Thread of Thought Unraveling Chaotic Contexts 大型语言模型&#xff08;LLMs&#xff09;在自然语言处理领域开启了一个变革的时代&#xff0c;在文本理解和生成任务上表现出色。然而&#xff0c;当面对混乱的上下文环境&#xff08;例如&#xff0c;干扰项而不是长的无关上…

【Java - 框架 - SpringMVC】(01) SpringMVC框架的简单创建与使用,快速上手

"SpringMVC"框架的简单创建与使用&#xff0c;快速上手&#xff1b; 环境 Java版本"1.8.0_202"&#xff1b;Spring Boot版本"2.5.9"&#xff1b;Windows 11 专业版_22621.2428&#xff1b;IntelliJ IDEA 2021.1.3(Ultimate Edition)&#xff1…

NVIDIA NCCL 源码学习(十三)- IB SHARP

背景 之前我们看到了基于ring和tree的两种allreduce算法&#xff0c;对于ring allreduce&#xff0c;一块数据在reduce scatter阶段需要经过所有的rank&#xff0c;allgather阶段又需要经过所有rank&#xff1b;对于tree allreduce&#xff0c;一块数据数据在reduce阶段要上行…

淘宝店铺如何从1688一键铺货?官方授权API接口,可满足多样化上货需求

那么新手卖家如何将1688的源头厂货一键铺货到淘宝店铺呢&#xff1f;下面我教大家几招&#xff1a; 1、通过淘宝复制一键复制上货 淘宝API接口采集 taobao.item_get 公共参数 名称类型必须描述keyString是调用key&#xff08;必须以GET方式拼接在URL中&#xff09;secretStr…

无粉乳胶手套:提升操作效率与舒适度的理想选择

作为一种新型的个人防护装备&#xff0c;无粉乳胶手套因其清洁、舒适和高靈敏度的特点&#xff0c;逐渐成为各个行业的首选。本文将通过对比分析无粉乳胶手套与其他手套的差异&#xff0c;探讨其在提升操作效率和舒适度方面的优势。 提升操作效率&#xff1a; 无粉乳胶手套具有…

[C语言]——函数递归

目录 一.什么是递归 1.递归的思想&#xff1a; 二.递归的限制条件 三.递归举例 1.举例1&#xff1a;求n的阶乘 1.1分析和代码实现 1.2画图推演 2.举例2&#xff1a;顺序打印⼀个整数的每⼀位 2.1分析和代码实现 2.2画图推演 四.递归与迭代 1.举例3&#xff1a;求第…

记录一次服务器内存使用率过高达到90%告警问题排查。

目录 一、前言二、问题排查处理三、 结尾 &#x1f469;&#x1f3fd;‍&#x1f4bb;个人主页&#xff1a;阿木木AEcru &#x1f525; 系列专栏&#xff1a;Docker容器化部署系列 &#x1f4b9;每一次技术突破&#xff0c;都是对自我能力的挑战和超越。 一、前言 一大早就有一…

EtherCAT 开源主站 IGH 在 linux 开发板的移植和伺服通信测试

手边有一套正点原子linux开发板imax6ul&#xff0c;一直在吃灰&#xff0c;周末业余时间无聊&#xff0c;把EtherCAT的开源IGH主站移植到开发板上玩玩儿&#xff0c;搞点事情做。顺便学习研究下EtherCAT总线协议及其对伺服驱动器的运动控制过程。实验很有意思&#xff0c;这里总…

01mysql

登陆mysql 默认数据库 进入&#xff0c;展示&#xff0c;删除 &#xff0c;查看当前正使用的库 select version()查看版本 查看表结构desc 查询 not in不会忽略空 in会自动忽略 like模糊查询 %o%中间带o的 _A%第二个字母是A的 查名字是下划线的 %\_% 排序 order …

Spring Batch | quick start

导学 官网介绍&#xff1a;https://docs.spring.io/spring-batch/reference/spring-batch-intro.html批处理就是将数据分批次进行处理的过程。常规的批处理操作步骤&#xff1a;系统A从数据库中导出数据到文件&#xff0c;系统B读取文件数据并写入到数据库批处理特点&#xff…

matlab 最小二乘拟合圆柱

目录 一、算法原理1、算法简介2、参考文献二、代码实现三、结果展示四、测试数据本文由CSDN点云侠原创,原文链接。如果你不是在点云侠的博客中看到该文章,那么此处便是不要脸的爬虫。 一、算法原理 1、算法简介 圆柱拟合步骤主要包括两步: 一是确定柱面模型参数初始值; 二是…

粤嵌6818开发板如何理解Linux文件IO?

一、文件IO的概述 1、什么是文件&#xff1f; Linux下一切皆文件。普通文件、目录文件、管道文件、套接字文件、链接文件、字符设备文件、块设备文件。 2、什么是IO&#xff1f; input output&#xff1a;输入输出 3、什么是文件IO&#xff1f; 对文件的输入输出&#xff0c;把…