Advertisement
ilianrusev

Christmas Dinner

Jan 31st, 2022
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. class ChristmasDinner {
  2. constructor(budget) {
  3. if (budget<0){
  4. throw new Error("The budget cannot be a negative number")
  5. }
  6. this.budget = budget;
  7. this.dishes = [];
  8. this.products = [];
  9. this.guests = {};
  10. }
  11.  
  12. shopping(product) {
  13. let type = product[0];
  14. let price = Number(product[1]);
  15.  
  16. if (price > this.budget) {
  17. throw new Error("Not enough money to buy this product")
  18. }
  19.  
  20. this.products.push(type);
  21. this.budget -= price;
  22.  
  23. return `You have successfully bought ${type}!`
  24. }
  25.  
  26. recipes(recipe) {
  27. let obj = recipe;
  28.  
  29. if (obj.productsList.every(meal => this.products.includes(meal))) {
  30. this.dishes.push(obj)
  31. return `${obj.recipeName} has been successfully cooked!`;
  32. } else {
  33. throw new Error("We do not have this product")
  34. }
  35. }
  36.  
  37. inviteGuests(name, dish) {
  38. let namesDishes = []
  39. let namesGuest = []
  40. for (let i = 0; i < this.dishes.length; i++) {
  41.  
  42. namesDishes.push(Object.values(this.dishes[i])[0])
  43. }
  44.  
  45. for (const key in this.guests) {
  46. namesGuest.push(key)
  47. }
  48.  
  49. if (!namesDishes.includes(dish)) {
  50. throw new Error("We do not have this dish")
  51. }
  52.  
  53. if (namesGuest.includes(name)) {
  54. throw new Error("This guest has already been invited")
  55. }
  56.  
  57. this.guests[name] = dish
  58. return `You have successfully invited ${name}!`
  59. }
  60.  
  61. showAttendance() {
  62. let output = []
  63. let productsString;
  64. let string;
  65. for (let [name, dish] of Object.entries(this.guests)) {
  66. for (let i = 0; i < this.dishes.length; i++) {
  67. if (this.dishes[i].recipeName == dish) {
  68. productsString = this.dishes[i].productsList.join(', ')
  69. }
  70. }
  71. string = `${name} will eat ${dish}, which consists of ` + productsString;
  72. output.push(string)
  73. }
  74. return output.join("\n")
  75. }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement