「任务描述」 本任务要求,使用所学知识编写一个基于控制台的蔬菜交易系统,实现跑腿买菜功能。输出所有蔬菜肉蛋类的信息:包括每种食材的类别、名称、单价、库存。 顾客买菜时,根据提示输入蔬菜瓜果类别来选购需要的食材,并根据提示输入购买数量。 购买完毕后输出顾客的订单信息,包括:订单号、订单明细、订单总额。 「思路」 (1)通过任务描述可知,该系统中必须包括3个实体类,类名及属性设置如下: 1.食材类(Food): a)食材编号(id) b)食材名称(name) c)食材单价(price) d)库存数量(storage) 2.订单项类(OrderItem): a)食材(food) b)购买数量(num) 3.订单类(Order): a)订单号(orderID) b)订单总额(total) c)订单项列表(items) (2)由于购买食材时,需要选择食材的数量,所以需要在订单项类里定义获取食材对象以及食材数量的方法。 (3)由于需要指定订单项以及获取订单的订单列表、订单号、订单总额等信息,所以需要有订单列表、订单号、订单总额指定订单项等方法。用Java完成文件名为Test7
时间: 2023-12-22 13:04:29 浏览: 64
.java的代码如下:
```java
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
class Food{
private String id;
private String name;
private double price;
private int storage;
public Food(String id, String name, double price, int storage) {
this.id = id;
this.name = name;
this.price = price;
this.storage = storage;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
public int getStorage() {
return storage;
}
}
class OrderItem{
private Food food;
private int num;
public OrderItem(Food food, int num) {
this.food = food;
this.num = num;
}
public Food getFood() {
return food;
}
public int getNum() {
return num;
}
public double getTotalPrice() {
return food.getPrice() * num;
}
}
class Order{
private static int orderCount = 0;
private int orderID;
private double total;
private List<OrderItem> items;
public Order() {
orderCount++;
orderID = orderCount;
total = 0;
items = new ArrayList<>();
}
public int getOrderID() {
return orderID;
}
public double getTotal() {
return total;
}
public void addOrderItem(OrderItem item) {
items.add(item);
total += item.getTotalPrice();
}
public List<OrderItem> getOrderItems() {
return items;
}
}
public class Test7 {
public static void main(String[] args) {
List<Food> foods = new ArrayList<>();
foods.add(new Food("001", "黄瓜", 2.5, 100));
foods.add(new Food("002", "西红柿", 3.0, 80));
foods.add(new Food("003", "茄子", 4.0, 50));
foods.add(new Food("004", "鸡腿", 8.0, 60));
foods.add(new Food("005", "猪肉", 12.0, 40));
foods.add(new Food("006", "鸭腿", 10.0, 50));
foods.add(new Food("007", "鸭蛋", 1.5, 100));
foods.add(new Food("008", "鸡蛋", 1.2, 120));
Scanner scanner = new Scanner(System.in);
System.out.println("欢迎使用蔬菜交易系统");
System.out.println("所有蔬菜肉蛋类的信息如下:");
System.out.println("编号\t名称\t单价\t库存");
for (Food food : foods) {
System.out.println(food.getId() + "\t" + food.getName() + "\t" + food.getPrice() + "\t" + food.getStorage());
}
Order order = new Order();
while (true) {
System.out.println("请输入要购买的食材编号:");
String foodID = scanner.nextLine();
boolean foundFood = false;
for (Food food : foods) {
if (food.getId().equals(foodID)) {
foundFood = true;
System.out.println("请输入购买数量:");
int num = scanner.nextInt();
scanner.nextLine();
if (food.getStorage() < num) {
System.out.println("库存不足!");
break;
}
OrderItem item = new OrderItem(food, num);
order.addOrderItem(item);
System.out.println(food.getName() + "已加入购物车");
System.out.println("当前购物车如下:");
System.out.println("名称\t单价\t数量\t总价");
for (OrderItem orderItem : order.getOrderItems()) {
System.out.println(orderItem.getFood().getName() + "\t" + orderItem.getFood().getPrice() + "\t" + orderItem.getNum() + "\t" + orderItem.getTotalPrice());
}
break;
}
}
if (!foundFood) {
System.out.println("该食材不存在!");
}
System.out.println("是否继续购买(Y/N):");
String answer = scanner.nextLine();
if (answer.equalsIgnoreCase("N")) {
break;
}
}
System.out.println("订单信息如下:");
System.out.println("订单号:" + order.getOrderID());
System.out.println("名称\t单价\t数量\t总价");
for (OrderItem item : order.getOrderItems()) {
System.out.println(item.getFood().getName() + "\t" + item.getFood().getPrice() + "\t" + item.getNum() + "\t" + item.getTotalPrice());
}
System.out.println("订单总额:" + order.getTotal());
}
}
```
注:此代码仅作为参考,可能还有待完善。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)