Advertisement
Liliana797979

Vegetable - js advanced - exam

Jan 28th, 2022
846
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.         if(this.availableProducts.length < 1) {
  10.             this.availableProducts.push({});
  11.         }
  12.  
  13.         let addedElements = new Set();
  14.  
  15.         vegetables.forEach((vegetable) => {
  16.             let [name, qty, price] = vegetable.split(' ');
  17.             qty = Number(qty);
  18.             price = Number(price);
  19.  
  20.             if(this.availableProducts[0].hasOwnProperty(name)) {
  21.                 const currentVegPrice = this.availableProducts[0][name].price;
  22.                 this.availableProducts[0][name].qty += qty;
  23.  
  24.                 if(price > currentVegPrice) {
  25.                     this.availableProducts[0][name].price = price;
  26.                 }
  27.             } else if (!this.availableProducts[0].hasOwnProperty(name)) {
  28.                 this.availableProducts[0][name] = {};
  29.                 this.availableProducts[0][name].qty = qty;
  30.                 this.availableProducts[0][name].price = price;
  31.             }
  32.             addedElements.add(name);
  33.         });
  34.        
  35.         addedElements = Array.from(addedElements);
  36.         return `Successfully added ${addedElements.join(', ')}`;
  37.     }
  38.  
  39.     buyingVegetables(selectedProducts) {
  40.         let totalPrice = 0;
  41.         selectedProducts.forEach(product => {
  42.             let [type, qty] = product.split(' ');
  43.             qty = Number(qty);
  44.  
  45.             if(this.availableProducts[0].hasOwnProperty(type)) {
  46.                 if(this.availableProducts[0][type].qty < qty) {
  47.                     throw new Error(`The quantity ${qty} for the vegetable ${type} is not available in the store, your current bill is $${totalPrice.toFixed(2)}.`)
  48.                 } else if (this.availableProducts[0][type].qty >= qty) {
  49.                     totalPrice += this.availableProducts[0][type].price * qty;
  50.                     this.availableProducts[0][type].qty -= qty;
  51.                 }
  52.             } else {
  53.                 throw new Error(`${type} is not available in the store, your current bill is $${totalPrice.toFixed(2)}.`);
  54.             }
  55.         });
  56.  
  57.         return `Great choice! You must pay the following amount $${totalPrice.toFixed(2)}.`
  58.     }
  59.  
  60.     rottingVegetable(type, qty) {
  61.         const qtyToRemove = Number(qty);
  62.         if(!this.availableProducts[0][type]) {
  63.             throw new Error(`${type} is not available in the store.`)
  64.         } else if(this.availableProducts[0][type]) {
  65.             if(this.availableProducts[0][type].qty <= qtyToRemove) {
  66.                 this.availableProducts[0][type].qty = 0;
  67.                 return `The entire quantity of the ${type} has been removed.`;
  68.             } else if(this.availableProducts[0][type].qty >= qtyToRemove) {
  69.                 return `Some quantity of the ${type} has been removed.`
  70.             };
  71.         }
  72.     }
  73.  
  74.     revision() {
  75.         const result = [];
  76.         result.push('Available vegetables:');
  77.         const sortedKeys = Object.keys(this.availableProducts[0]).sort((a,b) => this.availableProducts[0][a].price - this.availableProducts[0][b].price);
  78.         sortedKeys.forEach(key => result.push(`${key}-${this.availableProducts[0][key].qty}-$${this.availableProducts[0][key].price}`))
  79.         result.push(`The owner of the store is ${this.owner}, and the location is ${this.location}.`);
  80.         return result.join('\n');
  81.     }
  82.  
  83. }
  84.  
  85.  
  86. let vegStore = new VegetableStore("Jerrie Munro", "1463 Pette Kyosheta, Sofia");
  87. console.log(vegStore.loadingVegetables(["Okra 2.5 3.5", "Beans 10 2.8", "Celery 5.5 2.2", "Celery 0.5 2.5"]));
  88. console.log(vegStore.rottingVegetable("Okra", 1));
  89. console.log(vegStore.rottingVegetable("Okra", 2.5));
  90. console.log(vegStore.buyingVegetables(["Beans 8", "Celery 1.5"]));
  91. console.log(vegStore.revision());
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement