用JAVA程序设计食堂点餐系统,可以实现店铺类,食物类,顾客类,测试类的查询
时间: 2024-03-06 09:48:27 浏览: 100
基于java快餐店线上点餐系统设计与实现.docx
5星 · 资源好评率100%
好的,以下是一个简单的用JAVA编写的食堂点餐系统示例,包括店铺类、食品类、顾客类和测试类:
首先,我们需要定义食品类和店铺类,代码如下:
```java
// 食品类
public class Food {
private String name; // 食品名称
private double price; // 食品价格
public Food(String name, double price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
}
// 店铺类
public class Shop {
private String name; // 店铺名称
private List<Food> foodList; // 店铺所售食品列表
public Shop(String name) {
this.name = name;
this.foodList = new ArrayList<>();
}
public void addFood(Food food) {
foodList.add(food);
}
public List<Food> getFoodList() {
return foodList;
}
public String getName() {
return name;
}
}
```
接下来,我们定义顾客类和测试类,代码如下:
```java
// 顾客类
public class Customer {
private String name; // 顾客名称
public Customer(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void orderFood(Shop shop, int foodIndex, int count) {
List<Food> foodList = shop.getFoodList();
if (foodIndex >= 0 && foodIndex < foodList.size()) {
Food food = foodList.get(foodIndex);
System.out.println(name + "点了" + count + "份" + food.getName() + ",共" + count * food.getPrice() + "元");
} else {
System.out.println("该店铺没有该食品!");
}
}
}
// 测试类
public class Test {
public static void main(String[] args) {
// 店铺1
Shop shop1 = new Shop("食堂1");
shop1.addFood(new Food("炒饭", 10.0));
shop1.addFood(new Food("炒面", 12.0));
shop1.addFood(new Food("饺子", 8.0));
// 店铺2
Shop shop2 = new Shop("食堂2");
shop2.addFood(new Food("馄饨", 9.0));
shop2.addFood(new Food("牛肉面", 15.0));
shop2.addFood(new Food("炒年糕", 11.0));
// 顾客1点餐
Customer customer1 = new Customer("张三");
customer1.orderFood(shop1, 0, 2);
customer1.orderFood(shop2, 1, 1);
// 顾客2点餐
Customer customer2 = new Customer("李四");
customer2.orderFood(shop1, 2, 3);
customer2.orderFood(shop2, 3, 2);
customer2.orderFood(shop2, 4, 1);
}
}
```
这个程序定义了食品类和店铺类,并在测试类中创建了两个店铺和两个顾客进行点餐。您可以根据具体需求对程序进行修改和完善。
阅读全文