-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlashcards.java
More file actions
314 lines (290 loc) · 11.7 KB
/
Copy pathFlashcards.java
File metadata and controls
314 lines (290 loc) · 11.7 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
package flashcards;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;
public class Main {
private static final Logger logger = new Logger();
private static final Random random = new Random();
private static final Scanner scanner = new Scanner(System.in);
private static final Statistic stats = new Statistic();
private static final Map<String, String> flashCards = new HashMap<>();
private static final Map<String, String> reverseFlashCards = new HashMap<>();
private static boolean display = true;
private static String importPath = "";
private static String exportPath = "";
public static void main(String[] args) {
for (int i = 0; i < args.length;) {
if ("-import".toLowerCase().equals(args[i])) {
importPath = args[i + 1];
i += 2;
} else if ("-export".toLowerCase().equals(args[i])) {
exportPath = args[i + 1];
i += 2;
} else {
i += 1;
}
}
if (!importPath.equals("")){
importCards(importPath);
}
while (display) {
System.out.println("Input the action (add, remove, import, export, ask, exit, log, hardest card, reset stats):");
logger.write("Input the action (add, remove, import, export, ask, exit, log, hardest card, reset stats):");
String option = scanner.nextLine().toLowerCase();
logger.write("> " + option);
switch (option) {
case "add":
addCards();
break;
case "remove":
removeCards();
break;
case "import":
System.out.println("File name:");
logger.write("File name:");
String pathToImport = scanner.nextLine();
logger.write("> " + pathToImport);
importCards(pathToImport);
break;
case "export":
System.out.println("File name:");
logger.write("File name:");
String pathToExport = scanner.nextLine();
logger.write("> " + pathToExport);
exportCards(pathToExport);
break;
case "ask":
askQuestions();
break;
case "exit":
exit();
break;
case "log":
exportLogs();
break;
case "hardest card":
getHardestCard();
break;
case "reset stats":
resetStats();
break;
default:
logger.write("Not found.");
break;
}
logger.writeReturn("UNIX");
}
}
public static void addCards() {
System.out.println("The card:");
logger.write("The card:");
String card = scanner.nextLine();
logger.write("> " + card);
if (flashCards.containsKey(card)) {
System.out.println("The card \"" + card + "\" already exists.");
logger.write("The card \"" + card + "\" already exists.");
return;
}
System.out.println("The definition of the card:");
logger.write("The definition of the card:");
String definition = scanner.nextLine();
logger.write("> " + definition);
if (reverseFlashCards.containsKey(definition)) {
System.out.println("The definition \"" + definition + "\" already exists.");
logger.write("The definition \"" + definition + "\" already exists.");
return;
}
flashCards.put(card, definition);
reverseFlashCards.put(definition, card);
stats.loadCards(card);
System.out.println("The pair (\"" + card + "\":\"" + definition + "\") has been added.");
logger.write("The pair (\"" + card + "\":\"" + definition + "\") has been added.");
}
public static void removeCards() {
System.out.println("The card:");
logger.write("The card:");
String card = scanner.nextLine();
logger.write("> " + card);
if (!flashCards.containsKey(card)) {
System.out.println("Can't remove \"" + card + "\": there is no such card.");
logger.write("Can't remove \"" + card + "\": there is no such card.");
return;
}
String definition = flashCards.get(card);
flashCards.remove(card);
reverseFlashCards.remove(definition);
stats.remove(card);
System.out.println("Card \"" + card + "\" has been removed.");
logger.write("Card \"" + card + "\" has been removed.");
}
public static void importCards(String pathToFile) {
File file = new File(pathToFile);
try (Scanner reader = new Scanner(file)) {
int cardNum = 0;
while (reader.hasNext()) {
String[] line = reader.nextLine().split("\\s+");
if (flashCards.containsKey(line[0])) {
reverseFlashCards.remove(flashCards.get(line[0]));
}
flashCards.put(line[0], line[1]);
reverseFlashCards.put(line[1], line[0]);
stats.loadCards(line[0], Integer.parseInt(line[2]));
cardNum++;
}
System.out.println(cardNum + " cards have been loaded.");
logger.write(cardNum + " cards have been loaded.");
} catch (FileNotFoundException e) {
System.out.println("File not found.");
logger.write("File not found.");
}
}
public static void exportCards(String pathToFile) {
try (PrintWriter writer = new PrintWriter(pathToFile)) {
for (String key : flashCards.keySet()) {
writer.println(key + " " + flashCards.get(key) + " " + stats.getWrongCounts(key));
}
System.out.println(flashCards.size() + " cards have been saved.");
logger.write(flashCards.size() + " cards have been saved.");
} catch (IOException e) {
System.out.println("0 cards have been saved.");
logger.write("0 cards have been saved.");
}
}
public static void askQuestions() {
System.out.println("How many times to ask?");
logger.write("How many times to ask?");
int count = Integer.parseInt(scanner.nextLine());
logger.write("> " + count);
String[] keys = flashCards.keySet().toArray(new String[0]);
for (int i = 0; i < count; i++) {
String randomCard = keys[random.nextInt(keys.length)];
System.out.println("Print the definition of \"" + randomCard + "\":");
logger.write("Print the definition of \"" + randomCard + "\":");
String answer = scanner.nextLine();
logger.write("> " + answer);
if (answer.equals(flashCards.get(randomCard))) {
System.out.println("Correct answer.");
logger.write("Correct answer.");
} else {
stats.wrongAnswer(randomCard);
if (reverseFlashCards.containsKey(answer)) {
System.out.println("Wrong answer. The correct one is \"" + flashCards.get(randomCard) + "\", you've just written the definition of \"" + reverseFlashCards.get(answer) + "\".");
logger.write("Wrong answer. The correct one is \"" + flashCards.get(randomCard) + "\", you've just written the definition of \"" + reverseFlashCards.get(answer) + "\".");
} else {
System.out.println("Wrong answer. The correct one is \"" + flashCards.get(randomCard) + "\".");
logger.write("Wrong answer. The correct one is \"" + flashCards.get(randomCard) + "\".");
}
}
}
}
public static void exit() {
display = false;
System.out.println("Bye bye!");
logger.write("Bye bye!");
if (!exportPath.equals("")) {
exportCards(exportPath);
}
}
public static void exportLogs() {
System.out.println("File name:");
logger.write("File name:");
String pathToFile = scanner.nextLine();
logger.write("> " + pathToFile);
logger.exportToFile(pathToFile);
}
public static void getHardestCard() {
ArrayList<String> hardestCards = stats.getHardestCard();
if (hardestCards.size() == 1) {
System.out.println("There are no cards with errors.");
logger.write("There are no cards with errors.");
} else {
int wrongCounts = Integer.parseInt(hardestCards.get(hardestCards.size()-1));
hardestCards.remove(hardestCards.size()-1);
StringBuilder sb = new StringBuilder("The hardest card ");
if (hardestCards.size() == 1) {
sb.append("is ");
} else {
sb.append("are ");
}
hardestCards.forEach(card -> sb.append("\"").append(card).append("\", "));
sb.replace(sb.length()-2, sb.length(), ". ");
sb.append("You have ").append(wrongCounts).append(" errors answering them");
System.out.println(sb.toString());
logger.write(sb.toString());
}
}
public static void resetStats() {
stats.resetStats();
System.out.println("Card Statistics has been reset.");
logger.write("Card Statistics has been reset.");
}
}
class Logger {
private final ArrayList<String> log = new ArrayList<>();
public void write(String str) {
log.add(str);
}
public void writeReturn(String systemType) {
if (systemType.equals("UNIX")) {
log.add("\n");
} else {
log.add("\r\n");
}
}
public void exportToFile(String pathToFile) {
try (PrintWriter writer = new PrintWriter(pathToFile)) {
for (String line : log) {
writer.println(line);
}
System.out.println("The log has been saved.");
log.add("The log has been saved.");
} catch (FileNotFoundException e) {
System.out.println("File not found.");
log.add("File not found.");
}
}
}
class Statistic {
private static final Map<String, Integer> stats = new HashMap<>();
public void loadCards(String card) {
if (stats.containsKey(card)) {
return;
}
stats.put(card, 0);
}
public void loadCards(String card, Integer wrongCounts) {
stats.put(card, wrongCounts);
}
public void wrongAnswer(String card) {
Integer cardStat = stats.get(card);
cardStat += 1;
stats.put(card, cardStat);
}
public ArrayList<String> getHardestCard() {
ArrayList<String> hardestCards = new ArrayList<>();
int maxWrongCounts = 1;
for (String card : stats.keySet()) {
int cardWrongCounts = stats.get(card);
if (cardWrongCounts > maxWrongCounts) {
maxWrongCounts = cardWrongCounts;
hardestCards.clear();
hardestCards.add(card);
} else if (cardWrongCounts == maxWrongCounts) {
hardestCards.add(card);
}
}
hardestCards.add(String.valueOf(maxWrongCounts));
return hardestCards;
}
public int getWrongCounts(String card) {
return stats.get(card);
}
public void remove(String card) {
stats.remove(card);
}
public void resetStats() {
stats.replaceAll((c, v) -> 0);
}
}