diff --git a/src/seedu/addressbook/AddressBook.java b/src/seedu/addressbook/AddressBook.java index 5a158b67..5e19d552 100644 --- a/src/seedu/addressbook/AddressBook.java +++ b/src/seedu/addressbook/AddressBook.java @@ -22,6 +22,7 @@ import java.util.Optional; import java.util.Scanner; import java.util.Set; +import java.util.PrimitiveIterator.OfDouble; /* * NOTE : ============================================================= @@ -183,6 +184,8 @@ public class AddressBook { */ private static final ArrayList ALL_PERSONS = new ArrayList<>(); + + /** * Stores the most recent list of persons shown to the user as a result of a user command. * This is a subset of the full list. Deleting persons in the pull list does not delete @@ -194,6 +197,17 @@ public class AddressBook { * The path to the file used for storing person data. */ private static String storageFilePath; + + /* + * NOTE : ============================================================================================= + * Number of arguments while reading input. + * ==================================================================================================== + */ + private static final int SINGLE_ARGUMENT = 1; + private static final int NULL_ARGUMENT = 0; + + private static final int ZERO = 0; + private static final int TWO = 2; /* * NOTE : ============================================================= @@ -257,16 +271,12 @@ private static void echoUserCommand(String userCommand) { * @param args full program arguments passed to application main method */ private static void processProgramArgs(String[] args) { - if (args.length >= 2) { + if (args.length > SINGLE_ARGUMENT) { showToUser(MESSAGE_INVALID_PROGRAM_ARGS); exitProgram(); - } - - if (args.length == 1) { + } else if (args.length == SINGLE_ARGUMENT) { setupGivenFileForStorage(args[0]); - } - - if(args.length == 0) { + } else if (args.length == NULL_ARGUMENT) { setupDefaultFileForStorage(); } } @@ -281,10 +291,10 @@ private static void setupGivenFileForStorage(String filePath) { if (!isValidFilePath(filePath)) { showToUser(String.format(MESSAGE_INVALID_FILE, filePath)); exitProgram(); + } else { + storageFilePath = filePath; + createFileIfMissing(storageFilePath); } - - storageFilePath = filePath; - createFileIfMissing(filePath); } /** @@ -321,7 +331,16 @@ private static boolean isValidFilePath(String filePath) { } catch (InvalidPathException ipe) { return false; } - return hasValidParentDirectory(filePathToValidate) && hasValidFileName(filePathToValidate); + + if (hasValidParentDirectory(filePathToValidate) == false) { + return false; + } + + if (hasValidFileName(filePathToValidate) == false) { + return false; + } + + return true; } /** @@ -339,8 +358,20 @@ private static boolean hasValidParentDirectory(Path filePath) { * If a file already exists, it must be a regular file. */ private static boolean hasValidFileName(Path filePath) { - return filePath.getFileName().toString().lastIndexOf('.') > 0 - && (!Files.exists(filePath) || Files.isRegularFile(filePath)); + + if (filePath.getFileName().toString().lastIndexOf('.') > ZERO) { + return true; + } + + if (Files.exists(filePath) == false) { + return true; + } + + if (Files.isRegularFile(filePath) == true) { + return true; + } + + return false; } /** @@ -394,8 +425,8 @@ private static String executeCommand(String userInputString) { * @return size 2 array; first element is the command type and second element is the arguments string */ private static String[] splitCommandWordAndArgs(String rawUserInput) { - final String[] split = rawUserInput.trim().split("\\s+", 2); - return split.length == 2 ? split : new String[] { split[0] , "" }; // else case: no parameters + final String[] split = rawUserInput.trim().split("\\s+", TWO); + return split.length == TWO ? split : new String[] { split[0] , "" }; // else case: no parameters } /** @@ -989,12 +1020,12 @@ private static String extractPhoneFromPersonString(String encoded) { // phone is last arg, target is from prefix to end of string if (indexOfPhonePrefix > indexOfEmailPrefix) { - return removePrefixSign(encoded.substring(indexOfPhonePrefix, encoded.length()).trim(), + return removePrefix(encoded.substring(indexOfPhonePrefix, encoded.length()).trim(), PERSON_DATA_PREFIX_PHONE); // phone is middle arg, target is from own prefix to next prefix } else { - return removePrefixSign( + return removePrefix( encoded.substring(indexOfPhonePrefix, indexOfEmailPrefix).trim(), PERSON_DATA_PREFIX_PHONE); } @@ -1012,12 +1043,12 @@ private static String extractEmailFromPersonString(String encoded) { // email is last arg, target is from prefix to end of string if (indexOfEmailPrefix > indexOfPhonePrefix) { - return removePrefixSign(encoded.substring(indexOfEmailPrefix, encoded.length()).trim(), + return removePrefix(encoded.substring(indexOfEmailPrefix, encoded.length()).trim(), PERSON_DATA_PREFIX_EMAIL); // email is middle arg, target is from own prefix to next prefix } else { - return removePrefixSign( + return removePrefix( encoded.substring(indexOfEmailPrefix, indexOfPhonePrefix).trim(), PERSON_DATA_PREFIX_EMAIL); } @@ -1144,14 +1175,14 @@ private static String getUsageInfoForExitCommand() { */ /** - * Removes sign(p/, d/, etc) from parameter string + * Removes prefix(p/, d/, etc) from parameter string * - * @param s Parameter as a string - * @param sign Parameter sign to be removed - * @return string without the sign + * @param fullString Parameter as a string + * @param prefix Parameter sign to be removed + * @return string without the prefix */ - private static String removePrefixSign(String s, String sign) { - return s.replace(sign, ""); + private static String removePrefix(String fullString, String prefix) { + return fullString.replace(prefix, ""); } /**