-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCLILang.java
More file actions
198 lines (155 loc) · 5.46 KB
/
CLILang.java
File metadata and controls
198 lines (155 loc) · 5.46 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
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.LinkedList;
public class CLILang {
public static final void main(String[] args) {
try {
ArrayList<String[]> words = new ArrayList<String[]>();
Scanner in = new Scanner(new File(args[0]));
System.out.println("===CLILANG===");
if (!in.hasNextLine()) {
System.out.println("Please rerun with proper dictionary format described in README");
}
String languageName = in.nextLine().split(":")[1];
while (in.hasNextLine()) {
String[] wordPair = parseLine(in.nextLine());
words.add(wordPair);
}
Language lang = new Language(languageName, words);
String choice = "";
while (!choice.equals("exit")) {
System.out.println("What would you like to do?");
Scanner input = new Scanner(System.in);
choice = input.nextLine();
lang = parseChoice(choice, lang, input);
// To Update for Difficulty, Reload the language
if (choice.equals("test")) {
lang.sortWords();
}
}
} catch (FileNotFoundException | ArrayIndexOutOfBoundsException e) {
System.out.println("===CLILANG===");
System.out.println("Welcome to CLILang! You didn't provide a file,");
System.out.println("here are some preliminary questions to get you set up.");
System.out.println("");
System.out.println("What Language will you be working with? ");
System.out.print("=> ");
Scanner in = new Scanner(System.in);
String langName = in.nextLine();
System.out.println("");
System.out.println("Awesome! I love " + langName + ". Add some words to your");
System.out.println("language in the format 'word:definition'. Type 'STOP'");
System.out.println("when you're ready to stop typing.");
String wordPair = "";
ArrayList<String[]> words = new ArrayList<String[]>();
while (true) {
wordPair = in.nextLine();
if (wordPair.equals("STOP"))
break;
words.add(wordPair.split(":"));
}
Language lang = new Language(langName, words);
try {
PrintWriter out = new PrintWriter(langName + ".txt");
out.println("language:" + lang.getLang());
ArrayList<String[]> placeHolder = lang.getWords();
for (String[] word : placeHolder) {
out.println(word[0] + ":" + word[1] + ":0");
out.flush();
}
} catch (Exception ex) {
System.out.println(ex);
}
System.out.println("");
System.out.println("Looks like we're ready to go! We just made a file");
System.out.println("for you called '" + langName + ".txt' that has your language and");
System.out.println("the words you're learning, along with a number describing their ");
System.out.println("difficulty. Right now, they're all labeled with a zero, meaning");
System.out.println("that they haven't been practiced. Type 'test' to try it out! Type 'help'" );
System.out.println("to get additional info.");
System.out.println("");
String choice = "";
while (!choice.equals("exit")) {
System.out.println("What would you like to do?");
choice = in.nextLine();
parseChoice(choice, lang, in);
}
}
}
public static String[] parseLine(String line) {
return line.split(":");
}
public static Language parseChoice(String choice, Language lang, Scanner scan) {
if (choice.equals("exit")) {
return lang;
} else if (choice.equals("test")) {
lang = test(lang, scan);
System.out.println("");
} else if (choice.equals("help")) {
help();
System.out.println("");
} else {
System.out.println("Sorry, I didn't catch that. Try typing 'help'");
System.out.println("if you're stuck.");
}
return lang;
}
public static Language test(Language lang, Scanner scan) {
for (String[] pair : lang.getWords()) {
String answer = "";
int tries = 0;
while (!answer.equals(pair[1])) {
if (tries == 5) {
System.out.println("You just didn't get it");
System.out.println("Answer: " + pair[1]);
break;
}
System.out.print(pair[0] + ": ");
answer = scan.nextLine();
tries++;
}
// Changes the Difficulty of Word
lang.setDifficulty(pair[0], tries);
System.out.println(" ");
}
System.out.println("Congratulations! You got them all!");
try {
PrintWriter changes = new PrintWriter(lang.getLang() + ".txt");
changes.println("language:" + lang.getLang());
ArrayList<String[]> placeHolder = lang.getWords();
LinkedList<String> sortedWords = new LinkedList<String>();
for (String[] word : placeHolder) {
String wordValue = word[0] + ":" + word[1] + ":" + lang.getDifficulty(word[0]);
// If Statement to Determine how to handle wordValue
if (sortedWords.size() == 0) {
sortedWords.add(wordValue);
} else if (lang.getDifficulty(word[0]) < lang.getDifficulty(sortedWords.peek().split(":")[0])) {
sortedWords.add(wordValue);
} else {
sortedWords.push(wordValue);
}
}
for (String word : sortedWords) {
changes.println(word);
changes.flush();
}
} catch (Exception ex) {
System.out.println(ex);
}
return lang;
}
public static void help() {
System.out.println("Here are the Following Commands you can type: ");
System.out.println(" ");
System.out.println("'help'");
System.out.println(" Prints out a list of commands");
System.out.println("'test'");
System.out.println(" Tests you on the vocab provided in your");
System.out.println(" .txt file.");
System.out.println("'exit'");
System.out.println(" Stops the program.");
}
}