Menu
Coddy logo textTech

Den fertigen Kassenbon anzeigen

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

challenge icon

Aufgabe

Einfach

Erstellen Sie ein Programm, das das Warenkorb-Rechner-Projekt vervollständigt, indem es einen umfassenden endgültigen Beleg generiert, der alle Transaktionsdetails in einem professionellen Format anzeigt. Aufbauend auf dem Rabattsystem der vorherigen Lektion sollte Ihr Programm Folgendes tun:

  1. Verwenden Sie die vorhandene Produktkatalog-Map und die Warenkorb-Funktionalität 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, und fügen Sie gültige Artikel dem Warenkorb hinzu
  4. Berechnen Sie die Zwischensumme, indem Sie alle Artikelpreise im Warenkorb addieren
  5. Lesen Sie eine String-Eingabe ein, die den Schwellenwert für den Rabatt darstellt
  6. Lesen Sie eine String-Eingabe ein, die den anzuwendenden Rabattsatz in Prozent darstellt
  7. Wenden Sie den Rabatt nur an, wenn die Zwischensumme den Schwellenwert erreicht oder überschreitet
  8. Berechnen Sie den Rabattbetrag und den Endbetrag nach Anwendung des Rabatts
  9. Lesen Sie eine String-Eingabe ein, die den Kundennamen darstellt
  10. Lesen Sie eine String-Eingabe ein, die die Transaktions-ID darstellt
  11. Generieren und zeigen Sie einen vollständigen Beleg mit allen Transaktionsdetails im exakt unten gezeigten Format an

Wenn der Laden beispielsweise die Produkte "Apple" für 1.50, "Banana" für 0.80, "Orange" für 2.00, "Milk" für 3.25 hat, Kunden "Apple", "Orange", "Milk", "Apple" hinzufügen möchten, der Rabatt-Schwellenwert 5.00 ist, der Rabattsatz 10 ist, der Kundenname "John Smith" ist und die Transaktions-ID "TXN001" lautet, sollte Ihr Programm Folgendes ausgeben:

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!
========================================

Wenn der Laden die Produkte "Laptop" für 999.99, "Mouse" für 25.50, "Keyboard" für 75.00 hat, Kunden "Mouse", "Keyboard" hinzufügen möchten, der Rabatt-Schwellenwert 150.00 ist, der Rabattsatz 15 ist, der Kundenname "Sarah Johnson" ist und die Transaktions-ID "TXN002" lautet, sollte Ihr Programm Folgendes ausgeben:

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!
========================================

Wenn der Laden die Produkte "Novel" für 12.99, "Magazine" für 4.50, "Textbook" für 89.00 hat, Kunden "Novel", "Textbook", "Magazine", "Novel" hinzufügen möchten, der Rabatt-Schwellenwert 75.00 ist, der Rabattsatz 20 ist, der Kundenname "Mike Davis" ist und die Transaktions-ID "TXN003" lautet, sollte Ihr Programm Folgendes ausgeben:

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!
========================================

Ihr Programm muss den endgültigen Beleg mit korrekter Ausrichtung und Abständen formatieren. Verwenden Sie String-Interpolation, um alle Transaktionsdetails anzuzeigen, einschließlich Kundeninformationen, Einzelkäufe, Preisberechnungen und zusammenfassende Statistiken. Der Beleg sollte feste Werte für das Datum (2024-01-15), die Uhrzeit (14:30:25) und die Zahlungsmethode (Cash) enthalten. Formatieren Sie alle Geldwerte so, dass sie genau 2 Dezimalstellen anzeigen. Berechnen Sie die Anzahl der Artikel anhand der Länge des Warenkorbs und die Anzahl der eindeutigen Produkte, indem Sie den Warenkorb in ein Set umwandeln. Das Eingabeformat ist: Artikelnamen, die dem Warenkorb hinzugefügt werden sollen, endend mit "cart_done", gefolgt vom Rabatt-Schwellenwert, dem Rabattsatz, dem Kundennamen und der Transaktions-ID (alle als Strings).

Probier es selbst

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

Alle Lektionen in Logik & Ablauf