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 (value < 0) {
- throw new Error('The budget cannot be a negative number');
- }
- this._budget = 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.budget -= price;
- this.products.push(productName);
- return `You have successfully bought ${productName}!`;
- }
- recipes(recipe) {
- if (recipe.productsList.every((p) => this.products.includes(p))) {
- this.dishes.push({
- recipeName: recipe.recipeName,
- productsList: recipe.productsList,
- });
- return `${recipe.recipeName} has been successfully cooked!`;
- } else {
- throw new Error('We do not have this product');
- }
- }
- inviteGuests(name, dish) {
- let searchedDish = this.dishes.find((d) => d.recipeName == dish);
- if (!searchedDish) {
- throw new Error('We do not have this dish');
- }
- if (Object.keys(this.guests).includes(name)) {
- throw new Error('This guest has already been invited');
- }
- this.guests[name] = dish;
- return `You have successfully invited ${name}!`;
- }
- showAttendance() {
- let result = [];
- Object.entries(this.guests).forEach((g) => {
- let [name, dish] = g;
- let dishObj = this.dishes.find((d) => d.recipeName == dish);
- let products = dishObj.productsList;
- result.push(
- `${name} will eat ${dish}, which consists of ${products.join(
- ', '
- )}`
- );
- });
- return result.join('\n');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement