Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[T2A3][T16-A4] Bennett Goh #599

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>src</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
17 changes: 17 additions & 0 deletions src/bin/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>src</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
163 changes: 97 additions & 66 deletions src/seedu/addressbook/AddressBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -199,30 +199,61 @@ public class AddressBook {
* ====================================================================
*/
public static void main(String[] args) {
showWelcomeMessage();
processProgramArgs(args);
loadDataFromStorage();
String[] message = { DIVIDER, DIVIDER, VERSION, MESSAGE_WELCOME, DIVIDER };
for (String m : message) {
System.out.println(LINE_PREFIX + m);
}
if (args.length >= 2) {
String[] message1 = { MESSAGE_INVALID_PROGRAM_ARGS };
for (String m1 : message1) {
System.out.println(LINE_PREFIX + m1);
}
exitProgram();
}

if (args.length == 1) {
String filePath = args[0];
if (!isValidFilePath(filePath)) {
String[] message1 = { String.format(MESSAGE_INVALID_FILE, filePath) };
for (String m1 : message1) {
System.out.println(LINE_PREFIX + m1);
}
exitProgram();
}

storageFilePath = filePath;
createFileIfMissing(filePath);
}

if(args.length == 0) {
String[] message1 = { MESSAGE_USING_DEFAULT_FILE };
for (String m1 : message1) {
System.out.println(LINE_PREFIX + m1);
}
storageFilePath = DEFAULT_STORAGE_FILEPATH;
createFileIfMissing(storageFilePath);
}
ALL_PERSONS.clear();
ALL_PERSONS.addAll(loadPersonsFromFile(storageFilePath));
while (true) {
String userCommand = getUserInput();
echoUserCommand(userCommand);
String[] message1 = { "[Command entered:" + userCommand + "]" };
for (String m1 : message1) {
System.out.println(LINE_PREFIX + m1);
}
String feedback = executeCommand(userCommand);
showResultToUser(feedback);
String[] message2 = { feedback, DIVIDER };
for (String m2 : message2) {
System.out.println(LINE_PREFIX + m2);
}
}
}

/*
* ==============NOTE TO STUDENTS======================================
* The method header comment can be omitted if the method is trivial
* and the header comment is going to be almost identical to the method
* signature anyway.
* ====================================================================
*/
private static void showWelcomeMessage() {
showToUser(DIVIDER, DIVIDER, VERSION, MESSAGE_WELCOME, DIVIDER);
}

private static void showResultToUser(String result) {
showToUser(result, DIVIDER);
String[] message = { result, DIVIDER };
for (String m : message) {
System.out.println(LINE_PREFIX + m);
}
}

/*
Expand All @@ -236,7 +267,10 @@ private static void showResultToUser(String result) {
* Echoes the user input back to the user.
*/
private static void echoUserCommand(String userCommand) {
showToUser("[Command entered:" + userCommand + "]");
String[] message = { "[Command entered:" + userCommand + "]" };
for (String m : message) {
System.out.println(LINE_PREFIX + m);
}
}

/*
Expand All @@ -247,28 +281,6 @@ private static void echoUserCommand(String userCommand) {
* ====================================================================
*/

/**
* Processes the program main method run arguments.
* If a valid storage file is specified, sets up that file for storage.
* Otherwise sets up the default file for storage.
*
* @param args full program arguments passed to application main method
*/
private static void processProgramArgs(String[] args) {
if (args.length >= 2) {
showToUser(MESSAGE_INVALID_PROGRAM_ARGS);
exitProgram();
}

if (args.length == 1) {
setupGivenFileForStorage(args[0]);
}

if(args.length == 0) {
setupDefaultFileForStorage();
}
}

/**
* Sets up the storage file based on the supplied file path.
* Creates the file if it is missing.
Expand All @@ -277,7 +289,10 @@ private static void processProgramArgs(String[] args) {
private static void setupGivenFileForStorage(String filePath) {

if (!isValidFilePath(filePath)) {
showToUser(String.format(MESSAGE_INVALID_FILE, filePath));
String[] message = { String.format(MESSAGE_INVALID_FILE, filePath) };
for (String m : message) {
System.out.println(LINE_PREFIX + m);
}
exitProgram();
}

Expand All @@ -289,7 +304,10 @@ private static void setupGivenFileForStorage(String filePath) {
* Displays the goodbye message and exits the runtime.
*/
private static void exitProgram() {
showToUser(MESSAGE_GOODBYE, DIVIDER, DIVIDER);
String[] message = { MESSAGE_GOODBYE, DIVIDER, DIVIDER };
for (String m : message) {
System.out.println(LINE_PREFIX + m);
}
System.exit(0);
}

Expand All @@ -299,7 +317,10 @@ private static void exitProgram() {
* Exits program if the file cannot be created.
*/
private static void setupDefaultFileForStorage() {
showToUser(MESSAGE_USING_DEFAULT_FILE);
String[] message = { MESSAGE_USING_DEFAULT_FILE };
for (String m : message) {
System.out.println(LINE_PREFIX + m);
}
storageFilePath = DEFAULT_STORAGE_FILEPATH;
createFileIfMissing(storageFilePath);
}
Expand Down Expand Up @@ -580,28 +601,17 @@ private static String getUserInput() {
return inputLine;
}

/* ==============NOTE TO STUDENTS======================================
* Note how the method below uses Java 'Varargs' feature so that the
* method can accept a varying number of message parameters.
* ====================================================================
*/
/**
* Shows a message to the user
*/
private static void showToUser(String... message) {
for (String m : message) {
System.out.println(LINE_PREFIX + m);
}
}

/**
/**
* Shows the list of persons to the user.
* The list will be indexed, starting from 1.
*
*/
private static void showToUser(ArrayList<String[]> persons) {
String listAsString = getDisplayString(persons);
showToUser(listAsString);
String[] message = { listAsString };
for (String m : message) {
System.out.println(LINE_PREFIX + m);
}
updateLatestViewedPersonListing(persons);
}

Expand Down Expand Up @@ -686,14 +696,23 @@ private static void createFileIfMissing(String filePath) {
if (storageFile.exists()) {
return;
}
String[] message = { String.format(MESSAGE_ERROR_MISSING_STORAGE_FILE, filePath) };

showToUser(String.format(MESSAGE_ERROR_MISSING_STORAGE_FILE, filePath));
for (String m : message) {
System.out.println(LINE_PREFIX + m);
}

try {
storageFile.createNewFile();
showToUser(String.format(MESSAGE_STORAGE_FILE_CREATED, filePath));
String[] message1 = { String.format(MESSAGE_STORAGE_FILE_CREATED, filePath) };
for (String m : message1) {
System.out.println(LINE_PREFIX + m);
}
} catch (IOException ioe) {
showToUser(String.format(MESSAGE_ERROR_CREATING_STORAGE_FILE, filePath));
String[] message1 = { String.format(MESSAGE_ERROR_CREATING_STORAGE_FILE, filePath) };
for (String m : message1) {
System.out.println(LINE_PREFIX + m);
}
exitProgram();
}
}
Expand All @@ -708,7 +727,10 @@ private static void createFileIfMissing(String filePath) {
private static ArrayList<String[]> loadPersonsFromFile(String filePath) {
final Optional<ArrayList<String[]>> successfullyDecoded = decodePersonsFromStrings(getLinesInFile(filePath));
if (!successfullyDecoded.isPresent()) {
showToUser(MESSAGE_INVALID_STORAGE_FILE_CONTENT);
String[] message = { MESSAGE_INVALID_STORAGE_FILE_CONTENT };
for (String m : message) {
System.out.println(LINE_PREFIX + m);
}
exitProgram();
}
return successfullyDecoded.get();
Expand All @@ -723,10 +745,16 @@ private static ArrayList<String> getLinesInFile(String filePath) {
try {
lines = new ArrayList(Files.readAllLines(Paths.get(filePath)));
} catch (FileNotFoundException fnfe) {
showToUser(String.format(MESSAGE_ERROR_MISSING_STORAGE_FILE, filePath));
String[] message = { String.format(MESSAGE_ERROR_MISSING_STORAGE_FILE, filePath) };
for (String m : message) {
System.out.println(LINE_PREFIX + m);
}
exitProgram();
} catch (IOException ioe) {
showToUser(String.format(MESSAGE_ERROR_READING_FROM_FILE, filePath));
String[] message = { String.format(MESSAGE_ERROR_READING_FROM_FILE, filePath) };
for (String m : message) {
System.out.println(LINE_PREFIX + m);
}
exitProgram();
}
return lines;
Expand All @@ -743,7 +771,10 @@ private static void savePersonsToFile(ArrayList<String[]> persons, String filePa
try {
Files.write(Paths.get(storageFilePath), linesToWrite);
} catch (IOException ioe) {
showToUser(String.format(MESSAGE_ERROR_WRITING_TO_FILE, filePath));
String[] message = { String.format(MESSAGE_ERROR_WRITING_TO_FILE, filePath) };
for (String m : message) {
System.out.println(LINE_PREFIX + m);
}
exitProgram();
}
}
Expand Down