Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Storage {
- constructor(capacity) {
- this.capacity = Number(capacity);
- this.storage = [];
- this.totalCost = 0;
- }
- addProduct(product) {
- this.storage.push(product);
- let quantity = Number(product.quantity);
- let price = Number(product.price);
- this.capacity -= quantity;
- this.totalCost += quantity * price;
- }
- getProducts() {
- this.storage.forEach(element => {
- console.log(JSON.stringify(element));
- });
- }
- }
- let productOne = {name: 'Cucamber', price: 1.50, quantity: 15};
- let productTwo = {name: 'Tomato', price: 0.90, quantity: 25};
- let productThree = {name: 'Bread', price: 1.10, quantity: 8};
- let storage = new Storage(50);
- storage.addProduct(productOne);
- storage.addProduct(productTwo);
- storage.addProduct(productThree);
- storage.getProducts();
- console.log(storage.capacity);
- console.log(storage.totalCost);
Advertisement
Add Comment
Please, Sign In to add comment