介绍满汉楼项目
就是一个 点餐项目,
满汉楼只是一个名字,就是高级饭店的名字,
本项目不是界面的,是采用命令行的.
涉及到的技术:
javase+jdbc+mysql+druid+apache-dbutils工具类
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/9deae87ce1b0fde8239c6b9a59b4dd54.png)
第一步:创建数据库(mysql8.x)
1、创建数据库
create database mymhl charset utf8;
use mymhl;
2、创建员工表(用于登录系统)
create table employee(
empId int primary key auto_increment,
empCount varchar(30)not null unique default 'null',
empName varchar(30) not null default 'null',
empPwd varchar(32)not null default 'null',
empSex char(1) not null default '男',
empAge int not null default 20
)charset utf8;
insert into employee
values
(null,'666666','楠小弟',md5('123456'),'男',24),
(null,'666989','tom',md5('123456'),'男',23),
(null,'777777','lina',md5('123456'),'女',24);
3、创建餐桌表
create table diningTable(
dinId int primary key auto_increment,
dinState int default 0,
dinUser varchar(30) not null default '',
dinTel varchar(11) not null
);
insert into diningTable values (null,0,'','');
select *from diningTable;
4、创建菜单表(菜的列表)
create table menu(
mId int primary key auto_increment,
mName varchar(30) not null default 'null',
mPrice double not null default 0,
mType varchar(30)
);
insert into menu
values(null,'爆炒腐竹',12,'热菜'),
(null,'紫菜蛋花汤',15,'汤类'),
(null,'烧饼',2,'面食'),
(null,'水饺',15,'面食'),
(null,'牛肉烩面',10,'面食'),
(null,'紫菜蛋花汤',15,'汤类'),
(null,'麻球',10,'甜食'),
(null,'紫菜蛋花汤',15,'汤类'),
(null,'麻辣鸡',40,'肉类'),
(null,'麻辣鱼',35,'肉类'),
(null,'红糖年糕',15,'甜食'),
(null,'鱼香肉丝',15,'热菜');
5、创建账单表
create table bill(
bId int primary key auto_increment,
bCount char(36) not null default '',
mId int not null ,
bName varchar(30)not null default '',
bNums int not null default 0,
bPrice double not null default 0,
diningTableId int not null ,
bTime datetime ,
bState int not null default 0
);
第二步:在idea中创建mhlperson项目
1、项目目录
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/0c5ad14c0c38cc973bf0a58f429214da.png)
2、新建libs包
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/72c112fce82cffb5473a7dcafb28c3d6.png)
3、把相关jar包复制到libs包中并导入项目中
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/f023d269b81ef4ce4b2b9c822f60f807.png)
4、在src目录下新建文件:druid.properties
driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/mymhl?serverTimeZone=Asia/ShangHai
username=root
password=123456
initialSize=10
maxActive=100
maxWait=3000
5、在domain包中创建相对应的类
a、员工类employee.java
public class Employee {
private Integer empId;
private String empCount;
private String empName;
private String empPwd;
private String empSex;
private String empAge;
public Employee() {
}
public Integer getEmpId() {
return empId;
}
public void setEmpId(Integer empId) {
this.empId = empId;
}
public String getEmpCount() {
return empCount;
}
public void setEmpCount(String empCount) {
this.empCount = empCount;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public String getEmpPwd() {
return empPwd;
}
public void setEmpPwd(String empPwd) {
this.empPwd = empPwd;
}
public String getEmpSex() {
return empSex;
}
public void setEmpSex(String empSex) {
this.empSex = empSex;
}
public String getEmpAge() {
return empAge;
}
public void setEmpAge(String empAge) {
this.empAge = empAge;
}
}
b、餐桌类diningTable.java
public class DiningTable {
private Integer dinId;
private Integer dinState;
private String dinUser;
private String dinTel;
public DiningTable() {
}
@Override
public String toString() {
String str;
if(dinState==0){
str="空闲ing";
}else if (dinState==1){
str="已预定";
}else{
str="就餐中";
}
String na="";
if(!dinUser.isEmpty()){
na =dinUser.substring(0,1)+"**";
}
return dinId+"\t\t"+str+"\t\t"+na;
}
public Integer getDinId() {
return dinId;
}
public void setDinId(Integer dinId) {
this.dinId = dinId;
}
public Integer getDinState() {
return dinState;
}
public void setDinState(Integer dinState) {
this.dinState = dinState;
}
public String getDinUser() {
return dinUser;
}
public void setDinUser(String dinUser) {
this.dinUser = dinUser;
}
public String getDinTel() {
return dinTel;
}
public void setDinTel(String dinTel) {
this.dinTel = dinTel;
}
}
c、菜单类menu.java
public class Menu {
private Integer mId;
private String mName;
private Double mPrice;
private String mType;
public Menu() {
}
@Override
public String toString() {
return mId +
"\t\t" + mName +
"\t\t" + mPrice +
"\t\t" + mType;
}
public Integer getmId() {
return mId;
}
public void setmId(Integer mId) {
this.mId = mId;
}
public String getmName() {
return mName;
}
public void setmName(String mName) {
this.mName = mName;
}
public Double getmPrice() {
return mPrice;
}
public void setmPrice(Double mPrice) {
this.mPrice = mPrice;
}
public String getmType() {
return mType;
}
public void setmType(String mType) {
this.mType = mType;
}
}
d、账单类bill.java
public class Bill {
private Integer bId;
private String bCount;
private Integer mId;
private String bName;
private Integer bNums;
private Double bPrice;
private Integer diningTableId;
private String bTime;
private Integer bState;
public Bill() {
}
@Override
public String toString() {
String str="";
if(bState==0){
str="未付款";
}else if(bState==1){
str="现金付款";
}else if(bState==2){
str="微信付款";
}else {
str="支付宝付款";
}
return bId +"\t\t" + mId +
"\t\t" + bName +
"\t\t" + bNums +
"\t\t" + bPrice +
"\t\t" + diningTableId +
"\t\t" + bTime +
"\t" + str ;
}
public Integer getbId() {
return bId;
}
public void setbId(Integer bId) {
this.bId = bId;
}
public String getbCount() {
return bCount;
}
public void setbCount(String bCount) {
this.bCount = bCount;
}
public Integer getmId() {
return mId;
}
public void setmId(Integer mId) {
this.mId = mId;
}
public String getbName() {
return bName;
}
public void setbName(String bName) {
this.bName = bName;
}
public Integer getbNums() {
return bNums;
}
public void setbNums(Integer bNums) {
this.bNums = bNums;
}
public Double getbPrice() {
return bPrice;
}
public void setbPrice(Double bPrice) {
this.bPrice = bPrice;
}
public Integer getDiningTableId() {
return diningTableId;
}
public void setDiningTableId(Integer diningTableId) {
this.diningTableId = diningTableId;
}
public String getbTime() {
return bTime;
}
public void setbTime(String bTime) {
this.bTime = bTime;
}
public Integer getbState() {
return bState;
}
public void setbState(Integer bState) {
this.bState = bState;
}
}
6、在dao包中创建类
a、创建BasicDao父类
public class BasicDao<T> {
private QueryRunner qr=new QueryRunner();
private Connection conn=null;
public List<T>getAllDate(String sql,Class<T>clazz,Object...obj){
try {
conn= JdbcDruid.getConn();
return qr.query(conn,sql,new BeanListHandler<>(clazz),obj);
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
JdbcDruid.getCLose(null,null,conn);
}
}
public T getDateById(String sql,Class<T>clazz,Object...obj){
try {
conn=JdbcDruid.getConn();
return qr.query(conn,sql,new BeanHandler<>(clazz),obj);
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
JdbcDruid.getCLose(null,null,conn);
}
}
public Object getScalarDate(String sql,Object...obj){
try {
conn=JdbcDruid.getConn();
return qr.query(conn,sql,new ScalarHandler<>(),obj);
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
JdbcDruid.getCLose(null,null,conn);
}
}
public boolean updateDate(String sql,Object...obj){
int num=0;
try {
conn=JdbcDruid.getConn();
conn.setAutoCommit(false);
num= qr.update(conn,sql,obj);
conn.commit();
} catch (SQLException e) {
try {
conn.rollback();
} catch (SQLException ex) {
throw new RuntimeException(ex);
}
} finally {
JdbcDruid.getCLose(null,null,conn);
}
return num>0;
}
}
b、创建EmployeeDao
public class EmployeeDao extends BasicDao<Employee>{
}
c、创建diningTableDao
public class DiningTableDao extends BasicDao<DiningTable>{
}
d、创建MenuDao
public class MenuDao extends BasicDao<Menu>{
}
e、创建BillDao
public class BillDao extends BasicDao<Bill>{
}
7、在service包创建类
a、创建employeeService
public class EmployeeService {
private EmployeeDao ed=new EmployeeDao();
public Employee logIn(String count,String pwd){
String sql="select * from employee where empCount=? and empPwd=MD5(?)";
return ed.getDateById(sql, Employee.class, count, pwd);
}
}
b、创建diningTableService
public class DiningTableService {
private DiningTableDao dtd=new DiningTableDao();
private String sql;
public List<DiningTable> getAllTable(){
sql="select * from diningTable";
return dtd.getAllDate(sql,DiningTable.class);
}
public DiningTable getTableById(int id){
sql="select*from diningTable where dinId=?";
return dtd.getDateById(sql,DiningTable.class,id);
}
public boolean updateTable(int id,String name,String tel,int state){
sql="update diningTable set dinState=?,dinUser=?,dinTel=? where dinId=?";
return dtd.updateDate(sql,state,name,tel,id);
}
public boolean updateTableById(int id,int state){
sql="update diningTable set dinState=? where dinId=?";
return dtd.updateDate(sql,state,id);
}
public DiningTable getTableByUserInfo(String name,String tel){
sql="select *from diningTable where dinUser=? and dinTel=?";
return dtd.getDateById(sql,DiningTable.class,name,tel);
}
}
c、创建menuService
public class MenuService {
private MenuDao md=new MenuDao();
private String sql;
public List<Menu> getAllDate(){
sql="select*from menu";
return md.getAllDate(sql,Menu.class);
}
public Menu getDateById(int id){
sql="select*from menu where mId=?";
return md.getDateById(sql,Menu.class,id);
}
}
d、创建billService
public class BillService {
private BillDao bd=new BillDao();
private String sql;
private DiningTableService dts=new DiningTableService();
public Bill getByDinTable(int did){
sql="select * from bill where diningTableId=?";
return bd.getDateById(sql,Bill.class,did);
}
public boolean addBill(int mId,String bName,int nums,double price,int dinId,int state){
sql="insert into bill values(null,?,?,?,?,?,?,?,?)";
String uuid = UUID.randomUUID().toString();
boolean rs= bd.updateDate(sql,uuid,mId,bName,nums,price,dinId,new Date(),state);
if(!rs){
System.out.println("添加账单出错");
return false;
}
return dts.updateTableById(dinId,2);
}
public List<Bill> getDateNonPay(){
sql="select * from bill where bState=0";
return bd.getAllDate(sql,Bill.class);
}
public List<Bill> getDateAlPay(){
sql="select * from bill where bState>0";
return bd.getAllDate(sql,Bill.class);
}
public boolean updateSate(int state,int dinId){
sql="update bill set bState=? where diningTableId=?";
boolean rs= bd.updateDate(sql,state,dinId);
if(!rs){
System.out.println("结账时失败");
return false;
}
return dts.updateTable(dinId,"","",0);
}
}
8、在utils包创建类
a、创建JdbcDruid
public class JdbcDruid {
private static DataSource ds=null;
static {
Properties p=new Properties();
try {
p.load(new FileInputStream("src\\druid.properties"));
ds = DruidDataSourceFactory.createDataSource(p);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static Connection getConn(){
try {
return ds.getConnection();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public static void getCLose(ResultSet rs, Statement sta,Connection con){
try {
if(rs!=null){
rs.close();
}
if(sta!=null){
sta.close();
}
if(con!=null){
con.close();
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
b、创建KeyBoardTools
public class KeyBoardTools {
private static Scanner inp=new Scanner(System.in);
public static String readString(int limit){
String str="";
while (true){
str= inp.next();
if (str.length()>limit){
System.out.println("输入的请不要超过限定长度("+limit+"),请重新输入:");
continue;
}
break;
}
return str;
}
public static int readInt(int limit){
String str="";
while (true){
str=inp.next();
if(str.length()>limit){
System.out.println("长度请不要超过("+limit+"),请重新输入:");
continue;
}
if (!str.matches("\\d")) {
System.out.println("只能输入整数,请重新输入:");
continue;
}
break;
}
return Integer.parseInt(str);
}
}
9、在view包创建类
a、创建MhlView
public class MhlView {
private String key;
private boolean loop=true;
private EmployeeService es=new EmployeeService();
private DiningTableService dts=new DiningTableService();
private MenuService ms=new MenuService();
private BillService bs=new BillService();
public static void main(String[] args) {
new MhlView().showMenu();
}
private void showMenu(){
System.out.println("==========欢迎来到老六满汉楼餐厅==========");
while (loop){
System.out.println("\t\t1 登录系统");
System.out.println("\t\t9 退出系统");
System.out.print("请输入你的选择:");
key= KeyBoardTools.readString(1);
switch (key){
case "1":
System.out.println("==========登陆操作==========");
System.out.println("请输入员工账号:");
String count = KeyBoardTools.readString(30);
System.out.println("请输入员工密码:");
String pwd = KeyBoardTools.readString(30);
Employee emp = es.logIn(count, pwd);
if(emp!=null){
System.out.println("========欢迎你["+emp.getEmpName()+"]========");
while (loop){
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\t9 退出当前帐号");
System.out.print("请输入你的选择:");
int k = KeyBoardTools.readInt(1);
switch (k){
case 1:
showTable1();
break;
case 2:
yuDingTable2();
break;
case 3:
showCai3();
break;
case 4:
dianCai4();
break;
case 5:
showZhang5();
break;
case 6:
jieZha6();
break;
case 7:
showZhang7();
break;
case 9:
goBack9();
break;
default:
System.out.println("输入有误,请重新输入!");
break;
}
}
}else{
System.out.println("-----账户或密码有误-----");
}
break;
case "9":
System.out.println("确定要退出吗(Y/n):");
key=KeyBoardTools.readString(1).toUpperCase();
if("Y".equals(key)){
loop=false;
}
break;
default:
System.out.println("------输入的不正正确------");
break;
}
}
System.out.println("********你退出了,再见!********");
}
private void showTable1(){
System.out.println("===========显示餐桌==========");
System.out.println("餐桌号\t\t状态\t\t预定人");
List<DiningTable> tables = dts.getAllTable();
for (DiningTable table : tables) {
System.out.println(table);
}
System.out.println("===========显示完毕==========");
}
private void yuDingTable2(){
System.out.println("===========预定餐桌==========");
System.out.print("请输入要预定的餐桌号(-1退出):");
int dinId = KeyBoardTools.readInt(1);
if(dinId==-1){
System.out.println("--------取消了预定--------");
return;
}
DiningTable tab = dts.getTableById(dinId);
if(tab==null){
System.out.println("--------未查到编号为"+dinId+"的餐桌!!--------");
return;
}
if(tab.getDinState()>0){
System.out.println("--------编号为"+dinId+"的餐桌,已经被预定!!--------");
return;
}
System.out.print("请输入你的姓名(-1退出):");
String userName=KeyBoardTools.readString(30);
if("-1".equals(userName)){
System.out.println("--------取消了预定--------");
return;
}
System.out.print("请输入你的电话(-1退出):");
String userTel=KeyBoardTools.readString(11);
if("-1".equals(userTel)){
System.out.println("--------取消了预定--------");
return;
}
System.out.print("确定信息是否正确y/n(-1退出):");
key=KeyBoardTools.readString(1).toUpperCase();
if(!"Y".equals(key)){
System.out.println("--------取消了预定--------");
return;
}
boolean res = dts.updateTable(tab.getDinId(), userName, userTel, 1);
if(res){
System.out.println("==========预定成功!==========");
}else{
System.out.println("==========预定失败!==========");
}
}
private void showCai3(){
System.out.println("==========菜单列表==========");
System.out.println("编号\t\t菜名\t\t价格\t\t类型");
List<Menu> menus = ms.getAllDate();
for (Menu menu : menus) {
System.out.println(menu);
}
System.out.println("==========展示结束==========");
}
private void dianCai4(){
System.out.println("==========点餐服务=========");
System.out.print("请输入预定姓名:");
String name = KeyBoardTools.readString(30);
System.out.print("请输入预订电话:");
String tel = KeyBoardTools.readString(11);
DiningTable tab = dts.getTableByUserInfo(name, tel);
if(tab==null){
System.out.println("--------未查到相关信息--------");
return;
}
System.out.print("请输入菜品的编号(-1退出):");
int mId = KeyBoardTools.readInt(2);
if(mId==-1){
System.out.println("********点餐取消********");
return;
}
System.out.print("请输入数量(-1退出):");
int nums = KeyBoardTools.readInt(1);
if(nums==-1){
System.out.println("********点餐取消********");
return;
}
Menu m = ms.getDateById(mId);
if(m==null){
System.out.println("********本店暂未拥有编号为"+mId+"的菜品*******");
return;
}
boolean rs = bs.addBill(mId, m.getmName(), nums, m.getmPrice()*nums, tab.getDinId(), 0);
if(rs){
System.out.println("--------点菜成功-------");
}else{
System.out.println("-------点菜失败--------");
}
}
private void showZhang5(){
System.out.println("==========未付款账单显示==========");
System.out.println("账单编号\t\t菜编号\t\t菜名\t\t数量\t\t价格\t\t餐桌\t\t点餐时间\t\t\t\t\t\t状态");
List<Bill> bills = bs.getDateNonPay();
for (Bill bill : bills) {
System.out.println(bill);
}
System.out.println("==========显示完毕===========");
}
private void showZhang7(){
System.out.println("==========已付款账单显示==========");
System.out.println("账单编号\t\t菜编号\t\t菜名\t\t数量\t\t价格\t\t餐桌\t\t点餐时间\t\t\t\t\t\t状态");
List<Bill> bills = bs.getDateAlPay();
for (Bill bill : bills) {
System.out.println(bill);
}
System.out.println("==========显示完毕===========");
}
private void jieZha6(){
System.out.println("==========结账服务=========");
System.out.print("请输入预定姓名:");
String name = KeyBoardTools.readString(30);
System.out.print("请输入预订电话:");
String tel = KeyBoardTools.readString(11);
DiningTable tab = dts.getTableByUserInfo(name, tel);
if(tab==null){
System.out.println("--------未查到相关信息--------");
return;
}
Bill bi = bs.getByDinTable(tab.getDinId());
if(bi==null){
System.out.println("--------当前餐桌没有未支付的账单--------");
return;
}
System.out.println("请选择支付方式(现金,微信,支付宝):");
key=KeyBoardTools.readString(5);
int n=1;
if("微信".equals(key)){
n=2;
}else if("支付宝".equals(key)){
n=3;
}
boolean rs = bs.updateSate(n, tab.getDinId());
if(rs){
System.out.println("---------结账欧克--------");
}else{
System.out.println("----------结账失败----------");
}
}
private void goBack9(){
System.out.println("确定要退出吗y/n:");
key=KeyBoardTools.readString(1).toUpperCase();
if("Y".equals(key)){
loop=false;
}
}
}
源代码
资源请点击👉源码
-----------------------结束-----------------------