Menu
Coddy logo textTech

Adding Items to the Cart

Part of the Logic & Flow section of Coddy's Dart journey — lesson 34 of 65.

challenge icon

Challenge

Easy

Create a program that implements the functionality to add items to a shopping cart. Your program should use a hardcoded product catalog Map and an empty shopping cart List, then process item inputs from the user.

Define the following product catalog directly in your code:

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

Your program should:

  1. Use the product catalog Map defined above and an empty shopping cart List
  2. Read multiple string inputs representing items that customers want to add to their cart (the input will end when you receive "cart_done")
  3. For each item, check if it exists in the product catalog using containsKey()
  4. If the item exists, add it to the shopping cart List using the add() method
  5. If the item doesn't exist, skip it and continue processing
  6. Track successful additions and failed attempts
  7. Display the cart management report in the exact format shown below

For example, if customers want to add "Apple", "Bread", "Banana", "Apple", "Juice", your program should output:

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

If customers want to add "Mouse", "Monitor", "Keyboard", "Mouse", your program should output:

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

If customers want to add "Novel", "Dictionary", "Magazine", "Novel", "Comic", "Textbook", your program should output:

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

Your program must use containsKey() to verify that each item exists in the product catalog before adding it to the cart. Use the add() method to append valid items to the shopping cart List. Count the total items in the cart using the length property, and determine unique products by converting the cart to a Set and checking its length. Track both successful additions and items that were not found in the catalog. The input format will be: item names to add to the cart, ending with "cart_done".

Try it yourself

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

All lessons in Logic & Flow