Skip to content

Instantly share code, notes, and snippets.

@mcsee
Last active August 26, 2023 01:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcsee/a7571cf0ce9e7666bd6a393520f9a968 to your computer and use it in GitHub Desktop.
Save mcsee/a7571cf0ce9e7666bd6a393520f9a968 to your computer and use it in GitHub Desktop.
class CartItem {
constructor(price) {
this.price = price;
}
}
class DiscountCoupon {
constructor(rate) {
this.rate = rate;
}
discount(subtotal) {
return subtotal * (1 - this.rate);
}
}
class NullCoupon {
discount(subtotal) {
return subtotal;
}
}
class Cart {
constructor(selecteditems, discountCoupon) {
this.items = selecteditems;
this.discountCoupon = discountCoupon;
}
subtotal() {
return this.items.reduce(
(previous, current) => previous + current.price, 0);
}
total() {
return this.discountCoupon.discount(this.subtotal());
}
}
cart = new Cart([
new CartItem(1),
new CartItem(2),
new CartItem(7)
], new DiscountCoupon(0.15));
// 10 - 1.5 = 8.5
cart = new Cart([
new CartItem(1),
new CartItem(2),
new CartItem(7)
], new NullCoupon());
// 10 - nullObject = 10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment