AreWe

Class Storage

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