Advertisement
dilyana2001

Untitled

Dec 10th, 2021 (edited)
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class VegetableStore {
  2.     constructor(owner, location) {
  3.         this.owner = owner;
  4.         this.location = location;
  5.         this.availableProducts = [];
  6.     }
  7.  
  8.     loadingVegetables(vegetables) {
  9.         vegetables.forEach(x => {
  10.             let [type, quantity, price] = x.split(' ');
  11.             quantity = Number(quantity);
  12.             price = Number(price);
  13.             if (!this.availableProducts.some(x => x.split(' ')[0] === type)) {
  14.                 return this.availableProducts.push(x);
  15.             }
  16.             let index = this.availableProducts.find(x => x.split(' ')[0] === type);
  17.             let [oldType, oldQuantity, oldPrice] = index.split(' ');
  18.             oldQuantity = Number(oldQuantity);
  19.             oldPrice = Number(oldPrice);
  20.             oldQuantity += quantity;
  21.             if (price > oldPrice) {
  22.                 oldPrice = price;
  23.             }
  24.  
  25.             this.availableProducts.push(`${oldType} ${oldQuantity} ${oldPrice}`);
  26.             let indexOf = this.availableProducts.indexOf(index);
  27.             this.availableProducts.splice(indexOf, 1);
  28.         });
  29.  
  30.         const vegis = [];
  31.         vegetables.forEach(x => {
  32.             let [one] = x.split(' ');
  33.             vegis.push(one);
  34.         });
  35.         let result = new Set(vegis);
  36.  
  37.         return `Successfully added ${Array.from(result).join(', ')}`;
  38.     }
  39.  
  40.     buyingVegetables(selectedProducts) {
  41.         let totalPrice = 0
  42.         selectedProducts.forEach(x => {
  43.             let [type, quantity] = x.split(' ');
  44.             let element = this.availableProducts.find(x => x.split(' ')[0] === type)
  45.  
  46.             if (!this.availableProducts.some(x => x.split(' ')[0] === type)) {
  47.                 throw new Error(`${type} is not available in the store, your current bill is ${totalPrice.toFixed(2)}.`);
  48.             }
  49.             if (quantity > element.split(' ')[1]) {
  50.                 throw new Error(`The quantity ${quantity} for the vegetable ${type} is not available in the store, your current bill is ${totalPrice}.`)
  51.             }
  52.         })
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement