kstoyanov

08. Class Storage js fundementals

Jul 2nd, 2020
83
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.   }
  6.  
  7.   addProduct(product) {
  8.     this.storage.push(product);
  9.     this.capacity -= product.quantity;
  10.   }
  11.  
  12.   getProducts() {
  13.     const productList = [];
  14.     this.storage.forEach((element) => {
  15.       console.log(JSON.stringify(element));
  16.       productList.push(JSON.stringify(element));
  17.     });
  18.  
  19.     return productList.join('\n');
  20.   }
  21.  
  22.   get totalCost() {
  23.     let totalPrice = 0;
  24.     this.storage.forEach((element) => {
  25.       totalPrice += element.price * element.quantity;
  26.     });
  27.  
  28.     return totalPrice;
  29.   }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment