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 < 1) {
- throw new Error('The budget cannot be a negative number');
- } else {
- this._budget = value;
- }
- }
- shopping(productArr) {
- const [type, price] = [...productArr];
- if (Number(price) > this._budget) {
- throw new Error('Not enough money to buy this product');
- } else {
- this.products.push(type);
- this._budget -= Number(price);
- return `You have successfully bought ${type}!`;
- }
- }
- recipes(recipeObj) {
- const { productsList } = recipeObj;
- const { recipeName } = recipeObj;
- productsList.map((p) => {
- if (this.products.includes(p) === false) {
- 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.find((d) => d.recipeName === dish)) {
- 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 = '';
- Object.keys(this.guests)
- .map((name) => {
- const dish = this.dishes.find((d) => d.recipeName === this.guests[name]);
- const dishProducts = dish.productsList.join(', ');
- result += `${name} will eat ${this.guests[name]}, which consists of ${dishProducts}\n`;
- });
- return result.trim();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement