Advertisement
Grossos

02-prep-exam

Feb 7th, 2024
1,196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ⦁  Problem 2.Fashion Retail Management
  2.  
  3. // In the dynamic world of fashion retail, efficiently managing inventory is pivotal for catering to the ever-evolving demands of customers who seek clothing, shoes, and accessories. To meet these demands, you need to design a class named "FashionRetailInventory with updated functionality tailored to this specialized environment.
  4. // Functionality
  5. // Constructor
  6. // Should have these 3 properties:
  7. // ⦁  storehouse  (string);
  8. // ⦁  location  (string);
  9. // ⦁  productStock   (empty array);
  10. // At the initialization of the FashionRetailInventory class, the constructor accepts the storehouse and location.
  11.  
  12. // addProduct (productName, size, quantity, price)
  13. // This method adds a fashion product to the store's inventory. The method accepts four arguments:
  14. // ⦁  productName (string);
  15. // ⦁  size (string);
  16. // ⦁  quantity (number);
  17. // ⦁  price (number);
  18. // ⦁  If a product with the same name and size already exists in the productStock, add the quantity to the product and return the following message:
  19. // `You added ${quantity} more pieces of product ${productName} size ${size}`
  20. // ⦁  Otherwise, add the product with properties: {productName, size, quantity, price} to the productStock and return the following message:
  21.  
  22. // `The product ${productName}, size ${size} was successfully added to the inventory`
  23. // sendProduct (productName, size)
  24. // This method allows the sending of a product from the store's inventory. The method accepts two arguments:
  25. // ⦁  productName (string);
  26. // ⦁  size (string);
  27. // ⦁  If the product with the given name and size is not present in the productStock, throw an error with the following message:
  28.  
  29. // `The product ${productName}, size ${size} is not in the inventory`
  30.  
  31. // ⦁  Otherwise, remove the product from the productStock and return the following message:
  32. // `The product ${productName}, size ${size} was successfully removed from the inventory`
  33.  
  34. // findProductsBySize(size)
  35. // The method accepts one arguments:
  36. // ⦁  size (string);
  37.  
  38. // ⦁  Iterate through the productStock array and find all products that match the specified size and return a list of the products that match the given size in the format:
  39.  
  40. // `${product1}-${quantity} pieces, ${product2}-${quantity} pieces,…`
  41.  
  42. // ⦁  If no products matching the specified size are found in the productStock, return:
  43. // `There are no products available in that size`
  44. // listProducts ()
  45. // ⦁  If there are no products in stock, return:
  46. // `${storehouse} storehouse is empty`
  47. // ⦁  Otherwise, on the first line return:
  48. // `${storehouse} storehouse in ${location} available products:`
  49. // ⦁  On the next lines, display information about each product, sorted in alphabetical order by their productName in the following format:
  50. // `${productName}/Size:${size}/Quantity:${quantity}/Price:${price}$`
  51.  
  52. class FashionRetailInventory {
  53.     constructor(storehouse, location) {
  54.         this.storehouse = storehouse;
  55.         this.location = location;
  56.         this.productStock = [];
  57.     }
  58.  
  59.     addProduct(productName, size, qty, price) {
  60.         debugger
  61.         const existingProduct = this.productStock.find(product => product.productName === productName && product.size === size);
  62.  
  63.         if (existingProduct) {
  64.             existingProduct.qty += qty;
  65.             return `You added ${qty} more pieces of product ${productName} size ${size}`;
  66.         } else {
  67.             this.productStock.push({ productName, size, qty, price });
  68.             return `The product ${productName}, size ${size} was successfully added to the inventory`;
  69.         }
  70.     }
  71.  
  72.     sendProduct(productName, size) {
  73.         const existingProduct = this.productStock.find(product => product.productName === productName && product.size === size);
  74.  
  75.         if (existingProduct) {
  76.             const idx = this.productStock.indexOf(existingProduct);
  77.             this.productStock.splice(idx, 1);
  78.             return `The product ${productName}, size ${size} was successfully removed from the inventory`;
  79.         } else {
  80.  
  81.             throw new Error(`The product ${productName}, size ${size} is not in the inventory`);
  82.         }
  83.     }
  84.  
  85.     findProductsBySize(size) {
  86.         const result = this.productStock.filter(product => product.size === size);
  87.  
  88.         if (result.length > 0) {
  89.             const res = [];
  90.             result.forEach(el => res.push(`${el.productName}-${el.qty} pieces`));
  91.             return res.join(', ');
  92.         } else {
  93.             return `There are no products available in that size`;
  94.         }
  95.     }
  96.  
  97.     listProducts() {
  98.         const buff = `${this.storehouse} storehouse in ${this.location} available products:\n`;
  99.         const sorted = this.productStock.sort((a, b) => a.productName.localeCompare(b.productName));
  100.         const res = [];
  101.         if (this.productStock.length == 0) {
  102.             return `${this.storehouse} storehouse is empty`;
  103.         } else {
  104.             sorted.forEach(el => res.push(`${el.productName}/Size:${el.size}/Quantity:${el.qty}/Price:${el.price}$`))
  105.             return buff + res.join('\n');
  106.         }
  107.     }
  108. }
  109.  
  110. // const storeHouse = new FashionRetailInventory("East", "Milano");
  111. // console.log(storeHouse.addProduct("Shirt", "M", 10, 25.0));
  112. // console.log(storeHouse.addProduct("T-Shirt", "M", 10, 25.0));
  113. // console.log(storeHouse.addProduct("Sweather", "M", 10, 25.0));
  114. // console.log(storeHouse.addProduct("Sweather", "M", 10, 25.0));
  115.  
  116. // const storeHouse = new FashionRetailInventory("East", "Milano");
  117. // console.log(storeHouse.addProduct("Shirt", "M", 10, 25.0));
  118. // console.log(storeHouse.addProduct("T-Shirt", "M", 10, 25.0));
  119. // console.log(storeHouse.sendProduct("T-Shirt", "M"));
  120. // console.log(storeHouse.sendProduct("Sweather", "M"));
  121.  
  122. // const storeHouse = new FashionRetailInventory("East", "Milano");
  123. // console.log(storeHouse.addProduct("Shirt", "M", 10, 25.0));
  124. // console.log(storeHouse.addProduct("T-Shirt", "M", 10, 25.0));
  125. // console.log(storeHouse.findProductsBySize("M"));
  126. // console.log(storeHouse.findProductsBySize("XL"));
  127.  
  128. // console.log(storeHouse.listProducts());
  129.  
  130. const storeHouse = new FashionRetailInventory("East", "Milano");
  131. console.log(storeHouse.addProduct("Shirt", "M", 10, 25.0));
  132. console.log(storeHouse.addProduct("T-Shirt", "M", 10, 25.0));
  133. console.log(storeHouse.addProduct("Shirt", "L", 5, 30.0));
  134. console.log(storeHouse.addProduct("Shoes", "9", 8, 50.0));
  135. console.log(storeHouse.sendProduct("Shoes", "9", 8, 50.0));
  136. console.log(storeHouse.listProducts());
  137.  
  138.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement