Menu
CoddyTech

向购物车添加商品

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

challenge icon

挑战

简单

创建一个程序,实现将商品添加到购物车的功能。程序应使用硬编码的商品目录 Map 和一个空的购物车 List,然后处理用户输入的商品。

在代码中直接定义以下商品目录:

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
};

你的程序应:

  1. 使用上面定义的商品目录 Map 和一个空的购物车 List
  2. 读取多个表示顾客想要添加到购物车的字符串输入(收到 "cart_done" 时,输入结束)
  3. 对于每件商品,使用 containsKey() 检查它是否存在于商品目录中
  4. 如果商品存在,使用 add() 方法将其添加到购物车 List 中
  5. 如果商品不存在,则跳过它并继续处理
  6. 跟踪成功添加的次数和失败的尝试次数
  7. 按照下方所示的确切格式显示购物车管理报告

例如,如果顾客想添加 "Apple"、"Bread"、"Banana"、"Apple"、"Juice",你的程序应输出:

Shopping Cart Operations:
Adding 'Apple': Item added to cart successfully
Adding 'Bread': Item not available in store
Adding 'Banana': Item added to cart successfully
Adding 'Apple': Item added to cart successfully
Adding 'Juice': Item not available in store
Cart Contents: [Apple, Banana, Apple]
Cart Summary:
Total items in cart: 3
Unique products in cart: 2
Items successfully added: 3
Items not found: 2
Cart Status: Ready for checkout

如果顾客想添加 "Mouse"、"Monitor"、"Keyboard"、"Mouse",你的程序应输出:

Shopping Cart Operations:
Adding 'Mouse': Item added to cart successfully
Adding 'Monitor': Item not available in store
Adding 'Keyboard': Item added to cart successfully
Adding 'Mouse': Item added to cart successfully
Cart Contents: [Mouse, Keyboard, Mouse]
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

如果顾客想添加 "Novel"、"Dictionary"、"Magazine"、"Novel"、"Comic"、"Textbook",你的程序应输出:

Shopping Cart Operations:
Adding 'Novel': Item added to cart successfully
Adding 'Dictionary': Item not available in store
Adding 'Magazine': Item added to cart successfully
Adding 'Novel': Item added to cart successfully
Adding 'Comic': Item not available in store
Adding 'Textbook': Item added to cart successfully
Cart Contents: [Novel, Magazine, Novel, Textbook]
Cart Summary:
Total items in cart: 4
Unique products in cart: 3
Items successfully added: 4
Items not found: 2
Cart Status: Ready for checkout

你的程序必须使用 containsKey(),在将每件商品添加到购物车之前,验证它是否存在于商品目录中。使用 add() 方法将有效商品追加到购物车 List 中。使用 length 属性统计购物车中的商品总数,并将购物车转换为 Set,通过检查其 length 确定不重复商品的数量。跟踪成功添加的次数,以及商品目录中未找到的商品数量。输入格式为:要添加到购物车的商品名称,最后以 "cart_done" 结束。

亲手试试

import 'dart:io';

void main() {
  // Read store name
  String? storeName = stdin.readLineSync();
  
  // Initialize data structures
  Map<String, double> products = {};
  List<String> shoppingCart = [];
  
  // Read products until "products_done"
  while (true) {
    String? input = stdin.readLineSync();
    if (input == "products_done") {
      break;
    }
    String productName = input!;
    String? priceInput = stdin.readLineSync();
    double price = double.parse(priceInput!);
    
    // Add product to map
    products[productName] = price;
  }
  
  // Calculate statistics
  double totalPrice = 0;
  double maxPrice = 0;
  double minPrice = double.infinity;
  String mostExpensiveItem = '';
  String cheapestItem = '';
  
  for (String productName in products.keys) {
    double price = products[productName]!;
    totalPrice += price;
    
    if (price > maxPrice) {
      maxPrice = price;
      mostExpensiveItem = productName;
    }
    
    if (price < minPrice) {
      minPrice = price;
      cheapestItem = productName;
    }
  }
  
  double averagePrice = totalPrice / products.length;
  
  // Print the store setup report
  print('Store: $storeName');
  print('Product Catalog Setup:');
  print('Available Products: $products');
  print('Shopping Cart Initialized: $shoppingCart');
  print('System Status:');
  print('Total products available: ${products.length}');
  print('Cart status: Empty');
  print('Most expensive item: $mostExpensiveItem (\$${maxPrice.toStringAsFixed(2)})');
  print('Cheapest item: $cheapestItem (\$${minPrice.toStringAsFixed(2)})');
  print('Average product price: \$${averagePrice.toStringAsFixed(2)}');
  print('Setup Status: Shopping cart system ready for customers');
}

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

自己练习: Dart 在线编译器