Menu
Coddy logo textTech

Displaying the Final Receipt

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

challenge icon

Challenge

Easy

Create a program that completes the shopping cart calculator project by generating a comprehensive final receipt that displays all transaction details in a professional format. Building on the previous lesson's discount system, your program should:

  1. Use the existing product catalog Map and shopping cart functionality from the previous setup
  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() and add valid items to the cart
  4. Calculate the subtotal by summing up all item prices in the cart
  5. Read a string input representing the discount threshold amount
  6. Read a string input representing the discount percentage to apply
  7. Apply the discount only if the subtotal meets or exceeds the threshold amount
  8. Calculate the discount amount and final total after applying the discount
  9. Read a string input representing the customer name
  10. Read a string input representing the transaction ID
  11. Generate and display a complete receipt with all transaction details in the exact format shown below

For example, if the store has products "Apple" at 1.50, "Banana" at 0.80, "Orange" at 2.00, "Milk" at 3.25, customers want to add "Apple", "Orange", "Milk", "Apple", the discount threshold is 5.00, the discount percentage is 10, the customer name is "John Smith", and the transaction ID is "TXN001", your program should output:

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

If the store has products "Laptop" at 999.99, "Mouse" at 25.50, "Keyboard" at 75.00, customers want to add "Mouse", "Keyboard", the discount threshold is 150.00, the discount percentage is 15, the customer name is "Sarah Johnson", and the transaction ID is "TXN002", your program should output:

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

If the store has products "Novel" at 12.99, "Magazine" at 4.50, "Textbook" at 89.00, customers want to add "Novel", "Textbook", "Magazine", "Novel", the discount threshold is 75.00, the discount percentage is 20, the customer name is "Mike Davis", and the transaction ID is "TXN003", your program should output:

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

Your program must format the final receipt with proper alignment and spacing. Use string interpolation to display all transaction details including customer information, itemized purchases, pricing calculations, and summary statistics. The receipt should include fixed values for date (2024-01-15), time (14:30:25), and payment method (Cash). Format all monetary values to show exactly 2 decimal places. Calculate the items count using the cart length and unique products by converting the cart to a Set. The input format will be: item names to add to the cart ending with "cart_done", followed by the discount threshold amount, discount percentage, customer name, and transaction ID (all as strings).

Try it yourself

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

All lessons in Logic & Flow