Untitled
unknown
javascript
4 years ago
1.3 kB
8
Indexable
class Cart {
constructor(costumer) {
this.costumer = costumer;
this.cart = [];
}
addItem(product, price) {
let itemList = this.cart;
let item = { name: product, productPrice: price };
itemList.push(item);
}
showCart() {
let sum = 0;
console.log(this.costumer);
for (let index = 0; index < this.cart.length; index++) {
console.log(
`${this.cart[index].name} kainuoja: ${this.cart[index].productPrice}€`
);
sum += this.cart[index].productPrice;
}
if (this.cart.length == 0) {
console.log("Krepšelis tuščias");
}
console.log(
`Krepselio kaina: ${this.cart.length == 0 ? "- €" : sum + "€"}`
);
}
discardItem(number) {
this.cart.splice(number, 1);
}
payForItems() {
this.cart = [];
}
}
const firstCostumer = new Cart("Petras Petrosius");
const secondCostumer = new Cart("Jeronimas Petrofelis");
firstCostumer.addItem("batonas", 1.5);
firstCostumer.addItem("duona", 2.5);
firstCostumer.addItem("bananas", 1.2);
firstCostumer.showCart();
firstCostumer.discardItem(1);
firstCostumer.showCart();
firstCostumer.payForItems();
firstCostumer.showCart();
secondCostumer.addItem("alus (7.2 prom)", 3);
secondCostumer.addItem("kiauliu ausys", 4.5);
secondCostumer.showCart();
Editor is loading...