7

手撸一个外卖点餐系统后台,可以写上简历的实战项目!

 3 years ago
source link: https://segmentfault.com/a/1190000038333894
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

外卖点餐系统的实现

实体类的编写

(1)菜品类(菜品id,菜品名,菜品类型,上架时间,单价,月销售,总数量)

import java.util.Date;

public class Menu {

    private String mid;
    private String name;
    private String type;
    private Date dateIssued;
    private double price;
    private int monthlySales;
    private int totalQuantity;

    public Menu() {
        super();
        // TODO Auto-generated constructor stub
    }

    public Menu(String mid, String name, String type, Date dateIssued, double price, int monthlySales, int totalQuantity) {
        super();
        this.mid = mid;
        this.name = name;
        this.type = type;
        this.dateIssued = dateIssued;
        this.price = price;
        this.monthlySales = monthlySales;
        this.totalQuantity = totalQuantity;
    }

    public String getMid() {
        return mid;
    }

    public void setMid(String mid) {
        this.mid = mid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public Date getDateIssued() {
        return dateIssued;
    }

    public void setDateIssued(Date dateIssued) {
        this.dateIssued = dateIssued;
    }

    public double getPrice() {
        return price;
    }

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

    public int getMonthlySales() {
        return monthlySales;
    }

    public void setMonthlySales(int monthlySales) {
        this.monthlySales = monthlySales;
    }

    public int getTotalQuantity() {
        return totalQuantity;
    }

    public void setTotalQuantity(int totalQuantity) {
        this.totalQuantity = totalQuantity;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((dateIssued == null) ? 0 : dateIssued.hashCode());
        result = prime * result + ((mid == null) ? 0 : mid.hashCode());
        result = prime * result + monthlySales;
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        long temp;
        temp = Double.doubleToLongBits(price);
        result = prime * result + (int) (temp ^ (temp >>> 32));
        result = prime * result + totalQuantity;
        result = prime * result + ((type == null) ? 0 : type.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Menu other = (Menu) obj;
        if (dateIssued == null) {
            if (other.dateIssued != null)
                return false;
        } else if (!dateIssued.equals(other.dateIssued))
            return false;
        if (mid == null) {
            if (other.mid != null)
                return false;
        } else if (!mid.equals(other.mid))
            return false;
        if (monthlySales != other.monthlySales)
            return false;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        if (Double.doubleToLongBits(price) != Double.doubleToLongBits(other.price))
            return false;
        if (totalQuantity != other.totalQuantity)
            return false;
        if (type == null) {
            if (other.type != null)
                return false;
        } else if (!type.equals(other.type))
            return false;
        return true;
    }

    @Override
    public String toString() {
        return "Menu [mid=" + mid + ", name=" + name + ", type=" + type + ", dateIssued=" + dateIssued + ", price="
                + price + ", monthlySales=" + monthlySales + ", totalQuantity=" + totalQuantity + "]";
    }

}
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;
import java.util.stream.Collectors;

public class MenuManager implements DAO<Menu> {

    static Map<String, Menu> map = new HashMap<String, Menu>();

    static {
        map.put("01", new Menu("01", "邹蹄", "垃圾食品", new Date(2020,10,12), 200, 1200, 500));
        map.put("02", new Menu("02", "邹腰子", "垃圾食品", new Date(2020,9,22), 30, 1000, 20));
        map.put("03", new Menu("03", "邹大肠", "垃圾食品", new Date(2020,10,23), 88.8, 900, 1000));
        map.put("04", new Menu("04", "邹肉", "垃圾食品", new Date(2020,1,1), 99.99, 10, 8829));
        map.put("05", new Menu("05", "邹头", "垃圾食品", new Date(2020,7,7), 3300, 20000, 4287));
        map.put("06", new Menu("06", "邹鞭", "补品", new Date(), 998, 999999, 9999));
    }

    @Override
    public void insert(Menu menu) {
        map.put(menu.getMid(), menu);
    }

    @Override
    public Menu findById(String id) {
        return map.get(id);
    }

    //显示所有菜品(按菜品销量从高到低排序输出)
    @Override
    public List findAll() {
        List<Entry<String,Menu>> list = map.entrySet().stream()
                      .sorted((p1,p2) -> p2.getValue().getMonthlySales() - p1.getValue().getMonthlySales())
                      .collect(Collectors.toList());
        return list;
    }

    @Override
    public void delete(String id) {
        map.remove(id);
    }

    /**
     * 查看指定类别的菜品信息
     * @param type 菜品类别
     * @return
     */
    public List<Menu> findByType(String type) {
        List<Menu> list = map.entrySet().stream()
                      .filter(p -> p.getValue().getType().equals(type))
                      .map(p -> p.getValue())
                      .collect(Collectors.toList());
        return list;
    }

    //根据菜品id修改菜品价格
    public void replacePrice(String mid, double price) {
        map.replace(mid, new Menu(mid, map.get(mid).getName(), map.get(mid).getType(), map.get(mid).getDateIssued(), price, map.get(mid).getMonthlySales(), map.get(mid).getTotalQuantity()));
    }

    //根据菜品id查询菜品价格
    public double showPrice(String mid) {
        return map.get(mid).getPrice();
    }

    //根据菜品类别显示所有菜品
    public void showType() {
        map.entrySet().stream()
                      .sorted((p1,p2) -> p1.getValue().getType().compareTo(p2.getValue().getType()))
                      .collect(Collectors.toList())
                      .forEach(System.out::println);
    }

    //根据菜品id修改菜品数量
    public void replaceTotalQuantity(String mid, int totalQuantity) {
        map.replace(mid, new Menu(mid, map.get(mid).getName(), map.get(mid).getType(), map.get(mid).getDateIssued(), map.get(mid).getPrice(), map.get(mid).getMonthlySales(), map.get(mid).getTotalQuantity() - totalQuantity));
    }

}

管理员类(管理员id,账号,密码)

public class Administrator {

    private String aid;
    private String account;
    private String password;

    public Administrator() {
        super();
        // TODO Auto-generated constructor stub
    }

    public Administrator(String aid, String account, String password) {
        super();
        this.aid = aid;
        this.account = account;
        this.password = password;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((account == null) ? 0 : account.hashCode());
        result = prime * result + ((aid == null) ? 0 : aid.hashCode());
        result = prime * result + ((password == null) ? 0 : password.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Administrator other = (Administrator) obj;
        if (account == null) {
            if (other.account != null)
                return false;
        } else if (!account.equals(other.account))
            return false;
        if (aid == null) {
            if (other.aid != null)
                return false;
        } else if (!aid.equals(other.aid))
            return false;
        if (password == null) {
            if (other.password != null)
                return false;
        } else if (!password.equals(other.password))
            return false;
        return true;
    }

    @Override
    public String toString() {
        return "Administrator [aid=" + aid + ", account=" + account + ", password=" + password + "]";
    }

    public String getAid() {
        return aid;
    }

    public void setAid(String aid) {
        this.aid = aid;
    }

    public String getAccount() {
        return account;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

}
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class AdministratorManager implements DAO<Administrator> {

    static Map<String, Administrator> map = new HashMap<String, Administrator>();

    static {
        map.put("admin", new Administrator("admin", "admin123", "12345"));
    }

    public void insert(Administrator t) {
        map.put(t.getAid(), t);
    }

    @Override
    public Administrator findById(String id) {
        return map.get(id);
    }

    @Override
    public List<Administrator> findAll() {
        List<Administrator> list = map.entrySet().stream()
                  .map(e -> e.getValue())
                  .collect(Collectors.toList());
        return list;
    }

    @Override
    public void delete(String id) {
        map.remove(id);
    }

}

客户类(客户id,客户名,性别,密码,送餐地址,手机号,创建时间)

import java.util.Date;

public class Client {

    private String cid;
    private String name;
    private String sex;
    private String password;
    private String address;
    private String phone;
    private Date creationTime;

    public Client() {
        super();
        // TODO Auto-generated constructor stub
    }

    public Client(String cid, String name, String sex, String password, String address, String phone, Date creationTime) {
        super();
        this.cid = cid;
        this.name = name;
        this.sex = sex;
        this.password = password;
        this.address = address;
        this.phone = phone;
        this.creationTime = creationTime;
    }

    public String getCid() {
        return cid;
    }

    public void setCid(String cid) {
        this.cid = cid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public Date getCreationTime() {
        return creationTime;
    }

    public void setCreationTime(Date creationTime) {
        this.creationTime = creationTime;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((address == null) ? 0 : address.hashCode());
        result = prime * result + ((cid == null) ? 0 : cid.hashCode());
        result = prime * result + ((creationTime == null) ? 0 : creationTime.hashCode());
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        result = prime * result + ((password == null) ? 0 : password.hashCode());
        result = prime * result + ((phone == null) ? 0 : phone.hashCode());
        result = prime * result + ((sex == null) ? 0 : sex.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Client other = (Client) obj;
        if (address == null) {
            if (other.address != null)
                return false;
        } else if (!address.equals(other.address))
            return false;
        if (cid == null) {
            if (other.cid != null)
                return false;
        } else if (!cid.equals(other.cid))
            return false;
        if (creationTime == null) {
            if (other.creationTime != null)
                return false;
        } else if (!creationTime.equals(other.creationTime))
            return false;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        if (password == null) {
            if (other.password != null)
                return false;
        } else if (!password.equals(other.password))
            return false;
        if (phone == null) {
            if (other.phone != null)
                return false;
        } else if (!phone.equals(other.phone))
            return false;
        if (sex == null) {
            if (other.sex != null)
                return false;
        } else if (!sex.equals(other.sex))
            return false;
        return true;
    }

    @Override
    public String toString() {
        return "Client [cid=" + cid + ", name=" + name + ", sex=" + sex + ", password=" + password + ", address="
                + address + ", phone=" + phone + ", creationTime=" + creationTime + "]";
    }

}
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.stream.Collectors;

public class ClientManager implements DAO<Client> {

    static Map<String, Client> map = new HashMap<String, Client>();

    static {
        map.put("abc123", new Client("abc123", "张三", "男", "12345", "光谷金融港27栋9楼", "10086", new Date()));
    }

    @Override
    public void insert(Client t) {
        map.put(t.getCid(), t);
    }

    @Override
    public Client findById(String id) {
        return map.get(id);
    }

    @Override
    public List<Client> findAll() {
        List<Client> list = map.entrySet().stream()
                      .map(e -> e.getValue())
                      .collect(Collectors.toList());
        return list;
    }

    @Override
    public void delete(String id) {
        map.remove(id);
    }

    //修改密码
    public void replacePassword(Client client, String password) {
        client.setPassword(password);
        map.replace(client.getCid(), client);
    }

}

订单类(订单号,订单创建时间,菜品id,购买数,客户id,总价格,订单状态)

import java.util.Date;

public class Indent {

    private String no;
    private Date creationTime;
    private String mid;
    private int buyNumber;
    private String cid;
    private double totalPrice;
    private int orderStatus;

    public Indent() {
        super();
        // TODO Auto-generated constructor stub
    }

    public Indent(String no, Date creationTime, String mid, int buyNumber, String cid, double totalPrice,
            int orderStatus) {
        super();
        this.no = no;
        this.creationTime = creationTime;
        this.mid = mid;
        this.buyNumber = buyNumber;
        this.cid = cid;
        this.totalPrice = totalPrice;
        this.orderStatus = orderStatus;
    }

    public String getNo() {
        return no;
    }

    public void setNo(String no) {
        this.no = no;
    }

    public Date getCreationTime() {
        return creationTime;
    }

    public void setCreationTime(Date creationTime) {
        this.creationTime = creationTime;
    }

    public String getMid() {
        return mid;
    }

    public void setMid(String mid) {
        this.mid = mid;
    }

    public int getBuyNumber() {
        return buyNumber;
    }

    public void setBuyNumber(int buyNumber) {
        this.buyNumber = buyNumber;
    }

    public String getCid() {
        return cid;
    }

    public void setCid(String cid) {
        this.cid = cid;
    }

    public double getTotalPrice() {
        return totalPrice;
    }

    public void setTotalPrice(double totalPrice) {
        this.totalPrice = totalPrice;
    }

    public int getOrderStatus() {
        return orderStatus;
    }

    public void setOrderStatus(int orderStatus) {
        this.orderStatus = orderStatus;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + buyNumber;
        result = prime * result + ((cid == null) ? 0 : cid.hashCode());
        result = prime * result + ((creationTime == null) ? 0 : creationTime.hashCode());
        result = prime * result + ((mid == null) ? 0 : mid.hashCode());
        result = prime * result + ((no == null) ? 0 : no.hashCode());
        result = prime * result + orderStatus;
        long temp;
        temp = Double.doubleToLongBits(totalPrice);
        result = prime * result + (int) (temp ^ (temp >>> 32));
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Indent other = (Indent) obj;
        if (buyNumber != other.buyNumber)
            return false;
        if (cid == null) {
            if (other.cid != null)
                return false;
        } else if (!cid.equals(other.cid))
            return false;
        if (creationTime == null) {
            if (other.creationTime != null)
                return false;
        } else if (!creationTime.equals(other.creationTime))
            return false;
        if (mid == null) {
            if (other.mid != null)
                return false;
        } else if (!mid.equals(other.mid))
            return false;
        if (no == null) {
            if (other.no != null)
                return false;
        } else if (!no.equals(other.no))
            return false;
        if (orderStatus != other.orderStatus)
            return false;
        if (Double.doubleToLongBits(totalPrice) != Double.doubleToLongBits(other.totalPrice))
            return false;
        return true;
    }

    @Override
    public String toString() {
        return "Indent [no=" + no + ", creationTime=" + creationTime + ", mid=" + mid + ", buyNumber=" + buyNumber
                + ", cid=" + cid + ", totalPrice=" + totalPrice + ", orderStatus=" + orderStatus + "]";
    }

}
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class IndentManager implements DAO<Indent> {

    static Map<String, Indent> map = new HashMap<String, Indent>();

    @Override
    public void insert(Indent t) {
        map.put(t.getNo(), t);
    }

    @Override
    public Indent findById(String id) {
        return map.get(id);
    }

    @Override
    public List<Indent> findAll() {
        List<Indent> list = map.entrySet().stream()
                      .map(e -> e.getValue())
                      .collect(Collectors.toList());
        return list;
    }

    @Override
    public void delete(String no) {
        map.remove(no);
    }

    //根据id修改订单状态(0:未支付 1:已支付 2:配送中 3:已完成)
    public void replaceOrderStatus(String no, int orderStatus) {
        map.replace(no, new Indent(map.get(no).getNo(), map.get(no).getCreationTime(), map.get(no).getMid(), map.get(no).getBuyNumber(), map.get(no).getCid(), map.get(no).getTotalPrice(), orderStatus));
    }

    //查看当前登录客户的所有订单
    public void showIndent(String cid) {
        map.entrySet().stream()
                      .filter(p -> p.getValue().getCid().equals(cid))
                      .collect(Collectors.toList())
                      .forEach(System.out::println);
    }

}

主体实现

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

public class MySystem {

    static MenuManager menu = new MenuManager();
    static ClientManager client = new ClientManager();
    static IndentManager indent = new IndentManager();

    public static void p(Object s) {
        System.out.println(s);
    }

    public static void administratorPanel() {
        p("\t管理员系统面板\t");
        p("\t①添加菜品\t");
        p("\t②查看所有菜品信息(包含分页功能)\t");
        p("\t③查看指定类别的菜品信息\t");
        p("\t④根据菜品id修改菜品价格\t");
        p("\t⑤删除指定id的菜品\t");
        p("\t⑥添加客户\t");
        p("\t⑦查看客户列表\t");
        p("\t⑧删除指定id的客户\t");
        p("\t⑨订单列表显示\t");
        p("\t⑩根据订单id修改订单状态\t");
        p("\t⑪退出\t");

        p("请输入数字1~11,分别使用相应的功能");
        Scanner sc = new Scanner(System.in);
        switch (sc.nextInt()) {
        case 1:
            p("请按照顺序输入:菜品id/菜品名/菜品类型/上架时间/单价/月销售/总数量");
            menu.insert(new Menu(sc.next(), sc.next(), sc.next(), new Date(), sc.nextDouble(), sc.nextInt(), sc.nextInt()));
            administratorPanel();
            break;

        case 2:
            p("请输入需要查询的页数");
            int pageNow = sc.nextInt();
            p("请输入需要查询的页面数据条数");
            int pageSize = sc.nextInt();
            List<Menu> list = menu.findAll();
            list.stream()
                .skip((pageNow - 1) * pageSize)
                .limit(pageSize)
                .forEach(System.out::println);
            administratorPanel();
            break;

        case 3:
            p("请输入菜品类别");
            String type = sc.nextLine();
            menu.findByType(type).stream()
                                 .forEach(System.out::println);
            administratorPanel();
            break;

        case 4:
            p("请输入菜品id和修改后的菜品价格");
            String mid = sc.next();
            double price = sc.nextDouble();
            menu.replacePrice(mid, price);
            administratorPanel();
            break;

        case 5:
            p("请输入要删除菜品的id");
            String mid2 = sc.next();
            menu.delete(mid2);
            administratorPanel();
            break;

        case 6:
            p("添加客户,请按照顺序输入客户信息:客户id,客户名,性别,密码,送餐地址,手机号,创建时间");
            client.insert(new Client(sc.next(), sc.next(), sc.next(), sc.next(), sc.next(), sc.next(), new Date()));
            administratorPanel();
            break;

        case 7:
            List<Client> clientList = client.findAll();
            for (Client c : clientList) {
                System.out.println(c);
            }
            administratorPanel();
            break;

        case 8:
            p("请输入要删除客户的id");
            String cid = sc.next();
            client.delete(cid);
            administratorPanel();
            break;

        case 9:
            List<Indent> indentList = indent.findAll();
            for (Indent i : indentList) {
                System.out.println(i);
            }
            administratorPanel();
            break;

        case 10:
            p("请根据订单id修改订单状态orderStatus");
            p("请输入订单id");
            String no = sc.next();
            p("请输入要修改的订单状态(0:未支付 1:已支付 2:配送中 3:已完成)");
            int orderStatus = sc.nextInt();
            indent.replaceOrderStatus(no, orderStatus);
            administratorPanel();
            break;

        case 11:
            login();
            break;

        default:
            administratorPanel();
            break;
        }

    }

    public static void clientPanel(Client c) {
        p("\t用户系统面板\t");
        p("\t①显示所有菜品(按菜品销量从高到低排序输出)\t");
        p("\t②根据菜品类别显示所有菜品\t");
        p("\t③查看所有订单(当前登录用户的)\t");
        p("\t④修改密码(当前登录用户的)\t");
        p("\t⑤个人信息显示\t");
        p("\t⑥退出登录\t");
        p("请输入数字1~6,分别使用相应的功能");
        MenuManager menu = new MenuManager();
        Scanner sc = new Scanner(System.in);
        switch (sc.nextInt()) {
        case 1:
            //显示所有菜品(按菜品销量从高到低排序输出)
            menu.findAll().stream()
                          .forEach(System.out::println);
            p("请输入菜品id");
            String mid = sc.next();
            p("请输入购买数量");
            int buyNumber = sc.nextInt();
            //修改菜品总数
            menu.replaceTotalQuantity(mid, buyNumber);
            //查询菜品价格
            double price = menu.showPrice(mid);
            indent.insert(new Indent(c.getCid()+mid, new Date(), mid, buyNumber, c.getCid(), price * buyNumber, 0));
            clientPanel(c);
            break;

        case 2:
            menu.showType();
            clientPanel(c);
            break;

        case 3:
            indent.showIndent(c.getCid());
            clientPanel(c);
            break;

        case 4:
            System.out.println("请输入修改后的密码");
            String password = sc.next();
            client.replacePassword(c, password);
            clientPanel(c);
            break;

        case 5:
            System.out.println(c.toString());
            clientPanel(c);
            break;

        case 6:
            login();
            break;

        default :
            clientPanel(c);
        }

    }

    public static void login() {
        AdministratorManager am = new AdministratorManager();
        ClientManager cm = new ClientManager();
        Scanner sc = new Scanner(System.in);
        p("请输入id");
        String id = sc.nextLine();
        p("请输入密码");
        String password = sc.nextLine();
        if(am.map.get(id) != null && am.map.get(id).getPassword().equals(password)) {
            p("管理员登录成功");
            administratorPanel();
        } else if(cm.map.get(id) != null && cm.map.get(id).getPassword().equals(password)) {
            p("客户登录成功");
            clientPanel(cm.map.get(id));
        } else {
            p("登录失败,请重新登录");
            login();
        }
    }

    public static void main(String[] args) {
        login();
    }

}

最后

欢迎关注公众号:前程有光,领取一线大厂Java面试题总结+各知识点学习思维导+一份300页pdf文档的Java核心知识点总结!这些资料的内容都是面试时面试官必问的知识点,篇章包括了很多知识点,其中包括了有基础知识、Java集合、JVM、多线程并发、spring原理、微服务、Netty 与RPC 、Kafka、日记、设计模式、Java算法、数据库、Zookeeper、分布式缓存、数据结构等等。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK