-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileWordManager.java
More file actions
50 lines (42 loc) · 1.78 KB
/
FileWordManager.java
File metadata and controls
50 lines (42 loc) · 1.78 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
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
// A concrete implementation of the WordManager Abstract class.
// It loads words from a file and categorises them by difficulty level.
public class FileWordManager extends WordManager {
public FileWordManager() {
super(); // Calls the WordManager constructor.
}
// Loads words from the words.txt file.
// Words are stored in the appropriate difficulty list in the word bank
@Override
protected void loadWords() {
try (Scanner scanner = new Scanner(new File("words.txt"))) {
while (scanner.hasNextLine()) {
String[] parts = scanner.nextLine().split("\\s+", 2); // Splits the line into two parts - words and difficulty.
if (parts.length < 2) {
continue; // Skips invalid formats
}
// Convert the word to uppercase and get the difficulty.
String word = parts[0].trim().toUpperCase();
String difficultyStr = parts[1].trim();
Difficulty difficulty = resolveDifficulty(difficultyStr);
// Add the word to the correct word list if the difficulty is valid.
if (difficulty != null) {
wordBank.get(difficulty).add(word);
}
}
} catch (FileNotFoundException e) {
System.err.println("Error: words.txt file not found!");
}
}
// Resolves a string to its associated difficulty instance.
private Difficulty resolveDifficulty(String difficultyStr) {
return switch (difficultyStr) {
case "Easy" -> Easy.INSTANCE;
case "Medium" -> Medium.INSTANCE;
case "Hard" -> Hard.INSTANCE;
default -> null;
};
}
}