计算总价
本课属于 Coddy Dart 学习旅程的“逻辑与流程控制”单元。第 35 课,共 65 课。
挑战
简单创建一个程序,继续完成购物车计算器项目,实现计算购物车中所有商品总价的功能。在上一课购物车管理功能的基础上,你的程序应当:
- 使用前面设置中已有的商品目录 Map 和已填充的购物车 List
- 读取多个字符串输入,这些输入表示顾客想要加入购物车的商品(收到
"cart_done"时,输入结束) - 对于每件商品,使用
containsKey()检查它是否存在于商品目录中 - 如果商品存在,使用
add()方法将它加入购物车 List - 遍历购物车列表,并在商品 Map 中查询每件商品的价格,以计算总价
- 将所有价格相加,得出最终总价
- 按照下方所示的确切格式显示购物车计算报告
例如,如果商店有价格为 1.50 的商品 "Apple"、价格为 0.80 的商品 "Banana"、价格为 2.00 的商品 "Orange" 和价格为 3.25 的商品 "Milk",顾客想要添加 "Apple"、"Banana"、"Apple"、"Milk",那么你的程序应当输出:
Shopping Cart Operations:
Adding 'Apple': Item added to cart successfully
Adding 'Banana': Item added to cart successfully
Adding 'Apple': Item added to cart successfully
Adding 'Milk': Item added to cart successfully
Cart Contents: [Apple, Banana, Apple, Milk]
Cost Calculation:
Apple: $1.50
Banana: $0.80
Apple: $1.50
Milk: $3.25
Total Cost: $7.05
Cart Summary:
Total items in cart: 4
Unique products in cart: 3
Items successfully added: 4
Items not found: 0
Cart Status: Ready for checkout如果商店有价格为 999.99 的商品 "Laptop"、价格为 25.50 的商品 "Mouse" 和价格为 75.00 的商品 "Keyboard",顾客想要添加 "Mouse"、"Keyboard"、"Mouse"、"Monitor",那么你的程序应当输出:
Shopping Cart Operations:
Adding 'Mouse': Item added to cart successfully
Adding 'Keyboard': Item added to cart successfully
Adding 'Mouse': Item added to cart successfully
Adding 'Monitor': Item not available in store
Cart Contents: [Mouse, Keyboard, Mouse]
Cost Calculation:
Mouse: $25.50
Keyboard: $75.00
Mouse: $25.50
Total Cost: $126.00
Cart Summary:
Total items in cart: 3
Unique products in cart: 2
Items successfully added: 3
Items not found: 1
Cart Status: Ready for checkout如果商店有价格为 12.99 的商品 "Novel"、价格为 4.50 的商品 "Magazine" 和价格为 89.00 的商品 "Textbook",顾客想要添加 "Novel"、"Magazine"、"Novel"、"Textbook"、"Dictionary",那么你的程序应当输出:
Shopping Cart Operations:
Adding 'Novel': Item added to cart successfully
Adding 'Magazine': Item added to cart successfully
Adding 'Novel': Item added to cart successfully
Adding 'Textbook': Item added to cart successfully
Adding 'Dictionary': Item not available in store
Cart Contents: [Novel, Magazine, Novel, Textbook]
Cost Calculation:
Novel: $12.99
Magazine: $4.50
Novel: $12.99
Textbook: $89.00
Total Cost: $119.48
Cart Summary:
Total items in cart: 4
Unique products in cart: 3
Items successfully added: 4
Items not found: 1
Cart Status: Ready for checkout你的程序必须遍历购物车列表中的每件商品,在商品目录 Map 中查询其价格,并将该价格加到累计总价中。使用循环处理购物车中的每件商品并累计总价。在计算价格时,显示每件商品的单独价格。所有价格都必须格式化为恰好 2 位小数。输入格式为:要加入购物车的商品名称,以 "cart_done" 结束。
亲手试试
import 'dart:io';
void main() {
// Product catalog with items and prices
Map<String, double> productCatalog = {
'Apple': 1.50,
'Banana': 0.80,
'Orange': 2.00,
'Milk': 3.25,
'Laptop': 999.99,
'Mouse': 25.50,
'Keyboard': 75.00,
'Novel': 12.99,
'Magazine': 4.50,
'Textbook': 89.00
};
// Shopping cart to store selected items
List<String> shoppingCart = [];
// Counters for tracking operations
int successfulAdditions = 0;
int itemsNotFound = 0;
print("Shopping Cart Operations:");
// Read items until "cart_done" is received
String? item;
while ((item = stdin.readLineSync()) != "cart_done") {
if (item != null) {
if (productCatalog.containsKey(item)) {
shoppingCart.add(item);
successfulAdditions++;
print("Adding '$item': Item added to cart successfully");
} else {
itemsNotFound++;
print("Adding '$item': Item not available in store");
}
}
}
// Display cart contents
print("Cart Contents: $shoppingCart");
// Calculate unique products
Set<String> uniqueProducts = shoppingCart.toSet();
// Display cart summary
print("Cart Summary:");
print("Total items in cart: ${shoppingCart.length}");
print("Unique products in cart: ${uniqueProducts.length}");
print("Items successfully added: $successfulAdditions");
print("Items not found: $itemsNotFound");
print("Cart Status: Ready for checkout");
}逻辑与流程控制中的所有课
自己练习: Dart 在线编译器