Liliana797979

class storage2 - fundamentals

Jul 20th, 2021
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.      
  2. class Storage {
  3.     constructor(capacity) {
  4.         this.capacity = capacity;
  5.         this.storage = [];
  6.     }
  7.  
  8.     get totalCost() {
  9.         return this.storage.reduce((acc, el) => acc + el.price * el.quantity, 0);
  10.     }
  11.  
  12.     addProduct(product) {
  13.         this.storage.push(product);
  14.         this.capacity -= product.quantity;
  15.     }
  16.  
  17.     getProducts() {
  18.         let output = [];
  19.         this.storage.forEach(product => {
  20.             output.push(JSON.stringify(product));
  21.         })
  22.         return output.join('\n');
  23.     }
  24. }
Advertisement
Add Comment
Please, Sign In to add comment