Advertisement
Kamend1

01.Class Storage

Mar 13th, 2025
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Storage {
  2.     constructor (capacity) {
  3.         this.capacity = capacity;
  4.         this.storage = [];
  5.         this.totalCost = 0;
  6.  
  7.         this.getTotalCost();
  8.     }
  9.  
  10.     getTotalCost () {
  11.        
  12.         for (let product of this.storage) {
  13.             this.totalCost += (Number(product.quantity) * Number(product.price));
  14.         }
  15.  
  16.         return this.totalCost
  17.     }
  18.  
  19.     addProduct (object) {
  20.         if (Number(object.quantity) <= this.capacity) {
  21.             this.storage.push(object);
  22.             this.capacity -= Number(object.quantity);
  23.         }
  24.         this.totalCost = 0
  25.         this.getTotalCost();
  26.     }
  27.  
  28.     getProducts () {
  29.         let result = []
  30.         for (let product of this.storage) {
  31.             result.push(JSON.stringify(product));
  32.         }
  33.  
  34.         return result.join('\n');
  35.     }
  36. }
  37.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement