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<T> { void insert(T t); T findById(String id); List<T> 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 + ", account=" + account + ", apwd=" + apwd + "]"; } }
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 + ", utime=" + utime + ", dishes=" + dishes + ", Ordernum=" + Ordernum + ", uID=" + uID + ", Orderprice=" + Orderprice + ", OrderValue=" + OrderValue + "]"; } }
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 + ", uname=" + uname + ", usex=" + usex + ", upwd=" + upwd + ", uadress=" + uadress + ", utel=" + utel + ", utime=" + utime + "]"; } }
這裡需要注意的是,所定義的類中的變數最好設定為私有變數,這樣更能夠保證資料的安全性。但必須提供變數的操作介面。
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<Admin> { static Map<String, Admin> map = new HashMap<>(); // static Set<String> 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<Dishes> 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<User> 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<Order> 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<Dishes> 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<Order> 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<Admin> findAll() { // TODO Auto-generated method stub return null; } //先設定系統預設資料 public void addMessage() { map.put("qwl", new Admin("10086", "qwl", "123456")); LocalDate time = LocalDate.now(); Dishes d1 = new Dishes("1", "紅燒豬蹄", "肉類", time, 12.5, 20, 30); d.insert(d1); Dishes d2 = new Dishes("2", "雞公煲", "肉類", time, 21.5, 30, 20); d.insert(d2); Dishes d3 = new Dishes("3", "麻辣香鍋", "火鍋類", time, 30, 5, 10); d.insert(d3); Dishes d4 = new Dishes("4", "水煮肉片", "肉類", time, 15, 12, 15); d.insert(d4); Dishes d5 = new Dishes("5", "水果沙拉", "水果類", time, 6, 70, 60); 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", localdatetime, d1, 10, "1001", 60.0, 1); o.insert(o1); Order o2 = new Order("2", localdatetime, d2, 5, "1002", 50.0, 10); o.insert(o2); Order o3 = new Order("3", localdatetime, d3, 5, "1003", 40.0, 5); o.insert(o3); Order o4 = new Order("4", localdatetime, d4, 5, "1004", 30.0, 6); o.insert(o4); Order o5 = new Order("5", localdatetime, d5, 5, "1005", 20.0, 8); o.insert(o5); // String uID, String uname, String usex, String upwd, String uadress, String // utel, LocalDateTime utime User u1 = new User("1001", "張三", "男", "123456", "湖北", "13545286487", localdatetime); u.insert(u1); User u2 = new User("1002", "李四", "男", "234567", "湖南", "15927948976", localdatetime); u.insert(u2); User u3 = new User("1003", "王五", "男", "345678", "江蘇", "15927986854", localdatetime); u.insert(u3); User u4 = new User("1004", "劉柳", "女", "456789", "浙江", "18771580860", localdatetime); u.insert(u4); User u5 = new User("1005", "趙琦", "女", "567890", "新疆", "18771580750", localdatetime); 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<Order> { static Map<String, Order> ordermap = new HashMap<>(); static List<Order> 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<Order> findByuId(String uid) { List<Order> list = new ArrayList<>(); Set<String> 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<Order> findAll() { Set<String> 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<User> { static Map<String, User> usermap = new HashMap<>(); List<User> list = new ArrayList<>(); Scanner sc = new Scanner(System.in); /** * 新增客戶 */ @Override public void insert(User t) { usermap.put(t.getuID(), t); } /** * 檢視客戶列表 */ @Override public List<User> findAll() { Set<String> 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<Dishes> { // 建立一個菜品的map集合,其中菜品的id為map的鍵,整個菜品物件為map的值 static Map<String, Dishes> dishesmap = new HashMap<>(); Set<String> 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<Dishes> findByType(String type) { List<Dishes> 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<Dishes> findAll() { List<Dishes> 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