Skip to content

Instantly share code, notes, and snippets.

@xros
Created December 19, 2021 00:26
Show Gist options
  • Save xros/74d5d435d322e3c088040e7f48b809ce to your computer and use it in GitHub Desktop.
Save xros/74d5d435d322e3c088040e7f48b809ce to your computer and use it in GitHub Desktop.
import 'dart:io';
/*
* An implementation of shopping cart and checkout in Dart
*
*/
class Product {
const Product({required this.id, required this.name, required this.price});
final int id;
final String name;
final double price;
String get displayNames => '($initial)${name.substring(1)}, price \$$price';
String get initial => name.substring(0, 1);
}
class Item {
const Item(this.product, this.quantity);
final Product product;
final int quantity;
double get price => product.price * quantity;
@override
String toString() => "$quantity x ${product.name} \$$price";
}
class Cart {
// private value
final Map<int, Item> _items = {};
void addProduct(Product product) {
final item = _items[product.id];
if (item == null) {
_items[product.id] = Item(product, 1);
} else {
_items[product.id] = Item(product, item.quantity + 1);
}
}
bool get isEmpty => _items.isEmpty;
// get total
double total() => _items.values
.map((item) => item.price)
.reduce((value, element) => value + element);
@override
String toString() {
if (_items.isEmpty) {
return 'Cart is empty';
}
final itemizedList =
_items.values.map((item) => item.toString()).join('\n');
return "------\n$itemizedList\nTotal: \$${total()}\n------";
}
}
const List<Product> allProducts = [
Product(id: 1, name: 'apple', price: 1.1),
Product(id: 2, name: 'banana', price: 2.2),
Product(id: 3, name: 'milk', price: 3.3),
];
// loop (Pseudocode)
// prompt: view cart / add item / checkout
// if selection == add item
// choose a product
// add it to cart
// print cart
// else if selection == view cart
// print cart
// else if selection == checkout
// do checkout
// end
void main() {
final cart = Cart();
while (true) {
stdout.write(
'What do you want to do? (v)iew items, (a)dd items, (c)heckout: ');
final line = stdin.readLineSync();
if (line == 'a') {
final product = chooseProduct();
if (product != null) {
cart.addProduct(product);
print(cart);
}
} else if (line == 'v') {
print(cart);
} else if (line == 'c') {
if (checkout(cart)) {
break;
}
}
}
}
Product? chooseProduct() {
final productList = allProducts.map((e) => e.displayNames).join('\n');
stdout.write('Available products: \n$productList\nYour Choice: ');
final line = stdin.readLineSync();
for (var product in allProducts) {
if (product.initial == line) {
return product;
}
}
print('Not found');
return null;
}
bool checkout(Cart cart) {
if (cart.isEmpty) {
print('Cart is empty');
return false;
}
final total = cart.total();
print('Total: \$$total');
stdout.write('Payment in cash: ');
final line = stdin.readLineSync();
if (line == null || line.isEmpty) {
return false;
}
final paid = double.tryParse(line);
if (paid == null) {
return false;
}
if (paid >= total) {
final change = paid - total;
print('Change: \$${change.toStringAsFixed(2)}');
return true;
} else {
print('Not enough cash');
return false;
}
}
@xros
Copy link
Author

xros commented Dec 19, 2021

The interactive output in console
iShot2021-12-19 03 25 56

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment