package PrioritySchedule;
import java.text.DecimalFormat;
import java.util.Scanner;
public class priosched {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
DecimalFormat format = new DecimalFormat ("##.##");
System.out.println("Enter the number of processes: ");
int job = sc.nextInt();
int Burst[] = new int[job];
for (int i = 0; i < job; i++) {
System.out.println("Enter the burst time for " + (i+1) + ": ");
Burst[i] = sc.nextInt();
}
int Priority[] = new int[job];
for (int i = 0; i < job; i++) {
System.out.println("Enter the priority number for process " + (i+1) + ": ");
Priority[i] = sc.nextInt();
}
int P1[] = new int[job];
for ( int i = 0; i < job; i++) {
P1[i] = Priority[i];
}
for (int i = 0; i < job; i++) {
for (int j = 0; j < job-1; j++) {
if(Priority[j] > Priority[j+1]) {
int temp = Priority[j];
Priority[j] = Priority[j+1];
Priority[j+1] = temp;
}
}
}
int wait = 0;
int waitingTime[] = new int[job];
int turnaround[] = new int[job];
float totalWait = 0;
float totalTurn = 0;
int k = 0;
int l = 0;
turnaround[0] = Burst [0];
System.out.println("\n\t\t\t\t\t===============================================================================\t\t\t\t");
System.out.println("\t\t\t\t\tProcess\t\tBurst Time\t\tWaiting Time\t\tTurnaround Time");
System.out.println("\t\t\t\t\t===============================================================================\t\t\t\t\n");
for (int i = 0; i < job; i++) {
for (int j = 0; j < job; j++) {
if (Priority[i] == P1[j]) {
System.out.println("\t\t\t\t\tP" + (j+1) + "\t\t" + Burst[j] + "\t\t\t" + wait + "\t\t\t" + turnaround[j]);
waitingTime[k] = wait;
totalWait += wait;
wait += Burst[j];
turnaround[l] = waitingTime[k] + Burst[k];
totalTurn += turnaround[l];
}
}
}
System.out.println("\n\t\t\t\t\tThe Average Waiting Time is: " + format.format(totalWait/job));
System.out.println("\n\t\t\t\t\tThe Average Turnaround Time is: " + format.format(totalTurn/job));
System.out.println("\n\t\t\t\t\t===============================================================================\t\t\t\t");
System.out.println("\t\t\t\t\t===============================================================================\t\t\t\t\n");
}
}