Advertisement
social1986

Untitled

Feb 8th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input) {
  2.  
  3.     let store = {
  4.         brands: []
  5.     };
  6.  
  7.     input.forEach(x => {
  8.         let [command, brand, coffeeName, expirationDate, quantity] = x.split(', ');
  9.         switch (command) {
  10.             case 'IN': inCommand(brand, coffeeName, expirationDate, quantity); break;
  11.             case 'OUT': outCommand(brand, coffeeName, expirationDate, quantity); break;
  12.             case 'REPORT': reportCommand(); break;
  13.             case 'INSPECTION': inspectionCommand(); break;
  14.         }
  15.     });
  16.  
  17.     function inCommand(brand, coffeeName, expirationDate, quantity) {
  18.         let currentBrand = store.brands.find(x => x.brandName === brand);
  19.  
  20.         if (!currentBrand) {
  21.             currentBrand = {
  22.                 brandName: brand,
  23.                 cofees: []
  24.             }
  25.             store.brands.push(currentBrand);
  26.         }
  27.  
  28.         let currentCofee = currentBrand.cofees.find(x => x.coffeeName === coffeeName);
  29.         if (!currentCofee) {
  30.             currentCofee = {
  31.                 coffeeName,
  32.                 expirationDate,
  33.                 quantity
  34.             };
  35.             currentBrand.cofees.push(currentCofee);
  36.         } else {
  37.             let coffee = store.brands.find(x => x.brandName === brand).cofees.find(x => x.coffeeName === coffeeName);
  38.             if (expirationDate > coffee.expirationDate) {
  39.                 coffee.expirationDate = expirationDate;
  40.                 coffee.quantity = quantity;
  41.             } else if (expirationDate === coffee.expirationDate) {
  42.                 coffee.quantity += quantity;
  43.             }
  44.         }
  45.     }
  46.  
  47.     function outCommand(brand, coffeeName, expirationDate, quantity) {
  48.         let brandToCell = store.brands.find(x => x.brandName === brand);
  49.         if (brandToCell) {
  50.             let coffeeToCell = brandToCell.cofees.find(x => x.coffeeName === coffeeName)
  51.             if (coffeeToCell) {
  52.                 if (coffeeToCell.expirationDate >= expirationDate) {
  53.                     coffeeToCell.quantity -= quantity;
  54.                 }
  55.             }
  56.         }
  57.     }
  58.  
  59.     function reportCommand() {
  60.         console.log('>>>>> REPORT! <<<<<');
  61.         store.brands.forEach(x => {
  62.             console.log(`Brand: ${x.brandName}:`);
  63.             x.cofees.forEach(c =>  console.log(`-> ${c.coffeeName} -> ${c.expirationDate} -> ${c.quantity}.`))
  64.         });
  65.     }
  66.  
  67.     function inspectionCommand() {
  68.         console.log('>>>>> INSPECTION! <<<<<');
  69.         store.brands.sort((a, b) => a.brandName.localeCompare(b.brandName)).forEach(x => {
  70.             console.log(`Brand: ${x.brandName}:`);
  71.             x.cofees.sort((a, b) => b.quantity - a.quantity).forEach(c =>  console.log(`-> ${c.coffeeName} -> ${c.expirationDate} -> ${c.quantity}.`))
  72.         });    }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement