Advertisement
diela33

class

Feb 7th, 2022
898
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.         this._totalPrice = 0;
  7.     }
  8.  
  9.     loadingVegetables (vegetables) {
  10.         let result = [];
  11.         vegetables.forEach((el) => {
  12.             let [type, quantity, price] = el.split(' ');
  13.  
  14.             quantity = Number(quantity);
  15.             price = Number(price);
  16.  
  17.             if (!this.availableProducts.some(p => p.type == type)) {
  18.                 this.availableProducts.push({
  19.                     type,
  20.                     quantity,
  21.                     price,
  22.                 });
  23.                 result.push(type);
  24.             } else {
  25.                 let currentProduct = this.availableProducts.find(p => p.type == type);
  26.                 currentProduct.quantity += quantity;
  27.  
  28.                 if (currentProduct.price < price) {
  29.                     currentProduct.price = price;
  30.                 }
  31.             }
  32.         });
  33.         return 'Successfully added ' + result.join(', ');
  34.     }
  35.  
  36.     buyingVegetables (selectedProducts) {
  37.         this._totalPrice = 0;
  38.         selectedProducts.forEach((el) => {
  39.             let [type, quantity] = el.split(' ');
  40.  
  41.             quantity = Number(quantity);
  42.  
  43.             let currentProduct = this.availableProducts.find(p => p.type == type);
  44.  
  45.             if (currentProduct == undefined) {
  46.                 throw new Error(`${type} is not available in the store, your current bill is $${this._totalPrice.toFixed(2)}.`);
  47.             } else {
  48.                 if (currentProduct.quantity < quantity) {
  49.                     throw new Error(`The quantity ${quantity} for the vegetable ${type} is not available in the store, your current bill is $${this._totalPrice.toFixed(2)}.`)
  50.                 } else {
  51.                     this._totalPrice += (quantity * currentProduct.price);
  52.                     const index = this.availableProducts.indexOf(currentProduct);
  53.                     this.availableProducts[index].quantity -= quantity;
  54.                 }
  55.             }
  56.         });
  57.         return `Great choice! You must pay the following amount $${this._totalPrice.toFixed(2)}.`
  58.     }
  59.  
  60.     rottingVegetable (type, quantity) {
  61.         let currentProduct = this.availableProducts.find(p => p.type == type);
  62.         const index = this.availableProducts.indexOf(currentProduct);
  63.  
  64.         if (currentProduct == undefined) {
  65.             throw new Error(`${type} is not available in the store.`);
  66.         } else {
  67.             if (currentProduct.quantity < quantity) {
  68.                 this.availableProducts[index].quantity = 0;
  69.                 return `The entire quantity of the ${type} has been removed.`;
  70.             } else {
  71.                 this.availableProducts[index].quantity -= quantity;
  72.                 return `Some quantity of the ${type} has been removed.`
  73.             }
  74.         }
  75.     }
  76.  
  77.     revision () {
  78.         const result = [
  79.             'Available vegetables:'
  80.         ];
  81.  
  82.         this.availableProducts.sort(p => p.price).reverse();
  83.  
  84.         for (const product of this.availableProducts) {
  85.             result.push(`${product.type}-${product.quantity}-$${product.price}`);
  86.         }
  87.  
  88.         result.push(`The owner of the store is ${this.owner}, and the location is ${this.location}.`);
  89.         return result.join('\n');
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement