Advertisement
nikolayneykov

Untitled

May 30th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function coffeeStorage () {
  2.   let input = JSON.parse(document.querySelector('textarea').value)
  3.   let [reportOutput, inspectionOutput] = document.querySelectorAll('div p')
  4.  
  5.   let storage = (function () {
  6.     let storedCoffee = {}
  7.  
  8.     function report () {
  9.       for (let brand in storedCoffee) {
  10.         reportOutput.innerHTML += `${brand}:`
  11.         for (let name in storedCoffee[brand]) {
  12.           reportOutput.innerHTML += ` ${name} - ${
  13.             storedCoffee[brand][name].expireDate
  14.           } - ${storedCoffee[brand][name].quantity}.`
  15.         }
  16.  
  17.         reportOutput.innerHTML += '<br>'
  18.       }
  19.     }
  20.  
  21.     function inspection () {
  22.       Object.keys(storedCoffee)
  23.         .sort((a, b) => a.localeCompare(b))
  24.         .forEach(brand => {
  25.           inspectionOutput.innerHTML += `${brand}:`
  26.           Object.keys(storedCoffee[brand])
  27.             .sort(
  28.               (a, b) =>
  29.                 storedCoffee[brand][b].quantity -
  30.                 storedCoffee[brand][a].quantity
  31.             )
  32.             .forEach(coffeeKey => {
  33.               let coffee = storedCoffee[brand][coffeeKey]
  34.               inspectionOutput.innerHTML += ` ${coffee.name} - ${
  35.                 coffee.expireDate
  36.               } - ${coffee.quantity}.`
  37.             })
  38.  
  39.           inspectionOutput.innerHTML += '<br>'
  40.         })
  41.     }
  42.  
  43.     function addCoffee (brand, name, expireDate, quantity) {
  44.       if (!storedCoffee.hasOwnProperty(brand)) {
  45.         storedCoffee[brand] = {}
  46.       }
  47.  
  48.       if (!storedCoffee[brand].hasOwnProperty(name)) {
  49.         storedCoffee[brand][name] = {
  50.           name,
  51.           expireDate,
  52.           quantity
  53.         }
  54.       } else {
  55.         let currentCoffee = storedCoffee[brand][name]
  56.         if (currentCoffee.expireDate < expireDate) {
  57.           currentCoffee.expireDate = expireDate
  58.           currentCoffee.quantity = quantity
  59.         } else if (currentCoffee.expireDate === expireDate) {
  60.           currentCoffee.quantity += quantity
  61.         }
  62.       }
  63.     }
  64.  
  65.     function removeCoffee (brand, name, expireDate, quantity) {
  66.       if (
  67.         storedCoffee.hasOwnProperty(brand) &&
  68.         storedCoffee[brand].hasOwnProperty(name)
  69.       ) {
  70.         let currentCoffee = storedCoffee[brand][name]
  71.         if (
  72.           currentCoffee.expireDate > expireDate &&
  73.           currentCoffee.quantity >= quantity
  74.         ) {
  75.           currentCoffee.quantity -= quantity
  76.         }
  77.       }
  78.     }
  79.  
  80.     return { in: addCoffee, out: removeCoffee, report, inspection }
  81.   })()
  82.  
  83.   input.forEach(x => {
  84.     let [command, brand, name, expireDate, quantity] = x.split(', ')
  85.     storage[command.toLowerCase()](brand, name, expireDate, quantity)
  86.   })
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement