1. 首先,您需要定义一个菜品类,包含菜名和价格属性。
public class Dish {
private String name; // 菜名
private double price; // 价格
public Dish(String name, double price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
}
```
2. 接着,您需要定义一个订单类,包含点单者信息和所点的菜品信息。
```java
public class Order {
private String username; // 点单者
private List
public Order(String username, List
this.username = username;
this.dishes = dishes;
}
public String getUsername() {
return username;
}
public List
return dishes;
}
}
```
3. 最后,您需要定义一个订餐系统类,包含菜单、订单列表和点餐、查看菜单、查看订单等功能。
```java
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class OrderSystem {
private static List
private static final Dish[] dishes = { // 菜单
new Dish("菜品一", 10),
new Dish("菜品二", 20),
new Dish("菜品三", 3)
};
public static void main(String[] args) {
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. 退出");
int choice = scanner.nextInt();
scanner.nextLine(); // 把换行符从缓冲区读出,防止下一次输入被误读
switch (choice) {
case 1:
order(scanner);
break;
case 2:
showMenu();
break;
case 3:
showOrders();
break;
case 4:
return;
default:
System.out.println("非法输入!");
break;
}
}
}
// 点餐
private static void order(Scanner scanner) {
System.out.println("请输入点餐者姓名:");
String username = scanner.nextLine();
List
while (true) {
int index = -1;
System.out.println("请输入要点的菜品编号(输入0结束):");
showMenu();
index = scanner.nextInt();
scanner.nextLine();
if (index == 0) {
break;
}
if (index < 1 || index > dishes.length) {
System.out.println("非法输入!");
continue;
}
dishes.add(OrderSystem.dishes[index - 1]);
}
double totalPrice = calculateTotalPrice(dishes);
System.out.printf("%s 您点的菜品为:%s,总价为:%.2f 元,祝用餐愉快!\n", username, getDishesString(dishes), totalPrice);
orders.add(new Order(username, dishes)); // 添加到订单列表中
}
// 查看菜单
private static void showMenu() {
System.out.println("编号\t菜名\t价格");
for (int i = 0; i < dishes.length; i++) {
Dish dish = dishes[i];
System.out.printf("%d\t%s\t%.2f\n", i + 1, dish.getName(), dish.getPrice());
}
}
// 查看订单
private static void showOrders() {
for (Order order : orders) {
String username = order.getUsername();
List
double totalPrice = calculateTotalPrice(dishes);
System.out.printf("%s 点的菜品为:%s,总价为:%.2f 元\n", username, getDishesString(dishes), totalPrice);
}
}
// 计算菜品总价
private static double calculateTotalPrice(List
double totalPrice = 0.0;
for (Dish dish : dishes) {
totalPrice += dish.getPrice();
}
return totalPrice;
}
// 以字符串形式返回菜品列表
private static String getDishesString(List
StringBuilder sb = new StringBuilder();
for (Dish dish : dishes) {
sb.append(dish.getName());
sb.append("、");
}
sb.deleteCharAt(sb.length() - 1); // 删除最后一个多余的“、”
return sb.toString();
}
}
以上是一个简单的订餐系统框架和思路,您可以根据需求进行扩展和完善。