Advertisement
Pijomir

Inventory Management

Feb 16th, 2024
694
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 currentItem = this.items.find(i => i.itemName === itemName);
  18.         if (currentItem) {
  19.             currentItem.quantity += quantity;
  20.         } else {
  21.             this.items.push({ itemName, quantity });
  22.         }
  23.  
  24.         return `Added ${quantity} ${itemName}(s) to the inventory.`;
  25.     }
  26.  
  27.     sellItem(itemName, quantity) {
  28.         if (quantity <= 0) {
  29.             throw new Error('Quantity must be greater than zero.');
  30.         }
  31.  
  32.         let currentItem = this.items.find(i => i.itemName === itemName);
  33.         if (!currentItem) {
  34.             throw new Error(`The item ${itemName} is not available in the inventory.`);
  35.         }
  36.  
  37.         if (quantity > currentItem.quantity) {
  38.             throw new Error(`Not enough ${itemName}(s) in stock.`);
  39.         }
  40.  
  41.         currentItem.quantity -= quantity;
  42.         if (currentItem.quantity === 0) {
  43.             this.items = this.items.filter(i => i.itemName !== itemName);
  44.             this.outOfStock.push(itemName);
  45.         }
  46.  
  47.         return `Sold ${quantity} ${itemName}(s) from the inventory.`
  48.     }
  49.  
  50.     restockItem(itemName, quantity) {
  51.         if (quantity <= 0) {
  52.             throw new Error('Quantity must be greater than zero.');
  53.         }
  54.  
  55.         let currentItem = this.items.find(i => i.itemName === itemName);
  56.         if(currentItem) {
  57.             currentItem.quantity += quantity;
  58.         } else {
  59.             this.items.push({itemName, quantity});
  60.         }
  61.  
  62.         if(this.outOfStock.includes(itemName)) {
  63.             this.outOfStock.join('').split(itemName);
  64.         }
  65.  
  66.         return `Restocked ${quantity} ${itemName}(s) in the inventory.`
  67.     }
  68.  
  69.     getInventorySummary() {
  70.         const partOne = 'Current Inventory:\n';
  71.         const partTwo = this.items.map(i => `${i.itemName}: ${i.quantity}`).join('\n');
  72.         const partThree = `\n${this.outOfStock.join(', ')}`;
  73.         return this.outOfStock.length > 0 ? partOne + partTwo + partThree : partOne + partTwo;
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement