-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMenu.java
94 lines (82 loc) · 3 KB
/
Menu.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import java.util.Scanner;
public class Menu {
private int numberOfProcess;
private int[][] matrix = new int[10][6];
private int[][] adjustedMatrix;
private boolean flag = true;
Menu(){}
public void promptInput(){
do{
inputNumberOfProcess();
inputProcessDetails();
}while(!flag);
autofillProcessID(); //fill process id
adjustMatrix();
}
//get number of process
private void inputNumberOfProcess() {
flag = true;
do{
Scanner scan = new Scanner(System.in);
System.out.print("Enter number of process (3-10): ");
int input = scan.nextInt();
if(input>2 && input<11){
numberOfProcess = input;
break;
}
else{
System.out.println("Number of process must be in between 3 and 10");
}
}while(true);
}
//get processes' input
private void inputProcessDetails(){
Scanner scan = new Scanner(System.in);
String[] str = {"burst time","arrival time","priority"};
for(int i=0; i<3; i++){
System.out.print("Enter "+ str[i]+ ": ");
String input = scan.nextLine();
String[] inputStr = input.split("\\s+");
// make sure length entered is same as getNumberOfProcess
if(inputStr.length == numberOfProcess){ // if boundary is true
try{ //check input is int
for(int j = 0; j < inputStr.length; j++) {
int num = Integer.parseInt(inputStr[j]);
switch (i) {
case 0:
matrix[j][1] = num;break;//burst time
case 1:
matrix[j][2] = num;break;//arrival time
case 2:
matrix[j][3] = num;break;//priority
}
}
}catch(NumberFormatException ex){ //if not int
System.out.println("\nYou MUST only enter integer. Please Retry\n");
flag = false;
break;
}
}
else{ // if boundary is false
System.out.println("\nYour currently number of process is " + numberOfProcess +". So your input is invalid. Please Retry\n");
flag = false;
break;
}
}
}
private void autofillProcessID(){
for (int i = 0; i < numberOfProcess; i++) {
matrix[i][0] = i+1;
}
}
private void adjustMatrix(){
adjustedMatrix = new int[numberOfProcess][6];
for(int i = 0; i < numberOfProcess; i++){
for(int j = 0; j < 6; j++)
adjustedMatrix[i][j]=matrix[i][j];
}
}
//getter
public int getNumberOfProcess() { return numberOfProcess; }
public int[][] getAdjustedMatrix() { return adjustedMatrix; }
}