georgiev955

02. Inventory Managment

Sep 1st, 2023
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class InventoryManager {
  2.     constructor(capacity) {
  3.         this.capacity = capacity;
  4.         this.items = [];
  5.         this.outOfStock = [];
  6.     }
  7.  
  8.     addItem(itemName, quantity) {
  9.         if (quantity <= 0) {
  10.             throw new Error("Quantity must be greater than zero.");
  11.         }
  12.  
  13.         if (this.items.length >= this.capacity) {
  14.             throw new Error("The inventory is already full.");
  15.         }
  16.  
  17.         let index = this.items.findIndex(items => items.name === itemName);
  18.  
  19.         if (index === -1) {
  20.             let item = {
  21.                 itemName,
  22.                 quantity,
  23.             }
  24.             this.items.push(item);
  25.         } else {
  26.             this.items[index].quantity += quantity;
  27.         }
  28.  
  29.         return `Added ${quantity} ${itemName}(s) to the inventory.`;
  30.     }
  31.  
  32.     sellItem(itemName, quantity) {
  33.         if (quantity <= 0) {
  34.             throw new Error("Quantity must be greater than zero.");
  35.         }
  36.  
  37.         let index = this.items.findIndex(items => items.itemName === itemName);
  38.  
  39.         if (index === -1) {
  40.             throw new Error(`The item ${itemName} is not available in the inventory.`)
  41.         }
  42.  
  43.         let availableQuantity = this.items[index].quantity;
  44.  
  45.         if (quantity > availableQuantity) {
  46.             throw new Error(`Not enough ${itemName}(s) in stock.`);
  47.         }
  48.  
  49.         this.items[index].quantity -= quantity;
  50.  
  51.         if (this.items[index].quantity === 0) {
  52.             this.outOfStock.push(this.items[index].itemName);
  53.             this.items.splice(index, 1);
  54.         }
  55.  
  56.         return `Sold ${quantity} ${itemName}(s) from the inventory.`;
  57.     }
  58.  
  59.     restockItem(itemName, quantity) {
  60.         if (quantity <= 0) {
  61.             throw new Error("Quantity must be greater than zero.");
  62.         }
  63.  
  64.         let index = this.items.findIndex(items => items.itemName === itemName);
  65.  
  66.         if (index !== -1) {
  67.             this.items[index].quantity += quantity;
  68.         } else {
  69.             let itemToAdd = {
  70.                 itemName,
  71.                 quantity
  72.             };
  73.             this.items.push(itemToAdd);
  74.             if (this.outOfStock.includes(itemName)) {
  75.                 let indexR = this.outOfStock.indexOf(itemName);
  76.                 this.outOfStock.splice(indexR, 1);
  77.             }
  78.         }
  79.  
  80.         return `Restocked ${quantity} ${itemName}(s) in the inventory.`
  81.     }
  82.  
  83.     getInventorySummary() {
  84.         let result = 'Current Inventory:';
  85.  
  86.         for (const item of this.items) {
  87.             result += `\n${item.itemName}: ${item.quantity}`;
  88.         }
  89.  
  90.         if (this.outOfStock.length !== 0) {
  91.             result += `\nOut of Stock: ${this.outOfStock.join(',')}`;
  92.         }
  93.  
  94.         return result;
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment