Menu
Coddy logo textTech

Gesamtkosten berechnen

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

challenge icon

Aufgabe

Einfach

Erstellen Sie ein Programm, das das Projekt Warenkorb-Rechner fortsetzt, indem es die Funktionalität zur Berechnung der Gesamtkosten aller Artikel im Warenkorb implementiert. Aufbauend auf der Warenkorb-Verwaltung der vorherigen Lektion sollte Ihr Programm Folgendes tun:

  1. Verwenden Sie die vorhandene Produktkatalog-Map und die gefüllte Warenkorb-Liste aus dem vorherigen Setup
  2. Lesen Sie mehrere String-Eingaben ein, die Artikel darstellen, die Kunden ihrem Warenkorb hinzufügen möchten (die Eingabe endet, wenn Sie "cart_done" erhalten)
  3. Überprüfen Sie für jeden Artikel mit containsKey(), ob er im Produktkatalog existiert
  4. Wenn der Artikel existiert, fügen Sie ihn mit der Methode add() zur Warenkorb-Liste hinzu
  5. Berechnen Sie die Gesamtkosten, indem Sie die Warenkorb-Liste durchlaufen und den Preis jedes Artikels in der Produkt-Map nachschlagen
  6. Summieren Sie alle Preise auf, um die endgültigen Gesamtkosten zu erhalten
  7. Geben Sie den Bericht zur Warenkorbberechnung in dem unten gezeigten exakten Format aus

Wenn das Geschäft beispielsweise die Produkte "Apple" für 1.50, "Banana" für 0.80, "Orange" für 2.00, "Milk" für 3.25 führt und Kunden "Apple", "Banana", "Apple", "Milk" hinzufügen möchten, sollte Ihr Programm Folgendes ausgeben:

Shopping Cart Operations:
Adding 'Apple': Item added to cart successfully
Adding 'Banana': Item added to cart successfully
Adding 'Apple': Item added to cart successfully
Adding 'Milk': Item added to cart successfully
Cart Contents: [Apple, Banana, Apple, Milk]
Cost Calculation:
Apple: $1.50
Banana: $0.80
Apple: $1.50
Milk: $3.25
Total Cost: $7.05
Cart Summary:
Total items in cart: 4
Unique products in cart: 3
Items successfully added: 4
Items not found: 0
Cart Status: Ready for checkout

Wenn das Geschäft die Produkte "Laptop" für 999.99, "Mouse" für 25.50, "Keyboard" für 75.00 führt und Kunden "Mouse", "Keyboard", "Mouse", "Monitor" hinzufügen möchten, sollte Ihr Programm Folgendes ausgeben:

Shopping Cart Operations:
Adding 'Mouse': Item added to cart successfully
Adding 'Keyboard': Item added to cart successfully
Adding 'Mouse': Item added to cart successfully
Adding 'Monitor': Item not available in store
Cart Contents: [Mouse, Keyboard, Mouse]
Cost Calculation:
Mouse: $25.50
Keyboard: $75.00
Mouse: $25.50
Total Cost: $126.00
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 das Geschäft die Produkte "Novel" für 12.99, "Magazine" für 4.50, "Textbook" für 89.00 führt und Kunden "Novel", "Magazine", "Novel", "Textbook", "Dictionary" hinzufügen möchten, sollte Ihr Programm Folgendes ausgeben:

Shopping Cart Operations:
Adding 'Novel': Item added to cart successfully
Adding 'Magazine': Item added to cart successfully
Adding 'Novel': Item added to cart successfully
Adding 'Textbook': Item added to cart successfully
Adding 'Dictionary': Item not available in store
Cart Contents: [Novel, Magazine, Novel, Textbook]
Cost Calculation:
Novel: $12.99
Magazine: $4.50
Novel: $12.99
Textbook: $89.00
Total Cost: $119.48
Cart Summary:
Total items in cart: 4
Unique products in cart: 3
Items successfully added: 4
Items not found: 1
Cart Status: Ready for checkout

Ihr Programm muss jedes Element in der Warenkorb-Liste durchlaufen, seinen Preis in der Produktkatalog-Map nachschlagen und diesen Preis zu einer laufenden Summe addieren. Verwenden Sie eine Schleife, um jeden Warenkorb-Artikel zu verarbeiten und die Gesamtkosten zu akkumulieren. Zeigen Sie den Einzelpreis jedes Artikels während der Kostenberechnungsphase an. Formatieren Sie alle Preise so, dass sie genau 2 Dezimalstellen anzeigen. Das Eingabeformat ist: Artikelnamen, die dem Warenkorb hinzugefügt werden sollen, endend mit "cart_done".

Probier es selbst

import 'dart:io';

void main() {
  // Product catalog with items and prices
  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
  };
  
  // Shopping cart to store selected items
  List<String> shoppingCart = [];
  
  // Counters for tracking operations
  int successfulAdditions = 0;
  int itemsNotFound = 0;
  
  print("Shopping Cart Operations:");
  
  // Read items until "cart_done" is received
  String? item;
  while ((item = stdin.readLineSync()) != "cart_done") {
    if (item != null) {
      if (productCatalog.containsKey(item)) {
        shoppingCart.add(item);
        successfulAdditions++;
        print("Adding '$item': Item added to cart successfully");
      } else {
        itemsNotFound++;
        print("Adding '$item': Item not available in store");
      }
    }
  }
  
  // Display cart contents
  print("Cart Contents: $shoppingCart");
  
  // Calculate unique products
  Set<String> uniqueProducts = shoppingCart.toSet();
  
  // Display cart summary
  print("Cart Summary:");
  print("Total items in cart: ${shoppingCart.length}");
  print("Unique products in cart: ${uniqueProducts.length}");
  print("Items successfully added: $successfulAdditions");
  print("Items not found: $itemsNotFound");
  print("Cart Status: Ready for checkout");
}

Alle Lektionen in Logik & Ablauf