Advertisement
viligen

vegetableStore

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