Menu
Coddy logo textTech

Applying a Discount

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

challenge icon

Challenge

Easy

Create a program that continues the shopping cart calculator project by implementing a discount system that applies percentage-based discounts to qualifying orders. Building on the previous lesson's total cost calculation, 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. Display the discount calculation report 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, and the discount percentage is 10, 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
Cart Summary:
Total items in cart: 4
Unique products in cart: 3
Items successfully added: 4
Items not found: 0
Discount Status: 10% discount applied
Cart Status: Ready for checkout

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, and the discount percentage is 15, 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
Cart Summary:
Total items in cart: 2
Unique products in cart: 2
Items successfully added: 2
Items not found: 0
Discount Status: No discount applied
Cart Status: Ready for checkout

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, and the discount percentage is 20, 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
Cart Summary:
Total items in cart: 4
Unique products in cart: 3
Items successfully added: 4
Items not found: 0
Discount Status: 20% discount applied
Cart Status: Ready for checkout

Your program must calculate the subtotal first, then check if it meets or exceeds the discount threshold using a conditional if statement. If the threshold is met, calculate the discount amount by multiplying the subtotal by the discount percentage divided by 100. Subtract the discount amount from the subtotal to get the final total. Format all monetary values to show exactly 2 decimal places. The input format will be: item names to add to the cart ending with "cart_done", followed by the discount threshold amount and discount percentage (both as strings that need to be converted to appropriate numeric types).

Try it yourself

import 'dart:io';

void main() {
  // Product catalog with prices
  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
  };
  
  // Shopping cart list
  List<String> cart = [];
  
  // Counters for summary
  int itemsAdded = 0;
  int itemsNotFound = 0;
  
  print('Shopping Cart Operations:');
  
  // Read items until "cart_done"
  String? item;
  while ((item = stdin.readLineSync()) != 'cart_done') {
    if (item != null) {
      if (products.containsKey(item)) {
        cart.add(item);
        print("Adding '$item': Item added to cart successfully");
        itemsAdded++;
      } else {
        print("Adding '$item': Item not available in store");
        itemsNotFound++;
      }
    }
  }
  
  // Display cart contents
  print('Cart Contents: $cart');
  
  // Calculate total cost
  print('Cost Calculation:');
  double totalCost = 0.0;
  
  for (String cartItem in cart) {
    double price = products[cartItem]!;
    totalCost += price;
    print('$cartItem: \$${price.toStringAsFixed(2)}');
  }
  
  print('Total Cost: \$${totalCost.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('Cart Status: Ready for checkout');
}

All lessons in Logic & Flow