java编写代码设计点菜计价程序,根据输入的信息,计算并输出总价格。 输入内容按先后顺序包括两部分:菜单、订单,最后以"end"结束。 菜单由一条或多条菜品记录组成,每条记录一行 每条菜品记录包含:菜名、基础价格 两个信息。 订单分:点菜记录和删除信息。每一类信息都可包含一条或多条记录,每条记录一行。 点菜记录包含:序号、菜名、份额、份数。 份额可选项包括:1、2、3,分别代表小、中、大份。 删除记录格式:序号 delete 标识删除对应序号的那条点菜记录。 不同份额菜价的计算方法: 小份菜的价格=菜品的基础价格。 中份菜的价格=菜品的基础价格1.5。 小份菜的价格=菜品的基础价格2。 如果计算出现小数,按四舍五入的规则进行处理。
时间: 2023-05-29 14:02:22 浏览: 105
import java.util.HashMap;
import java.util.Scanner;
public class MenuPriceCalculator {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// 读取菜单信息
HashMap<String, Double> menu = new HashMap<>();
while (true) {
String input = sc.nextLine();
if (input.equals("end")) {
break;
}
String[] parts = input.split(" ");
menu.put(parts[0], Double.parseDouble(parts[1]));
}
// 读取订单信息
HashMap<Integer, Order> orders = new HashMap<>();
while (true) {
String input = sc.nextLine();
if (input.equals("end")) {
break;
}
String[] parts = input.split(" ");
int orderNum = Integer.parseInt(parts[0]);
if (parts.length == 2 && parts[1].equals("delete")) {
orders.remove(orderNum);
} else {
String dishName = parts[1];
int portion = Integer.parseInt(parts[2]);
String size = parts[3];
double pricePerPortion = menu.get(dishName);
if (size.equals("2")) {
pricePerPortion *= 1.5;
} else if (size.equals("3")) {
pricePerPortion *= 2;
}
double totalPrice = Math.round(pricePerPortion * portion * 10) / 10.0; // 保留1位小数,四舍五入
orders.put(orderNum, new Order(dishName, portion, size, totalPrice));
}
}
// 计算总价格并输出
double total = 0;
for (Order order : orders.values()) {
total += order.totalPrice;
}
System.out.println("Total price: " + total);
}
private static class Order {
String dishName;
int portion;
String size;
double totalPrice;
public Order(String dishName, int portion, String size, double totalPrice) {
this.dishName = dishName;
this.portion = portion;
this.size = size;
this.totalPrice = totalPrice;
}
}
}
```