-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path15.DesignRestaurantOrderManagement.java
More file actions
208 lines (180 loc) · 6.61 KB
/
15.DesignRestaurantOrderManagement.java
File metadata and controls
208 lines (180 loc) · 6.61 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/* ---------------------------------------------------------------------------- */
/* ( The Authentic JS/JAVA CodeBuff )
___ _ _ _
| _ ) |_ __ _ _ _ __ _ __| |_ __ ____ _ (_)
| _ \ ' \/ _` | '_/ _` / _` \ V V / _` || |
|___/_||_\__,_|_| \__,_\__,_|\_/\_/\__,_|/ |
|__/
*/
/* -------------------------------------------------------------------------- */
/* Youtube: https://youtube.com/@code-with-Bharadwaj */
/* Github : https://github.com/Manu577228 */
/* Portfolio : https://manu-bharadwaj-portfolio.vercel.app/portfolio */
/* ----------------------------------------------------------------------- */
import java.util.*;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/*
-----------------------------------------------------------
LLD Problem : Design a Restaurant Order Management System
-----------------------------------------------------------
1) REQUIREMENTS
-----------------------------------------------------------
a) Functional Requirements:
- Add menu items.
- Take customer orders.
- View order summary.
- Update order status (Pending → Preparing → Served).
- Generate total bill per order.
b) Non-Functional Requirements:
- In-memory only (no DB).
- Thread-safe for concurrent updates.
- Extendable and easy to read for demos.
2) ALGORITHM CHOICE DISCUSSION
-----------------------------------------------------------
- Object-Oriented Design using classes MenuItem, Order, Restaurant.
- HashMap for O(1) lookup for menu and orders.
- Synchronized updates using Lock for concurrency.
3) CONCURRENCY & DATA MODEL DISCUSSION
-----------------------------------------------------------
- Concurrency: ReentrantLock ensures atomic order updates.
- Data Model:
MenuItem → id, name, price
Order → id, List<MenuItem>, total, status
Restaurant→ menus, orders, lock
4) UML DIAGRAM (Textual)
-----------------------------------------------------------
+--------------------+
| Restaurant |
+--------------------+
| - menuItems |
| - orders |
| - lock |
+--------------------+
| + addMenuItem() |
| + placeOrder() |
| + updateOrder() |
| + showOrders() |
+--------------------+
|
+---------+---------+
| |
+--------------+ +----------------+
| MenuItem | | Order |
+--------------+ +----------------+
| id, name, | | id, items, |
| price | | total, status |
+--------------+ +----------------+
-----------------------------------------------------------
5) JAVA SOLUTION WITH EXPLANATION & OUTPUT
-----------------------------------------------------------
*/
class MenuItem {
int id;
String name;
double price;
MenuItem(int id, String name, double price) {
this.id = id;
this.name = name;
this.price = price;
}
}
class Order {
int id;
List<MenuItem> items;
double total;
String status;
Order(int id, List<MenuItem> items) {
this.id = id;
this.items = items;
this.total = items.stream().mapToDouble(i -> i.price).sum();
this.status = "Pending";
}
}
class Restaurant {
private final Map<Integer, MenuItem> menuItems;
private final Map<Integer, Order> orders;
private final Lock lock;
Restaurant() {
this.menuItems = new HashMap<>();
this.orders = new HashMap<>();
this.lock = new ReentrantLock();
}
public void addMenuItem(int id, String name, double price) {
menuItems.put(id, new MenuItem(id, name, price));
}
public Order placeOrder(int orderId, List<Integer> itemIds) {
lock.lock();
try {
List<MenuItem> selectedItems = new ArrayList<>();
for (int id : itemIds) {
if (menuItems.containsKey(id)) {
selectedItems.add(menuItems.get(id));
}
}
Order order = new Order(orderId, selectedItems);
orders.put(orderId, order);
return order;
} finally {
lock.unlock();
}
}
public void updateOrderStatus(int orderId, String newStatus) {
lock.lock();
try {
if (orders.containsKey(orderId)) {
orders.get(orderId).status = newStatus;
}
} finally {
lock.unlock();
}
}
public void showOrders() {
System.out.println("\n------ Current Orders ------");
for (Order o : orders.values()) {
List<String> itemNames = new ArrayList<>();
for (MenuItem i : o.items) {
itemNames.add(i.name);
}
System.out.println("Order #" + o.id + ": " + itemNames + " | Total: ₹" + o.total + " | Status: " + o.status);
}
}
}
public class RestaurantOrderManagement {
public static void main(String[] args) {
Restaurant restaurant = new Restaurant();
restaurant.addMenuItem(1, "Margherita Pizza", 250);
restaurant.addMenuItem(2, "Pasta Alfredo", 180);
restaurant.addMenuItem(3, "Cold Coffee", 90);
restaurant.placeOrder(101, Arrays.asList(1, 3));
restaurant.placeOrder(102, Arrays.asList(2));
restaurant.showOrders();
restaurant.updateOrderStatus(101, "Preparing");
restaurant.updateOrderStatus(102, "Served");
restaurant.showOrders();
}
}
/*
OUTPUT:
------ Current Orders ------
Order #101: [Margherita Pizza, Cold Coffee] | Total: ₹340.0 | Status: Pending
Order #102: [Pasta Alfredo] | Total: ₹180.0 | Status: Pending
------ Current Orders ------
Order #101: [Margherita Pizza, Cold Coffee] | Total: ₹340.0 | Status: Preparing
Order #102: [Pasta Alfredo] | Total: ₹180.0 | Status: Served
*/
/*
-----------------------------------------------------------
6) LIMITATIONS OF CURRENT CODE
-----------------------------------------------------------
- No persistence (data lost on restart).
- Single-thread lock limits parallel scalability.
- No tax, discount, or category support.
7) ALTERNATIVE ALGORITHMS & TRADE-OFFS
-----------------------------------------------------------
- Future: Use ExecutorService for async order processing.
- Add lightweight DB (H2/SQLite) for persistence.
- Event-driven messaging for live kitchen updates.
- Decouple menu and order microservices.
-----------------------------------------------------------
*/