-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
411 lines (372 loc) · 15.6 KB
/
Copy pathMain.java
File metadata and controls
411 lines (372 loc) · 15.6 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
import java.util.*;
import java.io.*;
// ============================== CLASS: AccountManager ==============================
class AccountManager {
private static final String FILE_NAME = "accounts.txt";
private static Map<String, String> accounts = new HashMap<>();
private static Map<String, String> roles = new HashMap<>();
static {
try (BufferedReader br = new BufferedReader(new FileReader(FILE_NAME))) {
String line;
while ((line = br.readLine()) != null) {
String[] parts = line.split(",");
if (parts.length == 3) {
accounts.put(parts[0], parts[1]);
roles.put(parts[0], parts[2]);
}
}
} catch (IOException e) {
// File may not exist initially
}
}
private static void saveAccount(String id, String password, String role) {
try (BufferedWriter bw = new BufferedWriter(new FileWriter(FILE_NAME, true))) {
bw.write(id + "," + password + "," + role);
bw.newLine();
} catch (IOException e) {
System.out.println("Error saving account.");
}
}
public static String signIn(Scanner sc) {
System.out.print("Enter ID: ");
String id = sc.next();
System.out.print("Enter Password: ");
String pw = sc.next();
if (accounts.containsKey(id) && accounts.get(id).equals(pw)) {
System.out.println("Login successful.");
return id;
} else {
System.out.println("Invalid credentials.");
return null;
}
}
public static String createAccount(Scanner sc) {
System.out.print("Enter new ID: ");
String id = sc.next();
if (accounts.containsKey(id)) {
System.out.println("ID already exists.");
return null;
}
System.out.print("Enter new Password: ");
String pw = sc.next();
System.out.print("Enter role (municipal/citizen): ");
String role = sc.next().toLowerCase();
if (!role.equals("municipal") && !role.equals("citizen")) {
System.out.println("Invalid role.");
return null;
}
accounts.put(id, pw);
roles.put(id, role);
saveAccount(id, pw, role);
System.out.println("Account created successfully.");
return id;
}
public static String getRole(String id) {
return roles.get(id);
}
}
// ============================== CLASS: Resource ==============================
class Resource {
String type, id, driverName;
boolean available = true;
Resource(String type, String id, String driverName) {
this.type = type;
this.id = id;
this.driverName = driverName;
}
}
// ============================== CLASS: Request ==============================
class Request {
String requesterID, type, location, status, allocatedResource;
int priority;
Request(String requesterID, String type, String location, int priority) {
this.requesterID = requesterID;
this.type = type;
this.location = location;
this.priority = priority;
this.status = "Pending";
this.allocatedResource = "None";
}
}
// ============================== CLASS: CityGraph (WEIGHTED) ==============================
class CityGraph {
// Modified: adjacency map now stores Map<neighbor, distance>
Map<String, Map<String, Integer>> adj = new HashMap<>();
Map<String, List<Resource>> resources = new HashMap<>();
void addArea(String area) {
adj.putIfAbsent(area, new HashMap<>());
}
// Modified: Add weighted road
void addRoad(String a, String b, int distance) {
if (!adj.containsKey(a) || !adj.containsKey(b)) {
System.out.println("One or both areas not found.");
return;
}
adj.get(a).put(b, distance);
adj.get(b).put(a, distance);
System.out.println("Road added between " + a + " and " + b + " with distance " + distance);
}
void addResourceCenter(String area) {
resources.putIfAbsent(area, new ArrayList<>());
System.out.println("Resource center added at " + area);
}
void addResource(String area, Resource r) {
if (!resources.containsKey(area)) {
System.out.println("Resource center not found for area.");
return;
}
resources.get(area).add(r);
System.out.println("Resource added successfully at " + area);
}
void displayMap() {
System.out.println("\n--- City Map ---");
for (String a : adj.keySet()) {
System.out.print(a + " -> ");
for (Map.Entry<String, Integer> e : adj.get(a).entrySet()) {
System.out.print(e.getKey() + "(" + e.getValue() + " km) ");
}
System.out.println();
}
}
void showAllResources() {
System.out.println("\n--- All Resources ---");
for (String area : resources.keySet()) {
System.out.println(area + ":");
for (Resource r : resources.get(area)) {
System.out.println(" " + r.type + " | ID: " + r.id + " | Driver: " + r.driverName +
" | Available: " + r.available);
}
}
}
// Modified: Dijkstra's algorithm for weighted shortest path
List<String> shortestPath(String start, String end) {
if (!adj.containsKey(start) || !adj.containsKey(end)) {
return new ArrayList<>();
}
Map<String, Integer> dist = new HashMap<>();
Map<String, String> parent = new HashMap<>();
PriorityQueue<String> pq = new PriorityQueue<>(Comparator.comparingInt(dist::get));
for (String node : adj.keySet()) {
dist.put(node, Integer.MAX_VALUE);
}
dist.put(start, 0);
pq.add(start);
while (!pq.isEmpty()) {
String node = pq.poll();
for (Map.Entry<String, Integer> e : adj.get(node).entrySet()) {
String nei = e.getKey();
int w = e.getValue();
if (dist.get(node) + w < dist.get(nei)) {
dist.put(nei, dist.get(node) + w);
parent.put(nei, node);
pq.add(nei);
}
}
}
if (dist.get(end) == Integer.MAX_VALUE)
return new ArrayList<>();
List<String> path = new ArrayList<>();
for (String at = end; at != null; at = parent.get(at))
path.add(at);
Collections.reverse(path);
return path;
}
Resource allocateResource(String emergencyArea, String type) {
for (String area : resources.keySet()) {
for (Resource r : resources.get(area)) {
if (r.type.equalsIgnoreCase(type) && r.available) {
r.available = false;
List<String> path = shortestPath(area, emergencyArea);
System.out.println("\nAllocated " + r.type + " (" + r.id + ") from " + area);
System.out.println("Driver: " + r.driverName);
if (path.isEmpty()) {
System.out.println("No direct path found.");
} else {
System.out.println("Shortest path: " + path);
}
return r;
}
}
}
System.out.println("No available resource of type " + type);
return null;
}
void markComplete(String id) {
for (List<Resource> list : resources.values()) {
for (Resource r : list) {
if (r.id.equals(id)) {
r.available = true;
System.out.println("Task completed for " + r.id);
return;
}
}
}
System.out.println("No resource found with given ID.");
}
}
// ============================== CLASS: HistoryManager ==============================
class HistoryManager {
private static Map<Integer, Request> allRequests = new HashMap<>();
private static int requestCounter = 1;
static void addRequest(Request req) {
allRequests.put(requestCounter++, req);
}
static void updateStatus(String resourceID) {
for (Request r : allRequests.values()) {
if (r.allocatedResource.equals(resourceID)) {
r.status = "Completed";
}
}
}
static void showMunicipalHistory() {
System.out.println("\n--- All Requests History ---");
if (allRequests.isEmpty()) {
System.out.println("No requests recorded yet.");
return;
}
for (Map.Entry<Integer, Request> e : allRequests.entrySet()) {
Request r = e.getValue();
System.out.println("Request ID: " + e.getKey() + " | Type: " + r.type + " | Location: " + r.location
+ " | Status: " + r.status + " | Resource: " + r.allocatedResource + " | Requested by: " + r.requesterID);
}
}
static void showUserHistory(String userID) {
System.out.println("\n--- Your Requests ---");
boolean found = false;
for (Map.Entry<Integer, Request> e : allRequests.entrySet()) {
Request r = e.getValue();
if (r.requesterID.equals(userID)) {
System.out.println("Request ID: " + e.getKey() + " | Type: " + r.type + " | Location: " + r.location
+ " | Status: " + r.status + " | Resource: " + r.allocatedResource);
found = true;
}
}
if (!found) System.out.println("You have no requests yet.");
}
}
// ============================== MAIN CLASS ==============================
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
CityGraph city = new CityGraph();
while (true) {
System.out.println("\n===== SMART CITY EMERGENCY RESOURCE OPTIMIZER =====");
System.out.println("1. Login");
System.out.println("2. Sign Up");
System.out.println("3. Exit");
System.out.print("Enter choice: ");
int opt = sc.nextInt();
String userID = null;
if (opt == 1) {
userID = AccountManager.signIn(sc);
} else if (opt == 2) {
userID = AccountManager.createAccount(sc);
} else if (opt == 3) {
System.out.println("Exiting system. Goodbye!");
return;
} else {
System.out.println("Invalid choice.");
continue;
}
if (userID == null)
continue;
String role = AccountManager.getRole(userID);
if (role.equals("municipal")) {
while (true) {
System.out.println("\n--- MUNICIPAL DASHBOARD ---");
System.out.println("1. Add Area");
System.out.println("2. Add Road");
System.out.println("3. Display City Map");
System.out.println("4. Add Resource Center");
System.out.println("5. Add Resource");
System.out.println("6. Show All Resources");
System.out.println("7. Mark Task Complete");
System.out.println("8. View All History");
System.out.println("9. Logout");
System.out.print("Enter choice: ");
int ch = sc.nextInt();
switch (ch) {
case 1 -> {
System.out.print("Enter area: ");
city.addArea(sc.next());
}
case 2 -> {
System.out.print("Enter area1: ");
String a = sc.next();
System.out.print("Enter area2: ");
String b = sc.next();
System.out.print("Enter distance (km): ");
int d = sc.nextInt();
city.addRoad(a, b, d);
}
case 3 -> city.displayMap();
case 4 -> {
System.out.print("Enter center area: ");
city.addResourceCenter(sc.next());
}
case 5 -> {
System.out.print("Enter center: ");
String area = sc.next();
System.out.print("Enter type: ");
String type = sc.next();
System.out.print("Enter vehicle ID: ");
String id = sc.next();
System.out.print("Enter driver name: ");
String dn = sc.next();
city.addResource(area, new Resource(type, id, dn));
}
case 6 -> city.showAllResources();
case 7 -> {
System.out.print("Enter resource ID: ");
String id = sc.next();
city.markComplete(id);
HistoryManager.updateStatus(id);
}
case 8 -> HistoryManager.showMunicipalHistory();
case 9 -> {
System.out.println("Returning to Main Menu...");
break;
}
default -> System.out.println("Invalid choice");
}
if (ch == 9) break;
}
} else if (role.equals("citizen")) {
while (true) {
System.out.println("\n--- CITIZEN DASHBOARD ---");
System.out.println("1. Show All Available Resources");
System.out.println("2. Create Emergency Request");
System.out.println("3. View My History");
System.out.println("4. Logout");
System.out.print("Enter choice: ");
int ch = sc.nextInt();
switch (ch) {
case 1 -> city.showAllResources();
case 2 -> {
System.out.print("Enter emergency area: ");
String ea = sc.next();
System.out.print("Enter resource type: ");
String type = sc.next();
System.out.print("Enter priority (0=High, 1=Medium, 2=Low): ");
int pri = sc.nextInt();
Request req = new Request(userID, type, ea, pri);
Resource allocated = city.allocateResource(ea, type);
if (allocated != null) {
req.allocatedResource = allocated.id;
req.status = "Assigned";
}
HistoryManager.addRequest(req);
}
case 3 -> HistoryManager.showUserHistory(userID);
case 4 -> {
System.out.println("Returning to Main Menu...");
break;
}
default -> System.out.println("Invalid choice");
}
if (ch == 4) break;
}
}
}
}
}