> 文档中心 > 来蟹堡王干饭---餐厅管理系统(javaSE和MySQL版)

来蟹堡王干饭---餐厅管理系统(javaSE和MySQL版)

 来蟹堡王干饭---餐厅管理系统(javaSE和MySQL版)

来蟹堡王干饭---餐厅管理系统(javaSE和MySQL版)

来蟹堡王干饭---餐厅管理系统(javaSE和MySQL版)

这个是我们现在所编写的数据库(当然大家要是需要也可以去编辑修改) 

/*SQLyog Ultimate v11.33 (64 bit)MySQL - 5.7.31-log : Database - xhb**********************************************************************//*!40101 SET NAMES utf8 */;/*!40101 SET SQL_MODE=''*/;/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;CREATE DATABASE /*!32312 IF NOT EXISTS*/`xhb` /*!40100 DEFAULT CHARACTER SET latin1 */;USE `xhb`;/*Table structure for table `bill` */DROP TABLE IF EXISTS `bill`;CREATE TABLE `bill` (  `id` int(11) NOT NULL AUTO_INCREMENT,  `billId` varchar(50) NOT NULL DEFAULT '',  `menuId` int(11) NOT NULL DEFAULT '0',  `nums` smallint(6) NOT NULL DEFAULT '0',  `money` double NOT NULL DEFAULT '0',  `diningTableId` int(11) NOT NULL DEFAULT '0',  `billDate` datetime NOT NULL,  `state` varchar(50) NOT NULL DEFAULT '',  PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;/*Data for the table `bill` */insert  into `bill`(`id`,`billId`,`menuId`,`nums`,`money`,`diningTableId`,`billDate`,`state`) values (1,'331ced11-0086-4625-81b6-648697a299a7',1,1,50,1,'2022-05-16 17:18:08','???');/*Table structure for table `diningtable` */DROP TABLE IF EXISTS `diningtable`;CREATE TABLE `diningtable` (  `id` int(11) NOT NULL AUTO_INCREMENT,  `state` varchar(20) NOT NULL DEFAULT '',  `orderName` varchar(50) NOT NULL DEFAULT '',  `orderTel` varchar(20) NOT NULL DEFAULT '',  PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;/*Data for the table `diningtable` */insert  into `diningtable`(`id`,`state`,`orderName`,`orderTel`) values (1,'???','1','1'),(2,'????','???','123'),(3,'空','',''),(4,'空','','');/*Table structure for table `menu` */DROP TABLE IF EXISTS `menu`;CREATE TABLE `menu` (  `id` int(11) NOT NULL AUTO_INCREMENT,  `name` varchar(50) NOT NULL DEFAULT '',  `type` varchar(50) NOT NULL DEFAULT '',  `price` double NOT NULL DEFAULT '0',  PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8;/*Data for the table `menu` */insert  into `menu`(`id`,`name`,`type`,`price`) values (1,'普通蟹黄堡','主食类',50),(2,'三层蟹黄堡','主食类',100),(3,'炸薯条','快餐类',30),(4,'可乐','快餐类',15),(5,'烤章鱼','饭后甜点类',9),(6,'水煮鱼','热菜类',26),(7,'甲鱼汤','汤类',100),(8,'炖蜗牛','汤类',16);/*Table structure for table `people` */DROP TABLE IF EXISTS `people`;CREATE TABLE `people` (  `id` int(11) NOT NULL AUTO_INCREMENT,  `popid` varchar(50) NOT NULL DEFAULT '',  `pwd` char(32) NOT NULL DEFAULT '',  `name` varchar(50) NOT NULL DEFAULT '',  `add` varchar(50) NOT NULL DEFAULT '',  PRIMARY KEY (`id`),  UNIQUE KEY `popid` (`popid`)) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;/*Data for the table `people` */insert  into `people`(`id`,`popid`,`pwd`,`name`,`add`) values (1,'001','e10adc3949ba59abbe56e057f20f883e','派大星','石头屋'),(2,'002','e10adc3949ba59abbe56e057f20f883e','珊迪','氧气仓'),(3,'003','e10adc3949ba59abbe56e057f20f883e','章鱼哥','章鱼屋'),(4,'004','e10adc3949ba59abbe56e057f20f883e','海绵宝宝','菠萝屋');/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

 

首先链接数据库,我们需要两个最起码的工具包(当然要是实力强大,你可以自己去慢慢写)

基于druid数据库连接池的工具类

package MySQL.project_.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;/** * @version 1.0 * 基于druid数据库连接池的工具类 */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) {     e.printStackTrace(); }    }    //编写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); }    }}

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

package MySQL.project_.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);//一个整数,长度 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()  limit) {  System.out.print("输入长度(不能大于" + limit + ")错误,请重新输入:");  continue;     }     break; } return line;    }}

有了这两个工具类我们对于输入和数据库的链接就会方便许多

我们先来拿people这个表来做一下讲解

people参数的设置(为之后函数的方便传参)

package MySQL.project_.domain;//id INT PRIMARY KEY AUTO_INCREMENT,-- 自增//     popid VARCHAR(50) NOT NULL DEFAULT '',//     pwd CHAR(32) NOT NULL DEFAULT '',//     `name` VARCHAR(50) NOT NULL DEFAULT '',//     `add` VARCHAR(50) NOT NULL DEFAULT ''public class People {    private Integer id;    private String popid;    private String pwd;    private String name;    private String add;    public People() {//无参构造器,底层    }    public People(Integer id, String popid, String pwd, String name, String add) { this.id = id; this.popid = popid; this.pwd = pwd; this.name = name; this.add = add;    }    public Integer getId() { return id;    }    public void setId(Integer id) { this.id = id;    }    public String getPopid() { return popid;    }    public void setPopid(String popid) { this.popid = popid;    }    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 getAdd() { return add;    }    public void setAdd(String add) { this.add = add;    }}

 

 调用其父类BasicDAO方便对people的其他操作

package MySQL.project_.Dao;import MySQL.project_.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;/** * 开发BasicDAO , 是其他DAO的父类 */public class BasicDAO { //泛型指定具体类型    private QueryRunner qr =  new QueryRunner();    //开发通用的dml方法, 针对任意的表    public int update(String sql, Object... parameters) { Connection connection = null; try {     connection = JDBCUtilsByDruid.getConnection();     int update = qr.update(connection, sql, parameters);     return  update; } 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 queryMulti(String sql, Class clazz, Object... parameters) { Connection connection = null; try {     connection = JDBCUtilsByDruid.getConnection();     return qr.query(connection, sql, new BeanListHandler(clazz), parameters); } catch (SQLException e) {     throw  new RuntimeException(e); //将编译异常->运行异常 ,抛出 } finally {     JDBCUtilsByDruid.close(null, null, connection); }    }    //查询单行结果 的通用方法    public T querySingle(String sql, Class clazz, Object... parameters) { Connection connection = null; try {     connection = JDBCUtilsByDruid.getConnection();     return  qr.query(connection, sql, new BeanHandler(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); }    }}
package MySQL.project_.Dao;import MySQL.project_.domain.People;public class PeopleDao extends BasicDAO{    //这里写特有的操作,但是BasicDAO 里面增删改查都有}

 

people的链接

package MySQL.project_.service;import MySQL.project_.Dao.PeopleDao;import MySQL.project_.domain.People;//该类完成对people表的各种操作(通过调用PeopleDao对象来完成的)public class PeopService {    //定义一个PeopleDao属性    private PeopleDao pop = new PeopleDao();    //根据popid和pwd返回一个People对象    //如果查询不到,就返回null    public People getPopidAndPwd(String popid,String pwd){return pop.querySingle("select * from people where popid = ? and pwd = md5(?)",People.class,popid,pwd);    }}

这个类主要实现对于这个该进行的某种操作,并返回查询结果

最后在主函数调用

package MySQL.project_.view;import MySQL.project_.domain.DiningTable;import MySQL.project_.domain.Menu;import MySQL.project_.domain.MultiTableBean;import MySQL.project_.domain.People;import MySQL.project_.service.BillService;import MySQL.project_.service.DiningTablese;import MySQL.project_.service.MenuService;import MySQL.project_.service.PeopService;import MySQL.project_.utils.Utility;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.*;import java.awt.*;import java.util.List;import static javax.swing.SwingConstants.CENTER;public class View {    private boolean loop = true;    private String key = "";    //定义PeopleServise对象    private PeopService popse = new PeopService();    //定义DiningTablese对象    private DiningTablese dinse = new DiningTablese();    //定义MenuService属性    private MenuService menuService = new MenuService();    //定义BillService属性    private BillService billService = new BillService();    public static void main(String[] args) { new View().MainView();    }    //密码登录    static class My0302 extends JFrame { public My0302() {     setVisible(true);     Container con = getContentPane();     setBounds(100, 100, 500, 500);     JLabel jl = new JLabel("无声的瞎子表演现在开始");     jl.setHorizontalAlignment(CENTER);     con.add(jl);     con.setBackground(Color.yellow); }    }    static class My0303 extends JFrame { public My0303() {     setVisible(true);     Container con = getContentPane();     setBounds(100, 100, 500, 500);     JButton jl = new JButton("给予差评");     Myaction myaction = new Myaction();     jl.addActionListener(myaction);//设置鼠标点击事件     jl.setHorizontalAlignment(CENTER);     con.add(jl);     con.setBackground(Color.yellow); }    }    static class Myaction implements ActionListener { @Override public void actionPerformed(ActionEvent e) {     System.out.println("海绵hong贼帅"); }    }//    static class Mya implements ActionListener {// public void actionPerformed(ActionEvent e) {//     TextField fe=(TextField) e.getSource();//获得一些资源,返回一个对象//     System.out.println(fe.getText());//     fe.setText(" ");//输出完之后为空// }//    }    public void show() { JFrame frame = new JFrame("欢迎来到蟹堡王"); frame.setBounds(100, 100, 500, 500); //标题 JLabel jl = new JLabel("派大星的独人秀"); jl.setBounds(200, 0, 200, 30); //输入框 JLabel jl1 = new JLabel("请写下你的意见"); jl1.setBounds(100, 100, 120, 20); JTextField jt1 = new JTextField(" "); jt1.setBounds(100, 150, 120, 20); //登录按钮 JButton jb = new JButton("开始"); jb.setBounds(100, 200, 120, 20); //注册按钮 JButton jb1 = new JButton("退出"); jb1.setBounds(100, 220, 120, 20); //登录验证 jb.addActionListener(new AbstractAction() {     @Override     public void actionPerformed(ActionEvent e) {  new My0302();     } }); jb1.addActionListener(new AbstractAction() {     @Override     public void actionPerformed(ActionEvent e) {  new My0303();     } }); //获得一个容器 Container con = frame.getContentPane(); con.setBackground(Color.yellow); con.setLayout(null); con.add(jl1); con.add(jt1); con.add(jb); con.add(jb1); con.add(jl); frame.setVisible(true); frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);    }    public void show08() { JFrame frame = new JFrame("章鱼哥差评系统"); frame.setBounds(100, 100, 500, 500); JButton jl = new JButton("给予差评"); Myaction_1 myaction = new Myaction_1(); jl.addActionListener(myaction);//设置鼠标点击事件 jl.setHorizontalAlignment(CENTER); Container con = frame.getContentPane(); con.add(jl); frame.setVisible(true); frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);    }    static class Myaction_1 implements ActionListener { @Override public void actionPerformed(ActionEvent e) {     System.out.println("章鱼哥工资-100"); }    }    //显示所有菜品    public void listMenu() { List list = menuService.list(); System.out.println("\n菜品编号\t\t菜品名\t\t类别\t\t价格"); for (Menu menu : list) {     System.out.println(menu); } System.out.println("==============显示完毕============");    }    public void payBill() { System.out.println("==============结账服务============"); System.out.print("请选择要结账的餐桌编号(-1退出): "); int diningTableId = Utility.readInt(); if (diningTableId == -1) {     System.out.println("=============取消结账============");     return; } DiningTable diningTable = dinse.getDiningTableById(diningTableId); if (diningTable == null) {     System.out.println("=============结账的餐桌不存在============");     return; } //验证餐桌是否有需要结账的账单 if (!billService.hasPayBillByDiningTableId(diningTableId)) {     System.out.println("=============该餐位没有未结账账单============");     return; } System.out.println("结账方式(现金/支付宝/微信)回车表示退出: "); String payMode = Utility.readString(20, "");//说明如果回车,就是返回 "" if ("".equals(payMode)) {     System.out.println("=============取消结账============");     return; } char key = Utility.readConfirmSelection(); if (key == 'Y') { //结账     //调用我们写的方法     if (billService.payBill(diningTableId, payMode)) {  System.out.println("=============完成结账============");     } else {  System.out.println("=============结账失败============");     } } else {     System.out.println("=============取消结账============"); }    }    //显示账单信息    public void listBill() { List multiTableBeans = billService.list2(); System.out.println("\n编号\t\t菜品号\t\t菜品量\t\t金额\t\t桌号\t\t日期\t\t\t\t\t\t\t状态\t\t菜品名\t\t价格"); for (MultiTableBean bill : multiTableBeans) {     System.out.println(bill); } System.out.println("==============显示完毕============");    }    //完成点餐    public void orderMenu() { System.out.println("==============点餐服务============"); System.out.print("请输入点餐的桌号(-1退出): "); int orderDiningTableId = Utility.readInt(); if (orderDiningTableId == -1) {     System.out.println("==============取消点餐============");     return; } System.out.print("请输入点餐的菜品号(-1退出): "); int orderMenuId = Utility.readInt(); if (orderMenuId == -1) {     System.out.println("==============取消点餐============");     return; } System.out.print("请输入点餐的菜品量(-1退出): "); int orderNums = Utility.readInt(); if (orderNums == -1) {     System.out.println("==============取消点餐============");     return; } //验证餐桌号是否存在. DiningTable diningTable = dinse.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("==============点餐失败============"); }    }    //显示所以的餐桌状态    private void fun1() { List list = dinse.list(); System.out.println("\n餐桌编号\t\t\t餐桌状态"); for (DiningTable diningTable : list) {     System.out.println(diningTable); }    }    //完成订座    public void orderdiningTable() { System.out.println("====================预定餐桌===================="); System.out.println("请选择要预定的餐桌的编号(-1退出):"); int orderId = Utility.readInt(); if (orderId == -1) {     System.out.println("取消预定");     return; } //该方法得到的是YorN char key = Utility.readConfirmSelection(); if (key == 'Y') {     //根据orderId返回 对应DiningTable对象,如果为null,说明该对象不存在     DiningTable dinT = dinse.getDiningTableById(orderId);     if (dinT == null) {  System.out.println("===================预定餐桌不存在======================");  return;     }     //判断该餐桌的状态是否”空“     if (!("空".equals(dinT.getState()))) {//说明当前这个餐桌不是空的状态  System.out.println("==========================餐桌已经预定=======================");     }     System.out.println("=========预定人的姓名=======");     String orderName = Utility.readString(50);     System.out.println("=========预定人的电话========");     String orderTab = Utility.readString(50);     //这个时候说明可以真的预定,更新餐桌状态     if (dinse.orderDiningTable(orderId, orderName, orderTab)) {  System.out.println("预定餐桌成功");     } else  System.out.println("预定失败"); } else {     System.out.println("============================取消预定餐桌============================="); }    }    public void MainView() { 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 popid = Utility.readString(50);      System.out.println("请输入密码");      String pwd = Utility.readString(50);      People pop1 = popse.getPopidAndPwd(popid, pwd);      //到数据库去判断      if (pop1 != null) {//说明用户存在   System.out.println("==================来自海绵宝宝的笑声:嘎嘎嘎嘎嘎=======================");   //显示二级菜单   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.println("\t\t8.给章鱼哥差评");System.out.println("\t\t9.不想接受服务");key = Utility.readString(1);//只能输入一个字符switch (key) {    case "1": fun1(); break;    case "2": orderdiningTable(); break;    case "3": listMenu(); break;    case "4": orderMenu(); break;    case "5": listBill();//显示所有账单 break;    case "6": payBill(); break;    case "7": show(); break;    case "8": show08(); break;    case "9": System.out.println("痞老板来了,全员进入戒备状态"); loop = false;    default: System.out.println("你的输入有误");}   }      } else   System.out.println("==========================登录失败===============");      break;  case "2":      System.out.println("痞老板,不要在这里捣乱");      loop = false;      break;  default:      System.out.println("你的输入有误,请重新输入");     } }    }}

我会将完整的项目传到资源里面供大家观看

 

 

狗狗宠物资料大全