• 小郡肝火锅点餐系统代码实现(部分)


    项目结构:

    Java代码实现部分 :

    Food.java

    
    
    package bean;

    import java.math.BigDecimal;

    public class Food {
    private int id; // 主键
    private String foodname; // 菜名
    private int type_id; // 菜系
    private String descript; // 描述
    private BigDecimal price; // 价格
    private int saling; // 在售
    private String img; //图片

    public String getDescript() {
    return descript;
    }

    public void setDescript(String deccript) {
    this.descript = deccript;
    }

    public String getFoodname() {
    return foodname;
    }

    public void setFoodname(String foodname) {
    this.foodname = foodname;
    }

    public int getId() {
    return id;
    }

    public void setId(int id) {
    this.id = id;
    }

    public String getImg() {
    return img;
    }

    public void setImg(String img) {
    this.img = img;
    }

    public BigDecimal getPrice() {
    return price;
    }

    public void setPrice(BigDecimal price) {
    this.price = price;
    }

    public int getSaling() {
    return saling;
    }

    public void setSaling(int saling) {
    this.saling = saling;
    }

    public int getType_id() {
    return type_id;
    }

    public void setType_id(int type_id) {
    this.type_id = type_id;
    }
    }

     

    OrderDao.java

    
    
    package dao;

    import bean.Order;

    import java.util.List;

    public interface OrderDao{

    void addOrder(Order order) throws Exception;

    Order findUF(Order order) throws Exception ;

    List<Order> findUserId(String userId) throws Exception;
    Order findById(String id) throws Exception;
    void removeOrder(int id) throws Exception;

    }

     

    O'r'de'r'DaoImpl.java

    
    
    package dao.daoImpl;


    import bean.Food;
    import bean.Order;
    import dao.FoodDao;
    import dao.OrderDao;
    import util.C3P0Util;

    import java.math.BigDecimal;
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.util.ArrayList;
    import java.util.List;

    public class OrderDaoImpl implements OrderDao {

    FoodDao foodDao = new FoodDaoImpl();

    @Override
    public void addOrder(Order order) throws Exception {
    //查找是否该用户已经添加了这道菜
    Order sc = this.findUF(order);

    if(sc == null){
    //该用户还没有添加这道菜
    String sql = "INSERT INTO `xiaojungan`.`t_order`(`food_id`,`user_id`,`table_id`,`food_account`) VALUES(?,?,?,?)";

    try(
    Connection conn = C3P0Util.getConnection();
    PreparedStatement ps = conn.prepareStatement(sql);
    ) {
    ps.setInt(1,order.getFood_id());
    ps.setInt(2,order.getUser_id());
    ps.setInt(3,order.getTable_id());
    ps.setInt(4,order.getFood_account());

    ps.executeUpdate();
    } catch (Exception e) {
    e.printStackTrace();
    throw new RuntimeException("添加失败!");
    }
    }else{ //已经添加这道菜,则更新数量
    String sql = "UPDATE `xiaojungan`.`t_order` SET `food_account` = "+ sc.getFood_account()+"" +
    "+1 WHERE id="+sc.getId();
    System.out.println(sql);
    try(
    Connection conn = C3P0Util.getConnection();
    PreparedStatement ps = conn.prepareStatement(sql);
    ) {
    ps.executeUpdate();
    } catch (Exception e) {
    e.printStackTrace();
    throw new RuntimeException("添加失败!");
    }
    }


    }

    @Override
    public Order findUF(Order order) throws Exception {

    String sql = "select * from t_order where user_id=? and food_id=?";

    Order s = null;

    try(
    Connection conn = C3P0Util.getConnection();
    PreparedStatement ps = conn.prepareStatement(sql);
    ) {
    ps.setInt(1,order.getUser_id());
    ps.setInt(2,order.getFood_id());
    try(
    ResultSet rs = ps.executeQuery();
    ){
    if(rs.next()) {
    s = new Order();

    s.setId(rs.getInt(1));
    s.setFood_id(rs.getInt(2));
    s.setUser_id(rs.getInt(3));
    s.setFood_account(rs.getInt(4));
    Food b = foodDao.findFood_id(order.getFood_id());
    BigDecimal sellingPrice = b.getPrice();
    String foodName = b.getFoodname();
    s.setPrice(sellingPrice);
    s.setFoodName(foodName);
    //计算总价
    s.setTotalPrice(sellingPrice.multiply(BigDecimal.valueOf(s.getFood_account())));

    System.out.println("s:" + s);
    }
    }catch (Exception e){
    }
    } catch (Exception e) {
    e.printStackTrace();
    }

    return s;

    }

    @Override
    public List<Order> findUserId(String userId) throws Exception {
    String sql = "select * from t_order where user_id=?";
    List<Order> shopCartList = new ArrayList<>();
    try(
    Connection conn = C3P0Util.getConnection();
    PreparedStatement ps = conn.prepareStatement(sql)
    ) {
    ps.setInt(1, Integer.parseInt(userId));
    try (
    ResultSet rs = ps.executeQuery()
    ) {

    //将查询出的结果数据封装到List对象中
    while(rs.next()){
    Order s = new Order();

    s.setId(rs.getInt(1));
    s.setFood_id(rs.getInt(2));
    s.setUser_id(rs.getInt(3));
    s.setTable_id(rs.getInt(4));
    s.setFood_account(rs.getInt(5));
    Food b = foodDao.findFood_id(s.getFood_id());
    BigDecimal sellingPrice = b.getPrice();
    String foodName = b.getFoodname();
    s.setPrice(sellingPrice);
    s.setFoodName(foodName);
    //计算总价
    s.setTotalPrice(sellingPrice.multiply(BigDecimal.valueOf(s.getFood_account())));

    shopCartList.add(s);
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    }catch (Exception e){
    e.printStackTrace();
    }
    return shopCartList;
    }
    public Order findById(String id) throws Exception {
    String sql = "select * from t_order where id=?";

    Order s = null;

    try(
    Connection conn = C3P0Util.getConnection();
    PreparedStatement ps = conn.prepareStatement(sql)
    ) {
    ps.setInt(1, Integer.valueOf(id));

    try (
    ResultSet rs = ps.executeQuery()
    ) {
    //将查询出的结果数据封装到Order对象中
    if(rs.next()){
    s = new Order();
    s.setId(rs.getInt(1));
    s.setFood_id(rs.getInt(2));
    s.setUser_id(rs.getInt(3));
    s.setTable_id(rs.getInt(4));
    s.setFood_account(rs.getInt(5));
    Food b = foodDao.findFood_id(s.getFood_id());
    BigDecimal sellingPrice = b.getPrice();
    String foodName = b.getFoodname();
    s.setPrice(sellingPrice);
    s.setFoodName(foodName);
    //计算总价
    s.setTotalPrice(sellingPrice.multiply(BigDecimal.valueOf(s.getFood_account())));
    return s;
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    }catch (Exception e){
    e.printStackTrace();
    }
    return s;
    }
    @Override
    public void removeOrder(int id) throws Exception {
    String sql = "DELETE FROM `xiaojungan`.`t_order` WHERE id=?";

    try(
    Connection conn = C3P0Util.getConnection();
    PreparedStatement ps = conn.prepareStatement(sql);
    ) {
    ps.setInt(1, id);

    ps.executeUpdate();
    } catch (Exception e) {
    e.printStackTrace();
    throw new RuntimeException("移除失败!");
    }
    }
    }
     

    OrderService.java

    
    
    package service;

    import bean.Order;

    import java.util.List;

    public interface OrderService {
    void addOrder(Order order) throws Exception ;

    List<Order> findByUserId(String usr_id) throws Exception;
    Order findById(String id) throws Exception;

    void clear();

    void removeOrder(String[] orders);


    }
     

    OrderServiceImpl.java

    
    
    package service.serviceImpl;


    import bean.Order;
    import dao.OrderDao;
    import dao.daoImpl.OrderDaoImpl;
    import service.OrderService;

    import java.math.BigDecimal;
    import java.util.List;

    public class OrderServiceImpl implements OrderService {

    BigDecimal totalPrice;
    private OrderDao orderDao = new OrderDaoImpl();

    @Override
    public void addOrder(Order order) throws Exception {
    orderDao.addOrder(order);
    }

    @Override
    public List<Order> findByUserId(String user_id) throws Exception {
    List<Order> orderList = orderDao.findUserId(user_id);
    return orderList;
    }

    @Override
    public void removeOrder(String[] orders) {
    //遍历数据
    for(int i=0;i<orders.length;i++) {
    //移除
    int shopId=Integer.valueOf(orders[i]);
    try {
    orderDao.removeOrder(shopId);
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }

    public Order findById(String id) throws Exception {
    Order order = orderDao.findById(id);
    return order;
    }
    @Override
    public void clear() {

    }
    }
     

     OrderServlet.java

    
    
    package servlet;

    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    import java.io.IOException;

    @WebServlet("/OrderServlet")
    public class OrderServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    HttpSession session = request.getSession();
    Object user = session.getAttribute("user");

    if(user != null){
    //用户登陆
    response.sendRedirect("/order.html");

    }else if(user == null){
    //未登录
    response.getWriter().write("请先登录!!1秒后转到登陆界面");
    response.setHeader("refresh", "1;url=" + request.getContextPath()
    + "/login.html");
    }


    }


    }
     

    User.java

    package bean;
    
    import java.util.Date;
    
    public class User  {
        private int id;
        private String username;
        private String password;
        private int table_id;
        private int role;
    
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    
        public int getRole() {
            return role;
        }
    
        public void setRole(int role) {
            this.role = role;
        }
    
        public int getTable_id() {
            return table_id;
        }
    
        public void setTable_id(int table_id) {
            this.table_id = table_id;
        }
    
        public String getUsername() {
            return username;
        }
    
        public void setUsername(String username) {
            this.username = username;
        }
    }

    UserDaoImpl.java

    package dao.daoImpl;
    
    import  bean.User;
    
    import  dao.UserDao;
    import util.C3P0Util;
    import java.sql.ResultSet;
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    
    public class UserDaoImpl implements UserDao {
        @Override
        public User login(User user)throws Exception {
            String sql = "select * from t_user where username=? and password=?";
            User lo= null;
            try(
                    Connection conn =  C3P0Util.getConnection();
                    PreparedStatement ps = conn.prepareStatement(sql);
            ) {
                ps.setString(1, user.getUsername());
                ps.setString(2, user.getPassword());
                try(
                        ResultSet rs = ps.executeQuery();
                ){
                    if(rs.next()) {
                        lo = new User();
                        lo.setId(rs.getInt(1));
                        lo.setTable_id(rs.getInt(2));
                        lo.setUsername(rs.getString(3));
                        lo.setPassword(rs.getString(4));
                        lo.setRole(rs.getInt(5));
                        System.out.println("lo:" + lo);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return lo;
        }
    
        @Override
        public void addUser(User user)  throws Exception {
            String sql = "INSERT INTO t_user(username,password)  VALUES(?,?)";
            try (
                    Connection conn = C3P0Util.getConnection();
                    PreparedStatement ps = conn.prepareStatement(sql)
            ) {
                ps.setString(1, user.getUsername());
                ps.setString(2, user.getPassword());
                ps.executeUpdate();
            } catch (Exception e) {
                e.printStackTrace();
                throw new RuntimeException("注册失败!");
            }
        }
         @Override
         public User findById(int id) throws Exception {
                String sql = "select * from t_user where id=?";
    
                User u = null;
    
                try(
                        Connection conn = C3P0Util.getConnection();
                        PreparedStatement ps = conn.prepareStatement(sql)
                ) {
                    ps.setInt(1, id);
    
                    try (
                            ResultSet rs = ps.executeQuery()
                    ) {
                        //将查询出的结果数据封装到Book对象中
                        if(rs.next()){
                            u = new User();
    
                            u.setId(rs.getInt(1));
                            u.setTable_id(rs.getInt(2));
                            u.setUsername(rs.getString(3));
                            u.setPassword(rs.getString(4));
                            u.setRole(rs.getInt(5));
    
                            return u;
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
                return u;
            }
    
    }
  • 相关阅读:
    JAVA整合阿里云OSS实现文件上传功能
    JAVA实现导出excel功能,封装通用导出工具类
    SpringBoot整合quartz实现动态启动,停止定时任务功能
    JAVA微信支付——企业付款(企业向微信用户个人付款、转账)
    Windows系统安装Redis服务
    MySQL设置表中字段的数据唯一性
    XSS工具类,清除参数中的特殊字符
    用伪代码表示第4题中各题的算法
    用N-S图表示第4题中各题的算法
    用N-S图表示第4题中各题的算法
  • 原文地址:https://www.cnblogs.com/luv-letter/p/10944996.html
Copyright © 2020-2023  润新知