-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCart.java
More file actions
44 lines (38 loc) · 1.15 KB
/
Cart.java
File metadata and controls
44 lines (38 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import java.util.ArrayList;
public class Cart {
private final ArrayList<Product> products;
public Cart() {
this.products = new ArrayList<>();
}
public void addProduct(Product product) {
products.add(product);
}
public boolean removeProduct(String name) {
for (int i = 0; i < products.size(); i++) {
if (products.get(i).getName().equalsIgnoreCase(name)) {
products.remove(i);
return true;
}
}
return false;
}
public double getTotal() {
double total = 0.0;
for (Product product : products) {
total += product.getPrice();
}
return total;
}
public void printCart() {
System.out.println("=== Корзина ===");
if (products.isEmpty()) {
System.out.println("Корзина пуста");
} else {
for (int i = 0; i < products.size(); i++) {
System.out.println((i + 1) + ". " + products.get(i));
}
System.out.println("Итого: " + getTotal() + " руб.");
}
System.out.println();
}
}