微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

JDBC与Dao模式的第一次项目训练——点餐系统

JDBC实战——点餐系统的实现

数据库的设计

在这里插入图片描述

类的设计

采用Dao模式进行设计

在这里插入图片描述

实体类

package cn.ordersystem.entity;
/**
 * 桌子的实体类
 * bno,桌子的编号
 * btype 桌子的状态,可为空
 * @author vcc
 *
 */
public class Board {
	private int bno;
	private int btype;
	private int count;
	
	//构造函数
	public Board() {
		
	}
	public Board(int count) {
		this.count=count;
	}
	public Board(int bno, int btype) {
		super();
		this.bno = bno;
		this.btype = btype;
	}

	public int getBno() {
		return bno;
	}

	public void setBno(int bno) {
		this.bno = bno;
	}

	public int getBtype() {
		return btype;
	}

	public void setBtype(int btype) {
		this.btype = btype;
	}
	public int getCount() {
		return count;
	}
	public void setCount(int count) {
		this.count = count;
	}
	
	
}
/**
 * 客户表的实体类
 * bno,桌号
 * ono 订单号
 * eid 负责人编号
 * @author vcc
 *
 */
public class Customer {

	private int bno;
	private int ono;
	private int eid;
	private double count;
	private String 	date;

	
	//构造函数
	public Customer() {
		
	}
	
	public Customer(int ono, int bno, int eid,  String date,double count) {
		super();
		this.bno = bno;
		this.ono = ono;
		this.eid = eid;
		this.count = count;
		this.date = date;
	}

	public Customer(double count) {
		this.count=count;
	}
	public Customer(int ono,double count) {
		this.ono=ono;
		this.count=count;
	}
	public Customer(String date) {
		this.date=date;
	}
	public Customer(int bno, int eid) {
		super();
		this.bno = bno;
		this.eid = eid;
	}
	
	public Customer( int ono, int bno,int eid) {
		super();
		this.bno = bno;
		this.ono=ono;
		this.eid = eid;
	}

	public int getBno() {
		return bno;
	}

	public void setBno(int bno) {
		this.bno = bno;
	}

	public int getono() {
		return ono;
	}

	public void setono(int ono) {
		this.ono = ono;
	}

	public int getEid() {
		return eid;
	}

	public void setEid(int eid) {
		this.eid = eid;
	}
	public double getCount() {
		return count;
	}
	public void setCount(double count) {
		this.count = count;
	}
	public String getDate() {
		return date;
	}
	public void setDate(String date) {
		this.date = date;
	}
	
}

package cn.ordersystem.entity;
/**
 * 员工表
 * eid 员工编号
 * ename 员工名
 * epwd 员工密码
 * @author vcc
 *
 */
public class Emp {
	
	private int eid;
	private String ename;
	private String epwd;
	private int count;
	
	//构造函数
	public Emp() {
		
	}
	public Emp(int count) {
		this.count=count;
	}
	public Emp(int eid, String ename) {
		super();
		this.eid = eid;
		this.ename = ename;
	}
	public Emp(String ename,String epwd) {
		super();
		this.ename = ename;
		this.epwd=epwd;
	}

	public Emp(int eid, String ename, String epawd) {
		super();
		this.eid = eid;
		this.ename = ename;
		this.epwd = epawd;
	}

	public int getEid() {
		return eid;
	}

	public void setEid(int eid) {
		this.eid = eid;
	}

	public String getEname() {
		return ename;
	}

	public void setEname(String ename) {
		this.ename = ename;
	}

	public String getEpwd() {
		return epwd;
	}

	public void setEpwd(String epawd) {
		this.epwd = epawd;
	}
	
	public int getCount() {
		return count;
	}
	public void setCount(int count) {
		this.count = count;
	}
	
}

package cn.ordersystem.entity;
/**
 * 菜单表的实体类
 * fid 菜品编号
 * fname 菜名
 * fprice 菜品价格
 * @author vcc
 *
 */
public class FoodMenu {

	private int fid;
	private String fname;
	private double fprice;
	private int count;
	
	//构造函数
	public FoodMenu() {
		
	}
	public FoodMenu(int count) {
		this.count=count;
	}
	
	public FoodMenu(int fid,  double fprice) {
		super();
		this.fid = fid;
		this.fprice = fprice;
	}
	public FoodMenu(String fname,  double fprice) {
		super();
		this.fname=fname;
		this.fprice = fprice;
	}
	public FoodMenu(int fid, String fname, double fprice) {
		super();
		this.fid = fid;
		this.fname = fname;
		this.fprice = fprice;
	}

	public int getFid() {
		return fid;
	}

	public void setFid(int fid) {
		this.fid = fid;
	}

	public String getFname() {
		return fname;
	}

	public void setFname(String fname) {
		this.fname = fname;
	}

	public double getFprice() {
		return fprice;
	}

	public void setFprice(double fprice) {
		this.fprice = fprice;
	}
	public int getCount() {
		return count;
	}
	public void setCount(int count) {
		this.count = count;
	}
	
}

package cn.ordersystem.entity;
/**
 * 订单表的实体类
 * ono 订单号
 * fid 菜品编号
 * @author vcc
 *
 */
public class OrderList {

	private int ono;
	private int fid;
	private double total;
	private Board board;
	private Customer date;
	public OrderList() {
		
	}
	
	public OrderList(int ono, int fid) {
		super();
		this.ono = ono;
		this.fid = fid;
	}
	
	public OrderList(int ono,double total) {
		super();
		this.ono = ono;
		this.total = total;
	}

	public Board getBno() {
		return board;
	}

	public void setBno(Board bno) {
		this.board = bno;
	}
	
	public Customer getDate() {
		return date;
	}

	public void setDate(Customer date) {
		this.date = date;
	}

	public int getono() {
		return ono;
	}

	public void setono(int ono) {
		this.ono = ono;
	}

	public int getFid() {
		return fid;
	}

	public void setFid(int fid) {
		this.fid = fid;
	}

	public double getTotal() {
		return total;
	}

	public void setTotal(double total) {
		this.total = total;
	}


	
	
}

实体类中的属性相对应与表的列,也有一些是在程序中需要用到的属性
构造函数主要用于对对象进行赋值,因为有多种不同的应用,所以也写了多个构造函数
值得注意的一点是,构造函数中,参数列表的顺序一定要理清楚,不然在为对象赋值时,可能会有数据的混乱

接口

package cn.ordersystem.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.sqlException;
import java.sql.Statement;
//BaseDao类提供了数据库连接的的方法,以及增删改查的方法
public class BaseDao {
	// 四个变量
	private static final String driver = "com.MysqL.cj.jdbc.Driver";// 驱动字符串
	// 数据库地址
	private static final String url = "jdbc:MysqL://localhost:3306/order_system?useSSL=false&serverTimezone=UTC";
	private static final String user = "root";// 用户名
	private static final String pwd = "123.com";// 密码

	/**
	 * 获取连接
	 */
	public static Connection getConnection() {
		Connection conn = null;
		// 1.加载驱动
		try {
			Class.forName(driver);
		} catch (ClassNotFoundException e) {
			e.printstacktrace();
		}
		// 2.获取连接对象
		try {
			conn = DriverManager.getConnection(url, user, pwd);
		} catch (sqlException e) {
			e.printstacktrace();
		}
		return conn;
	}

	/**
	 * 关闭连接
	 * 
	 * @param rs
	 * @param state
	 * @param conn
	 */
	public static void close(ResultSet rs, Statement state, Connection conn) {
		// 5.释放对象
		try {
			if (rs != null)
				rs.close();
		} catch (sqlException e1) {
			e1.printstacktrace();
		}
		try {
			if (state != null)
				state.close();
		} catch (sqlException e) {
			e.printstacktrace();
		}
		try {
			if (conn != null)
				conn.close();
		} catch (sqlException e) {
			e.printstacktrace();
		}
	}

	/**
	 * 重载关闭连接 查询用
	 * 
	 * @param rs
	 * @param state
	 * @param conn
	 */
	public static void close(ResultSet rs) {
		Statement state = null;
		Connection conn = null;

		try {
			state = rs.getStatement();
			conn = state.getConnection();
		} catch (sqlException e) {
			e.printstacktrace();
		}
		BaseDao.close(rs, state, conn);
	}

	/**
	 * 增删改
	 * 
	 * @return
	 */
	public static int update(String sql, Object[] objs) {
		Connection conn = null;
		PreparedStatement pstate = null;
		int rs = -1;
		// 1.2.加,连
		conn = getConnection();
		// 3.在参数中
		// 4.执行
		try {
			pstate = conn.prepareStatement(sql);
			if (objs != null) {
				for (int i = 0; i < objs.length; i++) {
					pstate.setobject(i + 1, objs[i]);
				}
			}
			rs = pstate.executeUpdate();
		} catch (sqlException e) {
			e.printstacktrace();
		} finally {
			BaseDao.close(null, pstate, conn);
		}
		return rs;
	}

	/**
	 * 查
	 */
	public static ResultSet query(String sql, Object[] objs) {
		Connection conn = null;
		PreparedStatement pstate = null;
		ResultSet rs = null;
		// 1.2加连
		conn = getConnection();

		// 3参数
		// 4执行
		try {
			pstate = conn.prepareStatement(sql);
			if (objs != null) {
				for (int i = 0; i < objs.length; i++) {
					pstate.setobject(i + 1, objs[i]);
				}
			}
			rs = pstate.executeQuery();
		} catch (sqlException e) {
			e.printstacktrace();
		}
		return rs;
	}
}
package cn.ordersystem.dao;

import java.util.ArrayList;

import cn.ordersystem.entity.Board;
import cn.ordersystem.entity.Emp;

/**
 * 桌子表的数据访问接口
 * 提供增删改查的方法
 * @author vcc
 *
 */
public interface BoardDao {
	
	/**
	 * 开桌时更改桌子状态
	 * @param board
	 * @return
	 */
	int updateStartBoard(Board board);
	
	/**
	 * 结账时更改桌状态
	 * @return
	 */
	int updateEndBoard(Board board);
	
	/**
	 * 查询当前无人坐的桌子
	 */
	ArrayList<Board> getBoards();
	
	/**
	 * 根据桌号查状态
	 */
	Board getBoardBybno(int bno);
	
	/**
	 * 判断桌子是否存在
	 */
	Board checkBoard(int bno);
}

package cn.ordersystem.dao;

import java.util.ArrayList;

import cn.ordersystem.entity.Customer;

/**
 * 客人表的接口
 * 实现增删改查的方法
 * @author vcc
 *
 */
public interface CustomerDao {
	/**
	 * 增加客人的方法
	 * @param customer
	 * @return
	 */
	int addCus(Customer customer);
	/**
	 * 更新客人信息的方法
	 * 增加总价
	 * @param customer
	 * @return
	 */
	int updateCus(Customer customer);
	
	/**
	 * 删除客人信息的方法
	 */
	int deleteCus(Customer customer);
	
	/**
	 * 根据订单号查询客人的方法
	 */
	Customer getCustomersByOrder(int ono);
	
	Customer getTimeByOrder(int ono);
	/**
	 * 根据桌号查询客人的方法
	 */
	Customer getCustomersByBoard(int bno);
	/**
	 * 判断订单是否存在的方法
	 */
	Customer checkOno(int ono);
}

package cn.ordersystem.dao;

import cn.ordersystem.entity.Emp;

/**
 * emp表的数据访问接口
 * 提供增删改查的方法
 * @author vcc
 *
 */
public interface EmpDao {
	/**
	 * 添加员工
	 * @param emp
	 * @return
	 */
	int addEmp(Emp emp);
	/**
	 * 更改员工密码的方法
	 * @param emp
	 * @return
	 */
	int updateEmp(Emp emp);
	/**
	 * 删除员工
	 * @param emp
	 * @return
	 */
	int deleteEmp(Emp emp);
	/**
	 * 根据id查找员工的方法
	 * @param eid
	 * @return
	 */
	Emp getEmp(int eid);
	/**
	 * 根据名字查id的方法
	 * @param ename
	 * @return
	 */
	Emp getEmp(String ename);
	/**
	 * 判断员工是否存在
	 */
	Emp checkEmp(int eid);
}

package cn.ordersystem.dao;

import java.util.ArrayList;

import cn.ordersystem.entity.Emp;
import cn.ordersystem.entity.FoodMenu;

/**
 * 菜单表的数据访问接口
 * 提供增删改查等方法
 * @author vcc
 *
 */
public interface FoodMenuDao {
	/**
	 * 添加菜品
	 * @return
	 */
	int addFood(FoodMenu foodMenu);
	/**
	 * 更新菜品价格
	 * @param foodMenu
	 * @return
	 */
	int updateFood(FoodMenu foodMenu);
	/**
	 * 删除此菜品
	 * @param foodMenu
	 * @return
	 */
	int deleteFood(FoodMenu foodMenu);
	
	/**
	 * 查询所有菜品
	 */
	ArrayList<FoodMenu> getFoodMenus();
	
	/**
	 * 根据菜品编号查询菜品信息(名称,价格)
	 */
	FoodMenu getFoodMenu(int fid);
	
	/**
	 * 根据订单号查看所点菜品的方法
	 */
	ArrayList<FoodMenu> getFoodMenus(int ono);
	
	/**
	 * 判断菜品是否存在
	 */
	FoodMenu checkFood(int fid);
}

package cn.ordersystem.dao;

import java.util.ArrayList;

import cn.ordersystem.entity.OrderList;

/**
 * orderlist 表的数据访问接口
 * 提供增删改查的方法
 * @author vcc
 *
 */
public interface OrderListDao {
	/**
	 * 增加订单
	 * @param orderList
	 * @return
	 */
	int addOrder(OrderList orderList);
	
	/**
	 * 结账时,清楚此订单所点的菜的数据
	 * @param orderList
	 * @return
	 */
	int clearOrder(OrderList orderList);
	/**
	 * 查询某订单点的消费
	 */
	OrderList getorderLists(int ono);
	

	

	
}

实现类

package cn.ordersystem.dao.impl;

import java.sql.ResultSet;
import java.util.ArrayList;

import cn.ordersystem.dao.BaseDao;
import cn.ordersystem.dao.BoardDao;
import cn.ordersystem.entity.Board;


public class BoardDaoImpl implements BoardDao{

	@Override
	public int updateEndBoard(Board board) {
		String sql="update board set btype=null where bno=?";
		Object[] objs= {board.getBno()};
		return BaseDao.update(sql, objs);
	}

	@Override
	public ArrayList<Board> getBoards() {
		String sql="select bno,btype from board where btype is null";
		ArrayList<Board> boards=new ArrayList<Board>();
		ResultSet rs=BaseDao.query(sql, null);
		Board board=null;
		try {
			while (rs.next()) {
				board=new Board(rs.getInt("bno"), rs.getInt("btype"));
				boards.add(board);
			}
		} catch (Exception e) {
			e.printstacktrace();
		}
		finally {
			BaseDao.close(rs);
		}
		return boards;
	}

	@Override
	public int updateStartBoard(Board board) {
		String sql="update board set btype=1 where bno=?";
		Object[] objs= {board.getBno()};
		return BaseDao.update(sql, objs);
	}

	@Override
	public Board getBoardBybno(int bno) {
		String sql="select bno,btype from board where bno=? ";
		ResultSet rs=BaseDao.query(sql, new Object[]{bno});
		//存放查询结果
		Board board=null;
		try {
			while(rs.next()){
				//将数据表中的一行数据转换成一个实体对象
				board=new Board(rs.getInt("bno"),rs.getInt("btype"));
			}
		} catch (Exception e) {
			e.printstacktrace();
		}
		finally{
			BaseDao.close(rs);
		}
		return board;
	}

	@Override
	public Board checkBoard(int bno) {
		String sql="select count(*) from board where bno=?";
		ResultSet rs=BaseDao.query(sql, new Object[] {bno});
		Board board=null;
		try {
			while(rs.next()){
				//将数据表中的一行数据转换成一个实体对象
				board=new Board(rs.getInt("count(*)"));
			}
		} catch (Exception e) {
			e.printstacktrace();
		}
		finally{
			BaseDao.close(rs);
		}
		return board;
	}

}

package cn.ordersystem.dao.impl;

import java.sql.ResultSet;

import cn.ordersystem.dao.BaseDao;
import cn.ordersystem.dao.CustomerDao;
import cn.ordersystem.entity.Customer;
import cn.ordersystem.entity.Emp;

public class CustomerDaoImpl implements CustomerDao{

	@Override
	public int addCus(Customer customer) {
		String sql="insert into customer(bno,eid,date) values(?,?,Now())";
		Object[] objs={
					   customer.getBno(),
					   customer.getEid()};
		return BaseDao.update(sql, objs);
	}

	@Override
	public int updateCus(Customer customer) {
		String sql="update customer set total=? where ono=?";
		Object[] objs={
						customer.getCount(),
						customer.getono()
					   };
		return BaseDao.update(sql, objs);
	}

	@Override
	public int deleteCus(Customer customer) {
		String sql="delete from customer where ono=?";
		Object[] objs={
						customer.getono()
					   };
		return BaseDao.update(sql, objs);
	}

	@Override
	public Customer getCustomersByOrder(int ono) {
		String sql="select ono,bno,eid,date,total from customer where ono=?";
		ResultSet rs=BaseDao.query(sql, new Object[]{ono});
		//存放查询结果
		Customer customer=null;
		try {
			while(rs.next()){
				//将数据表中的一行数据转换成一个实体对象
				customer=new Customer(rs.getInt("ono"),rs.getInt("bno"),
										rs.getInt("eid"),rs.getString("date"),rs.getDouble("total"));
			}
		} catch (Exception e) {
			e.printstacktrace();
		}
		finally{
			BaseDao.close(rs);
		}
		
		return customer;
	}

	@Override
	public Customer getCustomersByBoard(int bno) {
		String sql="select ono,bno,eid from customer where bno=?";
		ResultSet rs=BaseDao.query(sql, new Object[]{bno});
		//存放查询结果
		Customer customer=null;
		try {
			while(rs.next()){
				//将数据表中的一行数据转换成一个实体对象
				customer=new Customer(rs.getInt("ono"),rs.getInt("bno"),
										rs.getInt("eid"));
			}
		} catch (Exception e) {
			e.printstacktrace();
		}
		finally{
			BaseDao.close(rs);
		}
		
		return customer;
	}

	@Override
	public Customer checkOno(int ono) {
		String sql="select count(*) from customer where ono=?";
		ResultSet rs=BaseDao.query(sql, new Object[] {ono});
		Customer customer=null;
		try {
			while(rs.next()){
				//将数据表中的一行数据转换成一个实体对象
				customer=new Customer(rs.getInt("count(*)"));
			}
		} catch (Exception e) {
			e.printstacktrace();
		}
		finally{
			BaseDao.close(rs);
		}
		return customer;
	}

	@Override
	public Customer getTimeByOrder(int ono) {
		String sql="select date from customer where ono=?";
		ResultSet rs=BaseDao.query(sql, new Object[]{ono});
		//存放查询结果
		Customer customer=null;
		try {
			while(rs.next()){
				//将数据表中的一行数据转换成一个实体对象
				customer=new Customer(rs.getString("date"));
			}
		} catch (Exception e) {
			e.printstacktrace();
		}
		finally{
			BaseDao.close(rs);
		}
		
		return customer;
	}

}

package cn.ordersystem.dao.impl;

import java.sql.ResultSet;

import cn.ordersystem.dao.BaseDao;
import cn.ordersystem.dao.EmpDao;
import cn.ordersystem.entity.Emp;

public class EmpDaoImpl implements EmpDao{

	@Override
	public int addEmp(Emp emp) {
		String sql="INSERT INTO emp(ename,epwd) VALUES (?,?)";
		Object[] objs={
					   emp.getEname(),
					   emp.getEpwd()
						};
		return BaseDao.update(sql, objs);
	}

	@Override
	public int updateEmp(Emp emp) {
		String sql="update emp set epwd=? where eid=?";
		Object[] objs={emp.getEpwd(),
					   emp.getEid()
						};
		return BaseDao.update(sql, objs);
	}

	@Override
	public int deleteEmp(Emp emp) {
		String sql="delete from emp where eid=?";
		Object[] objs={emp.getEid()
						};
		return BaseDao.update(sql, objs);
	}

	@Override
	public Emp getEmp(int eid) {
		String sql="select eid,ename, epwd from emp where eid=?";
		ResultSet rs=BaseDao.query(sql, new Object[] {eid});
		Emp emp=null;
		try {
			while(rs.next()){
				//将数据表中的一行数据转换成一个实体对象
				emp=new Emp(rs.getInt("eid"),rs.getString("ename"),
							rs.getString("epwd")
							);
			}
		} catch (Exception e) {
			e.printstacktrace();
		}
		finally{
			BaseDao.close(rs);
		}
		return emp;
	}
	@Override
	public Emp getEmp(String ename) {
		String sql="select eid,ename, epwd from emp where ename=?";
		ResultSet rs=BaseDao.query(sql, new Object[] {ename});
		Emp emp=null;
		try {
			while(rs.next()){
				//将数据表中的一行数据转换成一个实体对象
				emp=new Emp(rs.getInt("eid"),rs.getString("ename"),
							rs.getString("epwd")
							);
			}
		} catch (Exception e) {
			e.printstacktrace();
		}
		finally{
			BaseDao.close(rs);
		}
		return emp;
	}

	@Override
	public Emp checkEmp(int eid) {
		String sql="select count(*) from emp where eid=?";
		ResultSet rs=BaseDao.query(sql, new Object[] {eid});
		Emp emp=null;
		try {
			while(rs.next()){
				//将数据表中的一行数据转换成一个实体对象
				emp=new Emp(rs.getInt("count(*)"));
			}
		} catch (Exception e) {
			e.printstacktrace();
		}
		finally{
			BaseDao.close(rs);
		}
		return emp;
	}

}

package cn.ordersystem.dao.impl;

import java.sql.ResultSet;
import java.util.ArrayList;

import cn.ordersystem.dao.BaseDao;
import cn.ordersystem.dao.FoodMenuDao;
import cn.ordersystem.entity.Emp;
import cn.ordersystem.entity.FoodMenu;

public class FoodMenuDaoImpl implements FoodMenuDao {

	@Override
	public int addFood(FoodMenu foodMenu) {
		String sql="INSERT INTO foodmenu(fname,fprice) VALUES (?,?)";
		Object[] objs={foodMenu.getFname(),
					   foodMenu.getFprice()};
		return BaseDao.update(sql, objs);
	}

	@Override
	public int updateFood(FoodMenu foodMenu) {
		String sql="update foodmenu set fprice=? where fid=?";
		Object[] objs={foodMenu.getFprice(),
					   foodMenu.getFid()};
		return BaseDao.update(sql, objs);
	}

	@Override
	public int deleteFood(FoodMenu foodMenu) {
		String sql="delete from foodmenu where fid=?";
		Object[] objs={
					   foodMenu.getFid()};
		return BaseDao.update(sql, objs);
	}

	@Override
	public ArrayList<FoodMenu> getFoodMenus() {
		String sql=" select fid,fname,fprice from foodmenu ";
		//存放查询结果
		ArrayList<FoodMenu> foodMenus=new ArrayList<>();
		ResultSet rs=BaseDao.query(sql, null);
		FoodMenu foodMenu=null;
		try {
			while(rs.next()){
				//将数据表中的一行数据转换成一个实体对象
				foodMenu=new FoodMenu(rs.getInt("fid"), rs.getString("fname"), rs.getDouble("fprice"));
				//将对象添加到集合中
				foodMenus.add(foodMenu);
			}
		} catch (Exception e) {
			e.printstacktrace();
		}
		finally{
			BaseDao.close(rs);
		}
		
		return foodMenus;
	}

	@Override
	public FoodMenu getFoodMenu(int fid) {
		String sql="select fid,fname,fprice from foodmenu where fid=?";
		ResultSet rs=BaseDao.query(sql, new Object[]{fid});
		FoodMenu foodMenu=null;
		try {
			while(rs.next()){
				//将数据表中的一行数据转换成一个实体对象
				foodMenu=new FoodMenu(rs.getInt("fid"), rs.getString("fname"), rs.getDouble("fprice"));
			}
		} catch (Exception e) {
			e.printstacktrace();
		}
		finally{
			BaseDao.close(rs);
		}
		
		return foodMenu;
	}

	@Override
	public ArrayList<FoodMenu> getFoodMenus(int ono) {
		String sql=" select f.fid,fname,fprice  " + 
				"from foodmenu f   " + 
				"left join orderlist o on f.fid=o.fid   " + 
				"where ono=?";
		//存放查询结果
		ArrayList<FoodMenu> foodMenus=new ArrayList<>();
		ResultSet rs=BaseDao.query(sql, new Object[]{ono});
		FoodMenu foodMenu=null;
		try {
			while(rs.next()){
				//将数据表中的一行数据转换成一个实体对象
				foodMenu=new FoodMenu(rs.getInt("fid"), rs.getString("fname"), rs.getDouble("fprice"));
				//将对象添加到集合中
				foodMenus.add(foodMenu);
			}
		} catch (Exception e) {
			e.printstacktrace();
		}
		finally{
			BaseDao.close(rs);
		}
		return foodMenus;
	}

	@Override
	public FoodMenu checkFood(int fid) {
		String sql="select count(*) from foodmenu where fid=?";
		ResultSet rs=BaseDao.query(sql, new Object[] {fid});
		FoodMenu foodMenu=null;
		try {
			while(rs.next()){
				//将数据表中的一行数据转换成一个实体对象
				foodMenu=new FoodMenu(rs.getInt("count(*)"));
			}
		} catch (Exception e) {
			e.printstacktrace();
		}
		finally{
			BaseDao.close(rs);
		}
		return foodMenu;
	}

}

package cn.ordersystem.dao.impl;

import java.sql.ResultSet;

import cn.ordersystem.dao.BaseDao;
import cn.ordersystem.dao.OrderListDao;
import cn.ordersystem.entity.Board;
import cn.ordersystem.entity.Customer;
import cn.ordersystem.entity.OrderList;

public class OrderListDaoImpl implements OrderListDao{

	@Override
	public int addOrder(OrderList orderList) {
		String sql="INSERT INTO orderlist VALUES (?,?)";
		Object[] objs={
					   orderList.getono(),
					   orderList.getFid()
						};
		return BaseDao.update(sql, objs);
	}



	@Override
	public OrderList getorderLists(int ono) {
		String sql=" select o.ono,c.bno, sum(fprice) " + 
				" from orderlist o " + 
				"left join foodmenu f on o.fid=f.fid  "
				+ "left join customer c on c.ono=o.ono " + 
				"where o.ono=?";
		//存放查询结果
		ResultSet rs=BaseDao.query(sql, new Object[] {ono});
		OrderList orderList=null;
		try {
			while(rs.next()){
				//将数据表中的一行数据转换成一个实体对象
					orderList=new OrderList(rs.getInt("ono"),rs.getDouble("sum(fprice)"));
					Board board =new Board();
					board.setBno(rs.getInt("c.bno"));
					orderList.setBno(board);
						//将对象添加到集合中
					}
			} catch (Exception e) {
					e.printstacktrace();
			}
			finally{
				BaseDao.close(rs);
			}
			return orderList;
	}

	@Override
	public int clearOrder(OrderList orderList) {
		String sql="delete from orderlist where ono=?";
		Object[] objs={
						orderList.getono()
					   };
		return BaseDao.update(sql, objs);
	}

	

}

菜单

package cn.ordersystem.view;

import java.util.ArrayList;
import java.util.Scanner;

import cn.ordersystem.dao.BoardDao;
import cn.ordersystem.dao.CustomerDao;
import cn.ordersystem.dao.EmpDao;
import cn.ordersystem.dao.FoodMenuDao;
import cn.ordersystem.dao.OrderListDao;
import cn.ordersystem.dao.impl.BoardDaoImpl;
import cn.ordersystem.dao.impl.CustomerDaoImpl;
import cn.ordersystem.dao.impl.EmpDaoImpl;
import cn.ordersystem.dao.impl.FoodMenuDaoImpl;
import cn.ordersystem.dao.impl.OrderListDaoImpl;
import cn.ordersystem.entity.Board;
import cn.ordersystem.entity.Customer;
import cn.ordersystem.entity.Emp;
import cn.ordersystem.entity.FoodMenu;
import cn.ordersystem.entity.OrderList;

public class Menu {
	/**
	 * 菜单
	 */
	public static void menu() {
		Scanner input = new Scanner(system.in);
		boolean flag = false;
		do {
			flag = false;
			System.out.println("*****************************************");
			System.out.println("*          欢迎使用本点餐系统           *");
			System.out.println("*****************************************");
			System.out.println("*                请登录                 *");
			// 判断登录方法
			if (!login()) {
				flag = true;
			}
		} while (flag);
		System.out.println("");
		do {
			System.out.println("*****************************************");
			System.out.println("0.退出系统");
			System.out.println("1.员工信息管理");
			System.out.println("2.进入点餐界面");
			System.out.println("请选择您的操作:");
			int chose = input.nextInt();
			switch (chose) {
			case 0:
				System.exit(0);
				break;
			case 1:
				userMenu();
				break;
			case 2:
				orderMenu();
				break;
			default:
				System.out.println("输入错误!");
				break;
			}

		} while (true);

	}

	/**
	 * 用户菜单
	 */
	public static void userMenu() {
		Scanner input = new Scanner(system.in);
		int chose = 0;
		do {
			System.out.println("*****************************************");
			System.out.println("0.返回上一级");
			System.out.println("1.添加员工信息");
			System.out.println("2.修改员工密码");
			System.out.println("3.删除员工信息");
			System.out.println("请选择您的操作:");
			chose = input.nextInt();
			switch (chose) {
			case 1:
				// 添加员工的方法
				addEmp();
				break;
			case 2:
				// 修改员工密码的方法
				updateEmp();
				break;
			case 3:
				// 删除员工
				deleteEmp();
				break;
			case 0:
				break;
			}
		} while (chose != 0);
	}

	public static void updateEmp() {
		Scanner input = new Scanner(system.in);
		EmpDao ed = new EmpDaoImpl();
		System.out.println("请输入修改的员工编号:");
		int eid = input.nextInt();
		System.out.println("请输入修改后密码:");
		String epwd = input.next();
		Emp emp = new Emp(eid, null, epwd);
		int rs = ed.updateEmp(emp);
		if (rs != 0) {
			System.out.println("修改成功");
		} else {
			System.out.println("修改失败");
		}
	}

	/**
	 * 删除员工的方法
	 */
	public static void deleteEmp() {
		Scanner input = new Scanner(system.in);
		EmpDao ed = new EmpDaoImpl();
		System.out.println("请输入要删除的员工编号:");
		int eid = input.nextInt();
		Emp emp = new Emp(eid, null);
		int rs = ed.deleteEmp(emp);
		if (rs != 0) {
			System.out.println("删除成功");
		} else {
			System.out.println("删除失败");
		}
	}

	/**
	 * 添加员工信息的方法
	 */
	public static void addEmp() {
		Scanner input = new Scanner(system.in);
		EmpDao ed = new EmpDaoImpl();
		System.out.println("请输入添加的员工姓名:");
		String ename = input.next();
		System.out.println("请输入密码:");
		String epwd = input.next();
		Emp emp = new Emp(ename, epwd);
		int rs = ed.addEmp(emp);
		if (rs != 0) {
			System.out.println("添加成功");
			System.err.println("您的员工编号为:" + getEid(ename));
		} else {
			System.out.println("添加失败");
		}
	}

	/**
	 * 获取员工id的方法
	 */
	private static int getEid(String ename) {
		EmpDao ed = new EmpDaoImpl();
		Emp emp = ed.getEmp(ename);
		return emp.getEid();
	}

	/**
	 * 点餐菜单
	 */
	public static void orderMenu() {
		Scanner input = new Scanner(system.in);
		int chose = 0;
		do {
			System.out.println("*****************************************");
			System.out.println("0.返回上一级");
			System.out.println("1.显示当前菜单");
			System.out.println("2.添加新菜品");
			System.out.println("3.菜品价格设置");
			System.out.println("4.查询当前空桌");
			System.out.println("5.创建顾客订单");
			System.out.println("6.帮助顾客点单");
			System.out.println("7.结账");
			System.out.println("8.查询订单信息");
			System.out.println("请选择您的操作:");
			chose = input.nextInt();
			switch (chose) {
			case 1:
				showFood();
				break;
			case 2:
				// 增加菜品的方法
				addFood();
				break;
			case 3:
				// 设置菜品打折的方法
				setSale();
				break;
			case 4:
				// 查询空桌的方法
				showfreeboard();
				break;
			case 5:
				// 增加订单的方法
				createOrder();
				break;
			case 6:
				// 点餐的方法
				OrderFood();
				break;
			case 7:
				// 结账
				getorderList();
				break;
			case 8:
				showOrder();
			case 0:
				break;
			}
		} while (chose != 0);
	}

	
	/**
	 * 显示订单明细
	 */
	public static void showOrder() {
		Scanner input=new Scanner(system.in);
		System.out.println("输入您的订单号:");
		int ono=input.nextInt();
		CustomerDao cd=new CustomerDaoImpl();
		Customer customer=cd.getCustomersByOrder(ono);
		System.out.println("订单号为:"+customer.getono());
		System.out.println("桌位为:"+customer.getBno());
		System.out.println("接待人员为:"+customer.getEid());
		System.out.println("订单时间为为:"+customer.getDate());
		System.out.println("实际消费为:"+customer.getCount());
	}
	/**
	 * 添加新菜品
	 */
	public static void addFood() {
		Scanner input = new Scanner(system.in);
		System.out.println("请输入您要添加的菜品名称:");
		String fname = input.next();
		System.out.println("请输入您要添加的菜品价格:");
		double fprice = input.nextDouble();
		FoodMenuDao fmd = new FoodMenuDaoImpl();
		FoodMenu food = new FoodMenu(fname, fprice);
		int rs = fmd.addFood(food);
		if (rs != 0) {
			System.out.println("添加成功");
		} else {
			System.out.println("添加失败");
		}

	}

	/**
	 * 设置菜品打折(修改商品价格)
	 */
	public static void setSale() {
		showFood();
		Scanner input = new Scanner(system.in);
		System.out.println("请输入您要设置的菜编号");
		int fid = input.nextInt();
		if (!checkFood(fid)) {
			System.out.println("请输入设置后的价格:");
			double fprice = input.nextDouble();
			FoodMenuDao fmd = new FoodMenuDaoImpl();
			FoodMenu foodMenu = new FoodMenu(fid, fprice);
			fmd.updateFood(foodMenu);
		} else {
			System.err.println("没有这道菜!");
		}
	}

	/**
	 * 获得具体消费明细的方法
	 * 
	 * @param i
	 */
	public static void getorderList() {
		System.out.println("请输入您的订单号:");
		int i = new Scanner(system.in).nextInt();
		if (!checkOno(i)) {
			getorder(i);
			clearBoard(i);
			clearOrderlist(i);
		} else {
			System.err.println("无此订单!!");
		}
	}

	/**
	 * 获取消费的方法
	 */
	public static void getorder(int i) {
		OrderListDao old = new OrderListDaoImpl();
		OrderList ol = old.getorderLists(i);
		System.out.print("订单号:" + ol.getono() + "\t");
		System.out.print("桌位号:" + ol.getBno().getBno() + "\t");
		System.out.println("订单创建时间为:"+getTime(i));
		double n = ol.getTotal();
		System.out.print("您的总消费为"+n);
		if (n < 100) {
		} else if (n < 200) {
			n=n * 0.95;
			System.out.println("满100打95折,实收您" + n + "元");
		} else if (n < 400) {
			n=n * 0.9;
			System.out.println("满200打9折,实收您" + n + "元");
		} else if (n < 600) {
			n=n * 0.85;
			System.out.println("满400打85折,实收您" + n  + "元");
		} else {
			n=n*0.8;
			System.out.println(",超过600元,打8折,实收您" + n  + "元");
		}
		CustomerDao cd=new CustomerDaoImpl();
		Customer customer=new Customer(i, n);
		cd.updateCus(customer);
		System.out.println("*****************************************");
		getorderFood(i);
		System.err.println("感谢您的光临,欢迎下次再来!");
	}
	/**
	 * 获取订单时间的方法
	 */
	public static String getTime(int ono) {
		CustomerDao cd=new CustomerDaoImpl();
		Customer customer=cd.getTimeByOrder(ono);
		return customer.getDate();
	}
	/**
	 * 获取所点菜的方法
	 */
	public static void getorderFood(int i) {
		System.out.println();
		FoodMenuDao fmd = new FoodMenuDaoImpl();
		ArrayList<FoodMenu> foodMenus = fmd.getFoodMenus(i);
		for (FoodMenu foodMenu : foodMenus) {
			System.out.print("菜品编号:" + foodMenu.getFid() + "\t");
			System.out.print("菜品名 :" + foodMenu.getFname() + "\t");
			System.out.println("菜品价格:" + foodMenu.getFprice() + "\t");
		}
	}

	/**
	 * 登录验证 成功返回true 失败返回false
	 */
	public static boolean login() {
		Scanner input = new Scanner(system.in);
		System.out.println("请输入您的id:");
		int eid = input.nextInt();
		System.out.println("请输入您的密码:");
		String pwd = input.next();
		EmpDao ed = new EmpDaoImpl();
		Emp emp = ed.getEmp(eid);
		if (emp.getEpwd().equals(pwd)) {
			System.err.println("登录成功");
			return true;
		} else {
			System.err.println("账号或密码错误");
			return false;
		}
	}

	/**
	 * 查询空桌的方法
	 */
	public static void showfreeboard() {
		BoardDao bd = new BoardDaoImpl();
		ArrayList<Board> fBoards = bd.getBoards();
		System.out.println("空的桌位编号如下");
		System.out.println("*****************************************");
		for (Board board : fBoards) {
			System.out.println("桌位编号" + board.getBno());
		}
	}

	/**
	 * 创建顾客订单的方法
	 */

	public static void createOrder() {
		Scanner input = new Scanner(system.in);
		// 先改变桌子状态
		System.out.println("输入您选择的桌位号:");
		int n = input.nextInt();
		if (!checkboard(n)) {
			BoardDao bd = new BoardDaoImpl();
			Board board = new Board(n, 1);
			int rs = bd.updateStartBoard(board);
			check(rs);
			// 增加客人记录表
			System.out.println("请输入负责人的id:");
			int id = input.nextInt();
			if (!checkid(id)) {
				CustomerDao cd = new CustomerDaoImpl();
				Customer customer = new Customer(n, id);
				int rs1 = cd.addCus(customer);
				System.out.println("创建成功");
				System.err.println("您的订单号为" + getono(n));
				// 如果客人记录表创建失败,则要将桌子的状态改为空
				if (check(rs1)) {
					bd.updateEndBoard(board);
				}
			} else {
				System.err.println("没有此员工,创建失败");
				bd.updateEndBoard(board);
			}
		} else {
			System.err.println("没有此桌子,请重试");
		}

	}

	/**
	 * 判断是否创建成功的方法
	 */
	public static boolean check(int rs) {
		if (rs > 0) {
			return false;
		} else {
			return true;
		}
	}

	/**
	 * 判断桌子否是存在的方法
	 */
	public static boolean checkboard(int bno) {
		BoardDao bd = new BoardDaoImpl();
		Board board = bd.checkBoard(bno);
		if (board.getCount() != 0) {
			return false;
		} else {
			return true;
		}
	}

	/**
	 * 判断负责人id是否存在的方法
	 */
	public static boolean checkid(int id) {
		EmpDao ed = new EmpDaoImpl();
		Emp emp = ed.checkEmp(id);
		if (emp.getCount() != 0) {
			return false;
		} else {
			return true;
		}
	}

	/**
	 * 得到订单号的方法
	 * 
	 * @return
	 */
	public static int getono(int n) {
		CustomerDao cd = new CustomerDaoImpl();
		Customer customer = cd.getCustomersByBoard(n);
		return customer.getono();
	}

	/**
	 * 点菜的方法
	 * 
	 * @param ono
	 */
	public static void OrderFood() {
		do {
			System.out.println("请输入您的订单号:");
			int ono = new Scanner(system.in).nextInt();
			if (checkOno(ono)) {
				System.err.println("无此订单");
				break;
			}
			if (cheakBtype(ono)) {
				System.err.println("订单已过时");
				break;
			}
			OrderListDao old = new OrderListDaoImpl();
			// 显示菜单方法
			showFood();
			boolean flag = true;
			while (flag) {
				System.out.println("请输入您要点的菜的编码(输入0结束)");
				int n = new Scanner(system.in).nextInt();
				if (!checkFood(n) || n == 0) {
					if (n == 0) {
						flag = false;
						break;
					}
					OrderList ol = new OrderList(ono, n);
					old.addOrder(ol);
				} else {
					System.err.println("没有此菜品");
				}
			}
			break;
		} while (true);

	}
	public static boolean cheakBtype(int ono) {
		CustomerDao cd=new CustomerDaoImpl();
		Customer customer=cd.getCustomersByOrder(ono);
		BoardDao bd=new BoardDaoImpl();
		Board board=bd.getBoardBybno(customer.getBno());
		if (board.getBtype()==1) {
			return false;
		}else {
			return true;
		}
	}

	/**
	 * 检查是否有此订单的方法
	 * 
	 * @param n
	 * @return
	 */
	public static boolean checkOno(int n) {
		CustomerDao cd = new CustomerDaoImpl();
		Customer customer = cd.checkOno(n);
		if (customer.getCount() != 0) {
			return false;
		} else {
			return true;
		}
	}

	/**
	 * 显示所有菜品的方法
	 */
	public static void showFood() {
		FoodMenuDao fmd = new FoodMenuDaoImpl();
		ArrayList<FoodMenu> foodMenus = fmd.getFoodMenus();
		for (FoodMenu foodMenu : foodMenus) {
			System.out.print("菜品编号:" + foodMenu.getFid() + "\t");
			System.out.print("菜品名:" + foodMenu.getFname() + "\t");
			System.out.println("菜品价格:" + foodMenu.getFprice());
		}
	}

	/**
	 * 判断是否有这道菜的方法
	 */
	public static boolean checkFood(int n) {
		FoodMenuDao fmd = new FoodMenuDaoImpl();
		FoodMenu foodMenu = fmd.checkFood(n);
		if (foodMenu.getCount() != 0) {
			return false;
		} else {
			return true;
		}
	}

	/**
	 * 结账时改变桌子状态
	 * 
	 * @param n
	 */
	public static void clearBoard(int n) {
		CustomerDao cd = new CustomerDaoImpl();
		Customer customer = cd.getCustomersByOrder(n);
		BoardDao bd = new BoardDaoImpl();
		Board board = bd.getBoardBybno(customer.getBno());
		bd.updateEndBoard(board);
	}

	/**
	 * 根据订单号,结账时,清除订单项
	 * 
	 * @param n
	 */
	public static void clearOrderlist(int n) {
		OrderListDao old = new OrderListDaoImpl();
		OrderList orderList = new OrderList();
		orderList.setono(n);
		old.clearOrder(orderList);
	}

}

函数

package cn.ordersystem.view;



public class Run {

	public static void main(String[] args) {
		Menu.menu();
	}
	
	
	
}

实现演示

重点关注customer表和board表以及orderlist表里的数据变化

程序未开始时

在这里插入图片描述


在这里插入图片描述


在这里插入图片描述


演示开始

在这里插入图片描述


在这里插入图片描述


在这里插入图片描述


在这里插入图片描述


这时查看表里的数据变化

在这里插入图片描述


在这里插入图片描述


在这里插入图片描述


程序继续,进行到点菜

在这里插入图片描述


看orderlist表中的数值变化

在这里插入图片描述


然后结账

在这里插入图片描述


然后再看数据库里的数据变化

在这里插入图片描述


在这里插入图片描述


在这里插入图片描述

然后查单

在这里插入图片描述


一些错误测试我就不具体演示在这里了,在代码中我针对了一些输入错误进行了排查,比如输入的菜品编号不存在,输入的员工编号不存在,订单编号不存在或以结账等。具体方法大概如下:
1.菜品要是打折,或者商家想对某客户设置友情价怎么办:
setSale()

2.获取客户的具体消费(总消费,和所点的菜)
在getorderList()方法调用了getorder(i);clearBoard(i);clearOrderlist(i);
getorder(i); 得到用户的总消费的方法
clearBoard(i); 当用户结账走后,清空该用户的桌子,并将btype还原至null(无人可用)状态
clearOrderlist();当用户结账走后,清空该用户所点的菜的详细列表
3.登录验证
login() 验证服务员登录的账户

4.获取现在空着可用的桌子
showfreeboard();

5.创建客户订单的方法
createOrder();

6.判断是否创建客户成功的方法
check();

7.判断负责人是否存在的方法
checkid();

8.得到订单号的方法
getono(int n);

9.点菜的方法
OrderFood();

10.显示菜单方法
showFood();

11.判断此订单是否存在的方法
checkOno(int n);

12.判断是否有这道菜的方法
checkFood(int n)

13.判断订单是否过时的方法
cheakBtype(int ono)

具体代码可以对照着方法名在上面查看

本次项目布置的时候课程只学习了MysqL和java基础,还没有学框架之类的。属于对jdbc的联系。所以代码会有一些杂乱,繁多。
希望大神轻喷,多多指教我这个小新人。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐