Skip to content

Instantly share code, notes, and snippets.

@ariestikto
Created February 23, 2023 20:59
Show Gist options
  • Save ariestikto/da111adc9059e1bbf4ccdce98807a9a3 to your computer and use it in GitHub Desktop.
Save ariestikto/da111adc9059e1bbf4ccdce98807a9a3 to your computer and use it in GitHub Desktop.
06-Stu-Multiple-Classes
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();
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;
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