Advertisement
Liliana797979

kitchen - js advanced - exam

Oct 8th, 2021
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Kitchen {
  2.     constructor(budget) {
  3.         this.budget = +budget;
  4.         this.menu = {};
  5.         this.productsInStock = {}
  6.         this.actionsHistory = []
  7.     }
  8.     loadProducts(products) {  //["product quantity price", ...
  9.         let messageLog = []
  10.         for (let entry of products) {
  11.             entry = entry.split(' ')
  12.             //let [product, quantity, price] = entry.split(' ')
  13.             let price = +entry.pop()
  14.             let quantity = +entry.pop()
  15.             let product = entry.join(' ')
  16.             if (this.budget - price >= 0) {
  17.                 if (this.productsInStock[product]) this.productsInStock[product] += quantity
  18.                 else this.productsInStock[product] = quantity
  19.                 this.budget -= price
  20.                 messageLog.push( `Successfully loaded ${quantity} ${product}`) //test 3
  21.             } else {
  22.                 messageLog.push(`There was not enough money to load ${quantity} ${product}`)//test 4
  23.             }
  24.         }
  25.         //this.actionsHistory.push(messageLog.join('\n'))
  26.         this.actionsHistory = [...this.actionsHistory, ...messageLog]
  27.         return this.actionsHistory.join('\n')  //test 5 pass
  28.        
  29.     }
  30.     addToMenu(meal, neededIngs, price) {  //neededIngs = ['product quantity', ...
  31.         if (!this.menu[meal]) {
  32.             // this.menu[meal] = [neededIngs, +price]
  33.             this.menu[meal] = {
  34.                 products: neededIngs,
  35.                 price: +price
  36.             }
  37.             return `Great idea! Now with the ${meal} we have ${Object.keys(this.menu).length} meals on the menu, other ideas?` //? no effect //TEST 6 ERROR
  38.         } else return `The ${meal} is already in our menu, try something different.` //test 7 - pass
  39.  
  40.     }
  41.     showTheMenu() {
  42.         let toPrint = []
  43.         for (let key of Object.keys(this.menu)) {
  44.             toPrint.push(`${key} - $ ${this.menu[key].price}`)
  45.         }
  46.         if (!toPrint.length) return ('Our menu is not ready yet, please come later...') //test 8 pass
  47.         else {return toPrint.join('\n') + '\n'} // // TEST 9 ERROR
  48.  
  49.     }
  50.     makeTheOrder(meal) {
  51.         if (!this.menu[meal]) return (`There is not ${meal} yet in our menu, do you want to order something else?`)
  52.         //check for products          
  53.         let ingredientsNeeded = this.menu[meal].products
  54.         for (let item of ingredientsNeeded) {  //item = 'product quantity'
  55.             item = item.split(' ')
  56.             let quantity = +item.pop()
  57.             let product = item.join(' ')
  58.             //let [product, quantity] = item.split(' ')
  59.             if (this.productsInStock[product] < quantity || !this.productsInStock[product]) {
  60.                 return (`For the time being, we cannot complete your order (${meal}), we are very sorry...`) // test 15
  61.             }
  62.         }
  63.  
  64.         for (let item of ingredientsNeeded) {
  65.             item = item.split(' ')
  66.             let quantity = +item.pop()
  67.             let product = item.join(' ')
  68.             this.productsInStock[product] -= quantity
  69.         } this.budget += this.menu[meal].price
  70.         return (`Your order (${meal}) will be completed in the next 30 minutes and will cost you ${this.menu[meal].price}.`) //test 13 pass
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement