Advertisement
Pijomir

class OnlineShop

Feb 10th, 2024
702
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class OnlineShop{
  2.     constructor(warehouseSpace) {
  3.         this.warehouseSpace = warehouseSpace;
  4.         this.products = [];
  5.         this.sales =[];
  6.     }
  7.  
  8.     loadingStore(product, quantity, spaceRequired) {
  9.         if (spaceRequired > this.warehouseSpace) {
  10.             throw new Error('Not enough space in the warehouse.');
  11.         }
  12.  
  13.         this.products.push({product, quantity});
  14.         this.warehouseSpace -= spaceRequired;
  15.         return `The ${product} has been successfully delivered in the warehouse.`;
  16.     }
  17.  
  18.     quantityCheck(product, minimalQuantity){
  19.         let currentproduct = this.products.find(p => p.product === product);
  20.         if (!currentproduct) {
  21.             throw new Error(`There is no ${product} in the warehouse.`);
  22.         }
  23.  
  24.         if (minimalQuantity <= 0) {
  25.             throw new Error('The quantity cannot be zero or negative.');
  26.         }
  27.  
  28.         if (minimalQuantity <= currentproduct.quantity) {
  29.             return `You have enough from product ${product}.`;
  30.         } else {
  31.             let difference = minimalQuantity - currentproduct.quantity;
  32.             currentproduct.quantity = minimalQuantity;
  33.             return `You added ${difference} more from the ${product} products.`;
  34.         }
  35.     }
  36.  
  37.     sellProduct(product) {
  38.         let currentproduct = this.products.find(p => p.product === product);
  39.         if(!currentproduct) {
  40.             throw new Error(`There is no ${product} in the warehouse.`);
  41.         }
  42.  
  43.         currentproduct.quantity--;
  44.         this.sales.push({product, quantity: 1});
  45.         return `The ${product} has been successfully sold.`
  46.     }
  47.  
  48.     revision() {
  49.         if (this.sales.length === 0) {
  50.             throw new Error('There are no sales today!');
  51.         }
  52.  
  53.         let firstline = `You sold ${this.sales.length} products today!\n`;
  54.         let secondLine = 'Products in the warehouse:\n';
  55.         let thirdLine = this.products.map(p => `${p.product}-${p.quantity} more left`).join('\n');
  56.         return firstline + secondLine + thirdLine;
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement