-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOrder.java
More file actions
119 lines (95 loc) · 3.44 KB
/
Copy pathOrder.java
File metadata and controls
119 lines (95 loc) · 3.44 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
116
117
118
119
package src;
import java.util.ArrayList;
import java.util.Random;
import java.sql.Connection;
import java.sql.PreparedStatement;
class Order {
ArrayList<FoodItem> cart = new ArrayList<>();
int orderId;
public Order() {
Random r = new Random();
orderId = 1000 + r.nextInt(9000);
}
public void addItem(FoodItem item) {
cart.add(item);
System.out.println(item.getName() + " added!");
}
public void removeItem(int id) {
for (FoodItem item : cart) {
if (item.getId() == id) {
cart.remove(item);
System.out.println(item.getName() + " removed!");
return;
}
}
System.out.println("Item not found!");
}
public void showCart() {
System.out.println("\n----- CART -----");
if (cart.isEmpty()) {
System.out.println("Cart is empty!");
return;
}
for (FoodItem item : cart) {
System.out.println(item.getName() + " - Rs." + item.getPrice());
}
}
public double calculateTotal() {
double total = 0;
for (FoodItem item : cart) {
total += item.getPrice();
}
return total;
}
public void saveOrderToDB(Customer c, double finalAmount) {
try {
Connection con = DBConnection.getConnection();
StringBuilder itemsList = new StringBuilder();
for (FoodItem item : cart) {
itemsList.append(item.getName())
.append(" (").append(item.getPrice()).append("), ");
}
String query = "INSERT INTO orders (order_id, customer_name, phone, address, items, total) VALUES (?, ?, ?, ?, ?, ?)";
PreparedStatement ps = con.prepareStatement(query);
ps.setInt(1, orderId);
ps.setString(2, c.getName());
ps.setString(3, c.getPhone());
ps.setString(4, c.getAddress());
ps.setString(5, itemsList.toString());
ps.setDouble(6, finalAmount);
ps.executeUpdate();
} catch (Exception e) {
System.out.println("DB Error");
}
}
public void trackOrder() {
try {
System.out.println("\nOrder Confirmed...");
Thread.sleep(1500);
System.out.println("Preparing Food...");
Thread.sleep(2000);
System.out.println("Out for Delivery...");
Thread.sleep(2000);
System.out.println("Delivered to your address!");
} catch (Exception e) {}
}
public void orderSummary(Customer c) {
if (cart.isEmpty()) {
System.out.println("No items ordered!");
return;
}
System.out.println("\n----- ORDER SUMMARY -----");
System.out.println("Order ID: " + orderId);
System.out.println("Name: " + c.getName());
System.out.println("Address: " + c.getAddress());
for (FoodItem item : cart) {
System.out.println(item.getName() + " - Rs." + item.getPrice());
}
double total = calculateTotal();
double finalAmount = c.getDiscount(total);
System.out.println("Total: Rs." + total);
System.out.println("After Discount: Rs." + finalAmount);
saveOrderToDB(c, finalAmount);
trackOrder();
}
}