Repository navigation
Expand file tree
/
Copy pathUserInterface.java
More file actions
169 lines (153 loc) · 5.11 KB
/
Copy pathUserInterface.java
File metadata and controls
169 lines (153 loc) · 5.11 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import java.util.Scanner;
import com.felight.utils.*;
import com.felight.algos.SortingAlgos;
public class UserInterface {
private Scanner reader;
private int[] array;
private int[] dupArray;
private int answerSizeOfArray;
int answerComplexity;
int answerSortingAlgorithm;
public UserInterface(Scanner reader) {
this.reader = reader;
}
public void start() {
askForArraySize();
askForComplexity();
fillArray();
dupArray = new int[answerSizeOfArray];
Calculator.initDupArray(array, dupArray);
askForSortingAlgo();
}
public void askForArraySize() {
while(true) {
try {
System.out.println();
System.out.println("Enter the size of the array: ");
answerSizeOfArray = Integer.parseInt(reader.nextLine());
if (answerSizeOfArray > Integer.MAX_VALUE || answerSizeOfArray == 0 || answerSizeOfArray < 0) {
throw new NumberFormatException();
}
break;
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
System.out.println("Provide a smaller non-zero(or a valid) integer value please :)");
}
}
}
public void askForComplexity() {
while(true) {
try {
System.out.println();
System.out.println("Choose complexity: ");
System.out.println("\t1. Best Case");
System.out.println("\t2. Average Case");
System.out.println("\t3. Worst Case");
answerComplexity = Integer.parseInt(reader.nextLine());
if (answerComplexity > 3 || answerComplexity == 0 || answerComplexity < 0) {
throw new NumberFormatException();
}
break;
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
System.out.println("Please pick a number from 1, 2 and 3 as shown on the menu!");
}
}
}
public void fillArray() {
while(true) {
try {
if (answerComplexity == 1) {
array = Calculator.getNaturalNumbersArray(answerSizeOfArray);
System.out.println("Natural array of size " + answerSizeOfArray + " is generated.");
System.out.println();
break;
} else if (answerComplexity == 2) {
array = Calculator.getRandomArray(answerSizeOfArray);
System.out.println("Random array of size " + answerSizeOfArray + " is generated.");
System.out.println();
break;
} else if (answerComplexity == 3) {
array = Calculator.getReverseArray(answerSizeOfArray);
System.out.println("Reversed array of size " + answerSizeOfArray + " is generated.");
System.out.println();
break;
}
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("Sorry an array for the given size cannot be created. Please start over and select a smaller size!");
}
}
}
public void askForSortingAlgo() {
while(true) {
try {
System.out.println("Choose sorting algorithm: ");
System.out.println("\t1. Bubble Sort");
System.out.println("\t2. Quick Sort");
System.out.println("\t3. Insertion Sort");
System.out.println("\t4. Selection Sort");
System.out.println("\t5. Merge Sort");
System.out.println("\t6. Benchmark All");
System.out.println("\t7. Start Over");
System.out.println("\t8. Exit");
answerSortingAlgorithm = Integer.parseInt(reader.nextLine());
if(answerSortingAlgorithm == 1) {
Calculator.initDupArray(array, dupArray);
SortingAlgos.bubbleSort(array);
System.out.println();
} else if (answerSortingAlgorithm == 2) {
Calculator.initDupArray(array, dupArray);
SortingAlgos.quickSortHelper(array);
System.out.println();
} else if (answerSortingAlgorithm == 3) {
Calculator.initDupArray(array, dupArray);
SortingAlgos.insertionSort(array);
System.out.println();
} else if (answerSortingAlgorithm == 4) {
Calculator.initDupArray(array, dupArray);
SortingAlgos.selectionSort(array);
System.out.println();
} else if (answerSortingAlgorithm == 5) {
Calculator.initDupArray(array, dupArray);
SortingAlgos.mergeSortHelper(array);
System.out.println();
} else if (answerSortingAlgorithm == 6) {
Calculator.initDupArray(array, dupArray);
SortingAlgos.bubbleSort(array);
Calculator.initDupArray(array, dupArray);
SortingAlgos.quickSortHelper(array);
Calculator.initDupArray(array, dupArray);
SortingAlgos.insertionSort(array);
Calculator.initDupArray(array, dupArray);
SortingAlgos.selectionSort(array);
Calculator.initDupArray(array, dupArray);
SortingAlgos.mergeSortHelper(array);
System.out.println();
} else if (answerSortingAlgorithm == 7) {
restart();
} else if (answerSortingAlgorithm == 8) {
System.out.println("Thank You for using me, GoodBye!");
System.exit(0);
} else {
System.out.println("Provide a proper number from the menu!");
System.out.println();
}
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
System.out.println("Please pick a number from 1 and 8 as displayed on the menu!");
}
}
}
public void initArray() {
for (int i = 0; i < array.length - 1 ; i++) {
array[i] = 0;
}
}
public void restart() {
answerSizeOfArray = 0;
answerComplexity = 0;
answerSortingAlgorithm = 0;
start();
}
}