-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTools.java
More file actions
267 lines (239 loc) · 8.9 KB
/
Copy pathTools.java
File metadata and controls
267 lines (239 loc) · 8.9 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;
public class Tools {
public Tools() {
}
/** Take a file name string and return the entire text file as a single string
* CS1800 Spring 2022, Project 4
* @author Alex Lee
* @version 4/14/2022
*/
public String loadTextFile(String filename) {
ArrayList<String> textFileLines = new ArrayList<String>();
try (BufferedReader bfr = new BufferedReader(new FileReader(filename))) {
String line;
while ((line = bfr.readLine()) != null) {
textFileLines.add(line);
}
} catch (IOException ie) {
System.out.println("Either the file doesn't exist or the file is in the wrong format!");
}
String entireFile = "";
for ( int i = 0 ; i < textFileLines.size() ; i++ ) {
entireFile += textFileLines.get(i);
}
return entireFile;
}
/** Take maximum number of values and a size and return an integer array of random values of size and
* less than maximum value
* CS1800 Spring 2022, Project 4
* @author Alex Lee
* @version 4/14/2022
*/
public int[] randomGenerator(int max, int size) {
int[] random = new int[size];
ArrayList<Integer> pool = new ArrayList<>();
for (int i = 0 ; i < max ; i++) {
pool.add(i);
}
Collections.shuffle(pool);
for ( int i = 0 ; i < size ; i++) {
random[i] = pool.get(i);
}
return random;
}
/** Take in a minimum value and run the program until a valid integer response is provided
* CS1800 Spring 2022, Project 4
* @author Alex Lee
* @version 4/14/2022
*/
public int receiveValidInt(int minValue, Scanner scan) {
int response = 0;
boolean flag;
do {
flag = false;
String responseString = scan.nextLine();
try {
response = Integer.parseInt(responseString);
if (response < minValue) {
System.out.printf("Please enter a value greater than %d\n", minValue);
flag = true;
}
} catch (Exception e) {
System.out.println("Please enter a valid response!");
flag = true;
}
} while (flag);
return response;
}
/** Take in a minimum value and run the program until a valid integer response is provided
* CS1800 Spring 2022, Project 4
* @author Alex Lee
* @version 4/14/2022
*/
public int receiveValidInt(int minValue, int maxValue, Scanner scan) {
int response = 0;
boolean flag;
do {
flag = false;
String responseString = scan.nextLine();
try {
response = Integer.parseInt(responseString);
if (response < minValue) {
System.out.printf("Please enter a value greater than %d\n", minValue);
flag = true;
} else if (response > maxValue) {
System.out.printf("Please enter a value less than %d\n", maxValue);
flag = true;
}
} catch (Exception e) {
System.out.println("Please enter a valid response!");
flag = true;
}
} while (flag);
return response;
}
/** Take in a minimum value and run the program until a valid integer response is provided
* CS1800 Spring 2022, Project 4
* @author Alex Lee
* @version 4/14/2022
*/
public String receiveValidString(String[] validResponses , Scanner scan) {
String response = "";
boolean valid;
do {
valid = false;
response = scan.nextLine();
if (Arrays.asList(validResponses).contains(response)) {
valid = true;
} else {
System.out.println("Please enter a valid response!");
}
} while (!valid);
return response;
}
/** Take a string array list and return it as an array
* CS1800 Spring 2022, Project 4
* @author Alex Lee
* @version 4/14/2022
*/
public String[] listToArrayString(ArrayList<String> stringArrayList) {
String[] returnArray = new String[stringArrayList.size()];
for (int i = 0 ; i < returnArray.length ; i++) {
returnArray[i] = stringArrayList.get(i);
}
return returnArray;
}
/** Take an int array list and return it as an array
* CS1800 Spring 2022, Project 4
* @author Alex Lee
* @version 4/14/2022
*/
public int[] listToArrayInt(ArrayList<Integer> stringArrayList) {
int[] returnArray = new int[stringArrayList.size()];
for (int i = 0 ; i < returnArray.length ; i++) {
returnArray[i] = stringArrayList.get(i);
}
return returnArray;
}
/** retrieve number of questions in the pool
* CS1800 Spring 2022, Project 4
* @author Alex Lee
* @version 4/14/2022
*/
public int getQuestionPoolSize() {
int size = 0;
String questionFile = loadTextFile("Questions.txt");
String[] lines = questionFile.split("/");
return lines.length;
}
/** Retrieve quizzes from text file and save it as an array
* CS1800 Spring 2022, Project 4
* @author Alex Lee
* @version 4/10/2022
*/
public Quiz[] retrieveQuizzes() {
String quizFile = loadTextFile("Quiz.txt");
String[] lines = quizFile.split("/");
Quiz[] quizzes = new Quiz[lines.length];
for ( int i = 0 ; i < lines.length ; i++ ) {
String[] elements = lines[i].split(":");
int quizID = Integer.parseInt((elements[0]));
int numberQuestions = Integer.parseInt(elements[1]);
int[] questionPoints = new int[numberQuestions];
for ( int j = 0 ; j < numberQuestions ; j++ ) {
questionPoints[j] = Integer.parseInt((elements[2]).split(",")[j]);
}
String deadlineString = (elements[3]);
deadlineString = String.format("%s-%s-%s %s:%s", deadlineString.split("-")[0],
deadlineString.split("-")[1], deadlineString.split("-")[2],
deadlineString.split("-")[3], deadlineString.split("-")[4]);
DateTimeFormatter deadlineFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
LocalDateTime deadline = LocalDateTime.parse( deadlineString, deadlineFormat );
int duration = Integer.parseInt(elements[4]);
int totalPoints = Integer.parseInt(elements[5]);
int[] questionsIndex = new int[numberQuestions];
for ( int j = 0 ; j < numberQuestions ; j++ ) {
questionsIndex[j] = Integer.parseInt(elements[6].split(",")[j]);
}
Quiz quiz = new Quiz(quizID, numberQuestions, deadline,
duration, totalPoints, questionsIndex, questionPoints);
quizzes[i] = quiz;
}
return quizzes;
}
/** Retrieve quizzes from text file and save it as an array
* CS1800 Spring 2022, Project 4
* @author Alex Lee
* @version 4/10/2022
*/
public int[] retrieveQuizIDs() {
Quiz[] quizzes = retrieveQuizzes();
int[] quizIDs = new int[quizzes.length];
for (int i = 0 ; i < quizIDs.length; i++) {
quizIDs[i] = quizzes[i].getQuizID();
}
return quizIDs;
}
/** Get a list of all questions in the question document
* CS1800 Spring 2022, Project 4
* @author Quinn Bell0
* @version 4/30/2022
*/
public ArrayList<String> getQuestionList()
{
ArrayList<String> questionList = new ArrayList<String>();
try (BufferedReader bfr = new BufferedReader(new FileReader("Questions.txt")))
{
String line;
while ((line = bfr.readLine()) != null)
questionList.add(line);
} catch (IOException ie)
{
System.out.println("Either the file doesn't exist or the file is in the wrong format!");
}
return questionList;
}
/** Returns a list of all the quizzes from Quiz.txt */
public ArrayList<String> getQuizList()
{
ArrayList<String> textFileLines = new ArrayList<>();
try (BufferedReader bfr = new BufferedReader(new FileReader("Quiz.txt")))
{
String line;
while ((line = bfr.readLine()) != null)
textFileLines.add(line);
} catch (IOException ie)
{
System.out.println("Either the file doesn't exist or the file is in the wrong format!");
}
return textFileLines;
}
}