Menu
CoddyTech

显示最终收据

本课属于 Coddy Dart 学习旅程的“逻辑与流程控制”单元。第 37 课,共 65 课。

challenge icon

挑战

简单

创建一个程序,通过生成一张包含所有交易详情、格式专业的完整收据,完成购物车计算器项目。在上一课折扣系统的基础上,你的程序应当:

  1. 使用前面设置中已有的商品目录 Map 和购物车功能
  2. 读取多个字符串输入,表示顾客想要加入购物车的商品(收到 "cart_done" 时结束输入)
  3. 对每件商品,使用 containsKey() 检查它是否存在于商品目录中,并将有效商品加入购物车
  4. 将购物车中所有商品的价格相加,计算小计
  5. 读取一个字符串输入,表示折扣门槛金额
  6. 读取一个字符串输入,表示要应用的折扣百分比
  7. 仅当小计达到或超过门槛金额时应用折扣
  8. 计算折扣金额以及应用折扣后的总金额
  9. 读取一个字符串输入,表示顾客姓名
  10. 读取一个字符串输入,表示交易 ID
  11. 按照下方所示的准确格式生成并显示包含所有交易详情的完整收据

例如,假设商店有价格分别为 "Apple" 的 1.50、"Banana" 的 0.80、"Orange" 的 2.00、"Milk" 的 3.25 这些商品,顾客想要加入 "Apple"、"Orange"、"Milk"、"Apple",折扣门槛为 5.00,折扣百分比为 10,顾客姓名为 "John Smith",交易 ID 为 "TXN001",那么你的程序应输出:

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
========================================
           FINAL RECEIPT
========================================
Transaction ID: TXN001
Customer: John Smith
Date: 2024-01-15
Time: 14:30:25
----------------------------------------
ITEMS PURCHASED:
Apple                           $1.50
Orange                          $2.00
Milk                            $3.25
Apple                           $1.50
----------------------------------------
Subtotal:                       $8.25
Discount (10%):                -$0.83
----------------------------------------
TOTAL:                          $7.42
----------------------------------------
Payment Method: Cash
Items Count: 4
Unique Products: 3
Thank you for shopping with us!
========================================

假设商店有价格分别为 "Laptop" 的 999.99、"Mouse" 的 25.50、"Keyboard" 的 75.00 这些商品,顾客想要加入 "Mouse"、"Keyboard",折扣门槛为 150.00,折扣百分比为 15,顾客姓名为 "Sarah Johnson",交易 ID 为 "TXN002",那么你的程序应输出:

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
========================================
           FINAL RECEIPT
========================================
Transaction ID: TXN002
Customer: Sarah Johnson
Date: 2024-01-15
Time: 14:30:25
----------------------------------------
ITEMS PURCHASED:
Mouse                          $25.50
Keyboard                       $75.00
----------------------------------------
Subtotal:                     $100.50
Discount (15%):                $0.00
----------------------------------------
TOTAL:                        $100.50
----------------------------------------
Payment Method: Cash
Items Count: 2
Unique Products: 2
Thank you for shopping with us!
========================================

假设商店有价格分别为 "Novel" 的 12.99、"Magazine" 的 4.50、"Textbook" 的 89.00 这些商品,顾客想要加入 "Novel"、"Textbook"、"Magazine"、"Novel",折扣门槛为 75.00,折扣百分比为 20,顾客姓名为 "Mike Davis",交易 ID 为 "TXN003",那么你的程序应输出:

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
========================================
           FINAL RECEIPT
========================================
Transaction ID: TXN003
Customer: Mike Davis
Date: 2024-01-15
Time: 14:30:25
----------------------------------------
ITEMS PURCHASED:
Novel                          $12.99
Textbook                       $89.00
Magazine                        $4.50
Novel                          $12.99
----------------------------------------
Subtotal:                     $119.48
Discount (20%):               -$23.90
----------------------------------------
TOTAL:                         $95.58
----------------------------------------
Payment Method: Cash
Items Count: 4
Unique Products: 3
Thank you for shopping with us!
========================================

你的程序必须正确设置最终收据的对齐方式和间距。使用字符串插值显示所有交易详情,包括顾客信息、逐项购买的商品、价格计算和汇总统计。收据应包含固定的日期(2024-01-15)、时间(14:30:25)和付款方式(Cash)。所有金额都要格式化为恰好 2 位小数。使用购物车长度计算商品数量,并通过将购物车转换为 Set 来计算不同商品的数量。输入格式为:要加入购物车的商品名称,以 "cart_done" 结束,之后依次输入折扣门槛金额、折扣百分比、顾客姓名和交易 ID(均为字符串)。

亲手试试

import 'dart:io';

void main() {
  // Product catalog
  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
  };
  
  List<String> cart = [];
  int itemsAdded = 0;
  int itemsNotFound = 0;
  
  print('Shopping Cart Operations:');
  
  // Read items until "cart_done"
  while (true) {
    String? item = stdin.readLineSync();
    if (item == 'cart_done') break;
    
    if (products.containsKey(item)) {
      cart.add(item!);
      print("Adding '$item': Item added to cart successfully");
      itemsAdded++;
    } else {
      print("Adding '$item': Item not found in catalog");
      itemsNotFound++;
    }
  }
  
  print('Cart Contents: $cart');
  
  // Calculate subtotal
  print('Cost Calculation:');
  double subtotal = 0.0;
  for (String item in cart) {
    double price = products[item]!;
    print('$item: \$${price.toStringAsFixed(2)}');
    subtotal += price;
  }
  print('Subtotal: \$${subtotal.toStringAsFixed(2)}');
  
  // Read discount threshold and percentage
  String? thresholdInput = stdin.readLineSync();
  String? discountPercentageInput = stdin.readLineSync();
  
  double threshold = double.parse(thresholdInput!);
  int discountPercentage = int.parse(discountPercentageInput!);
  
  // Discount analysis
  print('Discount Analysis:');
  print('Threshold: \$${threshold.toStringAsFixed(2)}');
  print('Discount Rate: $discountPercentage%');
  
  double discountAmount = 0.0;
  double finalTotal = subtotal;
  String discountStatus = 'No discount applied';
  
  if (subtotal >= threshold) {
    print('Subtotal (\$${subtotal.toStringAsFixed(2)}) meets threshold requirement');
    discountAmount = subtotal * (discountPercentage / 100);
    finalTotal = subtotal - discountAmount;
    discountStatus = '$discountPercentage% discount applied';
  } else {
    print('Subtotal (\$${subtotal.toStringAsFixed(2)}) does not meet threshold requirement');
  }
  
  print('Discount Applied: \$${discountAmount.toStringAsFixed(2)}');
  print('Final Total: \$${finalTotal.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('Discount Status: $discountStatus');
  print('Cart Status: Ready for checkout');
}

逻辑与流程控制中的所有课

自己练习: Dart 在线编译器