代码如下(示例):
菜品类
import java.util.Date;
public class Greens {
// 菜品类(菜品id,菜品名,菜品类型,上架时间,单价,月销售,总数量)
private String id;
private String name;
private String type;
private Date appearTime;
private double price;
private int monthSell;
private int allCount;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
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 getAppearTime() {
return appearTime;
}
public void setAppearTime(Date appearTime) {
this.appearTime = appearTime;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getMonthSell() {
return monthSell;
}
public void setMonthSell(int monthSell) {
this.monthSell = monthSell;
}
public int getAllCount() {
return allCount;
}
public void setAllCount(int allCount) {
this.allCount = allCount;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + allCount;
result = prime * result + ((appearTime == null) ? 0 : appearTime.hashCode());
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + monthSell;
result = prime * result + ((name == null) ? 0 : name.hashCode());
long temp;
temp = Double.doubleToLongBits(price);
result = prime * result + (int) (temp ^ (temp >>> 32));
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;
Greens other = (Greens) obj;
if (allCount != other.allCount)
return false;
if (appearTime == null) {
if (other.appearTime != null)
return false;
} else if (!appearTime.equals(other.appearTime))
return false;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (monthSell != other.monthSell)
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 (type == null) {
if (other.type != null)
return false;
} else if (!type.equals(other.type))
return false;
return true;
}
@Override
public String toString() {
return "Greens [id=" + id + ", name=" + name + ", type=" + type + ", appearTime=" + appearTime + ", price="
+ price + ", monthSell=" + monthSell + ", allCount=" + allCount + "]";
}
public Greens(String id, String name, String type, Date appearTime, double price, int monthSell, int allCount) {
super();
this.id = id;
this.name = name;
this.type = type;
this.appearTime = appearTime;
this.price = price;
this.monthSell = monthSell;
this.allCount = allCount;
}
public Greens() {
super();
}
}
顾客类
import java.util.Date;
public class Customer {
// 客户类(客户id,客户名,性别,密码,送餐地址,手机号,创建时间)
private String id;
private String name;
private String sex;
private String password;
private String address;
private String phoneNumber;
private Date setTime;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
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 getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public Date getSetTime() {
return setTime;
}
public void setSetTime(Date setTime) {
this.setTime = setTime;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((address == null) ? 0 : address.hashCode());
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((password == null) ? 0 : password.hashCode());
result = prime * result + ((phoneNumber == null) ? 0 : phoneNumber.hashCode());
result = prime * result + ((setTime == null) ? 0 : setTime.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;
Customer other = (Customer) obj;
if (address == null) {
if (other.address != null)
return false;
} else if (!address.equals(other.address))
return false;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
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 (phoneNumber == null) {
if (other.phoneNumber != null)
return false;
} else if (!phoneNumber.equals(other.phoneNumber))
return false;
if (setTime == null) {
if (other.setTime != null)
return false;
} else if (!setTime.equals(other.setTime))
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 "Customer [id=" + id + ", name=" + name + ", sex=" + sex + ", password=" + password + ", address="
+ address + ", phoneNumber=" + phoneNumber + ", setTime=" + setTime + "]";
}
public Customer(String id, String name, String sex, String password, String address, String phoneNumber,
Date setTime) {
super();
this.id = id;
this.name = name;
this.sex = sex;
this.password = password;
this.address = address;
this.phoneNumber = phoneNumber;
this.setTime = setTime;
}
public Customer() {
super();
}
}
订单类
import java.util.Date;
public class Indent {
// 订单类(订单号,订单创建时间,菜品id,购买数,客户id,总价格,订单状态)
private String indentNumber;
private Date indentSetTime;
private String greensId;
private int buyNumber;
private String customerId;
private double allPrice;
private int indentState;
public String getIndentNumber() {
return indentNumber;
}
public void setIndentNumber(String indentNumber) {
this.indentNumber = indentNumber;
}
public Date getIndentSetTime() {
return indentSetTime;
}
public void setIndentSetTime(Date indentSetTime) {
this.indentSetTime = indentSetTime;
}
public String getGreensId() {
return greensId;
}
public void setGreensId(String greensId) {
this.greensId = greensId;
}
public int getBuyNumber() {
return buyNumber;
}
public void setBuyNumber(int buyNumber) {
this.buyNumber = buyNumber;
}
public String getCustomerId() {
return customerId;
}
public void setCustomerId(String customerId) {
this.customerId = customerId;
}
public double getAllPrice() {
return allPrice;
}
public void setAllPrice(double allPrice) {
this.allPrice = allPrice;
}
public int getIndentState() {
return indentState;
}
public void setIndentState(int indentState) {
this.indentState = indentState;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
long temp;
temp = Double.doubleToLongBits(allPrice);
result = prime * result + (int) (temp ^ (temp >>> 32));
result = prime * result + buyNumber;
result = prime * result + ((customerId == null) ? 0 : customerId.hashCode());
result = prime * result + ((greensId == null) ? 0 : greensId.hashCode());
result = prime * result + ((indentNumber == null) ? 0 : indentNumber.hashCode());
result = prime * result + ((indentSetTime == null) ? 0 : indentSetTime.hashCode());
result = prime * result + indentState;
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 (Double.doubleToLongBits(allPrice) != Double.doubleToLongBits(other.allPrice))
return false;
if (buyNumber != other.buyNumber)
return false;
if (customerId == null) {
if (other.customerId != null)
return false;
} else if (!customerId.equals(other.customerId))
return false;
if (greensId == null) {
if (other.greensId != null)
return false;
} else if (!greensId.equals(other.greensId))
return false;
if (indentNumber == null) {
if (other.indentNumber != null)
return false;
} else if (!indentNumber.equals(other.indentNumber))
return false;
if (indentSetTime == null) {
if (other.indentSetTime != null)
return false;
} else if (!indentSetTime.equals(other.indentSetTime))
return false;
if (indentState != other.indentState)
return false;
return true;
}
@Override
public String toString() {
return "Indent [indentNumber=" + indentNumber + ", indentSetTime=" + indentSetTime + ", greensId=" + greensId
+ ", buyNumber=" + buyNumber + ", customerId=" + customerId + ", allPrice=" + allPrice
+ ", indentState=" + indentState + "]";
}
public Indent(String indentNumber, Date indentSetTime, String greensId, int buyNumber, String customerId,
double allPrice, int indentState) {
super();
this.indentNumber = indentNumber;
this.indentSetTime = indentSetTime;
this.greensId = greensId;
this.buyNumber = buyNumber;
this.customerId = customerId;
this.allPrice = allPrice;
this.indentState = indentState;
}
public Indent() {
super();
}
}
管理员类
public class Manager {
// 管理员类(管理员id,账号,密码)
private String id;
private String idName;
private String password;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getIdName() {
return idName;
}
public void setIdName(String idName) {
this.idName = idName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((idName == null) ? 0 : idName.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;
Manager other = (Manager) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (idName == null) {
if (other.idName != null)
return false;
} else if (!idName.equals(other.idName))
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 "Manager [id=" + id + ", idName=" + idName + ", password=" + password + "]";
}
public Manager(String id, String idName, String password) {
super();
this.id = id;
this.idName = idName;
this.password = password;
}
public Manager() {
super();
}
}
顾客购买类
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
public class CustomerBuy {
List<Greens> listcb = new ArrayList();
List<Indent> listcbI = new ArrayList();
ManagerGreens mg = new ManagerGreens();
{
mg.adds();
}
// 1.显示所有菜品
public List<Greens> showAll() {
mg.findAll().sort((g1, g2) -> g2.getMonthSell() - g1.getMonthSell());
return mg.findAll();
}
// 1-1.点餐
public boolean BuyGreens(String id, int num, Customer cnow) {
double allprice = 0;
for (Greens G : mg.findAll()) {
if (G.getId().equals(id)) {
allprice = G.getPrice() * num;
}
}
listcbI.add(new Indent(getIndentNumber(), getTime(), id, num, cnow.getId(), allprice, 1));
listcbI.forEach(System.out::println);
return true;
}
// 2.根据菜品类别显示所有菜品
public List<Greens> showGreens(String type) {
for (Greens G : mg.findAll()) {
if (G.getType().equals(type)) {
listcb.add(G);
}
}
listcb.forEach(System.out::println);
return listcb;
}
// 3.查看所有订单(当前用户)
public List<Indent> showIndent() {
listcbI.forEach(System.out::println);
return listcbI;
}
// 4.修改密码
public boolean changpas(Customer cnow, String nowpas) {
cnow.setPassword(nowpas);
System.out.println("修改成功");
return true;
}
// 5.个人信息显示
public Customer showself(Customer cnow) {
System.out.println(cnow);
return cnow;
}
// 随机生成8位订单号
public String getIndentNumber() {
Random r = new Random();
String s = "";
for (int i = 0; i < 8; i++) {
int j = r.nextInt(10);
s = s + j;
}
return s;
}
// 获取当前时间
public Date getTime() {
Date date = new Date(System.currentTimeMillis());
return date;
}
// 顾客界面
static public void CustomerInterface() {
System.out.println("1.显示所有菜品(按菜品销量从高到低排序输出)");
System.out.println("2.点餐(输入菜品id和购买数量)");
System.out.println("3.根据菜品类别显示所有菜品");
System.out.println("4.查看所有订单(当前登录用户的)");
System.out.println("5.修改密码");
System.out.println("6.个人信息显示");
System.out.println("7.退出");
}
static void exit() {
System.out.println("退出成功");
System.exit(0);
}
}
接口类
import java.util.List;
public interface DAO<T> {
// 添加T类型对象到Map中
public void insert(T t) ;
// 根据id获取Map中的元素
public T findById(String id);
// 返回map中的所有元素值集
public List<T> findAll();
// 根据id删除Map中的元素
public void delete(String id);
}
管理顾客类
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;
public class ManagerCustomer implements DAO<Customer> {
// 添加list用来存放顾客的信息
List<Customer> listc = new ArrayList();
//添加一部分顾客
{
adds();
}
public void adds() {
listc.add(new Customer("1","softeem","男","123456","softeem大楼","123456789",new Date(System.currentTimeMillis())));
listc.add(new Customer("2","softeem1","女","123456","softeem大楼","123456789",new Date(System.currentTimeMillis())));
listc.add(new Customer("3","softeem2","男","123456","softeem大楼","123456789",new Date(System.currentTimeMillis())));
listc.add(new Customer("4","softeem3","女","123456","softeem大楼","123456789",new Date(System.currentTimeMillis())));
listc.add(new Customer("5","softeem4","男","123456","softeem大楼","123456789",new Date(System.currentTimeMillis())));
}
// 6.添加顾客
@Override
public void insert(Customer c) {
listc.add(c);
System.out.println("添加成功");
}
// 7.查看客户列表
@Override
public List<Customer> findAll() {
if (listc.size() != 0) {
return listc;
} else {
return null;
}
}
// 8.删除指定id的客户
@Override
public void delete(String id) {
if(findById(id).getId()!=null) {
listc.remove(findById(id));
System.out.println("删除成功");
}
else {
System.out.println("没有该用户");
}
}
//根据id找顾客
@Override
public Customer findById(String id) {
for(Customer c:listc) {
if(c.getId().equals(id)) {
return c;
}
}
return null;
}
}
管理菜品类
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class ManagerGreens implements DAO<Greens> {
// 创建一个集合用来存储菜品
List<Greens> list = new ArrayList();
public void adds() {
// 先添加一部分菜品
list.add(new Greens("1", "海鲜面", "面条", new Date(System.currentTimeMillis()), 15, 1000, 300));
list.add(new Greens("2", "热干面", "面条", new Date(System.currentTimeMillis()), 5, 500, 260));
list.add(new Greens("3", "卤肉面", "面条", new Date(System.currentTimeMillis()), 10, 500, 700));
list.add(new Greens("4", "三线面", "面条", new Date(System.currentTimeMillis()), 8, 30, 4500));
list.add(new Greens("11", "蛋炒饭", "饭", new Date(System.currentTimeMillis()), 9, 40, 1800));
list.add(new Greens("12", "卤肉饭", "饭", new Date(System.currentTimeMillis()), 10, 60, 2300));
list.add(new Greens("13", "叉烧饭", "饭", new Date(System.currentTimeMillis()), 12, 1000, 3400));
list.add(new Greens("14", "海鲜饭", "饭", new Date(System.currentTimeMillis()), 13, 80, 22000));
list.add(new Greens("21", "鲜肉水饺", "水饺", new Date(System.currentTimeMillis()), 17, 660, 2200));
list.add(new Greens("22", "三鲜水饺", "水饺", new Date(System.currentTimeMillis()), 18, 340, 50));
list.add(new Greens("23", "猪肉水饺", "水饺", new Date(System.currentTimeMillis()), 19, 1230, 60));
list.add(new Greens("24", "白菜水饺", "水饺", new Date(System.currentTimeMillis()), 12, 130, 70));
}
// 1.添加菜品
@Override
public void insert(Greens g) {
list.add(g);
System.out.println("添加成功");
}
// 2.查看所有菜品(包含分页)
@Override
public List<Greens> findAll() {
if (list.size() != 0) {
return list;
} else {
return null;
}
}
// 2.1分页
public List<Greens> findByPage(int pageNow, int pageSize) {
// 新建一个list
List<Greens> list1 = new ArrayList();
// 数据总长度
int Alllength = list.size();
// 一共多少页
int page = 0;
if (Alllength == 0) {
System.out.println("没有菜品");
} else {
if (Alllength % pageSize != 0) {
page = Alllength / pageSize + 1;
} else if (Alllength % pageSize == 0) {
page = Alllength / pageSize;
} else if (Alllength < pageSize) {
page = 1;
}
}
if (page < pageNow) {
return null;
}
// 取出菜品放到另一个数组中
list1 = list.subList((pageNow - 1) * pageSize, pageNow * pageSize);
return list1;
}
// 3.查看指定类别的菜品信息
public List<Greens> findBytype(String type) {
// 新建一个list集合来接收相同的菜品
List list2 = new ArrayList();
for (Greens G : list) {
if (G.getType().equals(type)) {
list2.add(G);
}
}
list2.forEach(System.out::println);
return list2;
}
// 4.根据菜品id修改菜品价格
public void changPrice(String id, int afterprice) {
if (findById(id) != null) {
findById(id).setPrice(afterprice);
System.out.println("修改成功");
} else {
System.out.println("没有该菜品");
}
}
// 5.删除指定id的菜品
public void deleteGreens(String id) {
if (findById(id).getId() != null) {
list.remove(findById(id));
System.out.println("删除成功");
} else {
System.out.println("没有该菜品");
}
}
//
// 根据id找对应的菜品
@Override
public Greens findById(String id) {
for (Greens G : list) {
if (G.getId().equals(id)) {
return G;
}
}
return null;
}
@Override
public void delete(String id) {
// TODO Auto-generated method stub
}
}
管理订单类
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class ManagerIndent implements DAO<Indent> {
// 新建list来存储订单
List<Indent> listI = new ArrayList();
// 添加订单
@Override
public void insert(Indent t) {
listI.add(t);
}
// 根据订单id找订单
@Override
public Indent findById(String id) {
for (Indent i : listI) {
if (i.getIndentNumber().equals(id)) {
return i;
}
}
return null;
}
// 9.订单列表显示
@Override
public List<Indent> findAll() {
if (listI.size() != 0) {
listI.forEach(System.out::println);
return listI;
}
System.out.println("没有订单");
return null;
}
// 10.根据订单id找修改状态
public boolean Change(String id, int afterstatus) {
if (findById(id) != null) {
findById(id).setIndentState(afterstatus);
System.out.println("修改成功");
return true;
} else if (findById(id) == null) {
System.out.println("修改失败");
return false;
}
return false;
}
@Override
public void delete(String id) {
// TODO Auto-generated method stub
}
}
管理界面类
import java.util.ArrayList;
import java.util.List;
public class ManagerInterface {
// 新建两个管理员
List<Manager> list = new ArrayList();
{
adds();
}
public void adds() {
list.add(new Manager("admin", "softeemadmin", "123456"));
list.add(new Manager("admin1", "softeemadmin1", "123456"));
}
public List<Manager> findall() {
// list.forEach(System.out::println);
return list;
}
// 管理员界面
static public void interF() {
System.out.println("1添加菜品");
System.out.println("2查看所有菜品信息(包含分页功能)");
System.out.println("3查看指定类别的菜品信息");
System.out.println("4根据菜品id修改菜品价格");
System.out.println("5删除指定id的菜品");
System.out.println("6添加客户");
System.out.println("7查看客户列表");
System.out.println("8删除指定id的客户");
System.out.println("9订单列表显示");
System.out.println("10根据订单id修改订单状态");
System.out.println("11退出");
}
// 11.退出
static public void exit() {
System.exit(0);
}
}
测试类
import java.util.Date;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
ManagerInterface Mi = new ManagerInterface();
ManagerGreens Mg = new ManagerGreens();
Mg.adds();
ManagerCustomer Mc = new ManagerCustomer();
CustomerBuy Cb = new CustomerBuy();
ManagerIndent Md = new ManagerIndent();
System.out.println("1.普通用户登录");
System.out.println("2.管理员登录");
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
// 请输入是管理员登录还是普通用户登录
switch (i) {
case 1:
Customer cnow = new Customer();
System.out.println("请先登录");
System.out.println("请输入账号");
String id1 = sc.next();
System.out.println("请输入密码");
String pas1 = sc.next();
for (Customer c : Mc.findAll()) {
if (c.getId().equals(id1) && c.getPassword().equals(pas1)) {
cnow = c;
System.out.println("登录成功");
System.out.println(cnow);
}
}
if (cnow.getId() == null) {
System.out.println("登录失败");
return;
}
for (;;) {
Cb.CustomerInterface();
System.out.println("请输入要进行的操作");
int j = sc.nextInt();
switch (j) {
// 显示菜品
case 1:
Cb.showAll().forEach(System.out::println);
;
break;
// 点餐
case 2:
Cb.BuyGreens("1", 10, cnow);
break;
// 根据菜品类品显示
case 3:
Cb.showGreens("面条");
break;
// 查看所有订单
case 4:
Cb.showIndent();
break;
// 修改密码
case 5:
Cb.changpas(cnow, "777777");
break;
// 个人信息显示
case 6:
Cb.showself(cnow);
break;
// 退出
case 7:
Cb.exit();
break;
}
}
case 2:
Manager mnow = new Manager();
System.out.println("请先登录");
System.out.println("请输入账号");
String id2 = sc.next();
System.out.println("请输入密码");
String pas2 = sc.next();
for (Manager m : Mi.findall()) {
if (m.getId().equals(id2) && m.getPassword().equals(pas2)) {
mnow = m;
System.out.println("登录成功");
System.out.println(mnow);
}
}
if (mnow.getId() == null) {
System.out.println("登录失败");
return;
}
for (;;) {
Mi.interF();
System.out.println("请输入要进行的操作");
int m = sc.nextInt();
switch (m) {
// 添加菜品
case 1:
Mg.insert(new Greens("100", "土豆肉丝炒饭", "饭", new Date(System.currentTimeMillis()), 10, 45, 5000));
break;
// 查看所有菜品
case 2:
Mg.findAll().forEach(System.out::println);
;
break;
// 查看指定类别的菜品信息
case 3:
Mg.findBytype("面条");
break;
// 根据菜品id修改菜品价格
case 4:
Mg.changPrice("1", 300);
break;
// 删除指定id的菜品
case 5:
Mg.deleteGreens("2");
break;
// 添加客户
case 6:
Mc.insert(new Customer("10", "softeem10", "男", "123456", "softeem大楼", "123456789",
new Date(System.currentTimeMillis())));
break;
// 查看客户列表
case 7:
Mc.findAll().forEach(System.out::println);
;
break;
// 删除指定id的用户
case 8:
Mc.delete("2");
break;
// 订单列表显示
case 9:
Md.findAll();
break;
// 根据订单id修改订单状态
case 10:
Md.Change("id", 1);
break;
// 退出
case 11:
Mi.exit();
break;
}
}
}
}
}