Calculating the Total Cost
Part of the Logic & Flow section of Coddy's Dart journey — lesson 35 of 65.
Challenge
EasyCreate a program that continues the shopping cart calculator project by implementing the functionality to calculate the total cost of all items in the shopping cart. Building on the previous lesson's cart management, your program should:
- Use the existing product catalog Map and populated shopping cart List from the previous setup
- Read multiple string inputs representing items that customers want to add to their cart (the input will end when you receive
"cart_done") - For each item, check if it exists in the product catalog using
containsKey() - If the item exists, add it to the shopping cart List using the
add()method - Calculate the total cost by iterating through the cart list and looking up each item's price in the product map
- Sum up all the prices to get the final total cost
- Display the cart 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, and customers want to add "Apple", "Banana", "Apple", "Milk", your program should output:
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 checkoutIf the store has products "Laptop" at 999.99, "Mouse" at 25.50, "Keyboard" at 75.00, and customers want to add "Mouse", "Keyboard", "Mouse", "Monitor", your program should output:
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 checkoutIf the store has products "Novel" at 12.99, "Magazine" at 4.50, "Textbook" at 89.00, and customers want to add "Novel", "Magazine", "Novel", "Textbook", "Dictionary", your program should output:
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 checkoutYour program must iterate through each item in the shopping cart list, look up its price in the product catalog map, and add that price to a running total. Use a loop to process each cart item and accumulate the total cost. Display each item's individual price during the cost calculation phase. Format all prices to show exactly 2 decimal places. The input format will be: item names to add to the cart, ending with "cart_done".
Try it yourself
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");
}All lessons in Logic & Flow
1Advanced List Manipulation
List Properties: first & lastList State: isEmpty & isNotEmpReversing a ListAdding to a List: insertList Removal: removeWhereFinding in a List: indexOfSorting a ListShuffling a ListRecap - List Organizer4Advanced Map Manipulation
Iterating Over a MapChecking for Keys and ValuesMap Properties: keys & valuesConditional Add: putIfAbsentRemoving Entries from a MapNested MapsRecap - Inventory Update2Functional List Operations
Transforming with 'map'Filtering with 'where'Using '.toList()'Checking Conditions with 'any'Conditions with 'every'Finding with 'firstWhere'Recap - Data Filtering5Project: Shopping Cart Calc
Project SetupAdding Items to the Cart3Sets
What is a Set?Creating a SetAdding and Removing from SetsChecking for Elements in a SetConverting a List to a SetSet UnionSet IntersectionSet DifferenceRecap - Unique Guest List