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

[T2A2][W14-A3] Loh Zi Bin, Robin #311

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
81 changes: 56 additions & 25 deletions src/seedu/addressbook/AddressBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.Optional;
import java.util.Scanner;
import java.util.Set;
import java.util.PrimitiveIterator.OfDouble;

/*
* NOTE : =============================================================
Expand Down Expand Up @@ -183,6 +184,8 @@ public class AddressBook {
*/
private static final ArrayList<String[]> 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
Expand All @@ -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 : =============================================================
Expand Down Expand Up @@ -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();
}
}
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about the magic string for the regex here?

return split.length == TWO ? split : new String[] { split[0] , "" }; // else case: no parameters
}

/**
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -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, "");
}

/**
Expand Down