一、简介

本项目是个Java开发的简单的外卖订餐系统(未使用数据库)。

实现的功能:注册、登录、点餐、查看历史订单、余额充值及优惠券,并将数据写入本地文件。

二、大致过程

1、首先设计登录及注册功能,涉及管理员和用户,其中管理员是账号、密码都为admin的用,使用字节流将账号和密码写入本地文件,根据登录时不同的用户,展示不同的界面。

用户界面

管理员界面

代码

Admin类

package com.order;import java.io.*;import java.util.*;import java.util.function.BiConsumer;public class Admin extends User{//更新user文件public void userWriter(Map accountMap) throws IOException {OutputStream outputStream = new FileOutputStream(OrderSystem.path+"user.txt");Iterator<Map.Entry> iterator = accountMap.entrySet().iterator();while (iterator.hasNext()){String account = iterator.next().getKey();String psw = accountMap.get(account);outputStream.write((account+","+psw+"\n").getBytes());}outputStream.close();}//删除Userpublic Map deleteUser(Map accountMap) throws IOException {System.out.print("请输入要删除的用户名:");Scanner scanner = new Scanner(System.in);String name = scanner.nextLine();accountMap.remove(name);userWriter(accountMap);return accountMap;}//读取userpublic Map loadAccountMap() throws IOException {InputStream inputStream = new FileInputStream(OrderSystem.path+"user.txt");Map accountMap = new HashMap();InputStreamReader inputStreamReader = new InputStreamReader(inputStream);BufferedReader bufferedReader = new BufferedReader(inputStreamReader);String line;while ((line = bufferedReader.readLine())!=null){String[] strings = line.split(",");accountMap.put(strings[0],strings[1]);}inputStream.close();return accountMap;}//更新Foodpublic void foodWriter(Map foodMap) throws IOException {OutputStream outputStream = new FileOutputStream(OrderSystem.path+"food.txt");ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);objectOutputStream.writeObject(foodMap);objectOutputStream.close();}//添加Foodpublic Map addFood(Map foodMap) throws IOException, ClassNotFoundException {Scanner scanner = new Scanner(System.in);foodMap.forEach(new BiConsumer() {@Overridepublic void accept(Integer integer, Food food) {System.out.println(integer+"\t"+food.getName()+"\t"+food.getPrice());}});System.out.println("--------------");System.out.print("输入菜品名称:");String name = scanner.nextLine();System.out.print("输入菜品价格:");int price = scanner.nextInt();foodMap.put(foodMap.size()+1,new Food(name,price));foodWriter(foodMap);return foodMap;}//读取Foodpublic Map foodReader() throws IOException, ClassNotFoundException {InputStream inputStream = new FileInputStream(OrderSystem.path+"food.txt");ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);Object object = objectInputStream.readObject();objectInputStream.close();Map map = (Map) object;return map;}//更改价格public Map changePrice(Map foodMap) throws IOException {new User().selectFood(foodMap);Scanner scanner = new Scanner(System.in);System.out.print("更改价格的菜品编号");Integer num = scanner.nextInt();if (!foodMap.containsKey(num)){System.out.println("编号不存在");return foodMap;}System.out.print("更改价格为:");Integer price = scanner.nextInt();Food food = foodMap.get(num);food.setPrice(price);foodMap.put(num,food);foodWriter(foodMap);return foodMap;}//更改密码public Map changePassword(Map accountMap) throws IOException {Scanner scanner = new Scanner(System.in);System.out.print("输入账号:");String account0 = scanner.nextLine();System.out.print("将密码更改为:");String psw0 = scanner.nextLine();accountMap.put(account0,psw0);userWriter(accountMap);return accountMap;}public void getUser(Map accountMap){System.out.println("账号\t\t|密码");accountMap.forEach(new BiConsumer() {@Overridepublic void accept(String s, String s2) {System.out.println(s+"\t\t|"+s2);}});}public void main0(Map accountMap,Map foodMap) throws IOException, ClassNotFoundException { ArrayList list = new ArrayList();//管理员界面Scanner scanner = new Scanner(System.in);int select1 = -1;while (select1 != 0){Menu.adminMenu();select1 = scanner.nextInt();switch (select1){case 0://0---退出系统System.exit(0);break;case 1://1---添加用户register(accountMap);break;case 2://2---删除用户accountMap = deleteUser(accountMap);break;case 3://3---添加菜品foodMap = addFood(foodMap);break;case 4://4---查看用户getUser(accountMap);break;case 5://5---修改密码 changePassword(accountMap);break;case 6://6---修改菜价changePrice(foodMap);break;case 7://7---查询菜品new User().selectFood(foodMap);break;default:System.out.println("输入错误请重新输入");}}}}

Food类

package com.order;import java.io.Serializable;import java.util.*;public class Food implements Serializable {private String name;private int price;public Food(String name, int price) {this.name = name;this.price = price;}@Overridepublic String toString() {return name+"\t"+price;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getPrice() {return price;}public void setPrice(int price) {this.price = price;}}

Menu类

package com.order;public class Menu {public static void adminMenu(){System.out.println("----------------------");System.out.println("0---退出系统");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("7---查询菜品");System.out.println("----------------------");System.out.printf("请选择功能:");}public static void userMenu(){System.out.println("----------------------");System.out.println("0---退出系统");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("7---查看余额");System.out.println("8---充值余额");System.out.println("----------------------");System.out.printf("请选择功能:");}}

User类

package com.order;import java.io.*;import java.util.*;import java.util.function.BiConsumer;public class User implements Serializable{private String account;private transient String password;//优惠券Mapprivate Map discountCard = new HashMap();//订单表Map<订单号,Map>private Map<Integer,Map> orderMap = new HashMap();//余额private double balance;//优惠金额private int discount;//返回优惠券表public Map getDiscountCard() {return discountCard;}//充值余额public void recharge(double d){this.setBalance(this.balance+d);}//输出优惠券表public void printDiscountCard(){if (this.discountCard.isEmpty()){System.out.println("无优惠券");return;}System.out.println("优惠金额\t|数量");Iterator<Map.Entry> iterator = this.discountCard.entrySet().iterator();while(iterator.hasNext()){Map.Entry entry = iterator.next();System.out.println(entry.getKey()+"\t\t|"+entry.getValue());}}//领取优惠券public void setDiscountCard(LinkedList list) {Random random = new Random();int p = random.nextInt(5)+1;System.out.println("获得一张"+p+"元优惠券!");if (this.discountCard.containsKey(p)){this.discountCard.put(p,this.discountCard.get(p)+1);}else{this.discountCard.put(p,1);}//list.add(this);}//登录public int login(Map account, int count) throws InterruptedException {int c = 0;if (account.containsKey(this.account)&&account.get(this.account).equals(this.password)){if (this.account.equals("admin")){System.out.println("管理员登录成功");return 2;}else {System.out.println("登录成功");return 1;}}else{return 0;}}//注册public void register(Map accountMap) throws IOException {OutputStream outputStream = new FileOutputStream(OrderSystem.path+"user.txt",true);System.out.println("注册功能:");Scanner scanner = new Scanner(System.in);System.out.println("请输入");System.out.print("账号:");String uName = scanner.nextLine();System.out.print("密码:");String psw = scanner.nextLine();if (accountMap.containsKey(uName)){System.out.println("用户名已存在!");}else{for (int i = 0; i < OrderSystem.MAX_User; i++) {if (!accountMap.containsKey(i)){accountMap.put(uName,psw);}}outputStream.write((uName+","+psw+"\n").getBytes());}}//查询菜品public void selectFood(Map foodMap){System.out.println("菜品编号\t|菜品名称\t|价格");foodMap.forEach(new BiConsumer() {@Overridepublic void accept(Integer integer, Food food) {System.out.println(integer+"\t\t|"+food.getName()+"\t|"+food.getPrice());}});}//点餐public void orderFood(Map foodMap,Map shopMap){//点餐//菜品编号Scanner scanner = new Scanner(System.in);int num;System.out.print("请输入菜品编号:");num = scanner.nextInt();//校验编号是否合法while(!foodMap.containsKey(num)){if (!foodMap.containsKey(num)){System.out.println("编号不存在,请重新输入");}num = scanner.nextInt();}//购买数量int amount = 0;//校验数量是否合法while(amount<=0){System.out.printf("请输入购买数量:");amount = scanner.nextInt();if (amount<=0){System.out.println("输入错误,请重新输入");}}//购物车中存在相同商品将数量相加if (shopMap.containsKey(num)){shopMap.put(num,shopMap.get(num)+amount);}else {shopMap.put(num,amount);}System.out.println("菜品名称\t|数量\t|价格");//创建迭代器Iterator<Map.Entry> orderIterator = shopMap.entrySet().iterator();Map.Entry orderEntry ;Integer sumPrice = 0;String name = null;Integer count = null;Integer price = null;//迭代器循环while (orderIterator.hasNext()){orderEntry = orderIterator.next();//通过food类中的foodMap获取编号对应的名称name = foodMap.get(orderEntry.getKey()).getName();//数量count = orderEntry.getValue();//价格=单价*数量price = foodMap.get(orderEntry.getKey()).getPrice()*count;sumPrice +=price;System.out.println(name+"\t|"+count+"\t\t|"+price);}System.out.println("总价:"+sumPrice);}//输出历史订单public void getOrder(Map foodMap,int num){Integer sumPrice = 0;String name = null;Integer count = null;Integer price = null;//遍历历史订单并输出for (int i = num; i < this.orderMap.size(); i++) {System.out.println("----------------------");System.out.println("订单号:"+i);Iterator<Map.Entry> orderIterator = this.orderMap.get(i).entrySet().iterator();Map.Entry orderEntry ;//迭代器循环while (orderIterator.hasNext()){orderEntry = orderIterator.next();//通过food类中的foodMap获取编号对应的名称name = foodMap.get(orderEntry.getKey()).getName();//数量count = orderEntry.getValue();//价格=单价*数量price = foodMap.get(orderEntry.getKey()).getPrice()*count;sumPrice +=price;System.out.println(name+"\t|"+count+"\t\t|"+price);}System.out.println("总价:"+sumPrice);sumPrice = 0;System.out.println("----------------------");}}//下单public void placeOrder(Map foodMap,Map shopMap,LinkedList list) throws IOException {if (shopMap.isEmpty()){System.out.println("订单为空请重新点餐");return;}Map sMap = new HashMap();//拷贝购物车sMap.putAll(shopMap);int sum = 0;Iterator<Map.Entry> iterator = shopMap.entrySet().iterator();Map.Entry orderEntry ;while (iterator.hasNext()){orderEntry = iterator.next();//总价+=单价*数量sum += foodMap.get(orderEntry.getKey()).getPrice()*orderEntry.getValue();}Scanner scanner = new Scanner(System.in);//输出优惠券this.printDiscountCard();if (!this.discountCard.isEmpty()){System.out.println("是否使用优惠券");System.out.println("0---否");System.out.println("1---是");int select = -1;while(!(select==0 || select==1)){select = scanner.nextInt();switch (select){case 0:this.discount = 0;break;case 1:System.out.print("请输入你要使用的优惠券金额为:");this.discount = scanner.nextInt();//存在该金额,数量-1if (this.discountCard.containsKey(discount)){this.discountCard.put(discount,this.discountCard.get(discount)-1);if (this.discountCard.get(discount)==0){this.discountCard.remove(discount);}}break;default:System.out.println("输入错误,请重新输入");}}}sum -=discount;if (this.balance<sum){System.out.println("余额不足,请重新下单。当前余额:"+this.balance);shopMap.clear();return;}this.setBalance(this.balance-sum);orderMap.put(orderMap.size(), sMap);shopMap.clear();this.getOrder(foodMap,orderMap.size()-1);System.out.println("优惠:"+discount);System.out.println("支付:"+sum);System.out.println("----------------------");//将登录的user添加进list//list.add(this);//System.out.println("历史订单查询完毕");}//读取uuserpublic LinkedList listReader() throws IOException, ClassNotFoundException {InputStream inputStream = new FileInputStream(OrderSystem.path+"uuser.txt");ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);Object object = objectInputStream.readObject();objectInputStream.close();LinkedList list = (LinkedList) object;return list;}//更新文件public void listWriter(LinkedList list) throws IOException {list.add(this);OutputStream outputStream = new FileOutputStream(OrderSystem.path+"uuser.txt");ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);objectOutputStream.writeObject(list);objectOutputStream.close();list.remove(this);}//主方法public void main(Map foodMap,Map shopMap,LinkedList list) throws IOException, InterruptedException {Menu.userMenu();for (int i = 0; i < list.size(); i++) {if (list.get(i).equals(this)){//如果文件中存在同名user,把该user的历史订单拷贝this.orderMap.putAll(list.get(i).getOrderMap());this.discountCard.putAll(list.get(i).getDiscountCard());this.setBalance(list.get(i).getBalance());list.remove(i);break;}}Scanner scanner = new Scanner(System.in);intselect = scanner.nextInt();switch (select){case 0://0---退出系统System.exit(0);break;case 1://1---点餐orderFood(foodMap,shopMap);break;case 2://2---下单placeOrder(foodMap,shopMap,list);//list.add(this);this.listWriter(list);break;case 3://3---查看历史订单this.getOrder(foodMap,0);break;case 4://4---领取优惠券this.setDiscountCard(list);//更新文件this.listWriter(list);break;case 5://5---查看优惠券this.printDiscountCard();break;case 6://6---查看菜品selectFood(foodMap);break;case 7://7---查看余额System.out.println("余额:"+this.getBalance());break;case 8://8---充值余额System.out.print("输入要充值的金额:");double money = scanner.nextDouble();Thread.sleep(500);this.recharge(money);//更新文件this.listWriter(list);System.out.println("充值成功,当前余额:"+this.getBalance());break;default:System.out.println("输入错误请重新输入");}}@Overridepublic boolean equals(Object o) {User u = (User)o;if (u.getAccount().equals(this.getAccount())){return true;}else {return false;}}@Overridepublic int hashCode() {return Objects.hash(account, password);}public User() {}public User(String account,String password){this.account=account;this.password=password;}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;}public Map<Integer, Map> getOrderMap() {return orderMap;}public void setOrderMap(Map<Integer, Map> orderMap) {this.orderMap = orderMap;}public double getBalance() {return balance;}public void setBalance(double balance) {this.balance = balance;}}

OrderSystem类

package com.order;import java.io.*;import java.util.*;public class OrderSystem {//最大存储量public static int MAX_User = 99;//文件路径public static String path = "D:\\01Buka\\Java394\\src\\com\\order\\";public static void main(String[] args) throws InterruptedException, IOException, ClassNotFoundException {//账户表account(账号,密码)Map accountMap = new Admin().loadAccountMap();//购物车shopMapMap shopMap = new HashMap();//用户表list(账号,订单表)LinkedList list = new User().listReader();//LinkedList list = new LinkedList();//菜谱表Map foodMap= new Admin().foodReader();//未将序列化文件写入本地之前,需要将上面这行代码注释,运行下面这行代码;若已将序列化文件写入本地,则反之//FileReader fileReader = new FileReader(path+"user.txt");InputStream inputStream = new FileInputStream(path+"user.txt");InputStreamReader inputStreamReader = new InputStreamReader(inputStream);BufferedReader bufferedReader = new BufferedReader(inputStreamReader);//FileWriter fileWriter = new FileWriter(path+"user.txt");OutputStream outputStream = new FileOutputStream(path+"user.txt",true);Scanner scanner = new Scanner(System.in);User user = null;boolean flag = true;int select = 0;while (flag){System.out.println("----------------------");System.out.println("0---注册");System.out.println("1---登录");System.out.println("----------------------");System.out.print("请选择功能:");select = scanner.nextInt();switch (select){case 0:{new User().register(accountMap);break;}case 1:{Scanner scanner0 = new Scanner(System.in);String account;String password;int log = 0;for (int i = 0; i < 3; i++) {System.out.println("登录功能:");System.out.println("请输入");System.out.print("账号:");account = scanner0.nextLine();System.out.print("密码:");password = scanner0.nextLine();user = new User(account,password);log = user.login(accountMap,0);if (log == 0){System.out.println("用户名不存在或密码错误");}else {break;}if (i == 2){System.out.println("等待10s重试");Thread.sleep(10000);i=0;}}if (log == 2){//管理员界面new Admin().main0(accountMap,foodMap);}else if (log== 1){//用户界面System.out.println("用户界面");//orderMap = user.loadUserOrder();while (flag){user.main(foodMap, shopMap,list);}}else if (log== 0){System.out.println("登录失败");System.exit(0);}flag = false;break;}default:System.out.println("输入错误请重新输入");}}}}