乐趣区

关于java:手撸一个外卖点餐系统后台可以写上简历的实战项目

外卖点餐零碎的实现

实体类的编写

(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、分布式缓存、数据结构等等。

退出移动版