手擼一個外賣點餐系統後臺,可以寫上簡歷的實戰專案!

前程有光發表於2020-11-30

外賣點餐系統的實現

實體類的編寫

(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、分散式快取、資料結構等等。

相關文章