mihalkoff

class-storage.js

Mar 17th, 2021
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. class Storage {
  2. constructor(capacity) {
  3. this.capacity = Number(capacity);
  4. this.storage = [];
  5. this.totalCost = 0;
  6. }
  7.  
  8. addProduct(product) {
  9. this.storage.push(product);
  10. let quantity = Number(product.quantity);
  11. let price = Number(product.price);
  12. this.capacity -= quantity;
  13. this.totalCost += quantity * price;
  14. }
  15.  
  16. getProducts() {
  17. this.storage.forEach(element => {
  18. console.log(JSON.stringify(element));
  19. });
  20. }
  21. }
  22.  
  23. let productOne = {name: 'Cucamber', price: 1.50, quantity: 15};
  24. let productTwo = {name: 'Tomato', price: 0.90, quantity: 25};
  25. let productThree = {name: 'Bread', price: 1.10, quantity: 8};
  26. let storage = new Storage(50);
  27. storage.addProduct(productOne);
  28. storage.addProduct(productTwo);
  29. storage.addProduct(productThree);
  30. storage.getProducts();
  31. console.log(storage.capacity);
  32. console.log(storage.totalCost);
Advertisement
Add Comment
Please, Sign In to add comment