Skip to content

Instantly share code, notes, and snippets.

@sombochea
Last active June 7, 2024 08:27
Show Gist options
  • Save sombochea/afc19b283846b4ac831bf72e525d02f9 to your computer and use it in GitHub Desktop.
Save sombochea/afc19b283846b4ac831bf72e525d02f9 to your computer and use it in GitHub Desktop.
Sample code for Orders
class Topping {
String name;
double price;
Topping(this.name, this.price);
}
class CafeItem {
String name;
int quantity;
double price;
List<Topping> toppings;
CafeItem(this.name, this.price, this.toppings, {this.quantity = 1});
double get total =>
price * quantity +
toppings.fold(0, (total, topping) => total + topping.price);
}
class Tax {
double rate;
Tax(this.rate);
double apply(double amount) => amount * rate;
}
class Order {
List<CafeItem> items;
Tax tax;
Order(this.items, this.tax);
double get total => items.fold(0, (total, item) => total + item.total);
double get taxAmount => tax.apply(total);
void printReceipt() {
print('Receipt:');
for (var item in items) {
print(' ${item.name} x${item.quantity} - \$${item.total}');
for (var topping in item.toppings) {
print(' ${topping.name} - \$${topping.price}');
}
}
print('Subtotal: \$$total');
print('Tax: \$$taxAmount');
print('Total: \$${total + taxAmount}');
}
}
void main() {
var order = Order([
CafeItem(
'Coffee',
2.5,
[
Topping('Milk', 0.5),
Topping('Ice Level 70%', 0),
Topping('Sugar 50%', 0)
],
quantity: 2),
CafeItem('Iced Latte', 3.5, [
Topping('Milk', 0.5),
Topping('Ice Cream', 1.0),
Topping('Sugar 100%', 0),
Topping('Whipped Cream', 0.5)
]),
CafeItem('Sandwich', 4.5, [Topping('Cheese', 1.0), Topping('Ham', 1.5)]),
CafeItem('Salad', 5.5, [Topping('Olives', 0.5), Topping('Artichokes', 1.0)],
quantity: 3),
], Tax(0.1));
order.printReceipt();
}
class Topping {
String name;
double price;
Topping(this.name, this.price);
}
class CafeItem {
String name;
int quantity;
double price;
List<Topping> toppings;
CafeItem(this.name, this.price, this.toppings, {this.quantity = 1});
double get total =>
price * quantity +
toppings.fold(0, (total, topping) => total + topping.price);
}
class Tax {
double rate;
Tax(this.rate);
double apply(double amount) => amount * rate;
@override
String toString() {
return 'Tax Rate: ${(rate * 100).toStringAsFixed(0)}%';
}
}
class Promotion {
final String name;
final DateTime startDate;
final DateTime expiryDate;
final String schedule;
final String conditionType;
final List<String> conditionValues;
final String discountType;
final double discountValue;
Promotion({
required this.name,
required this.startDate,
required this.expiryDate,
required this.schedule,
required this.conditionType,
required this.conditionValues,
required this.discountType,
required this.discountValue,
});
bool isApplicable(DateTime date) {
if (date.isBefore(startDate) || date.isAfter(expiryDate)) {
return false;
}
if (schedule.toLowerCase() == 'every friday' &&
date.weekday != DateTime.friday) {
return false;
}
return true;
}
double applyDiscount(double originalPrice, String category) {
if (conditionValues.contains(category)) {
if (discountType.toLowerCase() == 'percentage') {
return originalPrice * (1 - discountValue / 100);
} else if (discountType.toLowerCase() == 'fixed amount') {
return originalPrice - discountValue;
} else if (discountType.toLowerCase() == 'free') {
return 0.0;
}
}
return originalPrice;
}
}
class Order {
List<CafeItem> items;
Tax tax;
Promotion? promotion;
Order(this.items, this.tax, {this.promotion});
double get total {
double total = 0;
DateTime now = DateTime.now();
for (var item in items) {
double itemTotal = item.total;
if (promotion != null && promotion!.isApplicable(now)) {
itemTotal = promotion!.applyDiscount(itemTotal, item.name);
}
total += itemTotal;
}
return total;
}
double get promotionDiscount {
double discount = 0;
DateTime now = DateTime.now();
for (var item in items) {
double itemTotal = item.total;
if (promotion != null && promotion!.isApplicable(now)) {
discount += itemTotal - promotion!.applyDiscount(itemTotal, item.name);
}
}
return discount;
}
double get taxAmount => tax.apply(total);
double get totalAmount => total + taxAmount;
void printReceipt() {
print('Receipt:');
for (var item in items) {
double itemTotal = item.total;
if (promotion != null && promotion!.isApplicable(DateTime.now())) {
itemTotal = promotion!.applyDiscount(itemTotal, item.name);
}
print(
' ${item.name} x${item.quantity} - \$${itemTotal.toStringAsFixed(2)}');
for (var topping in item.toppings) {
print(' ${topping.name} - \$${topping.price}');
}
}
String subtotal = (total + promotionDiscount).toStringAsFixed(2);
print('---');
print('Subtotal: \$$subtotal');
print('Promotion: ${promotion != null ? promotion!.name : 'None'}');
print('Discount: \$${promotionDiscount.toStringAsFixed(2)}');
print('Total: \$$total');
print('Tax: \$$taxAmount ($tax)');
print('Grand Total: \$${(totalAmount).toStringAsFixed(2)}');
}
}
void main() {
Promotion blackFridaySale = Promotion(
name: 'Black Friday Sale',
startDate: DateTime.parse('2024-06-05 08:00:00'),
expiryDate: DateTime.parse('2024-07-10 17:00:00'),
schedule: 'Every Friday',
conditionType: 'Category',
conditionValues: ['Coffee', 'Iced Latte', 'Sandwich', 'Salad'],
discountType: 'Percentage',
discountValue: 10.0,
);
var order = Order([
CafeItem(
'Coffee',
2.5,
[
Topping('Milk', 0.5),
Topping('Ice Level 70%', 0),
Topping('Sugar 50%', 0)
],
quantity: 2),
CafeItem('Iced Latte', 3.5, [
Topping('Milk', 0.5),
Topping('Ice Cream', 1.0),
Topping('Sugar 100%', 0),
Topping('Whipped Cream', 0.5)
]),
CafeItem('Sandwich', 4.5, [Topping('Cheese', 1.0), Topping('Ham', 1.5)]),
CafeItem('Salad', 5.5, [Topping('Olives', 0.5), Topping('Artichokes', 1.0)],
quantity: 3),
], Tax(0.1), promotion: blackFridaySale);
order.printReceipt();
}
class Topping {
String name;
double price;
Topping(this.name, this.price);
}
class Pizza {
String name;
double price;
List<Topping> toppings;
Pizza(this.name, this.price, this.toppings);
double get total => price + toppings.fold(0, (total, topping) => total + topping.price);
}
class Order {
List<Pizza> pizzas;
Order(this.pizzas);
double get total => pizzas.fold(0, (total, pizza) => total + pizza.total);
void printReceipt() {
print('Receipt:');
for (var pizza in pizzas) {
print(' ${pizza.name} - \$${pizza.total}');
for (var topping in pizza.toppings) {
print(' ${topping.name} - \$${topping.price}');
}
}
print('Total: \$$total');
}
}
void main() {
var order = Order([
Pizza('Margherita', 5.5, []),
Pizza('Capricciosa', 7.5, [Topping('Mushrooms', 1.0), Topping('Ham', 1.5)]),
Pizza('Quattro Stagioni', 8.5, [Topping('Mushrooms', 1.0), Topping('Ham', 1.5), Topping('Olives', 0.5), Topping('Artichokes', 1.0)]),
]);
order.printReceipt();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment