java写一个外卖订餐系统
时间: 2023-12-20 07:07:25 浏览: 96
好的,一个基本的外卖订餐系统需要具备以下功能:
1. 用户注册、登录、注销
2. 浏览餐厅列表、菜单和价格
3. 下单和支付
4. 查看订单历史记录
以下是一个简单的Java代码示例:
```java
import java.util.*;
public class Restaurant {
private String name;
private List<MenuItem> menu;
// 构造方法,getter和setter省略
public void displayMenu() {
System.out.println("Menu for " + name + ":");
for (MenuItem item : menu) {
System.out.println(item.getName() + "\t" + item.getPrice());
}
}
public void placeOrder(List<MenuItem> items) {
double totalCost = 0;
for (MenuItem item : items) {
totalCost += item.getPrice();
}
System.out.println("Order placed. Total cost: " + totalCost);
}
}
public class MenuItem {
private String name;
private double price;
// 构造方法,getter和setter省略
}
public class User {
private String username;
private String password;
private List<Order> orderHistory;
// 构造方法,getter和setter省略
public void displayOrderHistory() {
System.out.println("Order history for " + username + ":");
for (Order order : orderHistory) {
System.out.println(order.getRestaurantName() + "\t" + order.getTotalCost());
}
}
}
public class Order {
private String restaurantName;
private List<MenuItem> items;
private double totalCost;
// 构造方法,getter和setter省略
}
public class Main {
public static void main(String[] args) {
// 用户注册、登录、注销
User user = new User("username", "password");
// 浏览餐厅列表、菜单和价格
Restaurant restaurant = new Restaurant("Restaurant Name", new ArrayList<>(Arrays.asList(
new MenuItem("Item 1", 10),
new MenuItem("Item 2", 20)
)));
restaurant.displayMenu();
// 下单和支付
List<MenuItem> items = new ArrayList<>();
items.add(new MenuItem("Item 1", 10));
items.add(new MenuItem("Item 2", 20));
restaurant.placeOrder(items);
// 查看订单历史记录
user.getOrderHistory();
}
}
```
当然,这只是一个简单的示例,实际的外卖订餐系统需要考虑更多细节。
阅读全文