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,
- cofees: []
- }
- store.brands.push(currentBrand);
- }
- let currentCofee = currentBrand.cofees.find(x => x.coffeeName === coffeeName);
- if (!currentCofee) {
- currentCofee = {
- coffeeName,
- expirationDate,
- quantity
- };
- currentBrand.cofees.push(currentCofee);
- } else {
- let coffee = store.brands.find(x => x.brandName === brand).cofees.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.cofees.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.cofees.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.cofees.sort((a, b) => b.quantity - a.quantity).forEach(c => console.log(`-> ${c.coffeeName} -> ${c.expirationDate} -> ${c.quantity}.`))
- }); }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement