一、简介
本项目是个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<String, String> accountMap) throws IOException {
OutputStream outputStream = new FileOutputStream(OrderSystem.path+"user.txt");
Iterator<Map.Entry<String, String>> 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();
}
//删除User
public Map<String, String> deleteUser(Map<String, String> accountMap) throws IOException {
System.out.print("请输入要删除的用户名:");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
accountMap.remove(name);
userWriter(accountMap);
return accountMap;
}
//读取user
public Map<String, String> loadAccountMap() throws IOException {
InputStream inputStream = new FileInputStream(OrderSystem.path+"user.txt");
Map<String, String> 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;
}
//更新Food
public void foodWriter(Map<Integer, Food> foodMap) throws IOException {
OutputStream outputStream = new FileOutputStream(OrderSystem.path+"food.txt");
ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
objectOutputStream.writeObject(foodMap);
objectOutputStream.close();
}
//添加Food
public Map<Integer, Food> addFood(Map<Integer, Food> foodMap) throws IOException, ClassNotFoundException {
Scanner scanner = new Scanner(System.in);
foodMap.forEach(new BiConsumer<Integer, Food>() {
@Override
public 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;
}
//读取Food
public Map<Integer, Food> 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<Integer, Food> changePrice(Map<Integer, Food> 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<String, String> changePassword(Map<String, String> 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<String, String> accountMap){
System.out.println("账号\t\t|密码");
accountMap.forEach(new BiConsumer<String, String>() {
@Override
public void accept(String s, String s2) {
System.out.println(s+"\t\t|"+s2);
}
});
}
public void main0(Map<String,String> accountMap,Map<Integer,Food> 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;
}
@Override
public 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;
//优惠券Map<优惠金额,数量>
private Map<Integer,Integer> discountCard = new HashMap<>();
//订单表Map<订单号,Map<菜品编号,价格>>
private Map<Integer,Map<Integer, Integer>> orderMap = new HashMap<>();
//余额
private double balance;
//优惠金额
private int discount;
//返回优惠券表
public Map<Integer,Integer> 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<Integer,Integer>> iterator = this.discountCard.entrySet().iterator();
while(iterator.hasNext()){
Map.Entry<Integer,Integer> entry = iterator.next();
System.out.println(entry.getKey()+"\t\t|"+entry.getValue());
}
}
//领取优惠券
public void setDiscountCard(LinkedList<User> 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<String, String> 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<String, String> 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<Integer,Food> foodMap){
System.out.println("菜品编号\t|菜品名称\t|价格");
foodMap.forEach(new BiConsumer<Integer, Food>() {
@Override
public void accept(Integer integer, Food food) {
System.out.println(integer+"\t\t|"+food.getName()+"\t|"+food.getPrice());
}
});
}
//点餐
public void orderFood(Map<Integer,Food> foodMap,Map<Integer, Integer> 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<Integer,Integer>> orderIterator = shopMap.entrySet().iterator();
Map.Entry<Integer,Integer> 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<Integer,Food> 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<Integer, Integer>> orderIterator = this.orderMap.get(i).entrySet().iterator();
Map.Entry<Integer,Integer> 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<Integer,Food> foodMap,Map<Integer, Integer> shopMap,LinkedList<User> list) throws IOException {
if (shopMap.isEmpty()){
System.out.println("订单为空请重新点餐");
return;
}
Map<Integer, Integer> sMap = new HashMap<>();
//拷贝购物车
sMap.putAll(shopMap);
int sum = 0;
Iterator<Map.Entry<Integer,Integer>> iterator = shopMap.entrySet().iterator();
Map.Entry<Integer,Integer> 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();
//存在该金额,数量-1
if (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("历史订单查询完毕");
}
//读取uuser
public LinkedList<User> listReader() throws IOException, ClassNotFoundException {
InputStream inputStream = new FileInputStream(OrderSystem.path+"uuser.txt");
ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
Object object = objectInputStream.readObject();
objectInputStream.close();
LinkedList<User> list = (LinkedList<User>) object;
return list;
}
//更新文件
public void listWriter(LinkedList<User> 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<Integer,Food> foodMap,Map<Integer, Integer> shopMap,LinkedList<User> 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);
int select = 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("输入错误请重新输入");
}
}
@Override
public boolean equals(Object o) {
User u = (User)o;
if (u.getAccount().equals(this.getAccount())){
return true;
}else {
return false;
}
}
@Override
public 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<Integer, Integer>> getOrderMap() {
return orderMap;
}
public void setOrderMap(Map<Integer, Map<Integer, Integer>> 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<String, String> accountMap = new Admin().loadAccountMap();
//购物车shopMap
Map<Integer, Integer> shopMap = new HashMap<>();
//用户表list(账号,订单表)
LinkedList<User> list = new User().listReader();
//LinkedList<User> list = new LinkedList<>();
//菜谱表
Map<Integer, Food> 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("输入错误请重新输入");
}
}
}
}
文章出处登录后可见!
已经登录?立即刷新