-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEngine.java
More file actions
380 lines (308 loc) · 11.9 KB
/
Engine.java
File metadata and controls
380 lines (308 loc) · 11.9 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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/*
* @author - Zanyar Sherwani
* @author - Omar Zairi
*
* This is the driver class that displays
* menu prompts to the user and calls other
* classes as helpers.
*
*
*/
import java.util.Scanner;
import java.io.IOException;
import java.util.ArrayList;
import java.io.*;
public class Engine
{
// initializes the quotelist and stores all the quotes from xml file into quoteList variable
static QuoteList quoteList = new QuoteList();
static String quoteFileName = "quotes.xml";
// blacklist of profane words
static Blacklist blkList = new Blacklist();
// the current quote
static Quote quote = new Quote();
/* static Scanner in = new Scanner(System.in);
static Scanner searcher = new Scanner(System.in);
static QuoteSaxParser qParser;
//public Engine(){
qParser = new QuoteSaxParser (quoteFileName);
quoteList = qParser.getQuoteList();
// }*/
static int lastId;
public static void main(String[] args){
// keeps track of user input, if user enters 'exit' the while loop below will stop
String tracker = "a";
// parses the xml file into the quotelist
Scanner in = new Scanner(System.in);
Scanner searcher = new Scanner(System.in);
QuoteSaxParser qParser = new QuoteSaxParser (quoteFileName);
quoteList = qParser.getQuoteList();
lastId = Integer.parseInt(getIdOfLastQuote())+1;
// loop runs until user enters 'exit'
while( !tracker.equalsIgnoreCase("exit") ){
printRandomQuote();
// user menu
System.out.println("\nPress 1 to get another random quote.");
System.out.println("Press 2 to search by a keyword in a quote.");
System.out.println("Press 3 to search by an author.");
System.out.println("Press 4 to search by both.");
System.out.println("Press 5 to search by keywords.");
System.out.println("Press 6 to add a quote.");
System.out.println("Enter 'exit' to quit the program.");
String inn = in.nextLine();
tracker = inn;
// user input from possible menu options
String key;
// runs desired use case based on user input
switch(tracker) {
case "2":
System.out.println("Enter search query.");
System.out.print("-> ");
key = searcher.nextLine();
searchQuote( key );
break;
case "3":
System.out.println("Enter search query.");
System.out.print("-> ");
key = searcher.nextLine();
searchAuthor( key );
break;
case "4":
System.out.println("Enter search query.");
System.out.print("-> ");
key = searcher.nextLine();
searchBoth( key );
break;
case "5":
// Search by keywords
System.out.println("Enter search query.");
System.out.print("-> ");
key = searcher.nextLine();
searchKeywords( key );
break;
case "6":
System.out.println("Enter the quote.");
System.out.print("-> ");
key = searcher.nextLine();
String newQuote = key;
if(blkList.containsBadWord(newQuote))
System.out.println("Sorry, innappropriate language will not be added to our database!");
else{
System.out.println("Enter the author.");
System.out.print("-> ");
key = searcher.nextLine();
String newAuthor = key;
if(blkList.containsBadWord(newAuthor))
System.out.println("Sorry, innappropriate language will not be added to our database!");
else
{
String keywordString = null;
// Get keywords
System.out.println("Enter keywords.");
System.out.println("* Seperate keyword or phrases with a comma *");
System.out.println("* Limit of 5 keywords and 25 characters per each keyword *");
System.out.println("* Example: eating, motivate me, fitness, study guide *");
System.out.print("-> ");
key = searcher.nextLine();
keywordString = checkKeywords(key);
addQuote( newQuote , newAuthor , keywordString, key );
}
}
break;
}
}
}
/*
* @author - Zanyar Sherwani
*
* This function uses a non-optimal
* way to check for profanity within
* any string passed in
*
*/
public static boolean checkForProfanity(String str){
return true;
}
/*
* @author : Zanyar Sherwani
* @return id of last quote in the xml file
*
* returns the id of the last quote in the xml
* file so that the next quote's id can be incremented
* by one
*/
public static String getIdOfLastQuote(){
return quoteList.getLastId();
}
/*
* @author : Omar Zairi
* @param text of the quote
* @param author of the quote
*
* Gets a quote text and author name from the user
* Adds the quote to the current quote list array
* and adds it to the xml file
*
*/
public static void addQuote( String quote , String author , String keywords, String key )
{
// create new quote
Quote newQuote = new Quote(author,quote,strToArray(key).toArray(new String[strToArray(key).size()]), Integer.toString(lastId));
// put new quote in current quote list
quoteList.setQuote( newQuote );
// print the old list into a .tmp file and add the new quote to the file
try (BufferedReader br = new BufferedReader(new FileReader("quotes.xml"))) {
PrintWriter pw = new PrintWriter("quotes.tmp", "UTF-8");
String line;
// add old quotes to new file
while ((line = br.readLine()) != null) {
if( !line.equals("</quote-list>") )
pw.println(line);
}
// write new quote to file
pw.println(" <quote>");
pw.println(" <id>"+lastId+"</id>");
pw.println(" <quote-text>"+quote+"</quote-text>");
pw.println(" <author>"+author+"</author>");
pw.println(keywords);
pw.println(" </quote>");
// close out the list
pw.println("</quote-list>");
// rename the file
File renamer = new File("quotes.tmp");
renamer.renameTo(new File("quotes.xml"));
br.close();
pw.close();
}
catch(Exception e){
System.out.println("Error with file.");
}
lastId++;
}
// Searches quote list by quote text
public static void searchQuote( String keyword )
{
// retrieve list of results based on search
QuoteList results = quoteList.search( keyword , 1 );
// output the results
System.out.println("Results ("+results.getSize()+"):");
for ( int i = 0 ; i < results.getSize() ; i++ )
{
Quote quote1 = results.getQuote(i);
System.out.println( " "+quote1.getQuoteText() );
System.out.println( " -"+quote1.getAuthor() + "\n" );
}
}
public static void searchKeywords(String keywords){
QuoteList results = quoteList.search( keywords, 3 );
// output the results
System.out.println("Results ("+results.getSize()+"):");
for ( int i = 0 ; i < results.getSize() ; i++ )
{
Quote quote1 = results.getQuote(i);
System.out.println( " "+quote1.getQuoteText() );
System.out.println( " -"+quote1.getAuthor() + "\n" );
}
}
// Searches quote list by author name
public static void searchAuthor( String keyword )
{
QuoteList results = quoteList.search( keyword , 0 );
System.out.println("Results ("+results.getSize()+"):");
for ( int i = 0 ; i < results.getSize() ; i++ )
{
Quote quote1 = results.getQuote(i);
System.out.println( " "+quote1.getQuoteText() );
System.out.println( " -"+quote1.getAuthor() + "\n" );
}
}
// Searchs quote list by author name and quote text
public static void searchBoth( String keyword )
{
QuoteList results = quoteList.search( keyword , 2 );
System.out.println("Results ("+results.getSize()+"):");
for ( int i = 0 ; i < results.getSize() ; i++ )
{
Quote quote1 = results.getQuote(i);
System.out.println( " "+quote1.getQuoteText() );
System.out.println( " -"+quote1.getAuthor() + "\n" );
}
}
// Gets a random quote from quote list
public static void printRandomQuote()
{
quote = quoteList.getRandomQuote();
System.out.println("\nRandom quote of the day:");
System.out.println( " "+quote.getQuoteText() );
System.out.println( " -"+quote.getAuthor() );
}
public static ArrayList<String> strToArray(String keywords){
ArrayList<String> keywordList = new ArrayList<String>();
String[] wordsWithComma = keywords.split(",");
if(wordsWithComma != null){
int listSize = wordsWithComma.length;
for(int i = 0; i < listSize && i < 5 ; i++)
{
String trimmedWord = wordsWithComma[i];
trimmedWord = trimmedWord.trim();
if( trimmedWord.length() <= 25 && !trimmedWord.equals("") )
{
keywordList.add(trimmedWord);
}
}
return keywordList;
}
else{
keywordList.add(keywords);
return keywordList;
}
}
/*
* Author : Omar Zairi
*
* @param : String of keywords separated by commas
* @return : String of keywords separated by commas
* in between XML <keywords> brackets
*
* Gets a string passed in that contains the user's keywords
* for their quote. Each keyword is split by a comma and then
* checked if it is a valid keyword. Once all keywords are
* validated it is returned in a string in between the needed
* XML brackets.
*
* Example : <keywords>health,fitness,food</keywords>
*/
public static String checkKeywords(String keywords)
{
// Split keyword string by commas
String[] wordsWithComma = keywords.split(",");
// List that will store final list of validated keywords
ArrayList<String> keywordList = new ArrayList<String>();
// Amount of words separated by a comma
int listSize = wordsWithComma.length;
// Validates each keyword in wordsWithComma l ist
for(int i = 0; i < listSize && i < 5 ; i++)
{
// Trims the white space from keyword
String trimmedWord = wordsWithComma[i];
trimmedWord = trimmedWord.trim();
// Checks to see that the keyword isn't blank and is less than
// 26 characters
if( trimmedWord.length() <= 25 && !trimmedWord.equals("") )
{
keywordList.add(trimmedWord);
}
}
// String to put keywords in XML form
String xmlString = " <keywords>";
for(int i = 0; i < keywordList.size() ; i++)
{
if( i == keywordList.size()-1 )
xmlString += keywordList.get(i);
else
xmlString += keywordList.get(i)+",";
}
xmlString += "</keywords>";
return xmlString;
}
}