-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTextBuddy.java
462 lines (387 loc) · 13.1 KB
/
TextBuddy.java
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
/**
* Assumptions:
* - When user input command that required additional information (such as add or delete),there will be handler
* to ensure that the information is not empty.
* - Empty command will be access as invalid command.
* - When user wants to delete line number that is out of bound, the program will not allow.
* - The program will automatically create new file if the file that user wants to access is non existent
* - The program will not write empty line into the file.
*/
public class TextBuddy {
// Possible messages that can be showed to the user
private static final String MESSAGE_ADDED = "added to %1$s: "+'"'+"%2$s"+'"';
private static final String MESSAGE_DELETED = "deleted from %1$s: "+'"'+"%2$s"+'"';
private static final String MESSAGE_CLEARED = "all content deleted from %1$s";
private static final String MESSAGE_SORTED = "file has been sorted";
private static final String MESSAGE_RETURN_SEARCH = "total %1$d line(s) found";
private static final String MESSAGE_EMPTY_FILE = "%1$s is empty";
private static final String MESSAGE_WELCOME = "Welcome to TextBuddy.";
private static final String MESSAGE_READY = "%1$s is ready for use.";
//Another set of messages, mostly used when invalid situations take place.
private static final String INVALID_COMMAND = "invalid command.";
private static final String INVALID_LINE_NUMBER = "line %1$s does not exist or out of bound.";
private static final String INVALID_SEARCH_FORMAT = "invalid search format. Correct format : search <word_to_search>";
private static final String INVALID_ADD_FORMAT = "invalid add format. Correct format : add <line_to_add>";
private static final String INVALID_DELETE_FORMAT = "invalid delete format. Correct format : delete <line_number>";
//Indexes and first line number defined for later usage.
private static final int FIRST_COMMAND_INDEX = 0;
private static final int SECOND_COMMAND_INDEX = 1;
private static final int FIRST_LINE_NUMBER = 1;
//All possible command type
enum COMMAND_TYPE {
ADD , DELETE ,DISPLAY , CLEAR, INVALID, SORT , SEARCH ,EXIT ;
}
public TextBuddy(String fileName) throws IOException {
processInput(fileName);
}
//contentList is used to store all content of the file into an ArrayList
private static ArrayList<String> contentList = new ArrayList<String>();
//Global Scanner object to scan for input
private static Scanner sc = new Scanner(System.in);
//Reader and writer defined for file handling
private static BufferedReader reader = null;
private static BufferedWriter writer = null;
//Define an empty String for fileName. Will be use globally for convenience.
static String fileName = "";
public static void main(String[] args) throws IOException{
showWelcomeMessage(args);
runProgram();
}
private static void runProgram() throws IOException {
while (true) {
askForCommand();
String command = readUserInput();
showUserCommand(command);
String feedback = executeCommand(command);
showMessage(feedback);
}
}
private static void showUserCommand(String command) {
System.out.println(command+"\r\n");
}
private static void askForCommand() {
System.out.print("command:");
}
private static void showWelcomeMessage(String[] args) throws IOException {
fileName = args[0];
showMessage(MESSAGE_WELCOME + processInput(fileName));
}
/**
* This operation is used to process the file ( including load content and prepare reader and writer)
* Once finish, it will return message to notify that the file is ready
*
* @param fileName
* The name of the file we will be working with
* @return a ready message with fileName
*/
static String processInput (String fileName) throws IOException{
File file = new File(fileName);
if(!file.exists()){
file.createNewFile();
setUpReaderAndWriter(fileName);
}
else{
setUpReaderAndWriter(fileName);
}
return String.format(MESSAGE_READY,fileName);
}
private static String readUserInput() {
String command = sc.nextLine();
return command;
}
private static String executeCommand(String command) throws IOException{
if (command.trim().equals("")){
return String.format(INVALID_COMMAND);
}
String commandTypeString = getCommandType(command);
COMMAND_TYPE commandType = determineCommandType(commandTypeString);
switch (commandType) {
case ADD:
return addLine(command);
case DELETE:
return deleteLine(command);
case CLEAR:
return clear();
case DISPLAY:
return displayContent(fileName);
case SORT:
return sort();
case SEARCH:
return search(command);
case INVALID:
return String.format(INVALID_COMMAND);
case EXIT:
System.exit(0);
default:
//throw an error if the command is not recognized
throw new Error("Command type does not exist");
}
}
/**
* This operation is used to determine the command type that the user wishes to execute
*
* @param commandTypeString
* the string that contain the command type
* @return COMMAN_TYPE object
* corresponding command type
*/
private static COMMAND_TYPE determineCommandType(String commandTypeString){
if (commandTypeString == null){
throw new Error("command type string cannot be null!");
}
if (commandTypeString.equalsIgnoreCase("add")) {
return COMMAND_TYPE.ADD;
} else if (commandTypeString.equalsIgnoreCase("display")) {
return COMMAND_TYPE.DISPLAY;
} else if (commandTypeString.equalsIgnoreCase("delete")) {
return COMMAND_TYPE.DELETE;
} else if (commandTypeString.equalsIgnoreCase("clear")) {
return COMMAND_TYPE.CLEAR;
} else if (commandTypeString.equalsIgnoreCase("sort")) {
return COMMAND_TYPE.SORT;
} else if (commandTypeString.equalsIgnoreCase("search")) {
return COMMAND_TYPE.SEARCH;
} else if (commandTypeString.equalsIgnoreCase("exit")) {
return COMMAND_TYPE.EXIT;
} else {
return COMMAND_TYPE.INVALID;
}
}
/**
* This operation is used to set up reader and writer on the file
*
* @param fileName
* The name of the file we will be working with
*/
private static void setUpReaderAndWriter(String fileName)
throws FileNotFoundException, IOException {
readFromFile(fileName);
//writeToFile();
}
private static void showMessage(String message){
System.out.println(message+"\r\n");
}
/**
* This operation is used to display all content of the file
*
* @param fileName
* The name of the file we will be working with
* @return finalString
* A string contains all content of the file
*/
static String displayContent(String fileName) throws IOException{
readFromFile(fileName);
String finalString ="";
if(contentList.isEmpty()){
return String.format(MESSAGE_EMPTY_FILE,fileName);
}
else{
int lineNumber = FIRST_LINE_NUMBER;
for(String s : contentList){
String outputLine = constructOutput(s,lineNumber);
finalString += outputLine;
lineNumber++;
}
}
finalString = removeEmptyLine(finalString);
return finalString;
}
/**
* This operation is used to remove and empty line at the end of the display message
*/
private static String removeEmptyLine(String finalString) {
finalString = finalString.substring(0, finalString.length()-2);
return finalString;
}
/**
* This operation is used to construct a proper output for displaying
*/
static String constructOutput(String currentLine, int lineNumber) {
String outputLine = lineNumber+". "+currentLine+"\r\n";
return outputLine;
}
/**
* This operation is used to add a new line specified by the user to the end of the file
* @param userInput
* The line that user wants to be added into the file
* @return status of the addition (Invalid or successful)
*/
static String addLine(String userInput) throws IOException{
String inputLine = getUserInput(userInput);
if(inputLine.equalsIgnoreCase("")){
return String.format(INVALID_ADD_FORMAT);
}
contentList.add(inputLine);
writeToFile();
return String.format(MESSAGE_ADDED,fileName,inputLine);
}
/**
* This operation is used to write all content from an ArrayList contentList to the designated file.
* Mainly used to save file to the most updated version
*/
private static void writeToFile() throws IOException {
writer = new BufferedWriter(new FileWriter(fileName,false));
if(contentList.isEmpty()){
writer.write("");
}
else{
for (String s : contentList){
writer.write(s);
writer.newLine();
}
}
closeWriter();
}
/**
* This operation is used to read all content from the designated file to contentList.
* Mainly used to get most updated content in order for the program to work with
*/
private static void readFromFile(String fileName)
throws FileNotFoundException, IOException {
contentList.clear();
reader = new BufferedReader(new FileReader(fileName));
String currentLine;
while ((currentLine = reader.readLine()) != null) {
contentList.add(currentLine);
}
reader.close();
}
/**
* This operation is used to delete a line from the file, specified by the line number entered by the user
*
* @param userInput
* The line number that user wants to delete
* @return status of deletion (invalid or success)
*/
static String deleteLine(String userInput) throws IOException{
String input = getUserInput(userInput);
if(input.equalsIgnoreCase("")){
return String.format(INVALID_DELETE_FORMAT);
}
int lineNumber = Integer.parseInt(getUserInput(userInput));
if(contentList.isEmpty()){
return String.format(MESSAGE_EMPTY_FILE,fileName);
}
if(lineNumber > 0 && lineNumber <= contentList.size() ){
int actualIndex = lineNumber - 1;
String deletedLine = contentList.get(actualIndex);
contentList.remove(actualIndex);
writeToFile();
return String.format(MESSAGE_DELETED,fileName,deletedLine);
}
else{
return String.format(INVALID_LINE_NUMBER,lineNumber);
}
}
/**
* This operation is used to clear all content in the file
*/
static String clear() throws IOException{
contentList.clear();
writeToFile();
return String.format(MESSAGE_CLEARED,fileName);
}
/**
* This operation is search for lines that contain the word the user want to search for
*
* @param userInput
* The commend the user input in containing the word that need to be searched
* @return the number of lines contain the words and the full line
* @throws IOException
* @throws FileNotFoundException
*/
static String search(String userInput) throws FileNotFoundException, IOException{
readFromFile(fileName);
String input = getUserInput(userInput);
if(input.equalsIgnoreCase("")){
return String.format(INVALID_SEARCH_FORMAT);
}
else{
int numberOfLine = 0;
String finalString ="";
for(String i : contentList){
if(i.toLowerCase().contains(input.toLowerCase())){
numberOfLine++;
finalString += "\r\n"+removeEmptyLine(constructOutput(i,numberOfLine));
}
}
return String.format(MESSAGE_RETURN_SEARCH,numberOfLine)+finalString;
}
}
/**
* This operation is used to sort the file in alphabetical order
* @return MESSAGE_SORTED
* The message to say that the file has been sorted
* @throws IOException
*/
static String sort() throws IOException{
Collections.sort(contentList);
writeToFile();
return MESSAGE_SORTED;
}
/**
* This operation is used to close the writer used on the file
*/
private static void closeWriter() throws IOException{
writer.flush();
writer.close();
}
/**
* This operation is used to extract the command type that the user used
*
* @param command
* The full command string that the user has entered
* @return commandType
* The command type that the user entered
*/
private static String getCommandType(String command){
String commandType= splitInputCommand(command)[FIRST_COMMAND_INDEX];
return commandType;
}
/**
* This operation is used to determine that information that the user has input
*
* @param command
* The full command string that the user has entered
* @return userInput
* The input that the user has entered
*/
private static String getUserInput(String command){
String[] commandArray = splitInputCommand(command);
if(commandArray.length == 1){
return "";
}
String userInput = splitInputCommand(command)[SECOND_COMMAND_INDEX];
return userInput;
}
/**
* This operation is used to separate command type and user input from the full command string that
* the user has entered.These information will then be stored in an Array.
*
* @param command
* The full command string that the user has entered
* @return commandArray
* An Array that contains both command type and user input.
*/
private static String[] splitInputCommand(String command){
String[] commandArray;
if(command.trim().contains(" ")){
commandArray = command.trim().split(" ",2);
}
else{
commandArray = new String[1];
commandArray[FIRST_COMMAND_INDEX] = command.trim();
}
return commandArray;
}
}