当前位置:   article > 正文

满汉楼(Java+Mysql+Druid)_汉满楼代码

汉满楼代码

整体框架

 功能一览

 

 所需文件

如果需要jar包私信我发给你

MhlView

  1. package Mhl.view;
  2. import Chat.tool.Utility;
  3. import Mhl.daomain.*;
  4. import Mhl.service.BillService;
  5. import Mhl.service.DtService;
  6. import Mhl.service.EmpService;
  7. import Mhl.service.MenuService;
  8. import java.sql.SQLException;
  9. import java.util.List;
  10. /**
  11. * @author whlie(true){learn}
  12. */
  13. @SuppressWarnings({"all"})
  14. public class MhlView {
  15. private boolean loop = true;
  16. //一级菜单
  17. private String key = "";
  18. //二级菜单
  19. private String option = "";
  20. //员工业务层
  21. private EmpService es = new EmpService();
  22. //餐桌业务层
  23. private DtService dt = new DtService();
  24. //菜单业务层
  25. private MenuService menu = new MenuService();
  26. //点餐业务层
  27. private BillService bill = new BillService();
  28. public static void main(String[] args) throws SQLException {
  29. new MhlView().mainMenu();
  30. }
  31. /**
  32. * 主菜单显示
  33. *
  34. * @throws SQLException
  35. */
  36. public void mainMenu() throws SQLException {
  37. while (loop) {
  38. System.out.println("==========满汉楼=========");
  39. System.out.println("\t\t1.登录满汉楼");
  40. System.out.println("\t\t2.退出满汉楼");
  41. System.out.print("请输入您的选项:");
  42. key = Utility.readString(1);
  43. switch (key) {
  44. case "1":
  45. System.out.print("请输入员工号:");
  46. String empId = Utility.readString(20);
  47. System.out.print("请输入 密码:");
  48. String pwd = Utility.readString(20);
  49. Employee exist = es.getExist(empId, pwd);
  50. //判断用户是否存在
  51. if (exist != null) {
  52. System.out.println("===========登录成功[" + exist.getName() + "]==========");
  53. while (loop) {
  54. System.out.println("===========满汉楼二级菜单==========");
  55. System.out.println("\t\t1.显示餐桌的状态");
  56. System.out.println("\t\t2.预定餐桌");
  57. System.out.println("\t\t3.显示所有菜品");
  58. System.out.println("\t\t4.点餐服务");
  59. System.out.println("\t\t5.查看账单");
  60. System.out.println("\t\t6.结账");
  61. System.out.println("\t\t7.退出满汉楼");
  62. System.out.print("请输入您的选项:");
  63. option = Utility.readString(1);
  64. switch (option) {
  65. case "1":showDt();break;
  66. case "2":orderdt();break;
  67. case "3":showMenu();break;
  68. case "4":order();break;
  69. case "5":showBills();break;
  70. case "6":payBill();break;
  71. case "7":loop = false;break;
  72. default:
  73. System.out.println("输入没有在选项范围内");
  74. }
  75. }
  76. } else {
  77. System.out.println("===========登录失败==========");
  78. }
  79. case "2":loop = false;break;
  80. default:
  81. System.out.println("输入的有问题,请重新输入");
  82. }
  83. }
  84. System.out.println("退出满汉楼!");
  85. }
  86. /**
  87. * 显示餐桌状态
  88. */
  89. public void showDt() throws SQLException {
  90. List<DiningTable> diningTables = dt.ShowState();
  91. System.out.println("餐桌编号" + "\t\t" + "状态");
  92. for (DiningTable diningTable : diningTables) {
  93. System.out.println(diningTable);
  94. }
  95. }
  96. /**
  97. * 预定餐桌
  98. */
  99. public void orderdt() throws SQLException {
  100. System.out.print("请输入要预定的餐桌编号(-1退出):");
  101. int id = Utility.readInt();
  102. if (id == -1) {
  103. return;
  104. }
  105. System.out.print("是否确定预定该餐桌(Y/N):");
  106. char c = Utility.readConfirmSelection();
  107. if (c == 'Y') {
  108. DiningTable exist = dt.getExist(id);
  109. if (exist == null) {
  110. System.out.println("===========该餐桌不存在==========");
  111. return;
  112. }
  113. if (!("空".equals(exist.getState()))) {
  114. System.out.println("===========该餐桌已被预定或正在就餐==========");
  115. return;
  116. }
  117. System.out.print("预定人姓名:");
  118. String name = Utility.readString(50);
  119. System.out.print("预定人电话");
  120. String tel = Utility.readString(11);
  121. if (dt.updateState(id, name, tel)) {
  122. System.out.println("===========预定成功==========");
  123. } else {
  124. System.out.println("===========预定失败==========");
  125. }
  126. } else {
  127. System.out.println("===========取消预定==========");
  128. return;
  129. }
  130. }
  131. /**
  132. * 显示菜单
  133. */
  134. public void showMenu() throws SQLException {
  135. System.out.println("编号" + "\t\t" + "名称" + "\t\t\t" + "类型" + "\t\t" + "价格");
  136. List<Menu> menus = menu.showMenu();
  137. for (Menu menu1 : menus) {
  138. System.out.println(menu1);
  139. }
  140. }
  141. /**
  142. * 点菜
  143. */
  144. public void order() throws SQLException {
  145. System.out.print("请输入点餐的桌号(-1退出)");
  146. int dt1 = Utility.readInt();
  147. if (dt1 == -1) {
  148. System.out.println("===========取消点餐==========");
  149. return;
  150. }
  151. System.out.print("请输入点餐的菜品号(-1退出)");
  152. int MenuId = Utility.readInt();
  153. if (MenuId == -1) {
  154. System.out.println("===========取消点餐==========");
  155. return;
  156. }
  157. System.out.print("请输入点餐的菜品量(-1退出)");
  158. int nums = Utility.readInt();
  159. if (nums == -1) {
  160. System.out.println("===========取消点餐==========");
  161. return;
  162. }
  163. DiningTable exist = dt.getExist(dt1);
  164. if (exist == null) {
  165. System.out.println("===========餐桌不存在==========");
  166. return;
  167. }
  168. Menu menu = this.menu.getMenu(MenuId);
  169. if (menu == null) {
  170. System.out.println("===========菜品不存在==========");
  171. return;
  172. }
  173. if (bill.orderMenu(MenuId, nums, dt1)) {
  174. System.out.println("===========点餐成功==========");
  175. return;
  176. } else {
  177. System.out.println("===========点餐失败==========");
  178. return;
  179. }
  180. }
  181. /**
  182. * 显示账单
  183. */
  184. public void showBill() throws SQLException {
  185. System.out.println("\n编号\t\t菜品号\t\t菜品量\t\t金额\t\t\t桌号\t\t日期\t\t\t\t\t\t\t状态");
  186. List<Bill> bills = bill.showBill();
  187. for (Bill bill1 : bills) {
  188. System.out.println(bill1);
  189. }
  190. }
  191. /**
  192. * 显示账单(多表)
  193. */
  194. public void showBills() throws SQLException {
  195. System.out.println("\n编号\t\t菜品号\t\t菜品量\t\t金额\t\t\t桌号\t\t日期\t\t\t\t\t\t\t状态\t\t菜品名称");
  196. List<Multiple> multiples = bill.showBills();
  197. for (Multiple bill1 : multiples) {
  198. System.out.println(bill1);
  199. }
  200. }
  201. /**
  202. * 结账
  203. */
  204. public void payBill() throws SQLException {
  205. System.out.print("请输入要结账的餐桌号(-1退出)");
  206. int dt1 = Utility.readInt();
  207. if (dt1 == -1) {
  208. System.out.println("===========取消结账==========");
  209. return;
  210. }
  211. DiningTable exist = dt.getExist(dt1);
  212. if (exist == null) {
  213. System.out.println("===========餐桌不存在==========");
  214. return;
  215. }
  216. if (!bill.judge(dt1)) {
  217. System.out.println("===========该餐桌没有账单==========");
  218. return;
  219. }
  220. System.out.print("请输入结账方式(回车退出)");
  221. String payMode = Utility.readString(20, "");
  222. if ("".equals(payMode)) {
  223. System.out.println("===========取消结账==========");
  224. return;
  225. }
  226. if (bill.payBill(dt1, payMode)) {
  227. System.out.println("===========结账成功==========");
  228. } else {
  229. System.out.println("===========结账失败==========");
  230. }
  231. }
  232. }

BasicDAO

  1. package Mhl.dao;
  2. import JDBC.Tool.DruTool;
  3. import org.apache.commons.dbutils.QueryRunner;
  4. import org.apache.commons.dbutils.handlers.BeanHandler;
  5. import org.apache.commons.dbutils.handlers.BeanListHandler;
  6. import org.apache.commons.dbutils.handlers.ScalarHandler;
  7. import java.sql.Connection;
  8. import java.sql.SQLException;
  9. import java.util.List;
  10. /**
  11. * @author whlie(true){learn}
  12. */
  13. public class BasicDAO<T> {
  14. /**
  15. * 封装常用的各种方法
  16. * @param <T>类型不确定
  17. */
  18. private QueryRunner qr = new QueryRunner();
  19. public int update(String sql,Object... parameters) throws SQLException {
  20. Connection connection = DruTool.getConnection();
  21. int update = qr.update(connection, sql, parameters);
  22. DruTool.close(null,null,connection);
  23. return update;
  24. }
  25. /**
  26. * 返回多个对象(即查询的结果是多行), 针对任意表
  27. * @param sql sql 语句,可以有 ?
  28. * @param clazz 传入一个类的 Class 对象 比如 Actor.class
  29. * @param parameters 传入 ? 的具体的值,可以是多个
  30. * @return 根据 Actor.class 返回对应的 ArrayList 集合
  31. */
  32. public List<T> queryMulti(String sql, Class<T> clazz, Object... parameters) throws SQLException {
  33. Connection connection = DruTool.getConnection();
  34. List<T> list = qr.query(connection, sql, new BeanListHandler<T>(clazz), parameters);
  35. DruTool.close(null,null,connection);
  36. return list;
  37. }
  38. /**
  39. * 查询单行结果 的通用方法
  40. * @param sql
  41. * @param clazz
  42. * @param parameters
  43. * @return
  44. */
  45. public T querySingle(String sql, Class<T> clazz, Object... parameters) throws SQLException {
  46. Connection connection = DruTool.getConnection();
  47. T query = qr.query(connection, sql, new BeanHandler<T>(clazz), parameters);
  48. DruTool.close(null,null,connection);
  49. return query;
  50. }
  51. /**
  52. *返回单值的方法
  53. * @param sql
  54. * @param parameters
  55. * @return
  56. */
  57. public Object queryScalar(String sql, Object... parameters) throws SQLException {
  58. Connection connection = DruTool.getConnection();
  59. Object query = qr.query(connection, sql, new ScalarHandler(), parameters);
  60. DruTool.close(null,null,connection);
  61. return query;
  62. }
  63. }

BillDAO

  1. package Mhl.dao;
  2. import Mhl.daomain.Bill;
  3. /**
  4. * @author whlie(true){learn}
  5. */
  6. public class BillDAO extends BasicDAO<Bill>{
  7. }

dao包下的其他文件和BillDAO类似只是名字不同

BillService

  1. package Mhl.service;
  2. import Mhl.dao.BillDAO;
  3. import Mhl.dao.MultipleDAO;
  4. import Mhl.daomain.Bill;
  5. import Mhl.daomain.Multiple;
  6. import java.sql.SQLException;
  7. import java.util.List;
  8. import java.util.UUID;
  9. /**
  10. * @author whlie(true){learn}
  11. */
  12. public class BillService {
  13. private BillDAO ba=new BillDAO();
  14. private MenuService ms=new MenuService();
  15. private DtService dts=new DtService();
  16. //多表查询
  17. private MultipleDAO mp=new MultipleDAO();
  18. /**
  19. * 点菜
  20. * 生成账单
  21. * 更新餐桌状态
  22. */
  23. public boolean orderMenu(int menuId,int nums,int diningTable) throws SQLException {
  24. //生成随机账单号
  25. String billId = UUID.randomUUID().toString();
  26. //从菜单业务层根据菜单id获取到对应菜品的价格
  27. int update = ba.update("insert into bill values(null,?,?,?,?,?,now(),'未结账')",
  28. billId, menuId, nums, ms.getMenu(menuId).getPrice() * nums, diningTable);
  29. if (update<=0){
  30. return false;
  31. }
  32. return dts.updateState(diningTable,"就餐中");
  33. }
  34. /**
  35. * 返回所有账单(单表)
  36. */
  37. public List<Bill> showBill() throws SQLException {
  38. return ba.queryMulti("select *from bill",Bill.class);
  39. }
  40. /**
  41. * 返回所有账单(多表)
  42. * @return
  43. */
  44. public List<Multiple> showBills() throws SQLException {
  45. return mp.queryMulti("select bill.*,name from bill,Menu where bill.menuId=Menu.id", Multiple.class);
  46. }
  47. /**
  48. * 判断某张餐桌是否有未结账的账单
  49. */
  50. public boolean judge(int diningTable) throws SQLException {
  51. Bill bill = ba.querySingle("select *from bill where diningTable=? and state='未结账' limit 0,1", Bill.class, diningTable);
  52. return bill!=null;
  53. }
  54. /**
  55. * 完成结账
  56. */
  57. public boolean payBill(int diningTable,String payMode) throws SQLException {
  58. //修改bill表的状态
  59. int update = ba.update("update bill set state=? where diningTable=? and state='未结账'", payMode, diningTable);
  60. if (update<=0){
  61. return false;
  62. }
  63. //将对应的餐桌置空
  64. if (!dts.ToNull(diningTable,"空")){
  65. return false;
  66. }
  67. return true;
  68. }
  69. }

DtService

  1. package Mhl.service;
  2. import Mhl.dao.DiningTableDAO;
  3. import Mhl.daomain.DiningTable;
  4. import java.sql.SQLException;
  5. import java.util.List;
  6. /**
  7. * @author whlie(true){learn}
  8. */
  9. public class DtService {
  10. private DiningTableDAO dt=new DiningTableDAO();
  11. /**
  12. * 显示全部餐桌的状态
  13. */
  14. public List<DiningTable> ShowState() throws SQLException {
  15. return dt.queryMulti("select id,state from diningTable",DiningTable.class);
  16. }
  17. /**
  18. * 查找餐桌是否存在
  19. */
  20. public DiningTable getExist(int id) throws SQLException {
  21. return dt.querySingle("select *from diningTable where id=?",DiningTable.class,id);
  22. }
  23. /**
  24. * 如果餐桌可以预定则更新状态
  25. */
  26. public boolean updateState(int id,String ordername,String ordertel) throws SQLException {
  27. int update = dt.update("update diningTable set state='已预定',ordername=?,ordertel=? where id=?", ordername, ordertel, id);
  28. return update>0;
  29. }
  30. /**
  31. * 提供一个更新餐桌状态的方法
  32. */
  33. public boolean updateState(int id,String state) throws SQLException {
  34. int update = dt.update("update diningTable set state=? where id=?", state, id);
  35. return update>0;
  36. }
  37. /**
  38. * 将指定餐桌置空
  39. */
  40. public boolean ToNull(int id,String state) throws SQLException {
  41. int update = dt.update("update diningTable set state=?,ordername='',ordertel='' where id=?", state, id);
  42. return update>0;
  43. }
  44. }

EmpService

  1. package Mhl.service;
  2. import Mhl.dao.EmployeeDAO;
  3. import Mhl.daomain.Employee;
  4. import java.sql.SQLException;
  5. /**
  6. * @author whlie(true){learn}
  7. */
  8. public class EmpService {
  9. private EmployeeDAO emp=new EmployeeDAO();
  10. public Employee getExist(String empId,String pwd) throws SQLException {
  11. //因为使用了MD5加密所以pwd=MD5(?)
  12. return emp.querySingle("select *from employee where empId=? and pwd=MD5(?)",Employee.class,empId,pwd);
  13. }
  14. }

MenuService

  1. package Mhl.service;
  2. import Mhl.dao.MenuDAO;
  3. import Mhl.daomain.Menu;
  4. import java.sql.SQLException;
  5. import java.util.List;
  6. /**
  7. * @author whlie(true){learn}
  8. */
  9. public class MenuService {
  10. MenuDAO menu=new MenuDAO();
  11. /**
  12. * 返回所有菜品
  13. */
  14. public List<Menu> showMenu() throws SQLException {
  15. return menu.queryMulti("SELECT *from Menu",Menu.class);
  16. }
  17. /**
  18. *根据id返回Menu对象
  19. */
  20. public Menu getMenu(int id) throws SQLException {
  21. return menu.querySingle("select *from Menu where id=?",Menu.class,id);
  22. }
  23. }
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号