-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOnlineShoppingBillingSystem.java
More file actions
115 lines (94 loc) · 3.1 KB
/
Copy pathOnlineShoppingBillingSystem.java
File metadata and controls
115 lines (94 loc) · 3.1 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import java.util.*;
class Product {
private int id;
private String name;
private double price;
public Product(int id, String name, double price) {
this.id = id;
this.name = name;
this.price = price;
}
public int getId() { return id; }
public String getName() { return name; }
public double getPrice() { return price; }
}
class CartItem {
Product product;
int quantity;
public CartItem(Product product, int quantity) {
this.product = product;
this.quantity = quantity;
}
public double getTotal() {
return product.getPrice() * quantity;
}
}
class Customer {
private String name;
private ArrayList<CartItem> cart = new ArrayList<>();
public Customer(String name) {
this.name = name;
}
public void addToCart(Product product, int qty) {
cart.add(new CartItem(product, qty));
}
public double getBill() {
double total = 0;
for (CartItem item : cart) {
total += item.getTotal();
}
return total;
}
public void showBill() {
System.out.println("\n========== BILL ==========");
System.out.println("Customer: " + name);
System.out.println("--------------------------");
for (CartItem item : cart) {
System.out.println(item.product.getName() + " x" + item.quantity + " = ₹" + item.getTotal());
}
System.out.println("--------------------------");
System.out.println("Grand Total: ₹" + getBill());
System.out.println("==========================");
}
}
public class OnlineShoppingBillingSystem {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Product[] products = {
new Product(1, "Laptop", 55000),
new Product(2, "Mobile", 20000),
new Product(3, "Headphones", 1500),
new Product(4, "Smart Watch", 3000),
new Product(5, "Pendrive", 500)
};
System.out.print("Enter customer name: ");
String name = sc.nextLine();
Customer customer = new Customer(name);
while (true) {
System.out.println("\n======= PRODUCT LIST =======");
for (Product p : products) {
System.out.println(p.getId() + ". " + p.getName() + " - ₹" + p.getPrice());
}
System.out.println("0. Checkout");
System.out.print("\nEnter product ID to buy: ");
int id = sc.nextInt();
if (id == 0) break;
Product selected = null;
for (Product p : products) {
if (p.getId() == id) {
selected = p;
}
}
if (selected == null) {
System.out.println("Invalid product ID!");
continue;
}
System.out.print("Enter quantity: ");
int qty = sc.nextInt();
customer.addToCart(selected, qty);
System.out.println("Added to cart successfully!");
}
customer.showBill();
sc.close();
}
}