Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- lass ChristmasDinner {
- constructor(budget) {
- this.budget = budget;
- this.dishes = []
- this.products = []
- this.guests = {}
- }
- get budget() {
- return this._budget
- }
- set budget(value) {
- if (value < 0) {
- throw new Error('The budget cannot be a negative number')
- } else {
- this._budget = Number(value)
- }
- }
- shopping(product) {
- let [productName, price] = product
- price = Number(price)
- if (this.budget < price) {
- throw new Error('Not enough money to buy this product');
- }
- this.products.push(productName)
- this.budget -= price;
- return `You have successfully bought ${productName}!`;
- }
- recipes(recipe) {
- let {
- recipeName,
- productsList
- } = recipe;
- productsList.forEach(p => {
- if(!this.products.includes(p)) {
- throw new Error('We do not have this product');
- }
- });
- this.dishes.push(recipe)
- return `${recipeName} has been successfully cooked!`;
- }
- inviteGuests(name, dish) {
- let curDish = this.dishes.find(d => d.recipeName == dish);
- if (!curDish) {
- throw new Error('We do not have this dish');
- }
- if (this.guests.hasOwnProperty(name)) {
- throw new Error(`This guest has already been invited`);
- }
- this.guests[name] = dish
- return `You have successfully invited ${name}!`;
- }
- showAttendance() {
- let result = [];
- let guestArr = Object.entries(this.guests)
- guestArr.forEach(g => {
- let productArr = this.dishes.find(d => d.recipeName == g[1])
- result.push(`${g[0]} will eat ${g[1]}, which consists of ${productArr.productsList.join(', ')}`)
- })
- return result.join('\n')
- }
- }
RAW Paste Data
Copied