package Dijkstra;
//import static java.awt.geom.Point2D.distance;
import java.util.Scanner;
import java.lang.ArrayIndexOutOfBoundsException;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Formatter;
import java.util.InputMismatchException;
public class Traffic {
//private
static void dijkstra
(int
[][] array, int
[] distance
) { boolean[] visited = new boolean[size];
for (int i = 0; i < size; i++) {
distance[i] = Integer.MAX_VALUE;
}
Scanner value
= new Scanner
(System.in
); System.out
.println
("Please Select your From destination::");
try{
int value1 = value.nextInt();
distance[value1] = 0;
}catch(InputMismatchException e){
System.out
.println
("Invalid.Please try to type Integer value.."); }
for (int i = 0; i < size - 1; i++) {
int mindis = minimumDistance(distance, visited);
visited[mindis] = true;
for (int j = 0; j < size; j++) {
if (!visited
[j
] && array[mindis
][j
] != 0 && distance
[mindis
] != Integer
.MAX_VALUE
) { int newdis
= distance
[mindis
] + array[mindis
][j
]; if (newdis < distance[j]) {
distance[j] = newdis;
}
}
}
}
}
public static int minimumDistance(int[] distance, boolean[] visited) {
int minimum = -1;
int minDistance = Integer.MAX_VALUE;
for (int i = 0; i < distance.length; i++) {
if (!visited[i] && distance[i] < minDistance) {
minDistance = distance[i];
minimum = i;
}
}
return minimum;
}
public static void main(String[] args) {
// System.out.println(dir.getAbsoluteFile());
File file1
= new File("D:/java/OOP/person/Student.txt");
while (true) {
try {
file1.createNewFile();
System.out
.println
("....................Welcome to our traffic Management System......................\n");
System.out
.println
("1.Start the Program"); System.out
.println
("2.Exit \n"); System.out
.println
("Plase Choose Your Option..");
} catch (IOException e) {
}
try {
Scanner result
= new Scanner
(System.in
); int option;
option = result.nextInt();
switch (option) {
case 1 -> {
try {
int a, b, weight;
int n, v;
int[] distance;
try {
Formatter f1 = new Formatter("D:/java/OOP/person/Student.txt");
Scanner input
= new Scanner
(System.in
); System.out
.println
("Thank Your Sir or Ma'am for opening our Traffic management Service.\n"); System.out
.println
("Please Provide How many Location: "); n = input.nextInt();
System.out
.println
("Please Provide How many Roads: ");
v = input.nextInt();
f1.format("%d %d \r \n", n, v);
distance = new int[n];
array = new int
[n
][n
]; // 2D array used in Java System.out
.println
("Please Provide your Inputs::"); for (int i = 0; i < v; i++) {
a = input.nextInt();
b = input.nextInt();
weight = input.nextInt();
f1.format("%d %d %d \r \n", a, b, weight);
}
f1.close();
Traffic
.dijkstra
(array, distance
); for (int i = 0; i < n; i++) {
System.out
.println
(i
+ " --> " + i
+ ""); }
System.out
.println
("Please Select your To destination::"); int destination;
try{
destination = input.nextInt();
System.out
.println
("There are " + distance
[destination
] + " Cars on this road And the road is less congested.."); System.out
.println
("You can go this Way.."); }catch(InputMismatchException e){
System.out
.println
("Invalid.Please try to type Integer value.."); }
} catch (FileNotFoundException e) {
}
} catch (ArrayIndexOutOfBoundsException e) {
}
}
case 2 -> {
System.out
.println
("Thank Your our Using the service."); System.out
.println
("Exit the program"); }
default -> System.out
.println
("Invalid Option.Please choose again..");
}
} catch (InputMismatchException e) {
System.out
.println
("Invalid.Please try to type Integer value.."); }
}
}
}