整体框架
功能一览
所需文件
如果需要jar包私信我发给你
MhlView
package Mhl.view;
import Chat.tool.Utility;
import Mhl.daomain.*;
import Mhl.service.BillService;
import Mhl.service.DtService;
import Mhl.service.EmpService;
import Mhl.service.MenuService;
import java.sql.SQLException;
import java.util.List;
/**
* @author whlie(true){learn}
*/
@SuppressWarnings({"all"})
public class MhlView {
private boolean loop = true;
//一级菜单
private String key = "";
//二级菜单
private String option = "";
//员工业务层
private EmpService es = new EmpService();
//餐桌业务层
private DtService dt = new DtService();
//菜单业务层
private MenuService menu = new MenuService();
//点餐业务层
private BillService bill = new BillService();
public static void main(String[] args) throws SQLException {
new MhlView().mainMenu();
}
/**
* 主菜单显示
*
* @throws SQLException
*/
public void mainMenu() throws SQLException {
while (loop) {
System.out.println("==========满汉楼=========");
System.out.println("\t\t1.登录满汉楼");
System.out.println("\t\t2.退出满汉楼");
System.out.print("请输入您的选项:");
key = Utility.readString(1);
switch (key) {
case "1":
System.out.print("请输入员工号:");
String empId = Utility.readString(20);
System.out.print("请输入 密码:");
String pwd = Utility.readString(20);
Employee exist = es.getExist(empId, pwd);
//判断用户是否存在
if (exist != null) {
System.out.println("===========登录成功[" + exist.getName() + "]==========");
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\t7.退出满汉楼");
System.out.print("请输入您的选项:");
option = Utility.readString(1);
switch (option) {
case "1":showDt();break;
case "2":orderdt();break;
case "3":showMenu();break;
case "4":order();break;
case "5":showBills();break;
case "6":payBill();break;
case "7":loop = false;break;
default:
System.out.println("输入没有在选项范围内");
}
}
} else {
System.out.println("===========登录失败==========");
}
case "2":loop = false;break;
default:
System.out.println("输入的有问题,请重新输入");
}
}
System.out.println("退出满汉楼!");
}
/**
* 显示餐桌状态
*/
public void showDt() throws SQLException {
List<DiningTable> diningTables = dt.ShowState();
System.out.println("餐桌编号" + "\t\t" + "状态");
for (DiningTable diningTable : diningTables) {
System.out.println(diningTable);
}
}
/**
* 预定餐桌
*/
public void orderdt() throws SQLException {
System.out.print("请输入要预定的餐桌编号(-1退出):");
int id = Utility.readInt();
if (id == -1) {
return;
}
System.out.print("是否确定预定该餐桌(Y/N):");
char c = Utility.readConfirmSelection();
if (c == 'Y') {
DiningTable exist = dt.getExist(id);
if (exist == null) {
System.out.println("===========该餐桌不存在==========");
return;
}
if (!("空".equals(exist.getState()))) {
System.out.println("===========该餐桌已被预定或正在就餐==========");
return;
}
System.out.print("预定人姓名:");
String name = Utility.readString(50);
System.out.print("预定人电话");
String tel = Utility.readString(11);
if (dt.updateState(id, name, tel)) {
System.out.println("===========预定成功==========");
} else {
System.out.println("===========预定失败==========");
}
} else {
System.out.println("===========取消预定==========");
return;
}
}
/**
* 显示菜单
*/
public void showMenu() throws SQLException {
System.out.println("编号" + "\t\t" + "名称" + "\t\t\t" + "类型" + "\t\t" + "价格");
List<Menu> menus = menu.showMenu();
for (Menu menu1 : menus) {
System.out.println(menu1);
}
}
/**
* 点菜
*/
public void order() throws SQLException {
System.out.print("请输入点餐的桌号(-1退出)");
int dt1 = Utility.readInt();
if (dt1 == -1) {
System.out.println("===========取消点餐==========");
return;
}
System.out.print("请输入点餐的菜品号(-1退出)");
int MenuId = Utility.readInt();
if (MenuId == -1) {
System.out.println("===========取消点餐==========");
return;
}
System.out.print("请输入点餐的菜品量(-1退出)");
int nums = Utility.readInt();
if (nums == -1) {
System.out.println("===========取消点餐==========");
return;
}
DiningTable exist = dt.getExist(dt1);
if (exist == null) {
System.out.println("===========餐桌不存在==========");
return;
}
Menu menu = this.menu.getMenu(MenuId);
if (menu == null) {
System.out.println("===========菜品不存在==========");
return;
}
if (bill.orderMenu(MenuId, nums, dt1)) {
System.out.println("===========点餐成功==========");
return;
} else {
System.out.println("===========点餐失败==========");
return;
}
}
/**
* 显示账单
*/
public void showBill() throws SQLException {
System.out.println("\n编号\t\t菜品号\t\t菜品量\t\t金额\t\t\t桌号\t\t日期\t\t\t\t\t\t\t状态");
List<Bill> bills = bill.showBill();
for (Bill bill1 : bills) {
System.out.println(bill1);
}
}
/**
* 显示账单(多表)
*/
public void showBills() throws SQLException {
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<Multiple> multiples = bill.showBills();
for (Multiple bill1 : multiples) {
System.out.println(bill1);
}
}
/**
* 结账
*/
public void payBill() throws SQLException {
System.out.print("请输入要结账的餐桌号(-1退出)");
int dt1 = Utility.readInt();
if (dt1 == -1) {
System.out.println("===========取消结账==========");
return;
}
DiningTable exist = dt.getExist(dt1);
if (exist == null) {
System.out.println("===========餐桌不存在==========");
return;
}
if (!bill.judge(dt1)) {
System.out.println("===========该餐桌没有账单==========");
return;
}
System.out.print("请输入结账方式(回车退出)");
String payMode = Utility.readString(20, "");
if ("".equals(payMode)) {
System.out.println("===========取消结账==========");
return;
}
if (bill.payBill(dt1, payMode)) {
System.out.println("===========结账成功==========");
} else {
System.out.println("===========结账失败==========");
}
}
}
BasicDAO
package Mhl.dao;
import JDBC.Tool.DruTool;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
/**
* @author whlie(true){learn}
*/
public class BasicDAO<T> {
/**
* 封装常用的各种方法
* @param <T>类型不确定
*/
private QueryRunner qr = new QueryRunner();
public int update(String sql,Object... parameters) throws SQLException {
Connection connection = DruTool.getConnection();
int update = qr.update(connection, sql, parameters);
DruTool.close(null,null,connection);
return update;
}
/**
* 返回多个对象(即查询的结果是多行), 针对任意表
* @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) throws SQLException {
Connection connection = DruTool.getConnection();
List<T> list = qr.query(connection, sql, new BeanListHandler<T>(clazz), parameters);
DruTool.close(null,null,connection);
return list;
}
/**
* 查询单行结果 的通用方法
* @param sql
* @param clazz
* @param parameters
* @return
*/
public T querySingle(String sql, Class<T> clazz, Object... parameters) throws SQLException {
Connection connection = DruTool.getConnection();
T query = qr.query(connection, sql, new BeanHandler<T>(clazz), parameters);
DruTool.close(null,null,connection);
return query;
}
/**
*返回单值的方法
* @param sql
* @param parameters
* @return
*/
public Object queryScalar(String sql, Object... parameters) throws SQLException {
Connection connection = DruTool.getConnection();
Object query = qr.query(connection, sql, new ScalarHandler(), parameters);
DruTool.close(null,null,connection);
return query;
}
}
BillDAO
package Mhl.dao;
import Mhl.daomain.Bill;
/**
* @author whlie(true){learn}
*/
public class BillDAO extends BasicDAO<Bill>{
}
dao包下的其他文件和BillDAO类似只是名字不同
BillService
package Mhl.service;
import Mhl.dao.BillDAO;
import Mhl.dao.MultipleDAO;
import Mhl.daomain.Bill;
import Mhl.daomain.Multiple;
import java.sql.SQLException;
import java.util.List;
import java.util.UUID;
/**
* @author whlie(true){learn}
*/
public class BillService {
private BillDAO ba=new BillDAO();
private MenuService ms=new MenuService();
private DtService dts=new DtService();
//多表查询
private MultipleDAO mp=new MultipleDAO();
/**
* 点菜
* 生成账单
* 更新餐桌状态
*/
public boolean orderMenu(int menuId,int nums,int diningTable) throws SQLException {
//生成随机账单号
String billId = UUID.randomUUID().toString();
//从菜单业务层根据菜单id获取到对应菜品的价格
int update = ba.update("insert into bill values(null,?,?,?,?,?,now(),'未结账')",
billId, menuId, nums, ms.getMenu(menuId).getPrice() * nums, diningTable);
if (update<=0){
return false;
}
return dts.updateState(diningTable,"就餐中");
}
/**
* 返回所有账单(单表)
*/
public List<Bill> showBill() throws SQLException {
return ba.queryMulti("select *from bill",Bill.class);
}
/**
* 返回所有账单(多表)
* @return
*/
public List<Multiple> showBills() throws SQLException {
return mp.queryMulti("select bill.*,name from bill,Menu where bill.menuId=Menu.id", Multiple.class);
}
/**
* 判断某张餐桌是否有未结账的账单
*/
public boolean judge(int diningTable) throws SQLException {
Bill bill = ba.querySingle("select *from bill where diningTable=? and state='未结账' limit 0,1", Bill.class, diningTable);
return bill!=null;
}
/**
* 完成结账
*/
public boolean payBill(int diningTable,String payMode) throws SQLException {
//修改bill表的状态
int update = ba.update("update bill set state=? where diningTable=? and state='未结账'", payMode, diningTable);
if (update<=0){
return false;
}
//将对应的餐桌置空
if (!dts.ToNull(diningTable,"空")){
return false;
}
return true;
}
}
DtService
package Mhl.service;
import Mhl.dao.DiningTableDAO;
import Mhl.daomain.DiningTable;
import java.sql.SQLException;
import java.util.List;
/**
* @author whlie(true){learn}
*/
public class DtService {
private DiningTableDAO dt=new DiningTableDAO();
/**
* 显示全部餐桌的状态
*/
public List<DiningTable> ShowState() throws SQLException {
return dt.queryMulti("select id,state from diningTable",DiningTable.class);
}
/**
* 查找餐桌是否存在
*/
public DiningTable getExist(int id) throws SQLException {
return dt.querySingle("select *from diningTable where id=?",DiningTable.class,id);
}
/**
* 如果餐桌可以预定则更新状态
*/
public boolean updateState(int id,String ordername,String ordertel) throws SQLException {
int update = dt.update("update diningTable set state='已预定',ordername=?,ordertel=? where id=?", ordername, ordertel, id);
return update>0;
}
/**
* 提供一个更新餐桌状态的方法
*/
public boolean updateState(int id,String state) throws SQLException {
int update = dt.update("update diningTable set state=? where id=?", state, id);
return update>0;
}
/**
* 将指定餐桌置空
*/
public boolean ToNull(int id,String state) throws SQLException {
int update = dt.update("update diningTable set state=?,ordername='',ordertel='' where id=?", state, id);
return update>0;
}
}
EmpService
package Mhl.service;
import Mhl.dao.EmployeeDAO;
import Mhl.daomain.Employee;
import java.sql.SQLException;
/**
* @author whlie(true){learn}
*/
public class EmpService {
private EmployeeDAO emp=new EmployeeDAO();
public Employee getExist(String empId,String pwd) throws SQLException {
//因为使用了MD5加密所以pwd=MD5(?)
return emp.querySingle("select *from employee where empId=? and pwd=MD5(?)",Employee.class,empId,pwd);
}
}
MenuService
package Mhl.service;
import Mhl.dao.MenuDAO;
import Mhl.daomain.Menu;
import java.sql.SQLException;
import java.util.List;
/**
* @author whlie(true){learn}
*/
public class MenuService {
MenuDAO menu=new MenuDAO();
/**
* 返回所有菜品
*/
public List<Menu> showMenu() throws SQLException {
return menu.queryMulti("SELECT *from Menu",Menu.class);
}
/**
*根据id返回Menu对象
*/
public Menu getMenu(int id) throws SQLException {
return menu.querySingle("select *from Menu where id=?",Menu.class,id);
}
}