给你一笔账单帮我只能识别出对应的货物和数量并根据单价完成计算
时间: 2024-04-19 18:24:16 浏览: 11
好的,以下是一个简单的示例代码,它可以根据给定的账单信息识别每样货物并计算总价格:
```java
import java.util.HashMap;
import java.util.Map;
public class PriceCalculator {
public static void main(String[] args) {
// 创建货物数量和单价的映射关系
Map<String, Double> priceMap = new HashMap<>();
priceMap.put("豆干", 10.0);
priceMap.put("豆筋", 8.0);
priceMap.put("豆皮", 6.0);
// 添加更多货物的单价
// 创建账单信息
Map<String, Integer> bill = new HashMap<>();
bill.put("豆干", 5);
bill.put("豆筋", 5);
bill.put("豆皮", 5);
// 添加更多货物的数量
// 计算每样货物的价格
double total = 0.0;
for (String item : bill.keySet()) {
int quantity = bill.get(item);
double price = priceMap.get(item);
double itemTotal = price * quantity;
System.out.println(item + ":" + itemTotal + "元");
total += itemTotal;
}
System.out.println("总价格:" + total + "元");
}
}
```
你可以根据实际情况修改 `priceMap` 和 `bill` 的内容,添加更多的货物以及对应的单价和数量。运行该代码将会输出每样货物的价格以及总价格。