Last active
April 30, 2019 19:40
-
-
Save tamunoibi/6ae018da2c03f3e76465f74908f35998 to your computer and use it in GitHub Desktop.
Andela challanges
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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