social1986

Untitled

Feb 8th, 2019
68
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.                 coffees: []
  24.             }
  25.             store.brands.push(currentBrand);
  26.         }
  27.  
  28.         let currentCoffee = currentBrand.coffees.find(x => x.coffeeName === coffeeName);
  29.         if (!currentCoffee) {
  30.             currentCoffee = {
  31.                 coffeeName,
  32.                 expirationDate,
  33.                 quantity
  34.             };
  35.             currentBrand.coffees.push(currentCoffee);
  36.         } else {
  37.             let coffee = store.brands.find(x => x.brandName === brand).coffees.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.coffees.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.coffees.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.coffees.sort((a, b) => b.quantity - a.quantity).forEach(c =>  console.log(`-> ${c.coffeeName} -> ${c.expirationDate} -> ${c.quantity}.`))
  72.         });
  73.     }
  74. }
  75.  
  76. solve([
  77.     'IN, Folgers, Black Silk, 2023-03-01, 14',
  78.     'IN, Lavazza, Crema e Gusto, 2023-05-01, 5',
  79.     'IN, Lavazza, Crema, 2005-05-01, 5',
  80.     'IN, Lavazza, Crema, 2006-05-01, 50',
  81.     'IN, Batdorf & Bronson, Es, 2025-05-25, 20',
  82.     'IN, Batdorf & Bronson, Espresso, 2035-05-25, 70',
  83.     'IN, Batdorf & Bronson, Es, 2035-05-25, 60',
  84.     'IN, Lavazza, Crema e Gusto, 2023-05-02, 5',
  85.     'IN, Folgers, Black Silk, 2022-01-01, 10',
  86.     'IN, Lavazza, Intenso, 2022-07-19, 20',
  87.     'OUT, Dallmayr, Espresso, 2022-07-19, 5',
  88.     'OUT, Dallmayr, Crema, 2022-07-19, 5',
  89.     'OUT, Lavazza, Crema, 2020-01-28, 2',
  90.     'IN, Batdorf & Bronson, Es, 2025-05-25, 10',
  91.     'INSPECTION']
  92. );
Add Comment
Please, Sign In to add comment