Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class ChristmasDinner {
- constructor(budget) {
- this.budget = budget;
- this.dishes = [];
- this.products = [];
- this.guests = {};
- }
- get budget() {
- return this._budget;
- }
- set budget(value) {
- if (typeof value !== 'number' || value < 0) {
- throw new TypeError('The budget cannot be a negative number');
- }
- this._budget = value;
- }
- shopping(product) {
- const type = product[0];
- const price = +product[1];
- if (price > this.budget) {
- throw new Error('Not enough money to buy this product');
- }
- if (!this.products.includes(type)) {
- this.products.push(type);
- }
- this.budget -= price;
- return `You have successfully bought ${type}!`;
- }
- recipes(recipe) {
- const { recipeName } = recipe;
- const { productsList } = recipe;
- if (productsList.length < 1) {
- return;
- }
- for (const product of productsList) {
- if (!this.products.includes(product)) {
- throw new Error('We do not have this product');
- }
- }
- this.dishes.push({ recipeName, productsList });
- return `${recipeName} has been successfully cooked!`;
- }
- inviteGuests(name, dish) {
- if (!this.dishes.map((d) => d.recipeName).includes(dish)) {
- throw new Error('We do not have this dish');
- }
- if (this.guests[name]) {
- throw new Error('This guest has already been invited');
- }
- this.guests[name] = dish;
- return `You have successfully invited ${name}!`;
- }
- showAttendance() {
- let result = '';
- const guestsNames = Object.keys(this.guests);
- guestsNames.forEach((name) => {
- const dish = this.dishes.filter((d) => d.recipeName === this.guests[name]);
- result += `${name} will eat ${this.guests[name]}, which consists of ${dish[0].productsList.join(', ')}\n`;
- });
- return result.trim();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement