Java学习-第一部分-第三阶段-项目实战:满汉楼项目

打印 上一主题 下一主题

主题 920|帖子 920|积分 2760

满汉楼项目

笔记目录:(https://www.cnblogs.com/wenjie2000/p/16378441.html)
注意:笔记内容仅为实现该项目的基本后端功能,并不会实现可视化界面,效果都在控制台展示。
要完成的满汉楼项目说明
满汉楼项目功能多,界面复杂,涉及到复杂的awt和swing技术和事件编程,做如下调整:

  • 去掉界面和事件处理(工作中使用很少),使用控制台界面
  • 完成满汉楼项目的登录、订座、点餐和结账、查看账单等功能。
  • 提示:在实际工作中,独立完成项目新功能非常重要,这是锻炼编程能力和思想的重要途径
界面设计

用户登录

显示餐桌状态

预定

显示菜品

点餐

查看账单

结账

项目设计


准备工作

准备工具类Utility,提高开发效率,并搭建项目的整体结构
在实际开发中,公司都会提供相应的工具类和开发库,可以提高开发效率

  • 了解Utility类的使用
  • 测试Utility类
基本结构

在MySQL中创建表
  1. #用户表
  2. create table employee (
  3.         id int primary key auto_increment, #自增
  4.         empId varchar(50) not null default '',#员工号
  5.         pwd char(32) not null default '',#密码md5
  6.         name varchar(50) not null default '',#姓名
  7.         job varchar(50) not null default '' #岗位
  8. )charset=utf8;
  9. insert into employee values(null, '6668612', md5('123456'), '张三丰', '经理');
  10. insert into employee values(null, '6668622', md5('123456'),'小龙女', '服务员');
  11. insert into employee values(null, '6668633', md5('123456'), '张无忌', '收银员');
  12. insert into employee values(null, '666', md5('123456'), '老韩', '经理');
  13. #餐桌表
  14. create table diningTable (
  15.         id int primary key auto_increment, #自增, 表示餐桌编号
  16.         state varchar(20) not null default '',#餐桌的状态
  17.         orderName varchar(50) not null default '',#预订人的名字
  18.         orderTel varchar(20) not null default ''
  19. )charset=utf8;
  20. insert into diningTable values(null, '空','','');
  21. #菜谱
  22. create table menu (
  23.         id int primary key auto_increment, #自增主键,作为菜谱编号(唯一)
  24.         name varchar(50) not null default '',#菜品名称
  25.         type varchar(50) not null default '', #菜品种类
  26.         price double not null default 0#价格
  27. )charset=utf8;
  28. insert into menu values(null, '八宝饭', '主食类', 10);
  29. insert into menu values(null, '叉烧包', '主食类', 20);
  30. insert into menu values(null, '宫保鸡丁', '热菜类', 30);
  31. insert into menu values(null, '山药拨鱼', '凉菜类', 14);
  32. insert into menu values(null, '银丝卷', '甜食类', 9);
  33. insert into menu values(null, '水煮鱼', '热菜类', 26);
  34. insert into menu values(null, '甲鱼汤', '汤类', 100);
  35. insert into menu values(null, '鸡蛋汤', '汤类', 16);
  36. #账单流水, 考虑可以分开结账, 并考虑将来分别统计各个不同菜品的销售情况
  37. create table bill (
  38.         id int primary key auto_increment, #自增主键
  39.         billId varchar(50) not null default '',#账单号可以按照自己规则生成 UUID
  40.         menuId int not null default 0,#菜品的编号, 也可以使用外键
  41.         nums SMALLINT not null default 0,#份数
  42.         money double not null default 0, #金额
  43.         diningTableId int not null default 0, #餐桌
  44.         billDate datetime not null ,#订单日期
  45.         state varchar(50) not null default '' # 状态 '未结账' , '已经结账', '挂单'
  46. )charset=utf8;
复制代码
druid配置文件(需要根据自己情况进行配置)
  1. #key=value
  2. driverClassName=com.mysql.jdbc.Driver
  3. url=jdbc:mysql://localhost:3306/db02?rewriteBatchedStatements=true
  4. username=root
  5. password=123456
  6. #initial connection Size
  7. initialSize=10
  8. #min idle connecton size
  9. minIdle=5
  10. #max active connection size
  11. maxActive=50
  12. #max wait time (5000 mil seconds)
  13. maxWait=5000
复制代码
BasicDAO(如果看过我上一节的笔记,就可以不需要再写一遍,直接拿来用)
  1. package com.zwj.mhl.dao;
  2. import com.zwj.mhl.utils.JDBCUtilsByDruid;
  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. * 开发BasicDAO , 是其他DAO的父类
  12. */
  13. public class BasicDAO<T> { //泛型指定具体类型
  14.     private QueryRunner qr =  new QueryRunner();
  15.     //开发通用的dml方法, 针对任意的表
  16.     public int update(String sql, Object... parameters) {
  17.         Connection connection = null;
  18.         try {
  19.             connection = JDBCUtilsByDruid.getConnection();
  20.             int update = qr.update(connection, sql, parameters);
  21.             return  update;
  22.         } catch (SQLException e) {
  23.            throw  new RuntimeException(e); //将编译异常->运行异常 ,抛出
  24.         } finally {
  25.             JDBCUtilsByDruid.close(null, null, connection);
  26.         }
  27.     }
  28.     //返回多个对象(即查询的结果是多行), 针对任意表
  29.     /**
  30.      *
  31.      * @param sql sql 语句,可以有 ?
  32.      * @param clazz 传入一个类的Class对象 比如 Actor.class
  33.      * @param parameters 传入 ? 的具体的值,可以是多个
  34.      * @return 根据Actor.class 返回对应的 ArrayList 集合
  35.      */
  36.     public List<T> queryMulti(String sql, Class<T> clazz, Object... parameters) {
  37.         Connection connection = null;
  38.         try {
  39.             connection = JDBCUtilsByDruid.getConnection();
  40.             return qr.query(connection, sql, new BeanListHandler<T>(clazz), parameters);
  41.         } catch (SQLException e) {
  42.             throw  new RuntimeException(e); //将编译异常->运行异常 ,抛出
  43.         } finally {
  44.             JDBCUtilsByDruid.close(null, null, connection);
  45.         }
  46.     }
  47.     //查询单行结果 的通用方法
  48.     public T querySingle(String sql, Class<T> clazz, Object... parameters) {
  49.         Connection connection = null;
  50.         try {
  51.             connection = JDBCUtilsByDruid.getConnection();
  52.             return  qr.query(connection, sql, new BeanHandler<T>(clazz), parameters);
  53.         } catch (SQLException e) {
  54.             throw  new RuntimeException(e); //将编译异常->运行异常 ,抛出
  55.         } finally {
  56.             JDBCUtilsByDruid.close(null, null, connection);
  57.         }
  58.     }
  59.     //查询单行单列的方法,即返回单值的方法
  60.     public Object queryScalar(String sql, Object... parameters) {
  61.         Connection connection = null;
  62.         try {
  63.             connection = JDBCUtilsByDruid.getConnection();
  64.             return  qr.query(connection, sql, new ScalarHandler(), parameters);
  65.         } catch (SQLException e) {
  66.             throw  new RuntimeException(e); //将编译异常->运行异常 ,抛出
  67.         } finally {
  68.             JDBCUtilsByDruid.close(null, null, connection);
  69.         }
  70.     }
  71. }
复制代码
JDBCUtilsByDruid(基于druid数据库连接池的工具类)
  1. package com.zwj.mhl.utils;
  2. import com.alibaba.druid.pool.DruidDataSourceFactory;
  3. import javax.sql.DataSource;
  4. import java.io.FileInputStream;
  5. import java.sql.Connection;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9. import java.util.Properties;
  10. /**
  11. * 基于druid数据库连接池的工具类
  12. */
  13. public class JDBCUtilsByDruid {
  14.     private static DataSource ds;
  15.     //在静态代码块完成 ds初始化
  16.     static {
  17.         Properties properties = new Properties();
  18.         try {
  19.             properties.load(new FileInputStream("src\\druid.properties"));
  20.             ds = DruidDataSourceFactory.createDataSource(properties);
  21.         } catch (Exception e) {
  22.             e.printStackTrace();
  23.         }
  24.     }
  25.     //编写getConnection方法
  26.     public static Connection getConnection() throws SQLException {
  27.         return ds.getConnection();
  28.     }
  29.     //关闭连接, 老师再次强调: 在数据库连接池技术中,close 不是真的断掉连接
  30.     //而是把使用的Connection对象放回连接池
  31.     public static void close(ResultSet resultSet, Statement statement, Connection connection) {
  32.         try {
  33.             if (resultSet != null) {
  34.                 resultSet.close();
  35.             }
  36.             if (statement != null) {
  37.                 statement.close();
  38.             }
  39.             if (connection != null) {
  40.                 connection.close();
  41.             }
  42.         } catch (SQLException e) {
  43.             throw new RuntimeException(e);
  44.         }
  45.     }
  46. }
复制代码
Utility(辅助输入数据的工具类)
  1. package com.zwj.mhl.utils;
  2. /**
  3.    工具类的作用:
  4.    处理各种情况的用户输入,并且能够按照程序员的需求,得到用户的控制台输入。
  5. */
  6. import java.util.*;
  7. public class Utility {
  8.    //静态属性。。。
  9.     private static Scanner scanner = new Scanner(System.in);
  10.    
  11.     /**
  12.      * 功能:读取键盘输入的一个菜单选项,值:1——5的范围
  13.      * @return 1——5
  14.      */
  15.    public static char readMenuSelection() {
  16.         char c;
  17.         for (; ; ) {
  18.             String str = readKeyBoard(1, false);//包含一个字符的字符串
  19.             c = str.charAt(0);//将字符串转换成字符char类型
  20.             if (c != '1' && c != '2' &&
  21.                 c != '3' && c != '4' && c != '5') {
  22.                 System.out.print("选择错误,请重新输入:");
  23.             } else break;
  24.         }
  25.         return c;
  26.     }
  27.    /**
  28.     * 功能:读取键盘输入的一个字符
  29.     * @return 一个字符
  30.     */
  31.     public static char readChar() {
  32.         String str = readKeyBoard(1, false);//就是一个字符
  33.         return str.charAt(0);
  34.     }
  35.     /**
  36.      * 功能:读取键盘输入的一个字符,如果直接按回车,则返回指定的默认值;否则返回输入的那个字符
  37.      * @param defaultValue 指定的默认值
  38.      * @return 默认值或输入的字符
  39.      */
  40.    
  41.     public static char readChar(char defaultValue) {
  42.         String str = readKeyBoard(1, true);//要么是空字符串,要么是一个字符
  43.         return (str.length() == 0) ? defaultValue : str.charAt(0);
  44.     }
  45.    
  46.     /**
  47.      * 功能:读取键盘输入的整型,长度小于2位
  48.      * @return 整数
  49.      */
  50.     public static int readInt() {
  51.         int n;
  52.         for (; ; ) {
  53.             String str = readKeyBoard(2, false);//一个整数,长度<=2位
  54.             try {
  55.                 n = Integer.parseInt(str);//将字符串转换成整数
  56.                 break;
  57.             } catch (NumberFormatException e) {
  58.                 System.out.print("数字输入错误,请重新输入:");
  59.             }
  60.         }
  61.         return n;
  62.     }
  63.     /**
  64.      * 功能:读取键盘输入的 整数或默认值,如果直接回车,则返回默认值,否则返回输入的整数
  65.      * @param defaultValue 指定的默认值
  66.      * @return 整数或默认值
  67.      */
  68.     public static int readInt(int defaultValue) {
  69.         int n;
  70.         for (; ; ) {
  71.             String str = readKeyBoard(10, true);
  72.             if (str.equals("")) {
  73.                 return defaultValue;
  74.             }
  75.          
  76.          //异常处理...
  77.             try {
  78.                 n = Integer.parseInt(str);
  79.                 break;
  80.             } catch (NumberFormatException e) {
  81.                 System.out.print("数字输入错误,请重新输入:");
  82.             }
  83.         }
  84.         return n;
  85.     }
  86.     /**
  87.      * 功能:读取键盘输入的指定长度的字符串
  88.      * @param limit 限制的长度
  89.      * @return 指定长度的字符串
  90.      */
  91.     public static String readString(int limit) {
  92.         return readKeyBoard(limit, false);
  93.     }
  94.     /**
  95.      * 功能:读取键盘输入的指定长度的字符串或默认值,如果直接回车,返回默认值,否则返回字符串
  96.      * @param limit 限制的长度
  97.      * @param defaultValue 指定的默认值
  98.      * @return 指定长度的字符串
  99.      */
  100.    
  101.     public static String readString(int limit, String defaultValue) {
  102.         String str = readKeyBoard(limit, true);
  103.         return str.equals("")? defaultValue : str;
  104.     }
  105.    /**
  106.     * 功能:读取键盘输入的确认选项,Y或N
  107.     * 将小的功能,封装到一个方法中.
  108.     * @return Y或N
  109.     */
  110.     public static char readConfirmSelection() {
  111.         System.out.println("请输入你的选择(Y/N)");
  112.         char c;
  113.         for (; ; ) {//无限循环
  114.            //在这里,将接受到字符,转成了大写字母
  115.            //y => Y n=>N
  116.             String str = readKeyBoard(1, false).toUpperCase();
  117.             c = str.charAt(0);
  118.             if (c == 'Y' || c == 'N') {
  119.                 break;
  120.             } else {
  121.                 System.out.print("选择错误,请重新输入:");
  122.             }
  123.         }
  124.         return c;
  125.     }
  126.     /**
  127.      * 功能: 读取一个字符串
  128.      * @param limit 读取的长度
  129.      * @param blankReturn 如果为true ,表示 可以读空字符串。
  130.      *                   如果为false表示 不能读空字符串。
  131.      *           
  132.     * 如果输入为空,或者输入大于limit的长度,就会提示重新输入。
  133.      * @return
  134.      */
  135.     private static String readKeyBoard(int limit, boolean blankReturn) {
  136.         
  137.       //定义了字符串
  138.       String line = "";
  139.       //scanner.hasNextLine() 判断有没有下一行
  140.         while (scanner.hasNextLine()) {
  141.             line = scanner.nextLine();//读取这一行
  142.            
  143.          //如果line.length=0, 即用户没有输入任何内容,直接回车
  144.          if (line.length() == 0) {
  145.                 if (blankReturn) return line;//如果blankReturn=true,可以返回空串
  146.                 else continue; //如果blankReturn=false,不接受空串,必须输入内容
  147.             }
  148.          //如果用户输入的内容大于了 limit,就提示重写输入  
  149.          //如果用户如的内容 >0 <= limit ,我就接受
  150.             if (line.length() < 1 || line.length() > limit) {
  151.                 System.out.print("输入长度(不能大于" + limit + ")错误,请重新输入:");
  152.                 continue;
  153.             }
  154.             break;
  155.         }
  156.         return line;
  157.     }
  158. }
复制代码
代码实现

注意:建议看代码之前先根据上面的信息自己写

com.zwj.mhl
dao
BasicDAO
  1. package com.zwj.mhl.dao;
  2. import com.zwj.mhl.utils.JDBCUtilsByDruid;
  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. * 开发BasicDAO , 是其他DAO的父类
  12. */
  13. public class BasicDAO<T> { //泛型指定具体类型
  14.     private QueryRunner qr =  new QueryRunner();
  15.     //开发通用的dml方法, 针对任意的表
  16.     public int update(String sql, Object... parameters) {
  17.         Connection connection = null;
  18.         try {
  19.             connection = JDBCUtilsByDruid.getConnection();
  20.             int update = qr.update(connection, sql, parameters);
  21.             return  update;
  22.         } catch (SQLException e) {
  23.            throw  new RuntimeException(e); //将编译异常->运行异常 ,抛出
  24.         } finally {
  25.             JDBCUtilsByDruid.close(null, null, connection);
  26.         }
  27.     }
  28.     //返回多个对象(即查询的结果是多行), 针对任意表
  29.     /**
  30.      *
  31.      * @param sql sql 语句,可以有 ?
  32.      * @param clazz 传入一个类的Class对象 比如 Actor.class
  33.      * @param parameters 传入 ? 的具体的值,可以是多个
  34.      * @return 根据Actor.class 返回对应的 ArrayList 集合
  35.      */
  36.     public List<T> queryMulti(String sql, Class<T> clazz, Object... parameters) {
  37.         Connection connection = null;
  38.         try {
  39.             connection = JDBCUtilsByDruid.getConnection();
  40.             return qr.query(connection, sql, new BeanListHandler<T>(clazz), parameters);
  41.         } catch (SQLException e) {
  42.             throw  new RuntimeException(e); //将编译异常->运行异常 ,抛出
  43.         } finally {
  44.             JDBCUtilsByDruid.close(null, null, connection);
  45.         }
  46.     }
  47.     //查询单行结果 的通用方法
  48.     public T querySingle(String sql, Class<T> clazz, Object... parameters) {
  49.         Connection connection = null;
  50.         try {
  51.             connection = JDBCUtilsByDruid.getConnection();
  52.             return  qr.query(connection, sql, new BeanHandler<T>(clazz), parameters);
  53.         } catch (SQLException e) {
  54.             throw  new RuntimeException(e); //将编译异常->运行异常 ,抛出
  55.         } finally {
  56.             JDBCUtilsByDruid.close(null, null, connection);
  57.         }
  58.     }
  59.     //查询单行单列的方法,即返回单值的方法
  60.     public Object queryScalar(String sql, Object... parameters) {
  61.         Connection connection = null;
  62.         try {
  63.             connection = JDBCUtilsByDruid.getConnection();
  64.             return  qr.query(connection, sql, new ScalarHandler(), parameters);
  65.         } catch (SQLException e) {
  66.             throw  new RuntimeException(e); //将编译异常->运行异常 ,抛出
  67.         } finally {
  68.             JDBCUtilsByDruid.close(null, null, connection);
  69.         }
  70.     }
  71. }
复制代码
BillDAO
  1. package com.zwj.mhl.dao;
  2. import com.zwj.mhl.domain.Bill;
  3. public class BillDAO extends BasicDAO<Bill> {
  4. }
复制代码
DiningTableDAO
  1. package com.zwj.mhl.dao;
  2. import com.zwj.mhl.domain.DiningTable;
  3. public class DiningTableDAO extends BasicDAO<DiningTable> {
  4.     //如果有特别的操作,可以写在 DiningTableDAO
  5. }
复制代码
EmployeeDAO
  1. package com.zwj.mhl.dao;
  2. import com.zwj.mhl.domain.Employee;
  3. public class EmployeeDAO extends BasicDAO<Employee> {
  4.     //这里还可以写特有的操作.
  5. }
复制代码
MenuDAO
  1. package com.zwj.mhl.dao;
  2. import com.zwj.mhl.domain.Menu;
  3. public class MenuDAO extends BasicDAO<Menu> {
  4. }
复制代码
MultiTableDAO
  1. package com.zwj.mhl.dao;
  2. import com.zwj.mhl.domain.MultiTableBean;
  3. public class MultiTableDAO extends BasicDAO<MultiTableBean> {
  4. }
复制代码
domain
Bill
  1. package com.zwj.mhl.domain;
  2. import java.util.Date;
  3. /**
  4. * Bill 是javabean 和 bill对应
  5. * id int primary key auto_increment, #自增主键
  6. *     billId varchar(50) not null default '',#账单号可以按照自己规则生成 UUID
  7. *     menuId int not null default 0,#菜品的编号, 也可以使用外键
  8. *     nums int not null default 0,#份数
  9. *     money double not null default 0, #金额
  10. *     diningTableId int not null default 0, #餐桌
  11. *     billDate datetime not null ,#订单日期
  12. *     state varchar(50) not null default '' # 状态 '未结账' , '已经结账', '挂单'
  13. */
  14. public class Bill {
  15.     private Integer id;
  16.     private String billId;
  17.     private Integer menuId;
  18.     private Integer nums;
  19.     private Double money;
  20.     private Integer diningTableId;
  21.     private Date billDate;
  22.     private String state;
  23.     public Bill() {
  24.     }
  25.     public Bill(Integer id, String billId, Integer menuId, Integer nums, Double money, Integer diningTableId, Date billDate, String state) {
  26.         this.id = id;
  27.         this.billId = billId;
  28.         this.menuId = menuId;
  29.         this.nums = nums;
  30.         this.money = money;
  31.         this.diningTableId = diningTableId;
  32.         this.billDate = billDate;
  33.         this.state = state;
  34.     }
  35.     public Integer getId() {
  36.         return id;
  37.     }
  38.     public void setId(Integer id) {
  39.         this.id = id;
  40.     }
  41.     public String getBillId() {
  42.         return billId;
  43.     }
  44.     public void setBillId(String billId) {
  45.         this.billId = billId;
  46.     }
  47.     public Integer getMenuId() {
  48.         return menuId;
  49.     }
  50.     public void setMenuId(Integer menuId) {
  51.         this.menuId = menuId;
  52.     }
  53.     public Integer getNums() {
  54.         return nums;
  55.     }
  56.     public void setNums(Integer nums) {
  57.         this.nums = nums;
  58.     }
  59.     public Double getMoney() {
  60.         return money;
  61.     }
  62.     public void setMoney(Double money) {
  63.         this.money = money;
  64.     }
  65.     public Integer getDiningTableId() {
  66.         return diningTableId;
  67.     }
  68.     public void setDiningTableId(Integer diningTableId) {
  69.         this.diningTableId = diningTableId;
  70.     }
  71.     public Date getBillDate() {
  72.         return billDate;
  73.     }
  74.     public void setBillDate(Date billDate) {
  75.         this.billDate = billDate;
  76.     }
  77.     public String getState() {
  78.         return state;
  79.     }
  80.     public void setState(String state) {
  81.         this.state = state;
  82.     }
  83.     @Override
  84.     public String toString() {
  85.         return  id +
  86.                 "\t\t" + menuId +
  87.                 "\t\t\t" + nums +
  88.                 "\t\t\t" + money +
  89.                 "\t" + diningTableId +
  90.                 "\t\t" + billDate +
  91.                 "\t\t" + state ;
  92.     }
  93. }
复制代码
DiningTable
  1. package com.zwj.mhl.domain;
  2. /**
  3. * 这是一个javabean 和 diningTable 表对应
  4. *     id int primary key auto_increment, #自增, 表示餐桌编号
  5. *     state varchar(20) not null default '',#餐桌的状态
  6. *     orderName varchar(50) not null default '',#预订人的名字
  7. *     orderTel varchar(20) not null default ''
  8. */
  9. public class DiningTable {
  10.     private Integer id;
  11.     private String state;
  12.     private String orderName;
  13.     private String orderTel;
  14.     public DiningTable() {
  15.     }
  16.     public DiningTable(Integer id, String state, String orderName, String orderTel) {
  17.         this.id = id;
  18.         this.state = state;
  19.         this.orderName = orderName;
  20.         this.orderTel = orderTel;
  21.     }
  22.     public Integer getId() {
  23.         return id;
  24.     }
  25.     public void setId(Integer id) {
  26.         this.id = id;
  27.     }
  28.     public String getState() {
  29.         return state;
  30.     }
  31.     public void setState(String state) {
  32.         this.state = state;
  33.     }
  34.     public String getOrderName() {
  35.         return orderName;
  36.     }
  37.     public void setOrderName(String orderName) {
  38.         this.orderName = orderName;
  39.     }
  40.     public String getOrderTel() {
  41.         return orderTel;
  42.     }
  43.     public void setOrderTel(String orderTel) {
  44.         this.orderTel = orderTel;
  45.     }
  46.     @Override
  47.     public String toString() {
  48.         return id + "\t\t\t" + state;
  49.     }
  50. }
复制代码
Employee
  1. package com.zwj.mhl.domain;
  2. /**
  3. * 这是一个javabean 和 employee对应
  4. * id int primary key auto_increment, #自增
  5. *     empId varchar(50) not null default '',#员工号
  6. *     pwd char(32) not null default '',#密码md5
  7. *     name varchar(50) not null default '',#姓名
  8. *     job varchar(50) not null default '' #岗位
  9. */
  10. public class Employee {
  11.     private Integer id;
  12.     private String empId;
  13.     private String pwd;
  14.     private String name;
  15.     private String job;
  16.     public Employee() { //无参构造器,底层apache-dbutils反射需要
  17.     }
  18.     public Employee(Integer id, String empId, String pwd, String name, String job) {
  19.         this.id = id;
  20.         this.empId = empId;
  21.         this.pwd = pwd;
  22.         this.name = name;
  23.         this.job = job;
  24.     }
  25.     public Integer getId() {
  26.         return id;
  27.     }
  28.     public void setId(Integer id) {
  29.         this.id = id;
  30.     }
  31.     public String getEmpId() {
  32.         return empId;
  33.     }
  34.     public void setEmpId(String empId) {
  35.         this.empId = empId;
  36.     }
  37.     public String getPwd() {
  38.         return pwd;
  39.     }
  40.     public void setPwd(String pwd) {
  41.         this.pwd = pwd;
  42.     }
  43.     public String getName() {
  44.         return name;
  45.     }
  46.     public void setName(String name) {
  47.         this.name = name;
  48.     }
  49.     public String getJob() {
  50.         return job;
  51.     }
  52.     public void setJob(String job) {
  53.         this.job = job;
  54.     }
  55. }
复制代码
Menu
  1. package com.zwj.mhl.domain;
  2. /**
  3. * 该类(javabean)和 menu 表对应
  4. * id int primary key auto_increment, #自增主键,作为菜谱编号(唯一)
  5. * name varchar(50) not null default '',#菜品名称
  6. * type varchar(50) not null default '', #菜品种类
  7. * price double not null default 0#价格
  8. */
  9. public class Menu {
  10.     private Integer id;
  11.     private String name;
  12.     private String type;
  13.     private Double price;
  14.     public Menu() {//无参构造器
  15.     }
  16.     public Menu(Integer id, String name, String type, Double price) {
  17.         this.id = id;
  18.         this.name = name;
  19.         this.type = type;
  20.         this.price = price;
  21.     }
  22.     public Integer getId() {
  23.         return id;
  24.     }
  25.     public void setId(Integer id) {
  26.         this.id = id;
  27.     }
  28.     public String getName() {
  29.         return name;
  30.     }
  31.     public void setName(String name) {
  32.         this.name = name;
  33.     }
  34.     public String getType() {
  35.         return type;
  36.     }
  37.     public void setType(String type) {
  38.         this.type = type;
  39.     }
  40.     public Double getPrice() {
  41.         return price;
  42.     }
  43.     public void setPrice(Double price) {
  44.         this.price = price;
  45.     }
  46.     @Override
  47.     public String toString() {
  48.         return id + "\t\t\t" + name + "\t\t" + type + "\t\t" + price;
  49.     }
  50. }
复制代码
MultiTableBean
  1. package com.zwj.mhl.domain;
  2. import java.util.Date;
  3. /**
  4. * 这是一个javabean 可以和多张表进行对应
  5. */
  6. public class MultiTableBean {
  7.     private Integer id;
  8.     private String billId;
  9.     private Integer menuId;
  10.     private Integer nums;
  11.     private Double money;
  12.     private Integer diningTableId;
  13.     private Date billDate;
  14.     private String state;
  15.     //增加一个来自menu表的列 name
  16.     //思考 这里的属性名是否一定要和表的列名保持一致.
  17.     //答: 可以不一致,但是需要sql做相应的修改, 规范需要保持一致.
  18.     private String name;
  19.     //增加来自menu表的列 price
  20.     private Double price;//默认值 null
  21.     public MultiTableBean() {
  22.         System.out.println("反射调用....");
  23.     }
  24. //    public MultiTableBean(Integer id, String billId, Integer menuId, Integer nums, Double money, Integer diningTableId, Date billDate, String state, String name, Double price) {
  25. //        this.id = id;
  26. //        this.billId = billId;
  27. //        this.menuId = menuId;
  28. //        this.nums = nums;
  29. //        this.money = money;
  30. //        this.diningTableId = diningTableId;
  31. //        this.billDate = billDate;
  32. //        this.state = state;
  33. //        this.name = name;
  34. //        this.price = price;
  35. //    }
  36.     //给price生成setter 和 getter
  37.     public Double getPrice() {
  38.         return price;
  39.     }
  40.     public void setPrice(Double price) {
  41.         this.price = price;
  42.     }
  43.     //给name生成setter 和 getter
  44.     public String getName() {
  45.         return name;
  46.     }
  47.     public void setName(String name) {
  48.         this.name = name;
  49.     }
  50.     public Integer getId() {
  51.         return id;
  52.     }
  53.     public void setId(Integer id) {
  54.         this.id = id;
  55.     }
  56.     public String getBillId() {
  57.         return billId;
  58.     }
  59.     public void setBillId(String billId) {
  60.         this.billId = billId;
  61.     }
  62.     public Integer getMenuId() {
  63.         return menuId;
  64.     }
  65.     public void setMenuId(Integer menuId) {
  66.         this.menuId = menuId;
  67.     }
  68.     public Integer getNums() {
  69.         return nums;
  70.     }
  71.     public void setNums(Integer nums) {
  72.         this.nums = nums;
  73.     }
  74.     public Double getMoney() {
  75.         return money;
  76.     }
  77.     public void setMoney(Double money) {
  78.         this.money = money;
  79.     }
  80.     public Integer getDiningTableId() {
  81.         return diningTableId;
  82.     }
  83.     public void setDiningTableId(Integer diningTableId) {
  84.         this.diningTableId = diningTableId;
  85.     }
  86.     public Date getBillDate() {
  87.         return billDate;
  88.     }
  89.     public void setBillDate(Date billDate) {
  90.         this.billDate = billDate;
  91.     }
  92.     public String getState() {
  93.         return state;
  94.     }
  95.     public void setState(String state) {
  96.         this.state = state;
  97.     }
  98.     @Override
  99.     public String toString() {
  100.         return  id +
  101.                 "\t\t" + menuId +
  102.                 "\t\t\t" + nums +
  103.                 "\t\t\t" + money +
  104.                 "\t" + diningTableId +
  105.                 "\t\t" + billDate +
  106.                 "\t\t" + state +
  107.                 "\t\t" + name +
  108.                 "\t\t" + price;
  109.     }
  110. }
复制代码
service
BillService
  1. package com.zwj.mhl.service;
  2. import com.zwj.mhl.dao.BillDAO;
  3. import com.zwj.mhl.dao.MultiTableDAO;
  4. import com.zwj.mhl.domain.Bill;
  5. import com.zwj.mhl.domain.MultiTableBean;
  6. import java.util.List;
  7. import java.util.UUID;
  8. /**
  9. * 处理和账单相关的业务逻辑
  10. */
  11. public class BillService {
  12.     //定义BillDAO属性
  13.     private BillDAO billDAO = new BillDAO();
  14.     //定义MenuService 属性
  15.     private MenuService menuService = new MenuService();
  16.     //定义DiningTableService属性
  17.     private DiningTableService diningTableService = new DiningTableService();
  18.     private MultiTableDAO multiTableDAO = new MultiTableDAO();
  19.     //思考
  20.     //编写点餐的方法
  21.     //1. 生成账单
  22.     //2. 需要更新对应餐桌的状态
  23.     //3. 如果成功返回true, 否则返回false
  24.     public boolean orderMenu(int menuId, int nums, int diningTableId) {
  25.         //生成一个账单号,UUID
  26.         String billID = UUID.randomUUID().toString();
  27.         //将账单生成到bill表, 要求直接计算账单金额
  28.         int update = billDAO.update("insert into bill values(null,?,?,?,?,?,now(),'未结账')",
  29.                 billID, menuId, nums, menuService.getMenuById(menuId).getPrice() * nums, diningTableId);
  30.         if (update <= 0) {
  31.             return false;
  32.         }
  33.         //需要更新对应餐桌的状态
  34.         return diningTableService.updateDiningTableState(diningTableId, "就餐中");
  35.     }
  36.     //返回所有的账单, 提供给View调用
  37.     public List<Bill> list() {
  38.         return billDAO.queryMulti("select * from bill", Bill.class);
  39.     }
  40.     //返回所有的账单并带有菜品名,价格, 提供给View调用
  41.     public List<MultiTableBean> list2() {
  42.         return multiTableDAO.queryMulti("SELECT bill.*, name,price " +
  43.                 "FROM bill, menu " +
  44.                 "WHERE bill.menuId = menu.id", MultiTableBean.class);
  45.     }
  46.     //查看某个餐桌是否有未结账的账单
  47.     public boolean hasPayBillByDiningTableId(int diningTableId) {
  48.         Bill bill =
  49.                 billDAO.querySingle("SELECT * FROM bill WHERE diningTableId=? AND state = '未结账' LIMIT 0, 1", Bill.class, diningTableId);
  50.         return bill != null;
  51.     }
  52.     //完成结账[如果餐桌存在,并且该餐桌有未结账的账单]
  53.     //如果成功,返回true, 失败返回 false
  54.     public boolean payBill(int diningTableId, String payMode) {
  55.         //如果这里使用事务的话,需要用ThreadLocal来解决 , 框架中比如mybatis 提供了事务支持
  56.         //1. 修改bill表
  57.         int update = billDAO.update("update bill set state=? where diningTableId=? and state='未结账'", payMode, diningTableId);
  58.         if(update <= 0) { //如果更新没有成功,则表示失败...
  59.             return false;
  60.         }
  61.         //2. 修改diningTable表
  62.         //注意:不要直接在这里操作,而应该调用DiningTableService 方法,完成更新,体现各司其职
  63.         if(!diningTableService.updateDiningTableToFree(diningTableId, "空")) {
  64.             return false;
  65.         }
  66.         return true;
  67.     }
  68. }
复制代码
EmployeeService
  1. package com.zwj.mhl.service;
  2. import com.zwj.mhl.dao.DiningTableDAO;
  3. import com.zwj.mhl.domain.DiningTable;
  4. import java.util.List;
  5. public class DiningTableService { //业务层
  6.     //定义一个DiningTableDAO对象
  7.     private DiningTableDAO diningTableDAO = new DiningTableDAO();
  8.     //返回所有餐桌的信息
  9.     public List<DiningTable> list() {
  10.         return diningTableDAO.queryMulti("select id, state from diningTable", DiningTable.class);
  11.     }
  12.     //根据id , 查询对应的餐桌DiningTable 对象
  13.     //,如果返回null , 表示id编号对应的餐桌不存在
  14.     public DiningTable getDiningTableById(int id) {
  15.         //老韩小技巧:把sql语句放在查询分析器去测试一下.
  16.         return diningTableDAO.querySingle("select * from diningTable where id = ?", DiningTable.class, id);
  17.     }
  18.     //如果餐桌可以预定,调用方法,对其状态进行更新(包括预定人的名字和电话)
  19.     public boolean orderDiningTable(int id, String orderName, String orderTel) {
  20.         int update =
  21.                 diningTableDAO.update("update diningTable set state='已经预定', orderName=?, orderTel=? where id=?", orderName, orderTel, id);
  22.         return  update > 0;
  23.     }
  24.     //需要提供一个更新 餐桌状态的方法
  25.     public boolean updateDiningTableState(int id, String state) {
  26.         int update = diningTableDAO.update("update diningTable set state=? where id=?", state, id);
  27.         return update > 0;
  28.     }
  29.     //提供方法,将指定的餐桌设置为空闲状态
  30.     public boolean updateDiningTableToFree(int id, String state) {
  31.         int update = diningTableDAO.update("update diningTable set state=?,orderName='',orderTel='' where id=?", state, id);
  32.         return update > 0;
  33.     }
  34. }
复制代码
MenuService
  1. package com.zwj.mhl.service;
  2. import com.zwj.mhl.dao.EmployeeDAO;
  3. import com.zwj.mhl.domain.Employee;
  4. /**
  5. * 该类完成对employee表的各种操作(通过调用EmployeeDAO对象完成)
  6. */
  7. public class EmployeeService {
  8.     //定义一个 EmployeeDAO 属性
  9.     private EmployeeDAO employeeDAO = new EmployeeDAO();
  10.     //方法,根据empId 和 pwd 返回一个Employee对象
  11.     //如果查询不到,就返回null
  12.     public Employee getEmployeeByIdAndPwd(String empId, String pwd) {
  13.         return employeeDAO.querySingle("select * from employee where empId=? and pwd=md5(?)", Employee.class, empId, pwd);
  14.     }
  15. }
复制代码
utils
JDBCUtilsByDruid
  1. package com.zwj.mhl.utils;
  2. import com.alibaba.druid.pool.DruidDataSourceFactory;
  3. import javax.sql.DataSource;
  4. import java.io.FileInputStream;
  5. import java.sql.Connection;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9. import java.util.Properties;
  10. /**
  11. * 基于druid数据库连接池的工具类
  12. */
  13. public class JDBCUtilsByDruid {
  14.     private static DataSource ds;
  15.     //在静态代码块完成 ds初始化
  16.     static {
  17.         Properties properties = new Properties();
  18.         try {
  19.             properties.load(new FileInputStream("src\\druid.properties"));
  20.             ds = DruidDataSourceFactory.createDataSource(properties);
  21.         } catch (Exception e) {
  22.             e.printStackTrace();
  23.         }
  24.     }
  25.     //编写getConnection方法
  26.     public static Connection getConnection() throws SQLException {
  27.         return ds.getConnection();
  28.     }
  29.     //关闭连接, 老师再次强调: 在数据库连接池技术中,close 不是真的断掉连接
  30.     //而是把使用的Connection对象放回连接池
  31.     public static void close(ResultSet resultSet, Statement statement, Connection connection) {
  32.         try {
  33.             if (resultSet != null) {
  34.                 resultSet.close();
  35.             }
  36.             if (statement != null) {
  37.                 statement.close();
  38.             }
  39.             if (connection != null) {
  40.                 connection.close();
  41.             }
  42.         } catch (SQLException e) {
  43.             throw new RuntimeException(e);
  44.         }
  45.     }
  46. }
复制代码
Utility
  1. package com.zwj.mhl.utils;
  2. import com.alibaba.druid.pool.DruidDataSourceFactory;
  3. import javax.sql.DataSource;
  4. import java.io.FileInputStream;
  5. import java.sql.Connection;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9. import java.util.Properties;
  10. /**
  11. * 基于druid数据库连接池的工具类
  12. */
  13. public class JDBCUtilsByDruid {
  14.     private static DataSource ds;
  15.     //在静态代码块完成 ds初始化
  16.     static {
  17.         Properties properties = new Properties();
  18.         try {
  19.             properties.load(new FileInputStream("src\\druid.properties"));
  20.             ds = DruidDataSourceFactory.createDataSource(properties);
  21.         } catch (Exception e) {
  22.             e.printStackTrace();
  23.         }
  24.     }
  25.     //编写getConnection方法
  26.     public static Connection getConnection() throws SQLException {
  27.         return ds.getConnection();
  28.     }
  29.     //关闭连接, 老师再次强调: 在数据库连接池技术中,close 不是真的断掉连接
  30.     //而是把使用的Connection对象放回连接池
  31.     public static void close(ResultSet resultSet, Statement statement, Connection connection) {
  32.         try {
  33.             if (resultSet != null) {
  34.                 resultSet.close();
  35.             }
  36.             if (statement != null) {
  37.                 statement.close();
  38.             }
  39.             if (connection != null) {
  40.                 connection.close();
  41.             }
  42.         } catch (SQLException e) {
  43.             throw new RuntimeException(e);
  44.         }
  45.     }
  46. }
复制代码
view
MHLView
  1. package com.zwj.mhl.utils;
  2. /**
  3.    工具类的作用:
  4.    处理各种情况的用户输入,并且能够按照程序员的需求,得到用户的控制台输入。
  5. */
  6. import java.util.*;
  7. /**
  8.    
  9. */
  10. public class Utility {
  11.    //静态属性。。。
  12.     private static Scanner scanner = new Scanner(System.in);
  13.    
  14.     /**
  15.      * 功能:读取键盘输入的一个菜单选项,值:1——5的范围
  16.      * @return 1——5
  17.      */
  18.    public static char readMenuSelection() {
  19.         char c;
  20.         for (; ; ) {
  21.             String str = readKeyBoard(1, false);//包含一个字符的字符串
  22.             c = str.charAt(0);//将字符串转换成字符char类型
  23.             if (c != '1' && c != '2' &&
  24.                 c != '3' && c != '4' && c != '5') {
  25.                 System.out.print("选择错误,请重新输入:");
  26.             } else break;
  27.         }
  28.         return c;
  29.     }
  30.    /**
  31.     * 功能:读取键盘输入的一个字符
  32.     * @return 一个字符
  33.     */
  34.     public static char readChar() {
  35.         String str = readKeyBoard(1, false);//就是一个字符
  36.         return str.charAt(0);
  37.     }
  38.     /**
  39.      * 功能:读取键盘输入的一个字符,如果直接按回车,则返回指定的默认值;否则返回输入的那个字符
  40.      * @param defaultValue 指定的默认值
  41.      * @return 默认值或输入的字符
  42.      */
  43.    
  44.     public static char readChar(char defaultValue) {
  45.         String str = readKeyBoard(1, true);//要么是空字符串,要么是一个字符
  46.         return (str.length() == 0) ? defaultValue : str.charAt(0);
  47.     }
  48.    
  49.     /**
  50.      * 功能:读取键盘输入的整型,长度小于2位
  51.      * @return 整数
  52.      */
  53.     public static int readInt() {
  54.         int n;
  55.         for (; ; ) {
  56.             String str = readKeyBoard(2, false);//一个整数,长度<=2位
  57.             try {
  58.                 n = Integer.parseInt(str);//将字符串转换成整数
  59.                 break;
  60.             } catch (NumberFormatException e) {
  61.                 System.out.print("数字输入错误,请重新输入:");
  62.             }
  63.         }
  64.         return n;
  65.     }
  66.     /**
  67.      * 功能:读取键盘输入的 整数或默认值,如果直接回车,则返回默认值,否则返回输入的整数
  68.      * @param defaultValue 指定的默认值
  69.      * @return 整数或默认值
  70.      */
  71.     public static int readInt(int defaultValue) {
  72.         int n;
  73.         for (; ; ) {
  74.             String str = readKeyBoard(10, true);
  75.             if (str.equals("")) {
  76.                 return defaultValue;
  77.             }
  78.          
  79.          //异常处理...
  80.             try {
  81.                 n = Integer.parseInt(str);
  82.                 break;
  83.             } catch (NumberFormatException e) {
  84.                 System.out.print("数字输入错误,请重新输入:");
  85.             }
  86.         }
  87.         return n;
  88.     }
  89.     /**
  90.      * 功能:读取键盘输入的指定长度的字符串
  91.      * @param limit 限制的长度
  92.      * @return 指定长度的字符串
  93.      */
  94.     public static String readString(int limit) {
  95.         return readKeyBoard(limit, false);
  96.     }
  97.     /**
  98.      * 功能:读取键盘输入的指定长度的字符串或默认值,如果直接回车,返回默认值,否则返回字符串
  99.      * @param limit 限制的长度
  100.      * @param defaultValue 指定的默认值
  101.      * @return 指定长度的字符串
  102.      */
  103.    
  104.     public static String readString(int limit, String defaultValue) {
  105.         String str = readKeyBoard(limit, true);
  106.         return str.equals("")? defaultValue : str;
  107.     }
  108.    /**
  109.     * 功能:读取键盘输入的确认选项,Y或N
  110.     * 将小的功能,封装到一个方法中.
  111.     * @return Y或N
  112.     */
  113.     public static char readConfirmSelection() {
  114.         System.out.println("请输入你的选择(Y/N)");
  115.         char c;
  116.         for (; ; ) {//无限循环
  117.            //在这里,将接受到字符,转成了大写字母
  118.            //y => Y n=>N
  119.             String str = readKeyBoard(1, false).toUpperCase();
  120.             c = str.charAt(0);
  121.             if (c == 'Y' || c == 'N') {
  122.                 break;
  123.             } else {
  124.                 System.out.print("选择错误,请重新输入:");
  125.             }
  126.         }
  127.         return c;
  128.     }
  129.     /**
  130.      * 功能: 读取一个字符串
  131.      * @param limit 读取的长度
  132.      * @param blankReturn 如果为true ,表示 可以读空字符串。
  133.      *                   如果为false表示 不能读空字符串。
  134.      *           
  135.     * 如果输入为空,或者输入大于limit的长度,就会提示重新输入。
  136.      * @return
  137.      */
  138.     private static String readKeyBoard(int limit, boolean blankReturn) {
  139.         
  140.       //定义了字符串
  141.       String line = "";
  142.       //scanner.hasNextLine() 判断有没有下一行
  143.         while (scanner.hasNextLine()) {
  144.             line = scanner.nextLine();//读取这一行
  145.            
  146.          //如果line.length=0, 即用户没有输入任何内容,直接回车
  147.          if (line.length() == 0) {
  148.                 if (blankReturn) return line;//如果blankReturn=true,可以返回空串
  149.                 else continue; //如果blankReturn=false,不接受空串,必须输入内容
  150.             }
  151.          //如果用户输入的内容大于了 limit,就提示重写输入  
  152.          //如果用户如的内容 >0 <= limit ,我就接受
  153.             if (line.length() < 1 || line.length() > limit) {
  154.                 System.out.print("输入长度(不能大于" + limit + ")错误,请重新输入:");
  155.                 continue;
  156.             }
  157.             break;
  158.         }
  159.         return line;
  160.     }
  161. }
复制代码

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

反转基因福娃

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表