您的当前位置:首页正文

java实现一个点餐系统

2024-04-12 来源:步旅网
java实现⼀个点餐系统

转载于blog.csdn.net/weixin_44219955项⽬⼤体框架

菜品类(菜品id,菜品名,菜品类型,上架时间,单价,⽉销售,总数量)管理员类(管理员id,账号,密码)

客户类(客户id,客户名,性别,密码,送餐地址,⼿机号,创建时间)订单类(订单号,订单创建时间,菜品id,购买数,客户id,总价格,订单状态)实现不同⾓⾊⽤户登录系统

(1) 管理员登录系统看到如下菜单:① 添加菜品

② 查看所有菜品信息(包含分页功能)③ 查看指定类别的菜品信息④ 根据菜品id修改菜品价格⑤ 删除指定id的菜品⑥ 添加客户

⑦ 查看客户列表⑧ 删除指定id的客户⑨ 订单列表显⽰

⑩ 根据订单id修改订单状态11 退出

(2) 客户登录看到如下菜单:

① 显⽰所有菜品(按菜品销量从⾼到低排序输出)-------->点餐(输⼊菜品id和购买数量)② 根据菜品类别显⽰所有菜品③ 查看所有订单(当前登录⽤户的)④ 修改密码(当前登录⽤户的)⑤ 个⼈信息显⽰

建⽴接⼝来进⾏对四个管理类进⾏操作

可能有些⼈会有这样的疑问:为什么⼀定要创建接⼝呢?不是可以直接在类中定义相对应的⽅法,这样看来接⼝好像显得有点多余。但我们要知道接⼝在⼀定层⾯上是起⼀种规范的作⽤,在⽇常开发中,⼀个项⽬可能需要⼏个⼈同时开发最后再进⾏综合。所以在这时接⼝的作⽤就显得尤为重要。

举个例⼦:⼀个项⽬同时由两个⼈开发不同的模块,如果不定义⼀些接⼝,那么两个⼈写的代码就没有良好的分离性,不能够很好的进⾏融合,做到即拿即⽤的效果。

import java.util.List;public interface DAO { void insert(T t);

T findById(String id); List findAll();

void delete(String id);}

Admin类

public class Admin { private String aID;

private String account; private String apwd; public Admin() {

// TODO Auto-generated constructor stub }

public Admin(String aID, String account, String apwd) { super();

this.aID = aID;

this.account = account; this.apwd = apwd; }

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 getApwd() { return apwd; }

public void setApwd(String apwd) { this.apwd = apwd; }

@Override

public String toString() {

return \"Admin [aID=\" + aID + \ } }

Dishes类(菜品类)

import java.time.LocalDate;public class Dishes { private String dID; private String dname; private String dtype; private LocalDate dtime; private double price; private int dsales; private int dstocks;

public Dishes() {

// TODO Auto-generated constructor stub }

public Dishes(String dID, String dname, String dtype, LocalDate dtime, double price, int dsales, int dstocks) { super();

this.dID = dID;

this.dname = dname; this.dtype = dtype; this.dtime = dtime; this.price = price; this.dsales = dsales; this.dstocks = dstocks; }

public String getdID() { return dID; }

public void setdID(String dID) { this.dID = dID; }

public String getDname() { return dname; }

public void setDname(String dname) { this.dname = dname; }

public String getDtype() { return dtype; }

public void setDtype(String dtype) { this.dtype = dtype; }

public LocalDate getDtime() { return dtime; }

public void setDtime(LocalDate dtime) { this.dtime = dtime; }

public double getPrice() { return price; }

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

public int getDsales() { return dsales; }

public void setDsales(int dsales) { this.dsales = dsales; }

public int getDstocks() { return dstocks; }

public void setDstocks(int dstocks) { this.dstocks = dstocks; }

@Override

public String toString() {

return \"Dishes [菜品id:\" + dID + \菜品名:\" + dname + \菜品类型:\" + dtype + \上架时间:\" + dtime + \单价:\" + price + \⽉销量:\" + dsales + \总数量:\" + dstocks + \"]\"; }}

Order类

import java.time.LocalDateTime;public class Order { private String OrderID;

private LocalDateTime utime; private Dishes dishes; private int Ordernum; private String uID;

private Double Orderprice; private int OrderValue;

public Order() {

// TODO Auto-generated constructor stub }

public Order(String orderID, LocalDateTime utime, Dishes dishes, int ordernum, String uID, Double orderprice, int orderValue) { super();

OrderID = orderID; this.utime = utime; this.dishes = dishes; Ordernum = ordernum; this.uID = uID;

Orderprice = orderprice; OrderValue = orderValue; }

public String getOrderID() { return OrderID; }

public void setOrderID(String orderID) { OrderID = orderID; }

public LocalDateTime getUtime() { return utime; }

public void setUtime(LocalDateTime utime) { this.utime = utime; }

public Double getOrderprice() { return Orderprice; }

public void setOrderprice(Double orderprice) { Orderprice = orderprice; }

public Dishes getDishes() { return dishes; }

public void setDishes(Dishes dishes) { this.dishes = dishes; }

public int getOrdernum() { return Ordernum; }

public void setOrdernum(int ordernum) { Ordernum = ordernum; }

public String getuID() { return uID; }

public void setuID(String uID) { this.uID = uID; }

public int getOrderValue() { return OrderValue; }

public void setOrderValue(int orderValue) { OrderValue = orderValue; }

@Override

public String toString() {

return \"Order [OrderID=\" + OrderID + \ + \ }}

User类

import java.time.LocalDateTime;public class User { private String uID; private String uname; private String usex; private String upwd; private String uadress; private String utel;

private LocalDateTime utime;

public User() {

// TODO Auto-generated constructor stub }

public User(String uID, String uname, String usex, String upwd, String uadress, String utel, LocalDateTime utime) { super();

this.uID = uID;

this.uname = uname; this.usex = usex; this.upwd = upwd;

this.uadress = uadress; this.utel = utel; this.utime = utime; }

public String getuID() { return uID; }

public void setuID(String uID) { this.uID = uID; }

public String getUname() { return uname; }

public void setUname(String uname) { this.uname = uname; }

public String getUsex() { return usex; }

public void setUsex(String usex) { this.usex = usex; }

public String getUpwd() { return upwd; }

public void setUpwd(String upwd) { this.upwd = upwd; }

public String getUadress() { return uadress; }

public void setUadress(String uadress) { this.uadress = uadress; }

public String getUtel() { return utel; }

public void setUtel(String utel) { this.utel = utel; }

public LocalDateTime getUtime() { return utime; }

public void setUtime(LocalDateTime utime) { this.utime = utime; }

@Override

public String toString() {

return \"User [uID=\" + uID + \ + \ }}

这⾥需要注意的是,所定义的类中的变量最好设置为私有变量,这样更能够保证数据的安全性。但必须提供变量的操作接⼝。AdminSys

import java.time.LocalDate;

import java.time.LocalDateTime;//import java.util.ArrayList;import java.util.Collections;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.Scanner;//import java.util.Set;

public class AdminSys implements DAO { static Map map = new HashMap<>();// static Set keys = map.keySet(); UserSys u = new UserSys(); OrderSys o = new OrderSys(); DishesSys d = new DishesSys();

Scanner sc = new Scanner(System.in);

/**

* 添加菜品 */

public void addDishes() {

System.out.println(\"请输⼊您要添加的菜品:(按照:菜品ID/菜品名/菜品类型/单价/⽉销量/总数量)\"); String str = sc.next();

String[] info = str.split(\"/\"); //

if (info.length < 6) {

System.out.println(\"天啦撸,输⼊失败啦,请重新输⼊!\"); addDishes(); } else {

LocalDate dtime = LocalDate.now();

Dishes t = new Dishes(info[0], info[1], info[2], dtime, Double.parseDouble(info[3]), Integer.parseInt(info[4]), Integer.parseInt(info[5])); d.insert(t);

System.out.println(\"⼩主,恭喜你!添加成功了\"); } }

/**

* 查看所有菜品信息(包含分页功能) */

public void showAllDishes(int pageSize) { List list = d.findAll(); int start = 0;

//先写⼀个死循环,进⼊else后break掉 while (true) {

if (list.size() > (pageSize + start)) {

System.out.println(list.subList(start, pageSize + start)); } else {

System.out.println(list.subList(start, list.size())); break; }

start = start + pageSize; } }

/**

* 查看指定类别的菜品信息 * */

public void selecBytypeOfAdmin() {

System.out.println(\"请输⼊您要查询菜品的类别:\"); String typename = sc.next(); d.selectBytype(typename); }

/**

* 根据菜品id修改菜品价格 */

public void selectByDishesID() {

System.out.println(\"请输⼊您要查询的菜品id:\"); String id = sc.next();

Dishes dish = d.findById(id); if (dish == null) {

System.out.println(\"没有当前id的菜品呢\"); } else {

System.out.println(\"当前菜品为:\" + dish); System.out.println(\"请输⼊新的菜品单价:\"); double newprice = sc.nextDouble();

Dishes t = new Dishes(dish.getdID(), dish.getDname(), dish.getDtype(), dish.getDtime(), newprice, dish.getDsales(), dish.getDstocks()); d.insert(t);

System.out.println(\"修改成功\" + d.findById(t.getdID())); } }

/**

* 删除指定id的菜品 *

* @param args */

public void deleteDishes() {

System.out.println(\"请输⼊您要删除的菜品id\"); String id = sc.next(); d.delete(id);

System.out.println(\"删除成功!!\"); }

/**

* 添加客户 */

public void addUser() {

System.out.println(\"请输⼊您要添加的⽤户:按照(id/姓名/性别/密码/送餐地址/⼿机号)\"); String str = sc.next();

String[] info = str.split(\"/\"); if (info.length < 6) {

System.out.println(\"您输⼊的信息有误,请重新输⼊....\"); addUser(); } else {

LocalDateTime utime = LocalDateTime.now();

u.insert(new User(info[0], info[1], info[2], info[3], info[4], info[5], utime)); System.out.println(\"添加成功\"); }

}

/**

* 查看客户列表 */

public void findUser() {

List userlist = u.findAll(); for (User user : userlist) { System.out.println(user); } }

/**

* 根据id查找指定⽤户 */

public User findUserByid(String id) { return u.findById(id); }

/**

* 删除指定id的客户 */

public void deleteUserByAdmin() {

System.out.println(\"请输⼊您要删除的id:\"); String id = sc.next(); u.delete(id); }

/**

* 订单列表显⽰ */

public void showAllOrder() {

List allOrder = o.findAll(); for (Order order : allOrder) { System.out.println(order); } }

/**

* 根据订单id修改订单状态 */

public void changeOrderValue() {

System.out.println(\"请输⼊您要修改状态的订单id\"); String id = sc.next();

Order order = o.findById(id); if (order == null) {

System.out.println(\"没有当前id的订单,请检查输⼊\"); } else {

System.out.println(\"已找到当前id订单\" + order);

System.out.println(\"请输⼊您要修改的状态:0:未⽀付 1:已⽀付 2:配送中 3:已完成\"); int value = sc.nextInt();

Order t = new Order(order.getOrderID(), order.getUtime(), order.getDishes(), order.getOrdernum(), order.getuID(), order.getOrderprice(), value); o.insert(t);

System.out.println(\"修改成功了\"); }

} /**

* 显⽰所有菜品(按菜品销量从⾼到低排序输出) */

public void showAllDishesByUser() { List list = d.findAll();

Collections.sort(list, (p1, p2) -> p1.getDsales() - p2.getDsales()); System.out.println(list); }

/**

* 点餐(输⼊菜品id和购买数量) */

public void shopDishes(User user) { showAllDishesByUser();

System.out.println(\"请输⼊您要购买的id和数量:按照(id/数量)\"); String str = sc.next();

String[] info = str.split(\"/\");

// 判断输⼊是否符合要求,不符合则要求重新输⼊ if (info.length < 2) {

System.out.println(\"输⼊有误,请重新输⼊:\"); shopDishes(user); } else {

LocalDateTime l = LocalDateTime.now();

// String orderID, LocalDateTime utime, Dishes dishes, int ordernum, String uID, // Double orderprice,int orderValue

Order t = new Order(info[0], l, d.findById(info[0]), Integer.parseInt(info[1]), user.getuID(),

o.findById(info[0]).getOrderprice(), o.findById(info[0]).getOrderValue()); o.insert(t);

System.out.println(\"订单已⽣成\" + o.findById(info[0])); } }

/**

* 根据菜品类别显⽰所有菜品 */

public void ShowOfTypeByUser() {

System.out.println(\"请输⼊您要查找的类别:\"); String str = sc.next();

System.out.println(d.findByType(str)); }

/**

* 查看所有订单(当前登录⽤户的) */

public void showAllOrderByUser(User user) { List list = o.findByuId(user.getuID()); for (Order order : list) { System.out.println(order); } }

/**

* 修改密码(当前登录⽤户的) */

public void changePwdByUser(User user) { u.changepwd(user.getuID());

System.out.println(\"修改成功!!\"); }

/**

* 个⼈信息显⽰ */

public void showByUser(User user) {

User findById = u.findById(user.getuID()); System.out.println(findById); }

//待补充功能,删除管理员 @Override

public void delete(String id) {

}

//待补充功能,添加管理员 @Override

public void insert(Admin t) {

// TODO Auto-generated method stub }

//待补充功能,通过id即账号查找管理员 @Override

public Admin findById(String id) {

return map.get(id); }

//待补充功能,显⽰所有管理员 @Override

public List findAll() {

// TODO Auto-generated method stub return null; }

//先设置系统默认数据 public void addMessage() {

map.put(\"qwl\new Admin(\"10086\ LocalDate time = LocalDate.now();

Dishes d1 = new Dishes(\"1\红烧猪蹄\⾁类\ d.insert(d1);

Dishes d2 = new Dishes(\"2\鸡公煲\⾁类\ d.insert(d2);

Dishes d3 = new Dishes(\"3\⿇辣⾹锅\⽕锅类\ d.insert(d3);

Dishes d4 = new Dishes(\"4\⽔煮⾁⽚\⾁类\ d.insert(d4);

Dishes d5 = new Dishes(\"5\⽔果沙拉\⽔果类\ d.insert(d5);

// String orderID, LocalDateTime utime, Dishes dishes, int ordernum, String uID, // Double orderprice,int orderValue

LocalDateTime localdatetime = LocalDateTime.now();

Order o1 = new Order(\"1\ o.insert(o1);

Order o2 = new Order(\"2\

o.insert(o2);

Order o3 = new Order(\"3\ o.insert(o3);

Order o4 = new Order(\"4\ o.insert(o4);

Order o5 = new Order(\"5\ o.insert(o5);

// String uID, String uname, String usex, String upwd, String uadress, String // utel, LocalDateTime utime

User u1 = new User(\"1001\张三\男\湖北\ u.insert(u1);

User u2 = new User(\"1002\李四\男\湖南\ u.insert(u2);

User u3 = new User(\"1003\王五\男\江苏\ u.insert(u3);

User u4 = new User(\"1004\刘柳\⼥\浙江\ u.insert(u4);

User u5 = new User(\"1005\赵琦\⼥\新疆\ u.insert(u5); }}

OrderSys

import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.Objects;import java.util.Set;

public class OrderSys implements DAO {

static Map ordermap = new HashMap<>(); static List orderlist = new ArrayList<>(); /**

* 新增订单 */

@Override

public void insert(Order t) {

ordermap.put(t.getOrderID(), t); } /**

* 通过订单id查找订单 */

@Override

public Order findById(String id) { if (ordermap.get(id) == null) { return null; } else {

return ordermap.get(id); }

} /**

* 通过⽤户id查询⽤户的所有订单,并返回⼀个list集合 * @param uid * @return */

public List findByuId(String uid) { List list = new ArrayList<>(); Set keys = ordermap.keySet(); for (String key : keys) {

if (Objects.equals(uid, ordermap.get(key).getuID())) { list.add(ordermap.get(key)); } }

return list; }

/**

* 显⽰所有订单 */

@Override

public List findAll() {

Set keys = ordermap.keySet(); for (String key : keys) {

orderlist.add(ordermap.get(key)); }

return orderlist; } /**

* 待完成功能,删除订单

*/

@Override

public void delete(String id) {

// TODO Auto-generated method stub }}

UserSys

import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.Scanner;import java.util.Set;

//客户id,客户名,性别,密码,送餐地址,⼿机号,创建时间public class UserSys implements DAO {

static Map usermap = new HashMap<>(); List list = new ArrayList<>(); Scanner sc = new Scanner(System.in); /**

* 添加客户 */

@Override

public void insert(User t) { usermap.put(t.getuID(), t); }

/**

* 查看客户列表 */

@Override

public List findAll() {

Set keys = usermap.keySet(); for (String str : keys) {

list.add(usermap.get(str)); }

return list; }

/**

* 删除指定id的客户 */

@Override

public void delete(String id) { if (usermap.get(id) == null) {

System.out.println(\"没有当前id的客户\"); } else {

System.out.println(usermap.get(id) + \"已删除\"); usermap.remove(id); } }

/**

* 修改密码(当前登录⽤户的) */

public void changepwd(String id) { User user = findById(id);

System.out.println(\"请输⼊新密码:\"); String str = sc.next();

User t = new User(user.getuID(), user.getUname(), user.getUsex(), str, user.getUadress(), user.getUtel(), user.getUtime()); usermap.put(id, t); }

/**

* 通过id查找对应客户 */

@Override

public User findById(String id) { if (usermap.get(id) == null) { return null; } else {

return usermap.get(id); } }

}

DishesSys

import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.Objects;import java.util.Set;

public class DishesSys implements DAO {

// 建⽴⼀个菜品的map集合,其中菜品的id为map的键,整个菜品对象为map的值 static Map dishesmap = new HashMap<>(); Set keys = dishesmap.keySet(); /**

* 添加菜品 */

@Override

public void insert(Dishes t) { dishesmap.put(t.getdID(), t); }

/**

* 通过id来寻找菜品 */

@Override

public Dishes findById(String id) { if (dishesmap.get(id) == null) { return null; } else {

return dishesmap.get(id); } }

/**

* 根据菜品类型查找菜品 */

public List findByType(String type) { List list = new ArrayList<>(); for (String key : keys) {

if (Objects.equals(type, dishesmap.get(key).getDtype())) { list.add(dishesmap.get(key)); } }

return list; }

/**

* 查询所有菜品 */

@Override

public List findAll() {

List list = new ArrayList<>(); for (String str : keys) {

list.add(dishesmap.get(str)); }

return list; }

public void selectBytype(String typename) { int count = 0;

for (String key : keys) {

if (Objects.equals(dishesmap.get(key).getDtype(), typename)) { System.out.println(dishesmap.get(key)); count++; } }

if (count == 0) {

System.out.println(\"没有当前类别的菜品!\"); } }

/**

* 删除指定id菜品 */

@Override

public void delete(String id) { if (dishesmap.get(id) == null) {

System.out.println(\"输⼊id错误...\"); } else {

dishesmap.remove(id); } }}

Menu

import java.util.Objects;import java.util.Scanner;

public class Menu {

static AdminSys admin = new AdminSys(); Scanner sc = new Scanner(System.in); public void showMenu() { admin.addMessage();

System.out.println(\"请输⼊账号和密码:按照(账号/密码)\"); String str = sc.next();

String[] info = str.split(\"/\"); if (info.length < 2) {

System.out.println(\"输⼊有误,请重新输⼊:\"); showMenu(); } else {

if (admin.findById(info[0]) != null && Objects.equals(admin.findById(info[0]).getApwd(), info[1])) { adminMenu();

} else if (admin.findUserByid(info[0]) != null

&& Objects.equals(info[1], admin.findUserByid(info[0]).getUpwd())) { User user = admin.findUserByid(info[0]); userMenu(user); } else {

System.out.println(\"输⼊有误,请重新输⼊....\"); showMenu(); } } }

public void userMenu(User user) {

System.out.println(\"=========欢迎来到订餐系统=======\"); System.out.println(\"====【1】点餐=================\"); System.out.println(\"====【2】根据菜品类别显⽰所有菜品===\"); System.out.println(\"====【3】查看所有订单============\"); System.out.println(\"====【4】修改密码===============\"); System.out.println(\"====【5】个⼈信息显⽰============\"); System.out.println(\"====【6】退出==================\"); System.out.println(\"请输⼊您要进⾏的操作:\"); String n = sc.next(); switch (n) { case \"1\":

admin.shopDishes(user); userMenu(user); break; case \"2\":

admin.ShowOfTypeByUser(); userMenu(user); break; case \"3\":

admin.showAllOrderByUser(user); userMenu(user); break; case \"4\":

admin.changePwdByUser(user); userMenu(user); break; case \"5\":

admin.showByUser(user); userMenu(user); break; case \"6\":

System.out.println(\"谢谢使⽤,再见!\"); System.exit(0); default:

System.out.println(\"输⼊错误,请重新输⼊:\"); userMenu(user); } }

public void adminMenu() {

System.out.println(\"=========欢迎您尊贵的管理员=======\"); 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】退出=================\"); String m = sc.next(); switch (m) { case \"1\":

admin.addDishes(); adminMenu(); break; case \"2\":

System.out.println(\"请输⼊您需要每⾏显⽰多少数据:\"); int pageSize = sc.nextInt();

admin.showAllDishes(pageSize); adminMenu(); break; case \"3\":

admin.selecBytypeOfAdmin(); adminMenu(); break; case \"4\":

admin.selectByDishesID(); adminMenu(); break; case \"5\":

admin.deleteDishes(); adminMenu(); break; case \"6\":

admin.addUser(); adminMenu(); break; case \"7\":

admin.findUser(); adminMenu(); break; case \"8\":

admin.deleteUserByAdmin(); adminMenu(); break; case \"9\":

admin.showAllOrder(); adminMenu(); break; case \"10\":

admin.changeOrderValue(); adminMenu(); break; case \"11\":

System.out.println(\"谢谢使⽤,再见!\"); System.exit(0); break; default:

System.out.println(\"输⼊错误,请重新输⼊:\"); adminMenu(); } }}

Test

public class Test {

public static void main(String[] args) { Menu m = new Menu(); m.showMenu(); }}

运⾏截图

这时⾃⼰在学习javase时为了检测⾃⼰的基础,在通过取理解他⼈的项⽬源码进⾏查缺补漏。如果⼤家有什么疑问可以去原⽂地址进⾏阅读原⽂地址:blog.csdn.net/weixin_44219955

因篇幅问题不能全部显示,请点此查看更多更全内容