-
-
Save avi202020/f55cc37d0bf6db7a51e6c8cec514312d to your computer and use it in GitHub Desktop.
06-Stu-Multiple-Classes
This file contains hidden or 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
const Store = require("./store"); | |
const { toys } = require("./toy"); | |
const store = new Store("Big Al's Toy Barn", toys); | |
store.welcome(); | |
store.processProductSale("Action Figure"); | |
store.processProductSale("Action Figure"); | |
store.processProductSale("Rare Toy"); | |
store.processProductSale("Action Figure"); | |
store.processProductSale("Rare Toy"); | |
store.replenishStock("Rare Toy", 2); | |
store.processProductSale("Rare Toy"); | |
store.printRevenue(); |
This file contains hidden or 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 Store { | |
constructor(name, stock) { | |
this.name = name; | |
this.stock = stock; | |
this.revenue = 0; | |
} | |
processProductSale(name) { | |
this.stock.forEach(item => { | |
if (item.name === name) { | |
if (item.count > 0) { | |
item.count--; | |
this.revenue += item.price; | |
console.log(`Purchased ${item.name} for ${item.price}`); | |
} else { | |
console.log(`Sorry, ${item.name} is out of stock!`); | |
} | |
} | |
}); | |
} | |
replenishStock(name, count) { | |
this.stock.forEach(item => { | |
if (item.name === name) { | |
item.count += count; | |
console.log(`Replenished ${item.name} by ${item.count}`); | |
} | |
}); | |
} | |
printRevenue() { | |
console.log(`The revenue so far is ${this.revenue}`); | |
} | |
welcome() { | |
console.log(`Welcome to ${this.name}!`); | |
} | |
} | |
module.exports = Store; |
This file contains hidden or 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 Toy { | |
constructor(name, price, count) { | |
this.name = name; | |
this.price = price; | |
this.count = count; | |
} | |
} | |
const toys = [ | |
new Toy("Action Figure", 14.99, 5), | |
new Toy("Rare Toy", 17.99, 1) | |
]; | |
module.exports = { | |
Toy, | |
toys | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment