import java.util.Random;
import java.util.Scanner;
public class TrafficManagementSystem5 {
public static void main(String[] args) throws InterruptedException { // this execption is used to handle the
//error of thread wait time
Scanner input = new Scanner(System.in);
Random random = new Random(); // for random number of car passing
int northCars = 0, southCars = 0, eastCars = 0, westCars = 0;
int passingCars =0, passingCars1 =0, passingCars2 =0, passingCars3 =0;
int waitTime = 0;
int roadWaitTime = 0;
int roadDirection = 0;
System.out.print('\\u000C'); // for clearing the screen
System.out.println("Enter number of vehicle coming from north direction:");
northCars = input.nextInt();
northCars = northCars + 1; // add one in a original value bcoz while passing random number it gives more than 1 value but not zero
// so for exact vehicle will passed : input 20 +1 = 21 , after upto 20 value will decrease,
//loop will be executed and when value gets 1 it will terminated, thats why 20+1
System.out.println("Enter number of vehicle coming from south direction:");
southCars = input.nextInt();
southCars = southCars +1;
System.out.println("Enter number of vehicle coming from east direction:");
eastCars = input.nextInt();
eastCars = eastCars +1;
System.out.println("Enter number of vehicle coming from west direction:");
westCars = input.nextInt();
westCars = westCars +1;
System.out.println("Enter the waiting time in seconds for cars:");
waitTime = input.nextInt();
System.out.println("Enter the direction of road crossing (1 for North - South and 2 for West - East ):");
roadDirection = input.nextInt();
System.out.println("Enter the waiting time in seconds for road crossing:");
roadWaitTime = input.nextInt();
System.out.println(" ");
while (northCars > 1 || southCars >1 || eastCars >1 || westCars >1) {
if (northCars > 1||southCars>1) { // after first iteration now the value of northcars is =1 as becoz
//of northCars = northCars - passingCars ,so if both north and southcars =1 then the loop will terminated or
//else it will continue
System.out.println("West side and East side: Red light. Stoped....");
System.out.println(" ");
Thread.sleep(1000); //wait time 1 sec it counts on miliseconds so 1000 miliseconds = 1 sec
System.out.println("North side and South side: Green light. Vehicle passing...");
System.out.println(".........................");
int n= waitTime -1 ; // it is used to make program little slow , it is not necessary to use.
Thread.sleep(n*1000);
passingCars = random.nextInt(northCars);
northCars = northCars - passingCars;// eg= user value is 20 and we have added 1 northCars = 21,now random number
//will store in paasingCars var, lets say random number is 20 which is store in, passingCars = 20
passingCars1 = random.nextInt(southCars); // same random number for other var
// northCars= 21(northcars)-20(passingcars)= 1 now th update value of northCars =1
southCars -= passingCars1; //shorthand operator
System.out.println("West and East side: Orange light. Wating....");
System.out.println(".........................");
Thread.sleep(waitTime*1000); // it will wait the second that user has given
System.out.println("From North to South " + passingCars + " vehicle passed."); // so passingCars is = 20 as the
//value store in it whic means all cars has passed
System.out.println("From South to North " + passingCars1 + " vehicle passed.");
Thread.sleep(2*1000);
}
else {
System.out.println("North to South road is empty...");
System.out.println("South to North road is empty....");
Thread.sleep(2*1000);
}
if (eastCars > 1 || westCars > 1) {
System.out.println("North side and South side: Red light. Stoped....");
System.out.println(" ");
Thread.sleep(1*1000);
System.out.println("West side and East side: Green light. Vehicle passing...");
System.out.println(".........................");
int n= waitTime -1 ;
Thread.sleep(n*1000);
passingCars2 = random.nextInt(westCars ) ;
passingCars3 = random.nextInt(eastCars );
westCars -= passingCars2;
eastCars -= passingCars3;
System.out.println("North Side and South side: Orange light. Wating....");
System.out.println(".........................");
Thread.sleep(waitTime*1000);
System.out.println("From West to East " + passingCars2 + " vehicle passed.");
System.out.println("From East to West " + passingCars3 + " vehicle passed.");
Thread.sleep(2*1000);
}
else{
System.out.println("West to East road is empty...");
System.out.println("East to West road is empty....");
Thread.sleep(2*1000);
}
// Check if road crossing is open for N-S direction or W-E direction
if (roadDirection == 1) {
System.out.println("Traffic Stoped.");
System.out.println(" ");
System.out.println("North - South side road crossing open. Peoples are crossing the road...");
System.out.println("......... ");
Thread.sleep(roadWaitTime*1000);
}
else if (roadDirection == 2) {
System.out.println("Traffic Stoped.");
System.out.println(" ");
System.out.println("West - East road crossing open. Peoples are crossing the road....");
System.out.println("......... ");
Thread.sleep(roadWaitTime*1000);
}
}
System.out.println("All roads are empty.");
}
}