Java订餐系统简易版代码
时间: 2023-10-20 22:07:17 浏览: 144
java实现订餐系统
5星 · 资源好评率100%
这里提供一个简单的Java订餐系统的代码示例,包含用户登录、菜品选择和订单管理等功能:
```java
import java.util.ArrayList;
import java.util.Scanner;
public class OrderSystem {
// 菜品列表
private static ArrayList<Dish> dishes = new ArrayList<>();
// 购物车
private static ArrayList<Dish> cart = new ArrayList<>();
// 订单列表
private static ArrayList<Order> orders = new ArrayList<>();
public static void main(String[] args) {
// 初始化菜品列表
initDishes();
// 用户登录
User user = login();
// 菜品选择
selectDishes();
// 订单管理
manageOrders(user);
}
// 初始化菜品列表
private static void initDishes() {
dishes.add(new Dish("鱼香肉丝", 20));
dishes.add(new Dish("宫保鸡丁", 18));
dishes.add(new Dish("红烧肉", 25));
}
// 用户登录
private static User login() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入用户名:");
String username = scanner.nextLine();
System.out.println("请输入密码:");
String password = scanner.nextLine();
User user = new User(username, password);
System.out.println("登录成功,欢迎您," + username + "!");
return user;
}
// 菜品选择
private static void selectDishes() {
Scanner scanner = new Scanner(System.in);
System.out.println("请选择菜品:");
for (int i = 0; i < dishes.size(); i++) {
Dish dish = dishes.get(i);
System.out.println((i + 1) + ". " + dish.getName() + "(¥" + dish.getPrice() + ")");
}
while (true) {
System.out.println("请输入菜品编号(输入0结束选择):");
int num = scanner.nextInt();
if (num <= 0 || num > dishes.size()) {
break;
}
Dish dish = dishes.get(num - 1);
cart.add(dish);
System.out.println(dish.getName() + "已加入购物车。");
}
}
// 订单管理
private static void manageOrders(User user) {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("请选择操作:");
System.out.println("1. 查看购物车");
System.out.println("2. 修改购物车");
System.out.println("3. 提交订单");
System.out.println("4. 查看历史订单");
System.out.println("5. 退出系统");
int num = scanner.nextInt();
switch (num) {
case 1:
viewCart();
break;
case 2:
modifyCart();
break;
case 3:
submitOrder(user);
break;
case 4:
viewOrders(user);
break;
case 5:
System.out.println("欢迎下次光临!");
return;
default:
System.out.println("输入有误,请重新输入。");
break;
}
}
}
// 查看购物车
private static void viewCart() {
System.out.println("购物车:");
for (int i = 0; i < cart.size(); i++) {
Dish dish = cart.get(i);
System.out.println((i + 1) + ". " + dish.getName() + "(¥" + dish.getPrice() + ")");
}
}
// 修改购物车
private static void modifyCart() {
Scanner scanner = new Scanner(System.in);
viewCart();
System.out.println("请输入要修改的菜品编号:");
int num = scanner.nextInt();
if (num <= 0 || num > cart.size()) {
System.out.println("输入有误,请重新输入。");
return;
}
System.out.println("请输入修改后的份数:");
int count = scanner.nextInt();
if (count <= 0) {
System.out.println("输入有误,请重新输入。");
return;
}
Dish dish = cart.get(num - 1);
dish.setCount(count);
System.out.println("购物车已修改。");
}
// 提交订单
private static void submitOrder(User user) {
if (cart.isEmpty()) {
System.out.println("购物车为空,无法提交订单。");
return;
}
double totalPrice = 0;
for (Dish dish : cart) {
totalPrice += dish.getPrice() * dish.getCount();
}
Order order = new Order(cart, totalPrice, user);
orders.add(order);
cart.clear();
System.out.println("订单已提交,总价为¥" + totalPrice + "。");
}
// 查看历史订单
private static void viewOrders(User user) {
System.out.println("历史订单:");
for (Order order : orders) {
if (order.getUser().equals(user)) {
System.out.println(order);
}
}
}
}
class User {
private String username;
private String password;
public User(String username, String password) {
this.username = username;
this.password = password;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
}
class Dish {
private String name;
private double price;
private int count;
public Dish(String name, double price) {
this.name = name;
this.price = price;
this.count = 1;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
class Order {
private ArrayList<Dish> dishes;
private double totalPrice;
private User user;
public Order(ArrayList<Dish> dishes, double totalPrice, User user) {
this.dishes = dishes;
this.totalPrice = totalPrice;
this.user = user;
}
public ArrayList<Dish> getDishes() {
return dishes;
}
public double getTotalPrice() {
return totalPrice;
}
public User getUser() {
return user;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("订单详情:\n");
for (Dish dish : dishes) {
sb.append(dish.getName()).append("(¥").append(dish.getPrice()).append(") x ").append(dish.getCount()).append("\n");
}
sb.append("总价:¥").append(totalPrice).append("\n");
sb.append("用户:").append(user.getUsername()).append("\n");
return sb.toString();
}
}
```
这段代码实现了一个简单的Java订餐系统,包含了用户登录、菜品选择和订单管理等功能。当然,这只是一个简单的示例,实际的订餐系统需要更多的功能和细节处理。
阅读全文