-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCHSearchFrontendFD.java
More file actions
192 lines (178 loc) · 6.45 KB
/
CHSearchFrontendFD.java
File metadata and controls
192 lines (178 loc) · 6.45 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.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
* Provide a text-based user interface to the searching capaibilities of the
* CHSearchApplication.
*
* @author FrontendDeveloper
*/
public class CHSearchFrontendFD implements CHSearchFrontendInterface {
private Scanner userInput;
private CHSearchBackendInterface backend;
/**
* Initialize frontend to make use of a provided Scanner and CHSearchBackend.
*
* @param userInput can be used to read input from use, or to read from files
* for testing
* @param backend placeholder by me, working implementation by Backend
* Developer
*/
public CHSearchFrontendFD(Scanner userInput, CHSearchBackendInterface backend) {
this.userInput = userInput;
this.backend = backend;
}
/**
* Helper method to display a 79 column wide row of dashes: a horizontal rule.
*/
private void hr() {
System.out.println("-------------------------------------------------------------------------------");
}
/**
* This loop repeated prompts the user for commands and displays appropriate
* feedback for each. This continues until the user enters 'q' to quit.
*/
public void runCommandLoop() {
hr(); // display welcome message
System.out.println("Welcome to the Cheap and Healthy Search App.");
hr();
List<String> words;
char command = '\0';
while (command != 'Q') { // main loop continues until user chooses to quit
command = this.mainMenuPrompt();
switch (command) {
case 'L': // System.out.println(" [L]oad data from file");
loadDataCommand();
break;
case 'T': // System.out.println(" Search Post [T]itles");
words = chooseSearchWordsPrompt();
searchTitleCommand(words);
break;
case 'B': // System.out.println(" Search Post [B]odies");
words = chooseSearchWordsPrompt();
searchBodyCommand(words);
break;
case 'P': // System.out.println(" Search [P]ost titles and bodies");
words = chooseSearchWordsPrompt();
searchPostCommand(words);
break;
case 'S': // System.out.println(" Display [S]tatistics for dataset");
displayStatsCommand();
break;
case 'Q': // System.out.println(" [Q]uit");
// do nothing, containing loop condition will fail
break;
default:
System.out.println(
"Didn't recognize that command. Please type one of the letters presented within []s to identify the command you would like to choose.");
break;
}
}
hr(); // thank user before ending this application
System.out.println("Thank you for using the Cheap and Healthy Search App.");
hr();
}
/**
* Prints the command options to System.out and reads user's choice through
* userInput scanner.
*/
public char mainMenuPrompt() {
// display menu of choices
System.out.println("Choose a command from the list below:");
System.out.println(" [L]oad data from file");
System.out.println(" Search Post [T]itles");
System.out.println(" Search Post [B]odies");
System.out.println(" Search [P]ost titles and bodies");
System.out.println(" Display [S]tatistics for dataset");
System.out.println(" [Q]uit");
// read in user's choice, and trim away any leading or trailing whitespace
System.out.print("Choose command: ");
String input = userInput.nextLine().trim();
if (input.length() == 0) // if user's choice is blank, return null character
return '\0';
// otherwise, return an uppercase version of the first character in input
return Character.toUpperCase(input.charAt(0));
}
/**
* Prompt user to enter filename, and display error message when loading fails.
*/
public void loadDataCommand() {
System.out.print("Enter the name of the file to load: ");
String filename = userInput.nextLine().trim();
try {
backend.loadData(filename);
} catch (FileNotFoundException e) {
System.out.println("Error: Could not find or load file: " + filename);
}
}
/**
* This method gives user the ability to interactively add or remove individual
* words from their query, before performing any kind of search.
*/
public List<String> chooseSearchWordsPrompt() {
List<String> words = new ArrayList<>();
while (true) { // this loop ends when the user pressed enter (without typing any words)
System.out.println("List of words to search for: " + words.toString());
System.out.print("Word(s) to add-to/remove-from query, or press enter to search: ");
String input = userInput.nextLine().replaceAll(",", "").trim();
if (input.length() == 0) // an empty string ends this loop and method call
return words;
else
// otherwise the specified word's presence in the list is toggled:
for (String word : input.split(" "))
if (words.contains(word))
words.remove(word); // remove any words that were already in the list
else
words.add(word); // add any words that were previously missing
}
}
/**
* Prompts user for a list of words to search post titles for, and then displays
* the list of results.
*/
public void searchTitleCommand(List<String> words) {
List<String> results = backend.findPostsByTitleWords(words.toString());
int resultIndex = 1;
if (results.size() > 0)
System.out.println("Found Results:");
else
System.out.println("No matches found.");
for (String result : results)
System.out.println("[" + (resultIndex++) + "] " + result);
}
/**
* Prompts user for a list of words to search post bodies for, and then displays
* the list of results.
*/
public void searchBodyCommand(List<String> words) {
List<String> results = backend.findPostsByBodyWords(words.toString());
int resultIndex = 1;
if (results.size() > 0)
System.out.println("Found Results:");
else
System.out.println("No matches found.");
for (String result : results)
System.out.println("[" + (resultIndex++) + "] " + result);
}
/**
* Prompts user for a list of words to search post titles and bodies for, and
* then displays the list of results.
*/
public void searchPostCommand(List<String> words) {
List<String> results = backend.findPostsByTitleOrBodyWords(words.toString());
int resultIndex = 1;
if (results.size() > 0)
System.out.println("Found Results:");
else
System.out.println("No matches found.");
for (String result : results)
System.out.println("[" + (resultIndex++) + "] " + result);
}
/**
* Displays dataset statistics to System.out.
*/
public void displayStatsCommand() {
System.out.println(backend.getStatisticsString());
}
}