-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDictionaryApp.java
More file actions
192 lines (159 loc) · 6.58 KB
/
DictionaryApp.java
File metadata and controls
192 lines (159 loc) · 6.58 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
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Scanner;
/*
* DictionaryApp
* --------------
* Console-based dictionary application.
*
* v1.1 features:
* - Loads dictionary data from a markdown-style file
* - Stores entries in memory for fast lookup
* - Provides menu-driven interaction:
* - Search word
* - View full dictionary
* - Exit
*
* Design note:
* File parsing happens ONCE at startup.
* All user interaction works on in-memory data only.
*/
public class DictionaryApp {
public static void main(String[] args) {
// ===== Phase 1: Load Dictionary Data =====
HashMap<String, DictionaryEntry> dictionary;
try {
dictionary = loadDictionaryData("dictionary@java.md");
} catch (Exception e) {
System.out.println("Failed to load dictionary data.");
e.printStackTrace();
return;
}
// ===== Phase 2: User Interaction =====
try (Scanner sc = new Scanner(System.in)) {
greetUser(sc);
runMainMenu(dictionary, sc);
System.out.println("Exiting dictionary application");
}
}
// ===== Method: Load Dictionary =====
// Reads the dictionary file once and builds the in-memory HashMap
private static HashMap<String, DictionaryEntry> loadDictionaryData(String filename) throws Exception {
// Stores all dictionary entries in memory.
// Key is lowercased word for case-insensitive lookup.
HashMap<String, DictionaryEntry> dictionary = new HashMap<>();
// Temporary variables used while parsing the file.
String currentWord = null;
String currentDefinition = null;
String currentExample = null;
try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
String line;
while ((line = reader.readLine()) != null) {
line = line.trim();
// Marker detection:
int markerIndex1 = line.indexOf("### ");
int markerIndex2 = line.indexOf("Definition: ");
int markerIndex3 = line.indexOf("Example: ");
// WORD marker encountered → previous entry ends
if (markerIndex1 != -1) {
// Commit the previous entry before starting a new one
if (currentWord != null) {
DictionaryEntry entry =
new DictionaryEntry(currentWord, currentDefinition, currentExample);
dictionary.put(currentWord.toLowerCase(), entry);
}
// Start a new entry
currentWord = line.substring(markerIndex1 + 4).trim();
currentDefinition = null;
currentExample = null;
}
// Definition line belongs to the current word
else if (markerIndex2 != -1) {
currentDefinition = line.substring(markerIndex2 + 11).trim();
}
// Example line belongs to the current word
else if (markerIndex3 != -1) {
currentExample = line.substring(markerIndex3 + 8).trim();
}
}
// End-of-file commit
if (currentWord != null) {
DictionaryEntry entry =
new DictionaryEntry(currentWord, currentDefinition, currentExample);
dictionary.put(currentWord.toLowerCase(), entry);
}
}
return dictionary;
}
// ===== Method: Greet User =====
private static void greetUser(Scanner sc) {
System.out.print("Enter your name: ");
String name = sc.nextLine();
// ANSI formatting for bold greeting (terminal-dependent)
System.out.println("\u001B[1mHi, " + name + "\u001B[0m");
}
// ===== Method: Main Menu Controller =====
private static void runMainMenu(HashMap<String, DictionaryEntry> dictionary, Scanner sc) {
// Controls overall navigation of the application.
while (true) {
printMenu();
String menuOption = sc.nextLine().toLowerCase();
if (menuOption.equals("s")) {
handleSearch(dictionary, sc);
} else if (menuOption.equals("f")) {
printFullDictionary(dictionary);
} else if (menuOption.equals("exit")) {
System.out.println("Bye. See you again");
break;
} else {
System.out.println("Invalid input. Try again.");
}
}
}
// ===== Method: Print Menu =====
private static void printMenu() {
System.out.println("\u001B[1;4mMain Menu\u001B[0m");
System.out.println("For Search option, enter 's'");
System.out.println("For full dictionary, enter 'f'");
System.out.println("To exit, enter 'exit'");
System.out.print("=> ");
}
// ===== Method: Search Mode =====
private static void handleSearch(HashMap<String, DictionaryEntry> dictionary, Scanner sc) {
// Search loop allows repeated lookups
while (true) {
System.out.print("Enter a word (or type 'menu'/'exit'): ");
String userWord = sc.nextLine().toLowerCase();
if (userWord.equals("menu")) {
return; // back to main menu
}
if (userWord.equals("exit")) {
System.out.println("Bye. See you again.\nExiting dictionary application");
System.exit(0);
}
DictionaryEntry entry = dictionary.get(userWord);
if (entry != null) {
System.out.println("WORD: " + entry.getWord());
System.out.println("DEF: " + entry.getDefinition());
System.out.println("EX: " + entry.getExample());
} else {
System.out.println("Word not found. Try again.");
}
}
}
// ===== Method: Full Dictionary View =====
private static void printFullDictionary(HashMap<String, DictionaryEntry> dictionary) {
System.out.println("FULL DICTIONARY");
int count = 1;
for (DictionaryEntry entry : dictionary.values()) {
System.out.println(count + ". " + entry.getWord());
System.out.println(entry.getDefinition());
System.out.println("Ex: " + entry.getExample());
System.out.println();
System.out.println("------------------");
System.out.println();
count++;
}
}
}