应用折扣
本课属于 Coddy Dart 学习旅程的“逻辑与流程控制”单元。第 36 课,共 65 课。
挑战
简单创建一个程序,继续完成购物车计算器项目,实现一个折扣系统,为符合条件的订单提供按百分比计算的折扣。在上一课计算总费用的基础上,你的程序应当:
- 使用之前设置中的现有商品目录 Map 和购物车功能
- 读取多个字符串输入,表示顾客想要添加到购物车的商品(当你收到
"cart_done"时,输入结束) - 对于每件商品,使用
containsKey()检查它是否存在于商品目录中,并将有效商品添加到购物车 - 将购物车中所有商品的价格相加,计算小计
- 读取一个表示折扣门槛金额的字符串输入
- 读取一个表示要应用的折扣百分比的字符串输入
- 仅当小计达到或超过门槛金额时才应用折扣
- 计算折扣金额以及应用折扣后的最终总额
- 按照下方所示的准确格式显示折扣计算报告
例如,如果商店有价格为 "Apple" 对应 1.50、"Banana" 对应 0.80、"Orange" 对应 2.00、"Milk" 对应 3.25 的商品,顾客想要添加 "Apple"、"Orange"、"Milk"、"Apple",折扣门槛为 5.00,折扣百分比为 10,你的程序应输出:
Shopping Cart Operations:
Adding 'Apple': Item added to cart successfully
Adding 'Orange': Item added to cart successfully
Adding 'Milk': Item added to cart successfully
Adding 'Apple': Item added to cart successfully
Cart Contents: [Apple, Orange, Milk, Apple]
Cost Calculation:
Apple: $1.50
Orange: $2.00
Milk: $3.25
Apple: $1.50
Subtotal: $8.25
Discount Analysis:
Threshold: $5.00
Discount Rate: 10%
Subtotal ($8.25) meets threshold requirement
Discount Applied: $0.83
Final Total: $7.42
Cart Summary:
Total items in cart: 4
Unique products in cart: 3
Items successfully added: 4
Items not found: 0
Discount Status: 10% discount applied
Cart Status: Ready for checkout如果商店有价格为 "Laptop" 对应 999.99、"Mouse" 对应 25.50、"Keyboard" 对应 75.00 的商品,顾客想要添加 "Mouse"、"Keyboard",折扣门槛为 150.00,折扣百分比为 15,你的程序应输出:
Shopping Cart Operations:
Adding 'Mouse': Item added to cart successfully
Adding 'Keyboard': Item added to cart successfully
Cart Contents: [Mouse, Keyboard]
Cost Calculation:
Mouse: $25.50
Keyboard: $75.00
Subtotal: $100.50
Discount Analysis:
Threshold: $150.00
Discount Rate: 15%
Subtotal ($100.50) does not meet threshold requirement
Discount Applied: $0.00
Final Total: $100.50
Cart Summary:
Total items in cart: 2
Unique products in cart: 2
Items successfully added: 2
Items not found: 0
Discount Status: No discount applied
Cart Status: Ready for checkout如果商店有价格为 "Novel" 对应 12.99、"Magazine" 对应 4.50、"Textbook" 对应 89.00 的商品,顾客想要添加 "Novel"、"Textbook"、"Magazine"、"Novel",折扣门槛为 75.00,折扣百分比为 20,你的程序应输出:
Shopping Cart Operations:
Adding 'Novel': Item added to cart successfully
Adding 'Textbook': Item added to cart successfully
Adding 'Magazine': Item added to cart successfully
Adding 'Novel': Item added to cart successfully
Cart Contents: [Novel, Textbook, Magazine, Novel]
Cost Calculation:
Novel: $12.99
Textbook: $89.00
Magazine: $4.50
Novel: $12.99
Subtotal: $119.48
Discount Analysis:
Threshold: $75.00
Discount Rate: 20%
Subtotal ($119.48) meets threshold requirement
Discount Applied: $23.90
Final Total: $95.58
Cart Summary:
Total items in cart: 4
Unique products in cart: 3
Items successfully added: 4
Items not found: 0
Discount Status: 20% discount applied
Cart Status: Ready for checkout你的程序必须先计算小计,然后使用条件 if 语句检查小计是否达到或超过折扣门槛。如果达到门槛,则用小计乘以折扣百分比再除以 100,计算折扣金额。从小计中减去折扣金额,得到最终总额。所有金额都必须格式化为恰好 2 位小数。输入格式为:要添加到购物车的商品名称,以 "cart_done" 结束,随后输入折扣门槛金额和折扣百分比(两者都是需要转换为适当数值类型的字符串)。
亲手试试
import 'dart:io';
void main() {
// Product catalog with prices
Map<String, double> products = {
'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 list
List<String> cart = [];
// Counters for summary
int itemsAdded = 0;
int itemsNotFound = 0;
print('Shopping Cart Operations:');
// Read items until "cart_done"
String? item;
while ((item = stdin.readLineSync()) != 'cart_done') {
if (item != null) {
if (products.containsKey(item)) {
cart.add(item);
print("Adding '$item': Item added to cart successfully");
itemsAdded++;
} else {
print("Adding '$item': Item not available in store");
itemsNotFound++;
}
}
}
// Display cart contents
print('Cart Contents: $cart');
// Calculate total cost
print('Cost Calculation:');
double totalCost = 0.0;
for (String cartItem in cart) {
double price = products[cartItem]!;
totalCost += price;
print('$cartItem: \$${price.toStringAsFixed(2)}');
}
print('Total Cost: \$${totalCost.toStringAsFixed(2)}');
// Cart summary
Set<String> uniqueProducts = cart.toSet();
print('Cart Summary:');
print('Total items in cart: ${cart.length}');
print('Unique products in cart: ${uniqueProducts.length}');
print('Items successfully added: $itemsAdded');
print('Items not found: $itemsNotFound');
print('Cart Status: Ready for checkout');
}逻辑与流程控制中的所有课
自己练习: Dart 在线编译器