Advertisement
didkoslawow

Untitled

May 6th, 2023
113
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 (this.warehouseSpace < spaceRequired) {
  10.        throw new Error('Not enough space in the warehouse.');
  11.      }
  12.      this.products.push({
  13.        product,
  14.        quantity,
  15.      });
  16.  
  17.      this.warehouseSpace -= spaceRequired;
  18.      return `The ${product} has been successfully delivered in the warehouse.`;
  19.    }
  20.  
  21.    quantityCheck(product, minimalQuantity) {
  22.      const currentProduct = this.products.find((p) => p.product === product);
  23.      if (currentProduct === undefined) {
  24.        throw new Error(`There is no ${product} in the warehouse.`);
  25.      }
  26.      if (minimalQuantity <= 0) {
  27.        throw new Error('The quantity cannot be zero or negative.');
  28.      }
  29.      if (currentProduct.quantity >= minimalQuantity) {
  30.        return `You have enough from product ${product}.`;
  31.      }
  32.  
  33.      const diff = minimalQuantity - currentProduct.quantity;
  34.      currentProduct.quantity = minimalQuantity;
  35.      return `You added ${diff} more from the ${product} products.`;
  36.    }
  37.  
  38.    sellProduct(product) {
  39.      const currentProduct = this.products.find((p) => p.product === product);
  40.      if (currentProduct === undefined) {
  41.        throw new Error(`There is no ${product} in the warehouse.`);
  42.      }
  43.  
  44.      currentProduct.quantity--;
  45.  
  46.      this.sales.push({
  47.        product,
  48.        quantity: 1,
  49.      });
  50.  
  51.      return `The ${product} has been successfully sold.`;
  52.    }
  53.  
  54.    revision() {
  55.      if (!this.sales.length) {
  56.        throw new Error('There are no sales today!');
  57.      }
  58.  
  59.      const salesCount = this.sales.reduce((acc, x) => acc.quantity + x.quantity);
  60.      const res = [`You sold ${salesCount} products today!`];
  61.      res.push('Products in the warehouse:');
  62.  
  63.      this.products.forEach((p) =>
  64.        res.push(`${p.product}-${p.quantity} more left`)
  65.      );
  66.  
  67.      return res.join('\n');
  68.    }
  69.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement