-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
78 lines (73 loc) · 3.06 KB
/
Main.java
File metadata and controls
78 lines (73 loc) · 3.06 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
import java.util.InputMismatchException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
LocationManager manager = new LocationManager();
while (true) {
System.out.println("\n--- Smart City Route Planner ---");
System.out.println("1. Add a new location");
System.out.println("2. Remove a location");
System.out.println("3. Add a road between locations");
System.out.println("4. Remove a road");
System.out.println("5. Display all connections");
System.out.println("6. Display all locations (from AVL Tree)");
System.out.println("7. Find path between locations (BFS)");
System.out.println("8. Exit");
System.out.print("Enter your choice: ");
int choice = -1;
// FIX: Added input validation
try {
choice = sc.nextInt();
} catch (InputMismatchException e) {
System.out.println("Invalid input. Please enter a number.");
sc.nextLine(); // Clear the bad input
continue; // Skip the rest of the loop
}
sc.nextLine(); // Consume the newline character
switch (choice) {
case 1:
System.out.print("Enter location name: ");
manager.addLocation(sc.nextLine());
break;
case 2:
System.out.print("Enter location name to remove: ");
manager.removeLocation(sc.nextLine());
break;
case 3:
System.out.print("Enter first location: ");
String from = sc.nextLine();
System.out.print("Enter second location: ");
String to = sc.nextLine();
manager.addRoad(from, to);
break;
case 4:
System.out.print("Enter first location: ");
from = sc.nextLine();
System.out.print("Enter second location: ");
to = sc.nextLine();
manager.removeRoad(from, to);
break;
case 5:
manager.showConnections();
break;
case 6:
manager.showAllLocations();
break;
case 7: // NEW: Pathfinding
System.out.print("Enter start location: ");
from = sc.nextLine();
System.out.print("Enter end location: ");
to = sc.nextLine();
manager.findPath(from, to);
break;
case 8:
System.out.println("Exiting... Goodbye!");
sc.close();
return;
default:
System.out.println("Invalid choice. Please try again.");
}
}
}
}