tdd-workshop-exercises/shopping_cart/lib/shopping_cart.dart
fiatcode 3d94c96ed2 Initial commit: TDD Workshop exercises for participants
- Password Validator kata with starter code and tests
- Shopping Cart kata with starter code and tests
- FizzBuzz reference code (from live demo)
- Setup guide and TDD reference card
- No solutions included (participants implement themselves)
2026-03-10 15:37:58 +07:00

46 lines
1.2 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// TODO: Implement ShoppingCart using TDD.
//
// Domain rules (implement one at a time, test first!):
// 1. Add an item (name, price, quantity) to the cart
// 2. Get the subtotal (sum of price × quantity for all items)
// 3. Remove an item by name
// 4. Get total item count (sum of all quantities)
// 5. Apply a percentage discount to the subtotal (e.g. 10% off)
// 6. Items with zero or negative price are rejected
//
// CartItem is a Value Object — immutable, validated at construction.
// ShoppingCart is stateful — it accumulates items across calls.
class CartItem {
final String name;
final double price;
final int quantity;
CartItem({required this.name, required this.price, required this.quantity}) {
// TODO: validate price > 0 and quantity > 0
}
}
class ShoppingCart {
// TODO: implement
void addItem(CartItem item) {
throw UnimplementedError();
}
void removeItem(String name) {
throw UnimplementedError();
}
double subtotal() {
throw UnimplementedError();
}
int itemCount() {
throw UnimplementedError();
}
double totalAfterDiscount(double discountPercent) {
throw UnimplementedError();
}
}