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][W10-A4] Wang Mingrui #277

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
64 changes: 45 additions & 19 deletions src/seedu/addressbook/AddressBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,26 @@ public class AddressBook {
* The path to the file used for storing person data.
*/
private static String storageFilePath;

/**
* These are magic numbers used in the function process program args
*/
private static final int PROCESS_PROGRAM_ARGS_MAGIC_NUMBER_2 = 2;
private static final int PROCESS_PROGRAM_ARGS_MAGIC_NUMBER_1 = 1;
private static final int PROCESS_PROGRAM_ARGS_MAGIC_NUMBER_0 = 0;

/**
* These are magic numbers used in the function executeCommand
*/
private static final int COMMAND_TYPE_INDEX = 0;
private static final int COMMAND_ARGS_INDEX = 1;

/**
* These are magic numbers used in the function isPersonDataExtractableFrom
*/
private static final int SPLIT_ARGS_INDEX_0 = 0;
private static final int SPLIT_ARGS_INDEX_1 = 1;
private static final int SPLIT_ARGS_INDEX_2 = 2;

/*
* NOTE : =============================================================
Expand Down Expand Up @@ -257,17 +277,15 @@ 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 >= PROCESS_PROGRAM_ARGS_MAGIC_NUMBER_2) {
showToUser(MESSAGE_INVALID_PROGRAM_ARGS);
exitProgram();
}

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

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

Expand Down Expand Up @@ -366,8 +384,8 @@ private static void loadDataFromStorage() {
*/
private static String executeCommand(String userInputString) {
final String[] commandTypeAndParams = splitCommandWordAndArgs(userInputString);
final String commandType = commandTypeAndParams[0];
final String commandArgs = commandTypeAndParams[1];
final String commandType = commandTypeAndParams[COMMAND_TYPE_INDEX];
final String commandArgs = commandTypeAndParams[COMMAND_ARGS_INDEX];
switch (commandType) {
case COMMAND_ADD_WORD:
return executeAddPerson(commandArgs);
Expand All @@ -394,7 +412,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);
String trimmedUserInput = rawUserInput.trim();
final String[] split = trimmedUserInput.split("\\s+", 2);
return split.length == 2 ? split : new String[] { split[0] , "" }; // else case: no parameters
}

Expand Down Expand Up @@ -508,7 +527,7 @@ private static String executeDeletePerson(String commandArgs) {
return MESSAGE_INVALID_PERSON_DISPLAYED_INDEX;
}
final String[] targetInModel = getPersonByLastVisibleIndex(targetVisibleIndex);
return deletePersonFromAddressBook(targetInModel) ? getMessageForSuccessfulDelete(targetInModel) // success
return hasdeletedPersonFromAddressBook(targetInModel) ? getMessageForSuccessfulDelete(targetInModel) // success
: MESSAGE_PERSON_NOT_IN_ADDRESSBOOK; // not found
}

Expand All @@ -534,7 +553,8 @@ private static boolean isDeletePersonArgsValid(String rawArgs) {
* @return extracted index
*/
private static int extractTargetIndexFromDeletePersonArgs(String rawArgs) {
return Integer.parseInt(rawArgs.trim());
String trimmedArgs = rawArgs.trim();
return Integer.parseInt(trimmedArgs);
}

/**
Expand All @@ -544,7 +564,9 @@ private static int extractTargetIndexFromDeletePersonArgs(String rawArgs) {
* @return whether it is valid
*/
private static boolean isDisplayIndexValidForLastPersonListingView(int index) {
return index >= DISPLAYED_INDEX_OFFSET && index < latestPersonListingView.size() + DISPLAYED_INDEX_OFFSET;
int maxValidIndex = latestPersonListingView.size() + DISPLAYED_INDEX_OFFSET;
int minValidIndex = DISPLAYED_INDEX_OFFSET;
return index >= minValidIndex && index < maxValidIndex;
}

/**
Expand Down Expand Up @@ -793,7 +815,7 @@ private static void addPersonToAddressBook(String[] person) {
* @param exactPerson the actual person inside the address book (exactPerson == the person to delete in the full list)
* @return true if the given person was found and deleted in the model
*/
private static boolean deletePersonFromAddressBook(String[] exactPerson) {
private static boolean hasdeletedPersonFromAddressBook(String[] exactPerson) {
final boolean changed = ALL_PERSONS.remove(exactPerson);
if (changed) {
savePersonsToFile(getAllPersonsInAddressBook(), storageFilePath);
Expand Down Expand Up @@ -958,9 +980,9 @@ private static boolean isPersonDataExtractableFrom(String personData) {
final String matchAnyPersonDataPrefix = PERSON_DATA_PREFIX_PHONE + '|' + PERSON_DATA_PREFIX_EMAIL;
final String[] splitArgs = personData.trim().split(matchAnyPersonDataPrefix);
return splitArgs.length == 3 // 3 arguments
&& !splitArgs[0].isEmpty() // non-empty arguments
&& !splitArgs[1].isEmpty()
&& !splitArgs[2].isEmpty();
&& !splitArgs[SPLIT_ARGS_INDEX_0].isEmpty() // non-empty arguments
&& !splitArgs[SPLIT_ARGS_INDEX_1].isEmpty()
&& !splitArgs[SPLIT_ARGS_INDEX_2].isEmpty();
}

/**
Expand Down Expand Up @@ -1150,8 +1172,12 @@ private static String getUsageInfoForExitCommand() {
* @param sign Parameter sign to be removed
* @return string without the sign
*/
private static String removePrefixSign(String s, String sign) {
return s.replace(sign, "");
private static String removePrefixSign(String fullString, String prefix) {
if (fullString.startsWith(prefix)) {
return fullString.replaceFirst(prefix, "");
} else {
return fullString;
}
}

/**
Expand Down