Advertisement
simeonshopov

class Storage

Apr 1st, 2021
951
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.products = [];
  5.   }
  6.  
  7.   addProduct(product) {
  8.     this.products.push(product);
  9.     this.capacity -= product.quantity;
  10.   }
  11.  
  12.   getProducts() {
  13.     return this.products.map(x => JSON.stringify(x)).join('\n');
  14.   }
  15.  
  16.   get totalCost() {
  17.     return this.products.reduce((x, product) => x + product.price * product.quantity, 0);
  18.   }
  19. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement