Skip to content

Instantly share code, notes, and snippets.

@tamunoibi
Last active April 30, 2019 19:40
Show Gist options
  • Save tamunoibi/6ae018da2c03f3e76465f74908f35998 to your computer and use it in GitHub Desktop.
Save tamunoibi/6ae018da2c03f3e76465f74908f35998 to your computer and use it in GitHub Desktop.
Andela challanges
class ShoppingCart {
constructor(){
this.total = 0;
this.items = { };
}
addItem(itemName, quantity, price) {
let cost = quantity * price;
this.total = this.total + cost;
this.items[itemName] = quantity;
}
removeItem(itemName, quantity, price) {
let cost = quantity * price;
this.total = this.total - cost;
let oldQuantity = this.items[itemName];
this.items[itemName] = (quantity > oldQuantity) ? 0 : oldQuantity - quantity;
}
checkout(cashPaid) {
if (cashPaid < this.total) {
return "Cash paid not enough";
} else {
return cashPaid - this.total;
}
}
}
class Shop extends ShoppingCart {
constructor(){
super();
this.quantity = 100;
}
removeItem(...args) {
if (args.length === 0) {
this.quantity = this.quantity - 1;
} else {
let [itemName, quantity, price] = args;
let cost = quantity * price;
this.total = this.total - cost;
let oldQuantity = this.items[itemName];
this.items[itemName] = (quantity > oldQuantity) ? 0 : oldQuantity - quantity;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment