nuzzgrg

Untitled

Mar 16th, 2019
309
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.             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
  34.         } else return `The ${meal} is already in our menu, try something different.` //test 7 - pass
  35.  
  36.     }
  37.     showTheMenu() {
  38.         let toPrint = []
  39.         for (let key of Object.keys(this.menu)) {
  40.             toPrint.push(`${key} - $ ${this.menu[key][1]}`)
  41.         }
  42.         if (!toPrint.length) return ('Our menu is not ready yet, please come later...') //test 8 pass
  43.         else {return toPrint.join('\n')} // // TEST 9 ERROR
  44.  
  45.     }
  46.     makeTheOrder(meal) {
  47.         if (!this.menu[meal]) return (`There is not ${meal} yet in our menu, do you want to order something else?`)
  48.         //check for products          
  49.         let ingredientsNeeded = this.menu[meal][0]  
  50.         for (let item of ingredientsNeeded) {  //item = 'product quantity'
  51.             item = item.split(' ')
  52.             let quantity = +item.pop()
  53.             let product = item.join(' ')
  54.             //let [product, quantity] = item.split(' ')
  55.             if (this.productsInStock[product] < quantity || !this.productsInStock[product]) {
  56.                 return (`For the time being, we cannot complete your order (${meal}), we are very sorry...`) // test 15
  57.             }
  58.         }
  59.  
  60.         for (let item of ingredientsNeeded) {
  61.             item = item.split(' ')
  62.             let quantity = +item.pop()
  63.             let product = item.join(' ')
  64.             this.productsInStock[product] -= quantity
  65.         } this.budget += this.menu[meal][1]
  66.         return (`Your order (${meal}) will be completed in the next 30 minutes and will cost you ${this.menu[meal][1]}.`) //test 13 pass
  67.     }
  68. }
Add Comment
Please, Sign In to add comment