饿了么商家后台管理系统

1.项目概述

1.1.项目演示

  1. 运行 “饿了么商家后台管理系统” ,演示应用程序效果。

1.2.项目目标

  1. 本项目为课程级贯穿项目中的第一个项目(JDBC项目、前端项目、javaWeb项目)。
  2. 本项目完成后,学员将能够使用javaSE+JDBC+MySql技术开发基于控制台的C/S结构应用程序。

1.3.项目中所涉及到相关知识点

  • 封装JDBC
  • 封装DAO
  • 领域模型中的实体类
  • 增删改查操作
  • 多条件模糊查询
  • JDBC事务管理
  • 表的主外键关系

1.4.数据库设计

1.4.1.DB一览表

No 表名称 中文名 说明
1 business 商家表 存储所有商家信息
2 food 食品表 存储每个商家所拥有的所有食品信息
3 admin 管理员表 存储管理员信息

1.5.2.表结构

约束类型标识:
PK:primary key 主键
FK:foreign key 外键
NN:not null 非空
UQ:unique 唯一索引
AI:auto increment 自增

1.5.2.1.business(商家表)

No 字段名 数据类型 size 默认値 约束 说明
1 businessId int PK、AI、NN 商家编号
2 password varchar 20 NN 密码
3 businessName varchar 40 NN 商家名称
4 businessAddress varchar 50 商家地址
5 businessExplain varchar 40 商家介绍
6 starPrice decimal (5,2) 0.00 起送费
7 deliveryPrice decimal (5,2) 0.00 配送费

1.5.2.2.food(食品表)

No 字段名 数据类型 size 默认値 约束 说明
1 foodId int PK、AI、NN 食品编号
2 foodName varchar 30 NN 食品名称
3 foodExplain varchar 30 食品介绍
4 foodPrice decimal (5,2) NN 食品价格
5 businessId int FK、NN 所属商家编号

1.5.2.3.admin(管理员表)

No 字段名 数据类型 size 默认値 约束 说明
1 adminId int PK、AI、NN 管理员编号
2 adminName varchar 20 NN、UQ 管理员名称
3 password varchar 20 NN 密码

2.项目搭建

2.1.开发环境检查

  1. 开发工具:SpringToolSuite(STS)
  2. 检查开发工具的jdk配置:jdk8
  3. 检查开发工具的文件编码配置:utf-8

2.2.搭建javaWeb工程总体架构

2.2.1.工程类型

创建工程类型:java Project

2.2.2.导入jar包

  1. mysql-connector-java-bin.jar

2.2.3.工程目录结构

4.项目代码

4.1.领域模型(PO)

4.1.1.Business

package com.neusoft.elm.po;

public class Business {

    private Integer businessId;
    private String password;
    private String businessName;
    private String businessAddress;
    private String businessExplain;
    private Double starPrice;
    private Double deliveryPrice;
    
    @Override
    public String toString() {
        return "\n商家编号:"+this.businessId+
               "\n商家名称:"+this.businessName+
               "\n商家地址:"+this.businessAddress+
               "\n商家介绍:"+this.businessExplain+
               "\n起送费:¥"+this.starPrice+
               "\n配送费:¥"+this.deliveryPrice+"\n";
    }
    
    //get、set ... ...
}

4.1.2.Food

package com.neusoft.elm.po;

public class Food {

    private Integer foodId;
    private String foodName;
    private String foodExplain;
    private Double foodPrice;
    private Integer businessId;
    
    @Override
    public String toString() {
        return "\n食品编号:"+this.foodId+
               "\n食品名称:"+this.foodName+
               "\n食品介绍:"+this.foodExplain+
               "\n食品价格:"+this.foodPrice+
               "\n所属商家:"+this.businessId;
    }
    
    //get、set ... ...
}

4.1.3.Admin

package com.neusoft.elm.po;

public class Admin {

    private Integer adminId;
    private String adminName;
    private String password;
    
    //get、set ... ...
}

4.2.Dao层代码

4.2.1.Business

package com.neusoft.elm.dao;

import java.util.List;

import com.neusoft.elm.po.Business;

public interface BusinessDao {

    public List<Business> listBusiness(String businessName,String businessAddress);
    public int saveBusiness(String businessName);
    public int removeBusiness(int businessId);
    
    public Business getBusinessByIdByPass(Integer businessId,String password);
    public Business getBusinessById(Integer businessId);
    public int updateBusiness(Business business);
    public int updateBusinessByPassword(Integer businessId,String password);
}
package com.neusoft.elm.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.neusoft.elm.dao.BusinessDao;
import com.neusoft.elm.po.Admin;
import com.neusoft.elm.po.Business;
import com.neusoft.elm.util.DBUtil;

public class BusinessDaoImpl implements BusinessDao{
    
    private Connection con = null;
    private PreparedStatement pst = null;
    private ResultSet rs = null;

    @Override
    public List<Business> listBusiness(String businessName,String businessAddress) {
        List<Business> list = new ArrayList<>();
        StringBuffer sql = new StringBuffer("select * from business where 1=1 ");
        if(businessName!=null&&!businessName.equals("")) {
            sql.append(" and businessName like '%"+businessName+"%' ");
        }
        if(businessAddress!=null&&!businessAddress.equals("")) {
            sql.append(" and businessAddress like '%"+businessAddress+"%' ");
        }
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql.toString());
            rs = pst.executeQuery();
            while(rs.next()) {
                Business business = new Business();
                business.setBusinessId(rs.getInt("businessId"));
                business.setPassword(rs.getString("password"));
                business.setBusinessName(rs.getString("businessName"));
                business.setBusinessAddress(rs.getString("businessAddress"));
                business.setBusinessExplain(rs.getString("businessExplain"));
                business.setStarPrice(rs.getDouble("starPrice"));
                business.setDeliveryPrice(rs.getDouble("deliveryPrice"));
                list.add(business);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(rs, pst, con);
        }
        return list;
    }
    
    @Override
    public int saveBusiness(String businessName) {
        int businessId = 0;
        String sql = "insert into business(businessName,password) values(?,'123')";
        try {
            con = DBUtil.getConnection();
            //设置返回自增长列值
            pst = con.prepareStatement(sql,PreparedStatement.RETURN_GENERATED_KEYS);
            pst.setString(1, businessName);
            pst.executeUpdate();
            //获取自增长列值(一行一列)
            rs = pst.getGeneratedKeys();
            if(rs.next()) {
                businessId = rs.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(rs, pst, con);
        }
        return businessId;
    }
    
    @Override
    public int removeBusiness(int businessId) {
        int result = 0;
        String delFootSql = "delete from food where businessId=?";
        String delBusinessSql = "delete from business where businessId=?";
        try {
            con = DBUtil.getConnection();
            //开启一个事务
            con.setAutoCommit(false);
            
            pst = con.prepareStatement(delFootSql);
            pst.setInt(1, businessId);
            pst.executeUpdate();
            
            pst = con.prepareStatement(delBusinessSql);
            pst.setInt(1, businessId);
            result = pst.executeUpdate();
            
            con.commit();
        } catch (SQLException e) {
            result = 0;
            try {
                con.rollback();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        } finally {
            DBUtil.close(null, pst, con);
        }
        return result;
    }
    
    @Override
    public Business getBusinessByIdByPass(Integer businessId,String password) {
        Business business = null;
        String sql = "select * from business where businessId=? and password=?";
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql.toString());
            pst.setInt(1, businessId);
            pst.setString(2, password);
            rs = pst.executeQuery();
            while(rs.next()) {
                business = new Business();
                business.setBusinessId(rs.getInt("businessId"));
                business.setPassword(rs.getString("password"));
                business.setBusinessName(rs.getString("businessName"));
                business.setBusinessAddress(rs.getString("businessAddress"));
                business.setBusinessExplain(rs.getString("businessExplain"));
                business.setStarPrice(rs.getDouble("starPrice"));
                business.setDeliveryPrice(rs.getDouble("deliveryPrice"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(rs, pst, con);
        }
        return business;
    }
    
    @Override
    public Business getBusinessById(Integer businessId) {
        Business business = null;
        String sql = "select * from business where businessId=?";
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql.toString());
            pst.setInt(1, businessId);
            rs = pst.executeQuery();
            while(rs.next()) {
                business = new Business();
                business.setBusinessId(rs.getInt("businessId"));
                business.setPassword(rs.getString("password"));
                business.setBusinessName(rs.getString("businessName"));
                business.setBusinessAddress(rs.getString("businessAddress"));
                business.setBusinessExplain(rs.getString("businessExplain"));
                business.setStarPrice(rs.getDouble("starPrice"));
                business.setDeliveryPrice(rs.getDouble("deliveryPrice"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(rs, pst, con);
        }
        return business;
    }
    
    @Override
    public int updateBusiness(Business business) {
        int result = 0;
        String sql = "update business set businessName=?,businessAddress=?,businessExplain=?,starPrice=?,deliveryPrice=? where businessId=?";
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            pst.setString(1, business.getBusinessName());
            pst.setString(2, business.getBusinessAddress());
            pst.setString(3, business.getBusinessExplain());
            pst.setDouble(4, business.getStarPrice());
            pst.setDouble(5, business.getDeliveryPrice());
            pst.setInt(6, business.getBusinessId());
            result = pst.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(null, pst, con);
        }
        return result;
    }
    
    @Override
    public int updateBusinessByPassword(Integer businessId,String password) {
        int result = 0;
        String sql = "update business set password=? where businessId=?";
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            pst.setString(1, password);
            pst.setInt(2, businessId);
            result = pst.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(null, pst, con);
        }
        return result;
    }
}

4.2.2.Food

package com.neusoft.elm.dao;

import java.util.List;

import com.neusoft.elm.po.Food;

public interface FoodDao {

    public List<Food> listFoodByBusinessId(Integer businessId);
    public int saveFood(Food food);
    public Food getFoodById(Integer foodId);
    public int updateFood(Food food);
    public int removeFood(Integer foodId);
}
package com.neusoft.elm.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.neusoft.elm.dao.FoodDao;
import com.neusoft.elm.po.Food;
import com.neusoft.elm.util.DBUtil;

public class FoodDaoImpl implements FoodDao{
    
    private Connection con = null;
    private PreparedStatement pst = null;
    private ResultSet rs = null;

    @Override
    public List<Food> listFoodByBusinessId(Integer businessId) {
        List<Food> list = new ArrayList<>();
        String sql = "select * from food where businessId=?";
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            pst.setInt(1, businessId);
            rs = pst.executeQuery();
            while(rs.next()) {
                Food food = new Food();
                food.setFoodId(rs.getInt("foodId"));
                food.setFoodName(rs.getString("foodName"));
                food.setFoodExplain(rs.getString("foodExplain"));
                food.setFoodPrice(rs.getDouble("foodPrice"));
                food.setBusinessId(rs.getInt("businessId"));
                list.add(food);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(rs, pst, con);
        }
        return list;
    }
    
    @Override
    public int saveFood(Food food) {
        int result = 0;
        String sql = "insert into food values(null,?,?,?,?)";
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            pst.setString(1, food.getFoodName());
            pst.setString(2, food.getFoodExplain());
            pst.setDouble(3, food.getFoodPrice());
            pst.setInt(4, food.getBusinessId());
            result = pst.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(null, pst, con);
        }
        return result;
    }
    
    @Override
    public Food getFoodById(Integer foodId) {
        Food food = null;
        String sql = "select * from food where foodId=?";
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            pst.setInt(1, foodId);
            rs = pst.executeQuery();
            while(rs.next()) {
                food = new Food();
                food.setFoodId(rs.getInt("foodId"));
                food.setFoodName(rs.getString("foodName"));
                food.setFoodExplain(rs.getString("foodExplain"));
                food.setFoodPrice(rs.getDouble("foodPrice"));
                food.setBusinessId(rs.getInt("businessId"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(rs, pst, con);
        }
        return food;
    }
    
    @Override
    public int updateFood(Food food) {
        int result = 0;
        String sql = "update food set foodName=?,foodExplain=?,foodPrice=? where foodId=?";
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            pst.setString(1, food.getFoodName());
            pst.setString(2, food.getFoodExplain());
            pst.setDouble(3, food.getFoodPrice());
            pst.setInt(4, food.getFoodId());
            result = pst.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(null, pst, con);
        }
        return result;
    }
    
    @Override
    public int removeFood(Integer foodId) {
        int result = 0;
        String sql = "delete from food where foodId=?";
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            pst.setInt(1, foodId);
            result = pst.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(null, pst, con);
        }
        return result;
    }
}

4.2.3.Admin

package com.neusoft.elm.dao;

import com.neusoft.elm.po.Admin;

public interface AdminDao {

    public Admin getAdminByNameByPass(String adminName,String password);
}
package com.neusoft.elm.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.neusoft.elm.dao.AdminDao;
import com.neusoft.elm.po.Admin;
import com.neusoft.elm.util.DBUtil;

public class AdminDaoImpl implements AdminDao{
    
    private Connection con = null;
    private PreparedStatement pst = null;
    private ResultSet rs = null;

    @Override
    public Admin getAdminByNameByPass(String adminName, String password) {
        Admin admin = null;
        String sql = "select * from admin where adminName=? and password=?";
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            pst.setString(1, adminName);
            pst.setString(2, password);
            rs = pst.executeQuery();
            while(rs.next()) {
                admin = new Admin();
                admin.setAdminId(rs.getInt("adminId"));
                admin.setAdminName(rs.getString("adminName"));
                admin.setPassword(rs.getString("password"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(rs, pst, con);
        }
        return admin;
    }
}

4.3.View层代码

4.3.1.Business

package com.neusoft.elm.view;

import com.neusoft.elm.po.Business;

public interface BusinessView {

    public void listBusinessAll();
    public void listBusiness();
    public void saveBusiness();
    public void removeBusiness();
    
    public Business login();
    public void showBusiness(Integer businessId);
    public void editBusiness(Integer businessId);
    public void updateBusinessByPassword(Integer businessId);
}
package com.neusoft.elm.view.impl;

import java.util.List;
import java.util.Scanner;

import com.neusoft.elm.dao.BusinessDao;
import com.neusoft.elm.dao.impl.BusinessDaoImpl;
import com.neusoft.elm.po.Business;
import com.neusoft.elm.view.BusinessView;

public class BusinessViewImpl implements BusinessView{
    
    private Scanner input = new Scanner(System.in);

    @Override
    public void listBusinessAll() {
        BusinessDao dao = new BusinessDaoImpl();
        List<Business> list = dao.listBusiness(null,null);
        System.out.println("商家编号\t商家名称\t商家地址\t商家介绍\t起送费\t配送费");
        for(Business b : list) {
            System.out.println(b.getBusinessId()+"\t"+b.getBusinessName()+"\t"+b.getBusinessAddress()+"\t"+b.getBusinessExplain()+"\t"+b.getStarPrice()+"\t"+b.getDeliveryPrice());
        }
    }
    
    @Override
    public void listBusiness() {
        String businessName = "";
        String businessAddress = "";
        
        String inputStr = "";
        System.out.println("是否需要输入商家名称关键词(y/n):");
        inputStr = input.next();
        if(inputStr.equals("y")) {
            System.out.println("请输入商家名称关键词:");
            businessName = input.next();
        }
        
        System.out.println("是否需要输入商家地址关键词(y/n):");
        inputStr = input.next();
        if(inputStr.equals("y")) {
            System.out.println("请输入商家地址关键词:");
            businessAddress = input.next();
        }
        
        BusinessDao dao = new BusinessDaoImpl();
        List<Business> list = dao.listBusiness(businessName, businessAddress);
        System.out.println("商家编号\t商家名称\t商家地址\t商家介绍\t起送费\t配送费");
        for(Business b : list) {
            System.out.println(b.getBusinessId()+"\t"+b.getBusinessName()+"\t"+b.getBusinessAddress()+"\t"+b.getBusinessExplain()+"\t"+b.getStarPrice()+"\t"+b.getDeliveryPrice());
        }
    }
    
    @Override
    public void saveBusiness() {
        System.out.println("请输入商家名称:");
        String businessName = input.next();
        BusinessDao dao = new BusinessDaoImpl();
        int businessId = dao.saveBusiness(businessName);
        if(businessId>0) {
            System.out.println("新建商家成功!商家编号为:"+businessId);
        }else {
            System.out.println("新建商家失败!");
        }
    }
    
    @Override
    public void removeBusiness() {
        System.out.println("请输入商家编号:");
        int businessId = input.nextInt();
        
        BusinessDao dao = new BusinessDaoImpl();
        System.out.println("确认要删除吗(y/n):");
        if(input.next().equals("y")) {
            int result = dao.removeBusiness(businessId);
            if(result==1) {
                System.out.println("删除商家成功!");
            }else {
                System.out.println("删除商家失败!");
            }
        }
    }
    
    @Override
    public Business login() {
        System.out.println("请输入商家编号:");
        int businessId = input.nextInt();
        System.out.println("请输入密码:");
        String password = input.next();
        
        BusinessDao dao = new BusinessDaoImpl();
        return dao.getBusinessByIdByPass(businessId, password);
    }
    
    @Override
    public void showBusiness(Integer businessId) {
        BusinessDao dao = new BusinessDaoImpl();
        Business business = dao.getBusinessById(businessId);
        System.out.println(business);
    }
    
    @Override
    public void editBusiness(Integer businessId) {
        //先将商家信息查询出来显示,然后用户才能更新
        BusinessDao dao = new BusinessDaoImpl();
        Business business = dao.getBusinessById(businessId);
        System.out.println(business);
        
        String inputStr = "";
        System.out.println("是否修改商家名称(y/n):");
        inputStr = input.next();
        if(inputStr.equals("y")) {
            System.out.println("请输入新的商家名称:");
            business.setBusinessName(input.next());
        }
        
        System.out.println("是否修改商家地址(y/n):");
        inputStr = input.next();
        if(inputStr.equals("y")) {
            System.out.println("请输入新的商家地址:");
            business.setBusinessAddress(input.next());
        }
        
        System.out.println("是否修改商家介绍(y/n):");
        inputStr = input.next();
        if(inputStr.equals("y")) {
            System.out.println("请输入新的商家介绍:");
            business.setBusinessExplain(input.next());
        }
        
        System.out.println("是否修改起送费(y/n):");
        inputStr = input.next();
        if(inputStr.equals("y")) {
            System.out.println("请输入新的起送费:");
            business.setStarPrice(input.nextDouble());
        }
        
        System.out.println("是否修改配送费(y/n):");
        inputStr = input.next();
        if(inputStr.equals("y")) {
            System.out.println("请输入新的配送费:");
            business.setDeliveryPrice(input.nextDouble());
        }
        
        int result = dao.updateBusiness(business);
        if(result>0) {
            System.out.println("\n修改商家信息成功!\n");
        }else {
            System.out.println("\n修改商家信息失败!\n");
        }
    }
    
    @Override
    public void updateBusinessByPassword(Integer businessId) {
        BusinessDao dao = new BusinessDaoImpl();
        Business business = dao.getBusinessById(businessId);
        
        System.out.println("\n请输入旧密码:");
        String oldPass = input.next();
        System.out.println("\n请输入新密码:");
        String password = input.next();
        System.out.println("\n请再次输入新密码:");
        String beginPassword = input.next();
        
        if(!business.getPassword().equals(oldPass)) {
            System.out.println("\n旧密码输入错误!");
        }else if(!password.equals(beginPassword)) {
            System.out.println("\n两次输入密码不一致!");
        }else {
            int result = dao.updateBusinessByPassword(businessId, password);
            if(result>0) {
                System.out.println("\n修改密码成功!");
            }else {
                System.out.println("\n修改密码失败!");
            }
        }
    }
}

4.3.2.Food

package com.neusoft.elm.view;

import java.util.List;

import com.neusoft.elm.po.Food;

public interface FoodView {

    public List<Food> showFoodList(Integer businessId);
    public void saveFood(Integer businessId);
    public void updateFood(Integer businessId);
    public void removeFood(Integer businessId);
}
package com.neusoft.elm.view.impl;

import java.util.List;
import java.util.Scanner;

import com.neusoft.elm.dao.FoodDao;
import com.neusoft.elm.dao.impl.FoodDaoImpl;
import com.neusoft.elm.po.Food;
import com.neusoft.elm.view.FoodView;

public class FoodViewImpl implements FoodView{
    
    private Scanner input = new Scanner(System.in);

    @Override
    public List<Food> showFoodList(Integer businessId) {
        FoodDao dao = new FoodDaoImpl();
        List<Food> list = dao.listFoodByBusinessId(businessId);
        System.out.println("食品编号\t食品名称\t食品介绍\t食品价格");
        for(Food food : list) {
            System.out.println(food.getFoodId()+"\t"+food.getFoodName()+"\t"+food.getFoodExplain()+"\t"+food.getFoodPrice());
        }
        return list;
    }
    
    @Override
    public void saveFood(Integer businessId) {
        Food food = new Food();
        System.out.println("请输入食品名称:");
        food.setFoodName(input.next());
        System.out.println("请输入食品介绍:");
        food.setFoodExplain(input.next());
        System.out.println("请输入食品价格:");
        food.setFoodPrice(input.nextDouble());
        food.setBusinessId(businessId);
        
        FoodDao dao = new FoodDaoImpl();
        int result = dao.saveFood(food);
        if(result>0) {
            System.out.println("\n新增食品成功!\n");
        }else {
            System.out.println("\n新增食品失败!\n");
        }
    }
    
    @Override
    public void updateFood(Integer businessId) {
        FoodDao dao = new FoodDaoImpl();
        List<Food> list = showFoodList(businessId);
        
        if(list.size()==0) {
            System.out.println("没有任何食品!");
        }else {
            System.out.println("请选择要更新的食品编号:");
            int foodId = input.nextInt();
            Food food = dao.getFoodById(foodId);
            System.out.println(food);
            
            String inputStr = "";
            System.out.println("是否更新食品名称(y/n):");
            inputStr = input.next();
            if(inputStr.equals("y")) {
                System.out.println("请输入新的食品名称:");
                food.setFoodName(input.next());
            }
            
            System.out.println("是否更新食品介绍(y/n):");
            inputStr = input.next();
            if(inputStr.equals("y")) {
                System.out.println("请输入新的食品介绍:");
                food.setFoodExplain(input.next());
            }
            
            System.out.println("是否更新食品价格(y/n):");
            inputStr = input.next();
            if(inputStr.equals("y")) {
                System.out.println("请输入新的食品价格:");
                food.setFoodPrice(input.nextDouble());
            }
            
            int result = dao.updateFood(food);
            if(result>0) {
                System.out.println("\n修改食品成功!\n");
            }else {
                System.out.println("\n修改食品失败!\n");
            }
        }
    }
    
    @Override
    public void removeFood(Integer businessId) {
        FoodDao dao = new FoodDaoImpl();
        List<Food> list = showFoodList(businessId);
        
        if(list.size()==0) {
            System.out.println("没有任何食品!");
        }else {
            System.out.println("请选择要删除的食品编号:");
            int foodId = input.nextInt();
            
            System.out.println("确认要删除吗(y/n):");
            if(input.next().equals("y")) {
                int result = dao.removeFood(foodId);
                if(result>0) {
                    System.out.println("\n删除食品成功!\n");
                }else {
                    System.out.println("\n删除食品失败!\n");
                }
            }
        }
    }
}

4.3.3.Admin

package com.neusoft.elm.view;

import com.neusoft.elm.po.Admin;

public interface AdminView {

    public Admin login();
}
package com.neusoft.elm.view.impl;

import java.util.Scanner;

import com.neusoft.elm.dao.AdminDao;
import com.neusoft.elm.dao.impl.AdminDaoImpl;
import com.neusoft.elm.po.Admin;
import com.neusoft.elm.view.AdminView;

public class AdminViewImpl implements AdminView{
    
    private Scanner input = new Scanner(System.in);

    @Override
    public Admin login() {
        System.out.println("请输入管理员名称:");
        String adminName = input.next();
        System.out.println("请输入密码:");
        String password = input.next();
        
        AdminDao dao = new AdminDaoImpl();
        return dao.getAdminByNameByPass(adminName, password);
    }
}

4.4.程序入口

4.4.1.管理员入口

package com.neusoft.elm;

import java.util.Scanner;

import com.neusoft.elm.po.Admin;
import com.neusoft.elm.view.AdminView;
import com.neusoft.elm.view.BusinessView;
import com.neusoft.elm.view.impl.AdminViewImpl;
import com.neusoft.elm.view.impl.BusinessViewImpl;

public class ElmAdminEntry {

    public void work() {
        Scanner input = new Scanner(System.in);
        
        System.out.println("---------------------------------------------------------");
        System.out.println("|\t\t\t 饿了么后台管理系统  \t\t\t|");
        System.out.println("---------------------------------------------------------");
        
        AdminView adminView = new AdminViewImpl();
        BusinessView businessView = new BusinessViewImpl();
        Admin admin = adminView.login();
        
        //登录
        if(admin!=null) {
            int menu = 0;
            while(menu!=5) {
                //输出主菜单
                System.out.println("\n========= 1.所有商家列表=2.搜索商家=3.新建商家=4.删除商家=5.退出系统 =========");
                System.out.println("请输入你的选择:");
                menu = input.nextInt();
                switch(menu) {
                    case 1:
                        businessView.listBusinessAll();
                        break;
                    case 2:
                        businessView.listBusiness();
                        break;
                    case 3:
                        businessView.saveBusiness();
                        break;
                    case 4:
                        businessView.removeBusiness();
                        break;
                    case 5:
                        System.out.println("------------------------欢迎下次光临饿了么后台管理系统-----------------------");
                        break;  
                    default:
                        System.out.println("没有这个选项!\n");
                        break;
                }
            }
            
        }else {
            System.out.println("\n管理员名称或密码输入错误!\n");
        }
    }
    
    public static void main(String[] args) {
        new ElmAdminEntry().work();
    }

}

4.4.2.商家入口

package com.neusoft.elm;

import java.util.Scanner;

import com.neusoft.elm.po.Business;
import com.neusoft.elm.view.BusinessView;
import com.neusoft.elm.view.FoodView;
import com.neusoft.elm.view.impl.BusinessViewImpl;
import com.neusoft.elm.view.impl.FoodViewImpl;

public class ElmBusinessEntry {

    public void work() {
        Scanner input = new Scanner(System.in);
        
        System.out.println("---------------------------------------------------------");
        System.out.println("|\t\t\t 饿了么后台管理系统  \t\t\t|");
        System.out.println("---------------------------------------------------------");
        
        BusinessView businessView = new BusinessViewImpl();
        
        //商家登录
        Business business = businessView.login();
        
        if(business!=null) {
            int menu = 0;
            while(menu!=5) {
                //输出一级菜单
                System.out.println("\n======= 一级菜单(商家管理)1.查看商家信息=2.修改商家信息=3.更新密码=4.所属商品管理=5.退出系统=======");
                System.out.println("请输入你的选择:");
                menu = input.nextInt();
                
                switch(menu) {
                    case 1:
                        businessView.showBusiness(business.getBusinessId());
                        break;
                    case 2:
                        businessView.editBusiness(business.getBusinessId());
                        break;
                    case 3:
                        businessView.updateBusinessByPassword(business.getBusinessId());
                        break;
                    case 4:
                        foodManager(business.getBusinessId());
                        break;
                    case 5:
                        System.out.println("------------------------欢迎下次光临饿了么后台管理系统-----------------------");
                        break;  
                    default:
                        System.out.println("没有这个选项!\n");
                        break;
                }
            }
        }else {
            System.out.println("商家编号或密码输入错误!");
        }
        
    }
    
    private void foodManager(int businessId) {
        Scanner input = new Scanner(System.in);
        
        FoodView foodView = new FoodViewImpl();
        
        int menu = 0;
        while(menu!=5) {
            //输出二级菜单
            System.out.println("\n======= 二级菜单(食品管理)1.查看食品列表=2.新增食品=3.修改食品=4.删除食品=5.返回一级菜单 =======");
            System.out.println("请输入你的选择:");
            menu = input.nextInt();
            
            switch(menu) {
                case 1:
                    foodView.showFoodList(businessId);
                    break;
                case 2:
                    foodView.saveFood(businessId);
                    break;
                case 3:
                    foodView.updateFood(businessId);
                    break;
                case 4:
                    foodView.removeFood(businessId);
                    break;
                case 5:
                    break;  
                default:
                    System.out.println("没有这个选项!\n");
                    break;
            }
        }
    }
    
    public static void main(String[] args) {
        new ElmBusinessEntry().work();
    }
}
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,390评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,821评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,632评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,170评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,033评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,098评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,511评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,204评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,479评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,572评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,341评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,213评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,576评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,893评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,171评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,486评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,676评论 2 335