Menu
Coddy logo textTech

Artikel zum Warenkorb hinzufügen

Teil des Abschnitts Logik & Ablauf der Dart-Journey von Coddy — Lektion 34 von 65.

challenge icon

Aufgabe

Einfach

Erstellen Sie ein Programm, das die Funktionalität zum Hinzufügen von Artikeln zu einem Warenkorb implementiert. Ihr Programm sollte eine fest im Code definierte Produktkatalog-Map und eine leere Warenkorb-Liste verwenden und dann Artikeleingaben vom Benutzer verarbeiten.

Definieren Sie den folgenden Produktkatalog direkt in Ihrem 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
};

Ihr Programm sollte:

  1. Die oben definierte Produktkatalog-Map und eine leere Warenkorb-Liste verwenden
  2. Mehrere String-Eingaben lesen, die Artikel darstellen, die Kunden ihrem Warenkorb hinzufügen möchten (die Eingabe endet, wenn Sie "cart_done" erhalten)
  3. Für jeden Artikel mit containsKey() prüfen, ob er im Produktkatalog existiert
  4. Wenn der Artikel existiert, ihn mit der Methode add() zur Warenkorb-Liste hinzufügen
  5. Wenn der Artikel nicht existiert, ihn überspringen und mit der Verarbeitung fortfahren
  6. Erfolgreiche Hinzufügungen und fehlgeschlagene Versuche nachverfolgen
  7. Den Warenkorb-Verwaltungsbericht in dem unten gezeigten exakten Format anzeigen

Wenn Kunden beispielsweise "Apple", "Bread", "Banana", "Apple", "Juice" hinzufügen möchten, sollte Ihr Programm Folgendes ausgeben:

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

Wenn Kunden "Mouse", "Monitor", "Keyboard", "Mouse" hinzufügen möchten, sollte Ihr Programm Folgendes ausgeben:

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

Wenn Kunden "Novel", "Dictionary", "Magazine", "Novel", "Comic", "Textbook" hinzufügen möchten, sollte Ihr Programm Folgendes ausgeben:

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

Ihr Programm muss containsKey() verwenden, um zu überprüfen, ob jeder Artikel im Produktkatalog vorhanden ist, bevor er zum Warenkorb hinzugefügt wird. Verwenden Sie die Methode add(), um gültige Artikel an die Warenkorb-Liste anzuhängen. Zählen Sie die Gesamtzahl der Artikel im Warenkorb mit der Eigenschaft length und ermitteln Sie die Anzahl der eindeutigen Produkte, indem Sie den Warenkorb in ein Set umwandeln und dessen Länge prüfen. Verfolgen Sie sowohl erfolgreiche Hinzufügungen als auch Artikel, die nicht im Katalog gefunden wurden. Das Eingabeformat ist: Artikelnamen, die dem Warenkorb hinzugefügt werden sollen, endend mit "cart_done".

Probier es selbst

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

Alle Lektionen in Logik & Ablauf