Question: I need help with my route management system, aimed at calculating and optimizing routes between different locations using algorithms and data structures. Each location will

I need help with my route management system, aimed at calculating and optimizing routes between different locations using algorithms and data structures. Each location will be represented as a node in an undirected graph, and connections between locations will be modeled as edges. These edges should have weight and time attributes. All the methods to be used are in the LocationManager class, however, I feel like the main method is not presenting the menu as it should.
this task is based on a graph with nodes and edges (adjacency matrix). So, when prompted to enter a location in the main method, it's to enter the name of the vertex, for example, node"1", and then specify how many edges it will have and to which nodes it will connect. However, the program is not doing that. I need to fix the main method. Help.
public class Main implements Serializable{
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
LocationManager routeSystem = new LocationManager(5); // Example with 5 locations
// Add locations
while (true){
routeSystem.displayMenu();
int choice = scanner.nextInt();
scanner.nextLine(); // Consume newline
switch (choice){
case 1:
System.out.print("Enter location name: ");
String name = scanner.nextLine();
routeSystem.addLocation(new Location(name));
break;
case 2:
System.out.print("Enter index of location to edit: ");
int indexEdit = scanner.nextInt();
scanner.nextLine(); // Consume newline
System.out.print("Enter new location name: ");
String newName = scanner.nextLine();
routeSystem.editLocation(indexEdit, new Location(newName));
break;
case 3:
System.out.print("Enter index of location to delete: ");
int indexDelete = scanner.nextInt();
scanner.nextLine(); // Consume newline
routeSystem.deleteLocation(indexDelete);
break;
case 4:
System.out.print("Enter start location index: ");
int start = scanner.nextInt();
System.out.print("Enter end location index: ");
int end = scanner.nextInt();
List shortestPath = routeSystem.shortestPathDijkstra(start, end);
System.out.println("Shortest Path: "+ shortestPath);
break;
case 5:
routeSystem.minimumSpanningTreePrim();
break;
case 6:
routeSystem.minimumSpanningTreeKruskal();
break;
case 7:
routeSystem.shortestPathFloydWarshall();
break;
case 8:
routeSystem.minimizeTime();
break;
case 9:
routeSystem.minimizeDistance();
break;
case 0:
System.out.println("Exiting...");
System.exit(0);
break;
default:
System.out.println("Invalid choice. Please try again.");
}
public class LocationManager implements Serializable {
private static final long serialVersionUID =1760598463252095534L;
private Edge[][] matrizAdyacencia;
private List locations;
public LocationManager(int numLocations){
matrizAdyacencia = new Edge[numLocations][numLocations];
locations = new ArrayList<>();
}
public void setAdjacencyMatrix(Edge[][] matrizAdyacencia){
this.matrizAdyacencia = matrizAdyacencia;
}
public void getAdjacencyMatrix(Edge[][] matrizAdyacencia){
this.matrizAdyacencia = matrizAdyacencia;
}
public List getLocations(){
return locations;
}
public void setLocations(List locations){
this.locations = locations;
}
public void addLocation(Location location){
locations.add(location);
}
public void editLocation(int index, Location location){
if (index >=0 && index < locations.size()){
locations.set(index, location);
}
}public void addConnection(int source, int destination, int weight, int timeMin){
if (source >=0 && source < locations.size() &&
destination >=0 && destination < locations.size()){
matrizAdyacencia[source][destination]= new Edge(weight, timeMin);
matrizAdyacencia[destination][source]= new Edge(weight, timeMin); // Assuming undirected graph
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!