/
victor_t
/
simple_navigator
Обзор
Документация
Войти
/
victor_t
/
simple_navigator
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
1
CI/CD
Аналитика
Безопасность
master
src/graph/ConsoleInterface.java
173 строки
5 KB
Victor
fix: library classes
06 июл 2026, 01:06
06 июл 2026, 01:06
41665e7
Код
Авторство
О чём код?
package graph; import java.io.IOException; import java.util.Arrays; import java.util.List; import java.util.Scanner; public class ConsoleInterface { private final Scanner scanner; private boolean isLoaded; private final Graph graph; private final GraphAlgorithms algorithms; public ConsoleInterface() { scanner = new Scanner(System.in); isLoaded = false; graph = new Graph(); algorithms = new GraphAlgorithms(); loop(); } private void loop() { String choice; do { showMainMenu(); choice = scanner.nextLine().trim(); if (!choice.equalsIgnoreCase("q")) { try { userMakeChoice(choice); } catch (IllegalArgumentException e) { System.out.println("Input error: " + e.getMessage()); } catch (IllegalStateException e) { System.out.println("Algorithm error: " + e.getMessage()); } catch (IOException e) { System.out.println("File error: " + e.getMessage()); } catch (Exception e) { System.out.println("Error: " + e.getMessage()); } } } while (!choice.equalsIgnoreCase("q")); System.out.println("Goodbye!"); } private void showMainMenu() { System.out.println(); System.out.println("=== Simple Navigator ==="); System.out.println("1. Load graph from file"); System.out.println("2. Breadth-first search"); System.out.println("3. Depth-first search"); System.out.println("4. Shortest path between two vertices (Dijkstra)"); System.out.println("5. Shortest paths between all vertices (Floyd-Warshall)"); System.out.println("6. Minimal spanning tree (Prim)"); System.out.println("7. Traveling salesman problem (ACO)"); System.out.println("q. Quit"); System.out.print("Enter choice: "); } private void userMakeChoice(String choice) throws IOException { switch (choice) { case "1": runLoadingFile(); break; case "2": runBFS(); break; case "3": runDFS(); break; case "4": runShortestPath(); break; case "5": runShortestPathAll(); break; case "6": runLeastTree(); break; case "7": runACO(); break; default: System.out.println("Unknown option: '" + choice + "'"); } } private void runLoadingFile() throws IOException { System.out.print("Enter file path: "); String path = scanner.nextLine().trim(); graph.loadGraphFromFile(path); isLoaded = true; System.out.println("Graph loaded successfully. Vertices: " + graph.getSize()); } private void runBFS() { if (!checkLoaded()) { return; } List<Integer> result = algorithms.breadthFirstSearch(graph, 1); System.out.println("BFS traversal from vertex 1: " + result); } private void runDFS() { if (!checkLoaded()) { return; } List<Integer> result = algorithms.depthFirstSearch(graph, 1); System.out.println("DFS traversal from vertex 1: " + result); } private void runShortestPath() { if (!checkLoaded()) { return; } System.out.print("Enter vertex 1: "); int v1 = Integer.parseInt(scanner.nextLine().trim()); System.out.print("Enter vertex 2: "); int v2 = Integer.parseInt(scanner.nextLine().trim()); int result = algorithms.getShortestPathBetweenVertices(graph, v1, v2); System.out.println("Shortest path from " + v1 + " to " + v2 + ": " + result); } private void runShortestPathAll() { if (!checkLoaded()) { return; } int[][] result = algorithms.getShortestPathsBetweenAllVertices(graph); System.out.println("All-pairs shortest paths:"); printMatrix(result); } private void runLeastTree() { if (!checkLoaded()) { return; } int[][] result = algorithms.getLeastSpanningTree(graph); System.out.println("Minimum spanning tree adjacency matrix:"); printMatrix(result); } private void runACO() { if (!checkLoaded()) { return; } TsmResult result = algorithms.solveTravelingSalesmanProblem(graph); String route = Arrays.stream(result.vertices) .mapToObj(String::valueOf) .reduce((a, b) -> a + " -> " + b) .orElse(""); System.out.println("Resulting route calculated by ACO: " + route + "\nThe route distance: " + result.distance); } private boolean checkLoaded() { if (!isLoaded) { System.out.println("No graph loaded. Please load a graph first (option 1)."); return false; } return true; } private void printMatrix(int[][] matrix) { for (int[] row : matrix) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < row.length; i++) { if (i > 0) { sb.append(' '); } sb.append(row[i]); } System.out.println(sb); } } }