Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(input) {
- let store = {
- brands: []
- };
- input.forEach(x => {
- let [command, brand, coffeeName, expirationDate, quantity] = x.split(', ');
- switch (command) {
- case 'IN': inCommand(brand, coffeeName, expirationDate, quantity); break;
- case 'OUT': outCommand(brand, coffeeName, expirationDate, quantity); break;
- case 'REPORT': reportCommand(); break;
- case 'INSPECTION': inspectionCommand(); break;
- }
- });
- function inCommand(brand, coffeeName, expirationDate, quantity) {
- let currentBrand = store.brands.find(x => x.brandName === brand);
- if (!currentBrand) {
- currentBrand = {
- brandName: brand,
- coffees: []
- }
- store.brands.push(currentBrand);
- }
- let currentCoffee = currentBrand.coffees.find(x => x.coffeeName === coffeeName);
- if (!currentCoffee) {
- currentCoffee = {
- coffeeName,
- expirationDate,
- quantity
- };
- currentBrand.coffees.push(currentCoffee);
- } else {
- let coffee = store.brands.find(x => x.brandName === brand).coffees.find(x => x.coffeeName === coffeeName);
- if (expirationDate > coffee.expirationDate) {
- coffee.expirationDate = expirationDate;
- coffee.quantity = quantity;
- } else if (expirationDate === coffee.expirationDate) {
- coffee.quantity += quantity;
- }
- }
- }
- function outCommand(brand, coffeeName, expirationDate, quantity) {
- let brandToCell = store.brands.find(x => x.brandName === brand);
- if (brandToCell) {
- let coffeeToCell = brandToCell.coffees.find(x => x.coffeeName === coffeeName)
- if (coffeeToCell) {
- if (coffeeToCell.expirationDate >= expirationDate) {
- coffeeToCell.quantity -= quantity;
- }
- }
- }
- }
- function reportCommand() {
- console.log('>>>>> REPORT! <<<<<');
- store.brands.forEach(x => {
- console.log(`Brand: ${x.brandName}:`);
- x.coffees.forEach(c => console.log(`-> ${c.coffeeName} -> ${c.expirationDate} -> ${c.quantity}.`))
- });
- }
- function inspectionCommand() {
- console.log('>>>>> INSPECTION! <<<<<');
- store.brands.sort((a, b) => a.brandName.localeCompare(b.brandName)).forEach(x => {
- console.log(`Brand: ${x.brandName}:`);
- x.coffees.sort((a, b) => b.quantity - a.quantity).forEach(c => console.log(`-> ${c.coffeeName} -> ${c.expirationDate} -> ${c.quantity}.`))
- });
- }
- }
- solve([
- 'IN, Folgers, Black Silk, 2023-03-01, 14',
- 'IN, Lavazza, Crema e Gusto, 2023-05-01, 5',
- 'IN, Lavazza, Crema, 2005-05-01, 5',
- 'IN, Lavazza, Crema, 2006-05-01, 50',
- 'IN, Batdorf & Bronson, Es, 2025-05-25, 20',
- 'IN, Batdorf & Bronson, Espresso, 2035-05-25, 70',
- 'IN, Batdorf & Bronson, Es, 2035-05-25, 60',
- 'IN, Lavazza, Crema e Gusto, 2023-05-02, 5',
- 'IN, Folgers, Black Silk, 2022-01-01, 10',
- 'IN, Lavazza, Intenso, 2022-07-19, 20',
- 'OUT, Dallmayr, Espresso, 2022-07-19, 5',
- 'OUT, Dallmayr, Crema, 2022-07-19, 5',
- 'OUT, Lavazza, Crema, 2020-01-28, 2',
- 'IN, Batdorf & Bronson, Es, 2025-05-25, 10',
- 'INSPECTION']
- );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement