Repository navigation
Expand file tree
/
Copy path25-ShoppingSystem.java
More file actions
77 lines (63 loc) · 1.56 KB
/
Copy path25-ShoppingSystem.java
File metadata and controls
77 lines (63 loc) · 1.56 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import java.util.ArrayList;
import java.util.List;
class Product {
private String name;
private double price;
Product(String name, double price) {
this.name = name;
this.price = price;
}
String getName() {
return name;
}
double getPrice() {
return price;
}
}
class Customer {
String name;
Customer(String name) {
this.name = name;
}
}
class Cart {
private List<Product> products = new ArrayList<>();
void addProduct(Product product) {
products.add(product);
}
double getTotal() {
double total = 0;
for (Product product : products) {
total += product.getPrice();
}
return total;
}
void display() {
for (Product product : products) {
System.out.println(product.getName() + ": " + product.getPrice());
}
}
}
class Order {
private Customer customer;
private Cart cart;
Order(Customer customer, Cart cart) {
this.customer = customer;
this.cart = cart;
}
void placeOrder() {
System.out.println("Order placed for " + customer.name);
cart.display();
System.out.println("Total: " + cart.getTotal());
}
}
class ShoppingSystem {
public static void main(String[] args) {
Product phone = new Product("Phone", 25000);
Product cover = new Product("Cover", 500);
Cart cart = new Cart();
cart.addProduct(phone);
cart.addProduct(cover);
new Order(new Customer("Rahul"), cart).placeOrder();
}
}