文章目录
- 满汉楼
满汉楼
1、程序框架图
根据框架图可以知道要创建以下包:view,service,dao,domain,utils
2、前置工作:配置相应文件和创建mhl数据库
创建mhl数据库:
create database mhl;
在项目下创建libs目录,在该目录下导入相应的jar包.
在src目录下导入druid.properties配置文件:
#key=value
driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/mhl?rewriteBatchedStatements=true
username=root
password=123456
#initial connection Size
initialSize=10
#min idle connecton size
minIdle=5
#max active connection size
maxActive=50
#max wait time (5000 mil seconds)
maxWait=5000
在utils包下添加以下工具:JDBCUtilsByDruid,Utility
JDBCUtilsByDruid:
package utils;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
/**
* @author D
* @version 1.0
* @project JDBC
* @description 基于druid数据库连接池的工具类
* @date 2024/5/19 16:13:58
*/
public class JDBCUtilsByDruid {
private static DataSource ds;
//在静态代码块中完成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);
}
}
//编写getConnection方法
public static Connection getConnection() throws SQLException {
return ds.getConnection();
}
//关闭连接,再次强调:在数据库连接池技术中,close不是真正的断掉链接,而是把使用的Connection对象放回连接池
public static void close(ResultSet resultSet, Statement statement,Connection connection){
try {
if (resultSet!=null){
resultSet.close();
}
if (statement!=null){
statement.close();
}
if (connection!=null){
connection.close();
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
Utility(用来进行键盘读入):
package 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;
}
}
在dao包下创建BasicDAO:
/**
* @author D
* @version 1.0
* @project manhanlou
* @description 开发BasicDAO,是其他XxxDao的父类
* @date 2024/5/22 15:23:10
*/
package dao;
import 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;
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 rows = qr.update(connection, sql, parameters);
return rows;
} 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);
}
}
//开发返回一个对象(即查询的结果是d单行),针对任意表
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);
}
}
}
3、需求说明
完成满汉楼项目的登录,订座,点餐和结账,查看账单等功能。
4、界面设计
(1)用户登录
(2)二级菜单
1、显示餐桌状态
2、预定餐桌
3、显示所有菜品
4、点餐服务
5、查看账单
6、结账
9、退出满汉楼
5、具体代码
(1)先设计界面
package view;
import domain.Employee;
import org.junit.Test;
import service.EmployeeService;
import utils.Utility;
/**
* @author D
* @version 1.0
* @project manhanlou
* @description 满汉楼的菜单界面
* @date 2024/5/22 17:33:53
*/
public class MHLView {
//控制是否退出
private boolean loop=true;
private String key="";//接收用户的选择
public static void main(String[] args) {
MHLView mhlView = new MHLView();
mhlView.mainMenu();
}
//主菜单
public void mainMenu(){
while(loop){
System.out.println("==========满汉楼==========");
System.out.println("\t\t1、登录满汉楼");
System.out.println("\t\t2、退出满汉楼");
System.out.println("请输入你的选择:");
key=Utility.readString(1);
switch (key){
case "1":
System.out.println("请输入员工号:");
String empId=Utility.readString(50);
System.out.println("请输入密 码:");
String pwd=Utility.readString(50);
//到数据库去判断[一会儿写]
if ("123".equals(pwd)){
System.out.println("==========登录成功==========\n");
//显示二级菜单,这里二级菜单也是循环操作,所以用while
while(loop){
System.out.println("==========满汉楼二级菜单==========");
System.out.println("\t\t1、显示餐桌状态");
System.out.println("\t\t2、预定餐桌");
System.out.println("\t\t3、显示所有菜品");
System.out.println("\t\t4、点餐服务");
System.out.println("\t\t5、查看账单");
System.out.println("\t\t6、结账");
System.out.println("\t\t9、退出满汉楼");
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("你的输入有误,请重新输入:");
break;
}
}
}else {
System.out.println("==========登录失败==========");
}
break;
case "2":
loop=false;
break;
default:
System.out.println("你的输入有误,请重新输入~");
break;
}
}
System.out.println("你退出了满汉楼系统~");
}
}
在这里还没有实现用户验证功能先让我们测试以下界面是否能显示(我用"123".equals(pwd)来判断了用户名密码,之后会连接数据库进行判断):
(2)进行用户登录判断功能实现
第一步:在mysql中创建mhl数据库在库中创建employee表存放用户的密码等信息。
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'), 'Ghost-D', '经理');
第二步:在domain包下创建Employee类(JavaBean,与数据库中employee表相对应)
package domain;
/**
* @author D
* @version 1.0
* @project manhanlou
* @description 这时一个JavaBean
* @date 2024/5/23 10:56:33
*/
/*
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;
}
@Override
public String toString() {
return "Employee{" +
"id=" + id +
", empId='" + empId + '\'' +
", pwd='" + pwd + '\'' +
", name='" + name + '\'' +
", job='" + job + '\'' +
'}';
}
}
第三步:在dao包下创建EmployeeDAO类继承BasicDAO类
package dao;
import domain.Employee;
/**
* @author D
* @version 1.0
* @project manhanlou
* @description
* @date 2024/5/23 11:00:25
*/
public class EmployeeDAO extends BasicDAO<Employee>{
}
第四步:在service包下创建EmployeeService类(完成对employee表的各种操作(通过调用EmployeeDAO来完成))
package service;
import dao.EmployeeDAO;
import domain.Employee;
/**
* @author D
* @version 1.0
* @project manhanlou
* @description 该类完成对employee表的各种操作(通过调用EmployeeDAO来完成)
* @date 2024/5/23 11:01:48
*/
public class EmployeeService {
//定义一个EmployeeDAO属性
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;
}
}
第五步:这样具体的功能就完成了,只需要在界面代码的用户验证哪里进行修改就行了。
修改的地方:
- 定义了一个属性private EmployeeService employeeService=new EmployeeService();//定义一个employeeService属性
- 进行了一次数据库查询,查询是否有该员工,有的话就返回该员工,没有的话就返回一个null。Employee employee = employeeService.getEmployeeByIdAndPwd(empId, pwd);
- 在if语句中进行判断:if (employee!=null)
package view;
import domain.Employee;
import org.junit.Test;
import service.EmployeeService;
import utils.Utility;
/**
* @author D
* @version 1.0
* @project manhanlou
* @description 满汉楼的菜单界面
* @date 2024/5/22 17:33:53
*/
public class MHLView {
//控制是否退出
private boolean loop=true;
private String key="";//接收用户的选择
private EmployeeService employeeService=new EmployeeService();//定义一个employeeService属性
public static void main(String[] args) {
MHLView mhlView = new MHLView();
mhlView.mainMenu();
}
//主菜单
public void mainMenu(){
while(loop){
System.out.println("==========满汉楼==========");
System.out.println("\t\t1、登录满汉楼");
System.out.println("\t\t2、退出满汉楼");
System.out.println("请输入你的选择:");
key=Utility.readString(1);
switch (key){
case "1":
System.out.println("请输入员工号:");
String 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()+"==========\n");
//显示二级菜单,这里二级菜单也是循环操作,所以用while
while(loop){
System.out.println("==========满汉楼二级菜单==========");
System.out.println("\t\t1、显示餐桌状态");
System.out.println("\t\t2、预定餐桌");
System.out.println("\t\t3、显示所有菜品");
System.out.println("\t\t4、点餐服务");
System.out.println("\t\t5、查看账单");
System.out.println("\t\t6、结账");
System.out.println("\t\t9、退出满汉楼");
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("你的输入有误,请重新输入:");
break;
}
}
}else {
System.out.println("==========登录失败==========");
}
break;
case "2":
loop=false;
break;
default:
System.out.println("你的输入有误,请重新输入~");
break;
}
}
System.out.println("你退出了满汉楼系统~");
}
}
(3)进行显示餐桌状态实现
1、创建diningTable表
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, '空','','');
insert into diningTable values(null, '空','','');
2、在domain包下创建DiningTable类(JavaBean)
package domain;
/**
* @author D
* @version 1.0
* @project manhanlou
* @description
* @date 2024/5/23 16:07:25
*/
/*
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;
}
}
3、在dao包下创建DiningTableDAO类(继承了BasicDAO类)
package dao;
import domain.DiningTable;
/**
* @author D
* @version 1.0
* @project manhanlou
* @description
* @date 2024/5/23 16:09:59
*/
public class DiningTableDAO extends BasicDAO<DiningTable>{
//
}
4、在service包下创建DiningTableService类
package service;
import dao.DiningTableDAO;
import domain.DiningTable;
import java.util.List;
/**
* @author D
* @version 1.0
* @project manhanlou
* @description
* @date 2024/5/23 16:10:41
*/
public class DiningTableService {
//定义一个DiningTableDAO属性
private DiningTableDAO diningTableDAO=new DiningTableDAO();
//返回所有餐桌的信息
public List<DiningTable> list(){
return diningTableDAO.queryMulti("select id,state from diningTable", DiningTable.class);
}
}
5、这样具体功能代码就实现了,现在在界面代码去调用这些功能
private DiningTableService diningTableService=new DiningTableService();//定义一个DiningTableService属性
//显示餐桌状态方法
public void listDiningTable(){
System.out.println("\n餐桌编号\t\t餐桌状态");
List<DiningTable> list = diningTableService.list();
for (DiningTable diningTable :list) {
System.out.println(diningTable);
}
System.out.println("==========显示完毕==========");
}
//
(4)预定餐桌功能实现
1、在service包下的DiningTableService类里晚上以上的功能
//根据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 rows = diningTableDAO.update("update diningTable set state='已经预定',orderName=?,orderTel=? where id=?", orderName, orderTel,id);
return rows>0?true:false;
}
2、在MHLView类下完善代码
//预定餐桌方法
public void orderDiningTable(){
System.out.println("==========预定餐桌==========");
System.out.println("请选择要预定的餐桌编号(-1退出):");
int id=Utility.readInt();
if (id==-1){
System.out.println("==========取消预定餐桌==========");
return;
}
char key = Utility.readConfirmSelection();//这里key得到的要么是Y要么是N
if (key=='Y'){
//根据orderId返回对应DiningTable对象,如果为null,则该对象不存在
DiningTable diningTableById = diningTableService.getDiningTableById(id);
if (diningTableById==null){
System.out.println("==========预定餐桌不存在==========");
return;
}
//判断该餐桌状态是否为空
if (!("空".equals(diningTableById.getState()))){
System.out.println("==========餐桌已经预定或者就餐中==========");
return;
}
System.out.print("预定人名字:");
String orderName=Utility.readString(50);
System.out.print("预定人电话:");
String orderTel=Utility.readString(50);
//更新状态
if ( diningTableService.orderDiningTable(id, orderName, orderTel)){
System.out.println("==========餐桌预定成功==========");
}else {
System.out.println("==========餐桌预定失败==========");
}
}else {
System.out.println("==========取消预定餐桌==========");
return;
}
}
预定成功:
预定失败:
(5)显示所有菜品功能实现
1、创建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);
2、在domain包下创建Menu类(JavaBean)
package domain;
/**
* @author D
* @version 1.0
* @project manhanlou
* @description
* @date 2024/5/23 17:54:33
*/
/*
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;
}
}
3、在dao包下创建MenuDAO类(继承BasicDAO类)
package dao;
import domain.Menu;
/**
* @author D
* @version 1.0
* @project manhanlou
* @description
* @date 2024/5/23 17:56:41
*/
public class MenuDAO extends BasicDAO<Menu>{
//
}
4、在service包下创建MenuService类
package service;
import dao.MenuDAO;
import domain.Menu;
import java.util.List;
/**
* @author D
* @version 1.0
* @project manhanlou
* @description
* @date 2024/5/23 17:57:08
*/
public class MenuService {
//定义一个MenuDAO属性
private MenuDAO menuDAO=new MenuDAO();
//编写一个显示菜单的方法
public List<Menu> listMenu(){
return menuDAO.queryMulti("select * from menu", Menu.class);
}
}
5、在MHLView类下添加以下代码
private MenuService menuService=new MenuService();//定义一个MenuService属性
//显示所有菜品方法
public void listMenu(){
System.out.println("==========菜单==========");
System.out.println("菜品编号\t\t菜品名\t\t类别\t\t价格");
List<Menu> menus = menuService.listMenu();
for (Menu menu : menus) {
System.out.println(menu);
}
}
这样就完成显示菜单的功能:
(6)点餐服务功能实现
1、创建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;
2、在domain包下创建Bill类(JavaBean)
package domain;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
/**
* @author D
* @version 1.0
* @project manhanlou
* @description
* @date 2024/5/23 19:19:46
*/
/*
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 LocalDateTime billDate;
private String state;
public Bill() {
}
public Bill(Integer id, String billId, Integer menuId, Integer nums, double money, Integer diningTableId, LocalDateTime 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 LocalDateTime getBillDate() {
return billDate;
}
public void setBillDate(LocalDateTime 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\t" + state;
}
}
3、在dao包下创建BillDAO类(继承BasicDAO类)
package dao;
import domain.Bill;
/**
* @author D
* @version 1.0
* @project manhanlou
* @description
* @date 2024/5/23 19:23:46
*/
public class BillDAO extends BasicDAO<Bill>{
//
}
4、在service包下完善MenuService类,编写一个根据id获取Menu对象的方法
//编写一个方法根据id,返回一个Menu对象
public Menu getMenuById(int id){
return menuDAO.querySingle("select * from menu where id=?", Menu.class,id);
}
5、在service包下完善DiningTableService类,提供一个更新餐桌状态的方法
//需要提供一个更新 餐桌状态的方法
public boolean updateDiningTableState(int id,String state){
int rows = diningTableDAO.update("update diningTable set state=? where id=?", state, id);
return rows>0;
}
6、在service包下创建BillService类
package service;
import dao.BillDAO;
import domain.Bill;
import java.util.List;
import java.util.UUID;
/**
* @author D
* @version 1.0
* @project manhanlou
* @description
* @date 2024/5/23 19:24:50
*/
public class BIllService {
private BillDAO billDAO=new BillDAO();//定义一个BillDAO属性
private MenuService menuService=new MenuService();//定义一个MenuService属性
private DiningTableService diningTableService=new DiningTableService();//定义一个DiningTableService属性
/*
编写点餐方法:
1、生成账单
2、需要更新对应餐桌的状态
*/
public boolean orderMenu(int menuId,int nums,int diningTableId){
//生成一个账单号
String billID = UUID.randomUUID().toString();
//将账单生成到bill表
int rows = billDAO.update("insert into bill values(null,?,?,?,?,?,now(),'未结账')",
billID, menuId, nums, menuService.getMenuById(menuId).getPrice() * nums, diningTableId);
if (rows<=0){
return false;
}
//更新对应餐桌的状态
return diningTableService.updateDiningTableState(diningTableId,"就餐中");
}
}
7、在MHLView类下完善以下代码
private BIllService bIllService=new BIllService();//定义一个BIllService属性
//点餐服务方法
public void orderMenu(){
System.out.println("请输入点餐的桌号(-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 orderNums = Utility.readInt();
if (orderNums==-1){
System.out.println("==========取消点餐==========");
return;
}
//验证餐桌号是否存在
DiningTable diningTable = diningTableService.getDiningTableById(orderDiningTableId);
if (diningTable==null){
System.out.println("==========餐桌号不存在==========");
return;
}
//验证菜品编号
Menu menu = menuService.getMenuById(orderMenuId);
if (menu==null){
System.out.println("==========菜品编号不存在==========");
return;
}
//点菜
if (bIllService.orderMenu(orderMenuId,orderNums,orderDiningTableId)){
System.out.println("==========点餐成功==========");
}else{
System.out.println("==========点餐失败==========");
}
}
这样就完成了点餐功能:
失败界面:
(7)实现查看账单功能
1、在BillService类下完善以下代码
//返回账单信息
public List<Bill> listBill(){
return billDAO.queryMulti("select * from bill", Bill.class);
}
2、在MHLView类下完善以下代码
//查看账单的方法
public void listBill(){
System.out.println("\n编号\t\t菜品号\t\t菜品量\t\t金额\t\t桌号\t\t日期\t\t\t\t\t\t\t状态");
List<Bill> bills = bIllService.listBill();
for (Bill bill :bills) {
System.out.println(bill);
}
}
这样就完成了查看账单功能:
(8)实现结账功能
1、在BillService类下创建一个查看某个餐桌是否有未结账的账单的方法
//查看某个餐桌是否有未结账的账单
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;
}
2、在BillService类下创建一个完成结账[如果餐桌存在,且有未结账的账单]的方法
//完成结账[如果餐桌存在,且有未结账的账单]
public boolean payBill(String payMode,int diningTableId){
//如果这里使用事务的话,需要使用ThreadLocal来解决,框架中mybatis提供了事务支持
//1、修改bill表
int rows = billDAO.update("update bill set state=? where diningTableId=? and state='未结账'", payMode, diningTableId);
if (rows<=0){
return false;
}
//2、修改diningTable表
if (!(diningTableService.updateDiningTableToFree(diningTableId, "空"))){
return false;
}
return true;
}
3、在DiningTableService类下创建一个需要提供一个设置餐桌为初始状态的方法
//需要提供一个设置餐桌为初始状态
public boolean updateDiningTableToFree(int id,String state){
int rows = diningTableDAO.update("update diningTable set state=?,orderName='',orderTel='' where id=?", state, id);
return rows>0;
}
4、在MHLView类下创建一个结账功能的方法
//结账的方法
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(payMode,diningTableId)){
System.out.println("==========结账成功==========");
}else {
System.out.println("==========结账失败==========");
}
}else {
System.out.println("==========取消结账==========");
}
}
6、拓展思考
(1)多表查询
实现多表查询就是创建一个新的javaBean类包含多表里的所有属性。
package domain;
import java.time.LocalDateTime;
/**
* @author D
* @version 1.0
* @project manhanlou
* @description
* @date 2024/5/24 15:21:50
*/
public class MultiTableBean {
private Integer id;
private String billId;
private Integer menuId;
private Integer nums;
private double money;
private Integer diningTableId;
private LocalDateTime 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, LocalDateTime 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;
}
//给name生成set和get方法
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 LocalDateTime getBillDate() {
return billDate;
}
public void setBillDate(LocalDateTime 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\t" + state+
"\t\t" + name;
}
}
然后在创建对应的DAO类
package dao;
import domain.MultiTableBean;
/**
* @author D
* @version 1.0
* @project manhanlou
* @description
* @date 2024/5/24 15:25:50
*/
public class MultiTableDAO extends BasicDAO<MultiTableBean>{
//
}
最后在要用多表查询的service类里添加对应的DAO属性(MultiTableDAO)和添加多表查询的功能.
private MultiTableDAO multiTableDAO=new MultiTableDAO();
//返回账单信息,带name
public List<MultiTableBean> listBill2(){
return multiTableDAO.queryMulti("SELECT bill.*,name FROM bill,menu where bill.menuId=menu.id", MultiTableBean.class);
}
最后在MHLView创建新方法调用service即可
//查看账单的方法
public void listBill(){
// System.out.println("\n编号\t\t菜品号\t\t菜品量\t\t金额\t\t桌号\t\t日期\t\t\t\t\t\t\t状态");
// List<Bill> bills = bIllService.listBill();
// for (Bill bill :bills) {
// System.out.println(bill);
// }
System.out.println("\n编号\t\t菜品号\t\t菜品量\t\t金额\t\t桌号\t\t日期\t\t\t\t\t\t\t状态\t\t\t菜品名");
List<MultiTableBean> multiTableBeans = bIllService.listBill2();
for (MultiTableBean bill :multiTableBeans) {
System.out.println(bill);
}
}
(2)分表设计
1、创建employeeinfo表
CREATE table employeeinfo(
//根据需求自行添加字段
);
2、在domain包下创建EmployeeInfo类
//与employeeinfo表属性一一对应(映射),添加空构造器,每个属性的set和get方法以及ToString方法
3、在dao包下创建EmployeeInfoDAO类
package dao;
import domain.EmployeeInfo;
/**
* @author D
* @version 1.0
* @project manhanlou
* @description
* @date 2024/5/24 16:38:50
*/
public class EmployeeInfoDAO extends BasicDAO<EmployeeInfo>{
//
}
4、在service包下创建EmployeeInfoService类
package service;
import dao.EmployeeInfoDAO;
import domain.EmployeeInfo;
import java.util.List;
/**
* @author D
* @version 1.0
* @project manhanlou
* @description
* @date 2024/5/24 16:39:19
*/
public class EmployeeInfoService {
private EmployeeInfoDAO employeeInfoDAO=new EmployeeInfoDAO();
//查看雇员信息
public List<EmployeeInfo> getEmployeeInfo(){
return employeeInfoDAO.queryMulti("select * from employeeinfo", EmployeeInfo.class);
}
//其他功能实现可根据需求自行扩展
}
5、在MHLView类下添加人事管理功能
private EmployeeInfoService employeeInfoService=new EmployeeInfoService();//定义一个EmployeeService属性
//人事管理菜单
public void MenuByEmp(){
while(loop){
System.out.println("==========人事管理功能==========");
System.out.println("\t\t1、查看所有雇员");
//其他功能根据需求自行添加
System.out.println("\t\t9、退出满汉楼");
System.out.println("请输入你的选择:");
key=Utility.readString(1);
switch (key){
case "1":
List<EmployeeInfo> employeeInfo = employeeInfoService.getEmployeeInfo();
for (EmployeeInfo info : employeeInfo) {
System.out.println(info);
}
break;
case "9":
loop=false;
break;
default:
System.out.println("请输入正确选项");
break;
}
}
}
//与employeeinfo表属性一一对应(映射),添加空构造器,每个属性的set和get方法以及ToString方法
3、在dao包下创建EmployeeInfoDAO类
package dao;
import domain.EmployeeInfo;
/**
* @author D
* @version 1.0
* @project manhanlou
* @description
* @date 2024/5/24 16:38:50
*/
public class EmployeeInfoDAO extends BasicDAO<EmployeeInfo>{
//
}
4、在service包下创建EmployeeInfoService类
package service;
import dao.EmployeeInfoDAO;
import domain.EmployeeInfo;
import java.util.List;
/**
* @author D
* @version 1.0
* @project manhanlou
* @description
* @date 2024/5/24 16:39:19
*/
public class EmployeeInfoService {
private EmployeeInfoDAO employeeInfoDAO=new EmployeeInfoDAO();
//查看雇员信息
public List<EmployeeInfo> getEmployeeInfo(){
return employeeInfoDAO.queryMulti("select * from employeeinfo", EmployeeInfo.class);
}
//其他功能实现可根据需求自行扩展
}
5、在MHLView类下添加人事管理功能
private EmployeeInfoService employeeInfoService=new EmployeeInfoService();//定义一个EmployeeService属性
//人事管理菜单
public void MenuByEmp(){
while(loop){
System.out.println("==========人事管理功能==========");
System.out.println("\t\t1、查看所有雇员");
//其他功能根据需求自行添加
System.out.println("\t\t9、退出满汉楼");
System.out.println("请输入你的选择:");
key=Utility.readString(1);
switch (key){
case "1":
List<EmployeeInfo> employeeInfo = employeeInfoService.getEmployeeInfo();
for (EmployeeInfo info : employeeInfo) {
System.out.println(info);
}
break;
case "9":
loop=false;
break;
default:
System.out.println("请输入正确选项");
break;
}
}
}
标签:Java,String,System,源码,println,return,顺平,public,out
From: https://blog.csdn.net/m0_71187513/article/details/139179320