forked from leoabubucker/JavaCourse_THS-CS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelperFunctions.java
More file actions
484 lines (457 loc) · 18.1 KB
/
HelperFunctions.java
File metadata and controls
484 lines (457 loc) · 18.1 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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
import java.util.concurrent.TimeUnit;
import java.util.Scanner;
import java.io.FileWriter;
import java.io.IOException;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
public class HelperFunctions{
//colors for text
public static final String ANSI_RESET = "\u001B[0m";
public static final String ANSI_BLACK = "\u001B[30m";
public static final String ANSI_RED = "\u001B[31m";
public static final String ANSI_GREEN = "\u001B[32m";
public static final String ANSI_YELLOW = "\u001B[33m";
public static final String ANSI_BLUE = "\u001B[34m";
public static final String ANSI_PURPLE = "\u001B[35m";
public static final String ANSI_CYAN = "\u001B[36m";
public static final String ANSI_WHITE = "\u001B[37m";
private static ArrayList<String> courses = new ArrayList<String>(){
{
add("intro");
add("beginner");
add("intermediate");
add("advanced");
}
};
private static ArrayList<String> beginnerActivities = new ArrayList<String>(){
{
add("beginnerIntro");
add("learnPrinting");
add("learnVariables");
add("learnVariableManipulation");
}
};
private static ArrayList<String> intermediateActivities = new ArrayList<String>(){
{
add("intermediateIntro");
add("learnOperators");
add("learnConditionals");
add("learnMethods");
add("learnMethodParameters");
add("learnReturnStatements");
add("learnStringMethods");
add("learnArrays");
add("learnSplitAndSubstring");
add("learnWhileLoops");
add("learnForLoops");
add("learnClassesAndObjects");
}
};
private static ArrayList<String> advancedActivities = new ArrayList<String>(){
{
add("advancedIntro");
}
};
private static final Scanner CONSOLE = new Scanner(System.in);
public static void printAnimated(String initStr){
System.out.println("\n");
for(int i = 0; i < initStr.length(); i++){
System.out.print(initStr.charAt(i));
try{
//change speed of animation
TimeUnit.MILLISECONDS.sleep(25);
}
catch(InterruptedException e){
e.printStackTrace();
}
}
}
public static void clearConsole(){
System.out.print("\033[H\033[2J");
System.out.flush();
}
public static void sleep(int time, String unit){
try {
if(unit.equals("sec")){
TimeUnit.SECONDS.sleep(time);
}
else if(unit.equals("msec")){
TimeUnit.MILLISECONDS.sleep(time);
}
else if(unit.equals("nano")){
TimeUnit.NANOSECONDS.sleep(time);
}
else if(unit.equals("micro")){
TimeUnit.MICROSECONDS.sleep(time);
}
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void waitForCorrectInput(String correctInput){
String userInput = CONSOLE.nextLine();
while (!userInput.equalsIgnoreCase(correctInput)) {
HelperFunctions.printAnimated("Not Quite! Try Again:\n");
userInput = CONSOLE.nextLine();
}
}
public static void waitForCorrectInput(String correctInput, String incorrectMsg){
String userInput = CONSOLE.nextLine();
while (!userInput.equalsIgnoreCase(correctInput)) {
HelperFunctions.printAnimated(incorrectMsg + "\n");
userInput = CONSOLE.nextLine();
}
}
public static void waitForCorrectInput(String correctInput, boolean caseSpecific){
String userInput = CONSOLE.nextLine();
if(caseSpecific){
while (!userInput.equals(correctInput)) {
HelperFunctions.printAnimated("Not Quite! Try Again:\n");
userInput = CONSOLE.nextLine();
}
}
else{
while (!userInput.equalsIgnoreCase(correctInput)) {
HelperFunctions.printAnimated("Not Quite! Try Again:\n");
userInput = CONSOLE.nextLine();
}
}
}
public static void waitForCorrectInput(String correctInput, String incorrectMsg, boolean caseSpecific){
String userInput = CONSOLE.nextLine();
if(caseSpecific){
while (!userInput.equals(correctInput)) {
HelperFunctions.printAnimated(incorrectMsg + "\n");
userInput = CONSOLE.nextLine();
}
}
else{
while (!userInput.equalsIgnoreCase(correctInput)) {
HelperFunctions.printAnimated(incorrectMsg + "\n");
userInput = CONSOLE.nextLine();
}
}
}
public static double truncateDecimal(double num, int places){
double multiplier = Math.pow(10, places);
int tempNum = (int) (num*multiplier);
return (double) tempNum/multiplier;
}
public static String getProgress(String txtFile){
Scanner progressScanner = null;
try{
File progressFile = new File(txtFile);
progressScanner = new Scanner(progressFile);
}
catch(FileNotFoundException e){
e.printStackTrace();
}
String progress = progressScanner.nextLine();
return progress;
}
public static void updateProgress(String txtFile, String newProgress){
int newProgressIndex = -1;
int currentProgressIndex = -1;
String newActivityType = "";
String currentActivityType = "";
String currentProgress = getProgress(txtFile);
if(txtFile.equals("courseProgress.txt")){
for(int i = 0; i < courses.size(); i++){
if(courses.get(i).equals(newProgress)){
newProgressIndex = i;
}
if(courses.get(i).equals(currentProgress)){
currentProgressIndex = i;
}
}
}
else if(txtFile.equals("activityProgress.txt")){
for(int i = 0; i < beginnerActivities.size(); i++){
if(beginnerActivities.get(i).equals(newProgress)){
newProgressIndex = i;
newActivityType = "beginner";
}
if(beginnerActivities.get(i).equals(currentProgress)){
currentProgressIndex = i;
currentActivityType = "beginner";
}
}
for(int i = 0; i < intermediateActivities.size(); i++){
if(intermediateActivities.get(i).equals(newProgress)){
newProgressIndex = i;
newActivityType = "intermediate";
}
if(intermediateActivities.get(i).equals(currentProgress)){
currentProgressIndex = i;
currentActivityType = "intermediate";
}
}
for(int i = 0; i < advancedActivities.size(); i++){
if(advancedActivities.get(i).equals(newProgress)){
newProgressIndex = i;
newActivityType = "advanced";
}
if(advancedActivities.get(i).equals(currentProgress)){
currentProgressIndex = i;
currentActivityType = "advanced";
}
}
}
if( (newProgressIndex > currentProgressIndex) && ( (newProgressIndex != -1) && (currentProgressIndex != -1)) && (newActivityType.equals(currentActivityType)) ){
try{
FileWriter myWriter = new FileWriter(txtFile);
myWriter.write(newProgress);
myWriter.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
public static void loadProgress(){
HelperFunctions.clearConsole();
ArrayList<String> unlockedCourses = new ArrayList<String>();
ArrayList<String> unlockedActivities = new ArrayList<String>();
String courseProgress = getProgress("courseProgress.txt");
String activityProgress = getProgress("activityProgress.txt");
if(courseProgress.equals("intro")){
PlacementQuiz.placeUser();
}
else{
HelperFunctions.printAnimated("Welcome back! Please see the unlocked courses below to continue!");
for(int i = 0; i < courses.size(); i++){
if(courseProgress.equals(courses.get(i))){
for(int j = i; j >= 0; j--){
HelperFunctions.printAnimated(" - " + (String) courses.get(j));
unlockedCourses.add(((String) courses.get(j)).toLowerCase());
}
}
}
}
HelperFunctions.printAnimated("Please type the name of the course you would like to do.\n");
String userInput = CONSOLE.nextLine();
if(unlockedCourses.contains(userInput.toLowerCase())){
if(userInput.equalsIgnoreCase(courses.get(1))){
HelperFunctions.printAnimated("You are in the beginner course! Please see the unlocked activities below to continue!");
//Returns activities unlocked in beginner course
for(int i = 0; i < beginnerActivities.size(); i++){
if(activityProgress.equals(beginnerActivities.get(i))){
for(int j = i; j >= 0; j--){
HelperFunctions.printAnimated(" - " + (String) beginnerActivities.get(j));
unlockedActivities.add(((String) beginnerActivities.get(j)).toLowerCase());
}
}
}
//Returns all beginner activities if courseProgress = intermediate or advanced
if(courseProgress.equals("intermediate") || courseProgress.equals("advanced")){
for(Object activity: beginnerActivities){
HelperFunctions.printAnimated(" - " + (String) activity);
unlockedActivities.add(((String)activity).toLowerCase());
}
}
HelperFunctions.printAnimated("Please type the name of the activity you would like to do.\n");
userInput = CONSOLE.nextLine();
if(unlockedActivities.contains(userInput.toLowerCase())){
if(userInput.equalsIgnoreCase("beginnerintro")){
HelperFunctions.printAnimated("Launching the beginner course now!");
sleep(1, "sec");
BeginnerCourse.launchBeginnerCourse();
}
else if(userInput.equalsIgnoreCase("learnprinting")){
HelperFunctions.printAnimated("Launching the printing activity now!");
sleep(1, "sec");
BeginnerCourse.learnPrinting();
}
else if(userInput.equalsIgnoreCase("learnvariables")){
HelperFunctions.printAnimated("Launching the variables activity now!");
sleep(1, "sec");
BeginnerCourse.learnVariables();
}
else if(userInput.equalsIgnoreCase("learnvariablemanipulation")){
HelperFunctions.printAnimated("Launching the variable manipulation activity now!");
sleep(1, "sec");
BeginnerCourse.learnVariableManipulation();
}
}
else if(beginnerActivities.contains(userInput)){
HelperFunctions.printAnimated("You have not unlocked the " + userInput + " activity yet! Please select an unlocked activity!\nReloading...");
sleep(1, "sec");
loadProgress();
}
else{
HelperFunctions.printAnimated("Invalid Input! Please make sure your input is the name of an activity.\nReloading...");
sleep(1, "sec");
loadProgress();
}
}
else if(userInput.equalsIgnoreCase(courses.get(2))){
HelperFunctions.printAnimated("You are in the intermediate course! Please see the unlocked activities below to continue!");
//Returns unlocked intermediate activities
for(int i = 0; i < intermediateActivities.size(); i++){
if(activityProgress.equals(intermediateActivities.get(i))){
for(int j = i; j >= 0; j--){
HelperFunctions.printAnimated(" - " + (String) intermediateActivities.get(j));
unlockedActivities.add(((String) intermediateActivities.get(j)).toLowerCase());
}
}
}
//Returns all intermediate activities if courseProgress = advanced
if(courseProgress.equals("advanced")){
for(Object activity: intermediateActivities){
HelperFunctions.printAnimated(" - " + (String) activity);
unlockedActivities.add(((String)activity).toLowerCase());
}
}
HelperFunctions.printAnimated("Please type the name of the activity you would like to do.\n");
userInput = CONSOLE.nextLine();
if(unlockedActivities.contains(userInput.toLowerCase())){
if(userInput.equalsIgnoreCase("intermediateintro")){
HelperFunctions.printAnimated("Launching the intermediate course now!");
sleep(1, "sec");
IntermediateCourse.launchIntermediateCourse();
}
else if(userInput.equalsIgnoreCase("learnoperators")){
HelperFunctions.printAnimated("Launching the operators activity now!");
sleep(1, "sec");
IntermediateCourse.learnOperators();
}
else if(userInput.equalsIgnoreCase("learnconditionals")){
HelperFunctions.printAnimated("Launching the conditionals activity now!");
sleep(1, "sec");
IntermediateCourse.learnConditionals();
}else if(userInput.equalsIgnoreCase("learnmethods")){
HelperFunctions.printAnimated("Launching the methods activity now!");
sleep(1, "sec");
IntermediateCourse.learnMethods();
}else if (userInput.equalsIgnoreCase("learnmethodparameters")){
HelperFunctions.printAnimated("Launching the method parameters activity now!");
sleep(1, "sec");
IntermediateCourse.learnMethodParameters();
}else if (userInput.equalsIgnoreCase("learnreturnstatements")){
HelperFunctions.printAnimated("Launching the return statements activity now!");
sleep(1, "sec");
IntermediateCourse.learnReturnStatements();
}else if (userInput.equalsIgnoreCase("learnStringMethods")){
HelperFunctions.printAnimated("Launching the string methods activity now!");
sleep(1, "sec");
IntermediateCourse.learnStringMethods();
}else if (userInput.equalsIgnoreCase("learnArrays")){
HelperFunctions.printAnimated("Launching the arrays activity now!");
sleep(1, "sec");
IntermediateCourse.learnArrays();
}else if (userInput.equalsIgnoreCase("learnSplitAndSubstring")){
HelperFunctions.printAnimated("Launching the split and substring activity now!");
sleep(1, "sec");
IntermediateCourse.learnSplitandSubstring();
}else if (userInput.equalsIgnoreCase("learnWhileLoops")){
HelperFunctions.printAnimated("Launching the loops activity now!");
sleep(1, "sec");
IntermediateCourse.learnWhileLoops();
}else if (userInput.equalsIgnoreCase("learnForLoops")){
HelperFunctions.printAnimated("Launching the loops activity now!");
sleep(1, "sec");
IntermediateCourse.learnForLoops();
}else if (userInput.equalsIgnoreCase("learnClassesAndObjects")){
HelperFunctions.printAnimated("Launching the classes and objects activity now!");
sleep(1, "sec");
IntermediateCourse.learnClassesAndObjects();
}
}
else if(intermediateActivities.contains(userInput)){
HelperFunctions.printAnimated("You have not unlocked the " + userInput + " activity yet! Please select an unlocked activity!\nReloading...");
sleep(1, "sec");
loadProgress();
}
else{
HelperFunctions.printAnimated("Invalid Input! Please make sure your input is the name of an activity.\nReloading...");
sleep(1, "sec");
loadProgress();
}
}
}
else if(userInput.equalsIgnoreCase(courses.get(3))){
HelperFunctions.printAnimated("You are in the advanced course! Please see the unlocked activities below to continue!");
//Returns unlocked advanced activities
for(int i = 0; i < advancedActivities.size(); i++){
if(activityProgress.equals(advancedActivities.get(i))){
for(int j = i; j >= 0; j--){
HelperFunctions.printAnimated(" - " + (String) advancedActivities.get(j));
unlockedActivities.add(((String) advancedActivities.get(j)).toLowerCase());
}
}
}
HelperFunctions.printAnimated("Please type the name of the activity you would like to do.\n");
userInput = CONSOLE.nextLine();
if(unlockedActivities.contains(userInput.toLowerCase())){
if(userInput.equalsIgnoreCase("advancedintro")){
HelperFunctions.printAnimated("Launching the advanced course now!");
sleep(1, "sec");
IntermediateCourse.launchIntermediateCourse();
}
}
else if(advancedActivities.contains(userInput)){
HelperFunctions.printAnimated("You have not unlocked the " + userInput + " activity yet! Please select an unlocked activity!\nReloading...");
sleep(1, "sec");
loadProgress();
}
else{
HelperFunctions.printAnimated("Invalid Input! Please make sure your input is the name of an activity.\nReloading...");
sleep(1, "sec");
loadProgress();
}
}
else{
HelperFunctions.printAnimated("Invalid Input! Please make sure your input is the name of an activity.\nReloading...");
sleep(1, "sec");
loadProgress();
}
}
public static boolean validateUserInput(String userInput, String correctInput, boolean caseSpecific, boolean removeSpaces){
if(caseSpecific){
if(removeSpaces){
String[] userInputSplit= userInput.split("");
String userInputNoSpaces = "";
for(String c : userInputSplit){
if(!c.equals(" ")){
userInputNoSpaces += c;
}
}
String[] correctInputSplit = correctInput.split(" ");
String correctInputNoSpaces = "";
for(String c : correctInputSplit){
if(!c.equals(" ")){
correctInputNoSpaces += c;
}
}
return userInputNoSpaces.equals(correctInputNoSpaces);
}
else{
return userInput.equals(correctInput);
}
}
else{
if(removeSpaces){
String[] userInputSplit = userInput.split("");
String userInputNoSpaces = "";
for(String c : userInputSplit){
if(!c.equals(" ")){
userInputNoSpaces += c;
}
}
String[] correctInputSplit = correctInput.split("");
String correctInputNoSpaces = "";
for(String c : correctInputSplit){
if(!c.equals(" ")){
correctInputNoSpaces += c;
}
}
return userInputNoSpaces.equalsIgnoreCase(correctInputNoSpaces);
}
else{
return userInput.equalsIgnoreCase(correctInput);
}
}
}
}