From b4c199c61401ceb96044f5c7441814141bd91a3c Mon Sep 17 00:00:00 2001 From: "DESKTOP-9K2NI32\\Liu Yiming" Date: Fri, 2 Feb 2018 16:56:04 +0800 Subject: [PATCH 1/4] test --- src/seedu/addressbook/AddressBook.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/seedu/addressbook/AddressBook.java b/src/seedu/addressbook/AddressBook.java index 5a158b67..4b84a4d4 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.HashMap; /* * NOTE : ============================================================= @@ -145,6 +146,7 @@ public class AddressBook { private static final int PERSON_DATA_INDEX_PHONE = 1; private static final int PERSON_DATA_INDEX_EMAIL = 2; + /** * The number of data elements for a single person. */ @@ -181,14 +183,15 @@ public class AddressBook { /** * List of all persons in the address book. */ - private static final ArrayList ALL_PERSONS = new ArrayList<>(); + 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 * those persons from this list. */ - private static ArrayList latestPersonListingView = getAllPersonsInAddressBook(); // initial view is of all + private static ArrayList> latestPersonListingView = + getAllPersonsInAddressBook(); // initial view is of all /** * The path to the file used for storing person data. From 7e61a609d1ce5f300e43752929730661b94d81b5 Mon Sep 17 00:00:00 2001 From: "DESKTOP-9K2NI32\\Liu Yiming" Date: Fri, 2 Feb 2018 16:56:22 +0800 Subject: [PATCH 2/4] Revert "test" This reverts commit b4c199c61401ceb96044f5c7441814141bd91a3c. --- src/seedu/addressbook/AddressBook.java | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/seedu/addressbook/AddressBook.java b/src/seedu/addressbook/AddressBook.java index 4b84a4d4..5a158b67 100644 --- a/src/seedu/addressbook/AddressBook.java +++ b/src/seedu/addressbook/AddressBook.java @@ -22,7 +22,6 @@ import java.util.Optional; import java.util.Scanner; import java.util.Set; -import java.util.HashMap; /* * NOTE : ============================================================= @@ -146,7 +145,6 @@ public class AddressBook { private static final int PERSON_DATA_INDEX_PHONE = 1; private static final int PERSON_DATA_INDEX_EMAIL = 2; - /** * The number of data elements for a single person. */ @@ -183,15 +181,14 @@ public class AddressBook { /** * List of all persons in the address book. */ - private static final ArrayList> ALL_PERSONS = new ArrayList<>(); + 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 * those persons from this list. */ - private static ArrayList> latestPersonListingView = - getAllPersonsInAddressBook(); // initial view is of all + private static ArrayList latestPersonListingView = getAllPersonsInAddressBook(); // initial view is of all /** * The path to the file used for storing person data. From 9ec7dff4b260728565b1df0815df550e972a6f7c Mon Sep 17 00:00:00 2001 From: "DESKTOP-9K2NI32\\Liu Yiming" Date: Sat, 3 Feb 2018 10:32:43 +0800 Subject: [PATCH 3/4] Changed the data type of person from String[] to HashMap. Edited some of the comments and constants. --- src/seedu/addressbook/AddressBook.java | 121 ++++++++++++------------- 1 file changed, 59 insertions(+), 62 deletions(-) diff --git a/src/seedu/addressbook/AddressBook.java b/src/seedu/addressbook/AddressBook.java index 5a158b67..7e166cca 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.HashMap; /* * NOTE : ============================================================= @@ -136,19 +137,14 @@ public class AddressBook { private static final String DIVIDER = "==================================================="; - /* We use a String array to store details of a single person. - * The constants given below are the indexes for the different data elements of a person - * used by the internal String[] storage format. - * For example, a person's name is stored as the 0th element in the array. + /* We use a HashMap to store details of a single person. + * The constants given below are the keys for the different data elements of a person + * used by the internal HashMap storage format. + * For example, a person's name is stored as the "name" element in the HashMap. */ - private static final int PERSON_DATA_INDEX_NAME = 0; - private static final int PERSON_DATA_INDEX_PHONE = 1; - private static final int PERSON_DATA_INDEX_EMAIL = 2; - - /** - * The number of data elements for a single person. - */ - private static final int PERSON_DATA_COUNT = 3; + private static final String PERSON_PROPERTY_NAME = "name"; + private static final String PERSON_PROPERTY_PHONE = "phone"; + private static final String PERSON_PROPERTY_EMAIL = "email"; /** * Offset required to convert between 1-indexing and 0-indexing.COMMAND_ @@ -181,14 +177,15 @@ public class AddressBook { /** * List of all persons in the address book. */ - private static final ArrayList ALL_PERSONS = new ArrayList<>(); + 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 * those persons from this list. */ - private static ArrayList latestPersonListingView = getAllPersonsInAddressBook(); // initial view is of all + private static ArrayList> latestPersonListingView = getAllPersonsInAddressBook(); + // initial view is of all /** * The path to the file used for storing person data. @@ -417,7 +414,7 @@ private static String getMessageForInvalidCommandInput(String userCommand, Strin */ private static String executeAddPerson(String commandArgs) { // try decoding a person from the raw args - final Optional decodeResult = decodePersonFromString(commandArgs); + final Optional> decodeResult = decodePersonFromString(commandArgs); // checks if args are valid (decode result will not be present if the person is invalid) if (!decodeResult.isPresent()) { @@ -425,7 +422,7 @@ private static String executeAddPerson(String commandArgs) { } // add the person as specified - final String[] personToAdd = decodeResult.get(); + final HashMap personToAdd = decodeResult.get(); addPersonToAddressBook(personToAdd); return getMessageForSuccessfulAddPerson(personToAdd); } @@ -437,7 +434,7 @@ private static String executeAddPerson(String commandArgs) { * @param addedPerson person who was successfully added * @return successful add person feedback message */ - private static String getMessageForSuccessfulAddPerson(String[] addedPerson) { + private static String getMessageForSuccessfulAddPerson(HashMap addedPerson) { return String.format(MESSAGE_ADDED, getNameFromPerson(addedPerson), getPhoneFromPerson(addedPerson), getEmailFromPerson(addedPerson)); } @@ -451,7 +448,7 @@ private static String getMessageForSuccessfulAddPerson(String[] addedPerson) { */ private static String executeFindPersons(String commandArgs) { final Set keywords = extractKeywordsFromFindPersonArgs(commandArgs); - final ArrayList personsFound = getPersonsWithNameContainingAnyKeyword(keywords); + final ArrayList> personsFound = getPersonsWithNameContainingAnyKeyword(keywords); showToUser(personsFound); return getMessageForPersonsDisplayedSummary(personsFound); } @@ -462,7 +459,7 @@ private static String executeFindPersons(String commandArgs) { * @param personsDisplayed used to generate summary * @return summary message for persons displayed */ - private static String getMessageForPersonsDisplayedSummary(ArrayList personsDisplayed) { + private static String getMessageForPersonsDisplayedSummary(ArrayList> personsDisplayed) { return String.format(MESSAGE_PERSONS_FOUND_OVERVIEW, personsDisplayed.size()); } @@ -482,9 +479,9 @@ private static Set extractKeywordsFromFindPersonArgs(String findPersonCo * @param keywords for searching * @return list of persons in full model with name containing some of the keywords */ - private static ArrayList getPersonsWithNameContainingAnyKeyword(Collection keywords) { - final ArrayList matchedPersons = new ArrayList<>(); - for (String[] person : getAllPersonsInAddressBook()) { + private static ArrayList> getPersonsWithNameContainingAnyKeyword(Collection keywords) { + final ArrayList> matchedPersons = new ArrayList<>(); + for (HashMap person : getAllPersonsInAddressBook()) { final Set wordsInName = new HashSet<>(splitByWhitespace(getNameFromPerson(person))); if (!Collections.disjoint(wordsInName, keywords)) { matchedPersons.add(person); @@ -507,7 +504,7 @@ private static String executeDeletePerson(String commandArgs) { if (!isDisplayIndexValidForLastPersonListingView(targetVisibleIndex)) { return MESSAGE_INVALID_PERSON_DISPLAYED_INDEX; } - final String[] targetInModel = getPersonByLastVisibleIndex(targetVisibleIndex); + final HashMap targetInModel = getPersonByLastVisibleIndex(targetVisibleIndex); return deletePersonFromAddressBook(targetInModel) ? getMessageForSuccessfulDelete(targetInModel) // success : MESSAGE_PERSON_NOT_IN_ADDRESSBOOK; // not found } @@ -554,7 +551,7 @@ private static boolean isDisplayIndexValidForLastPersonListingView(int index) { * @param deletedPerson successfully deleted * @return successful delete person feedback message */ - private static String getMessageForSuccessfulDelete(String[] deletedPerson) { + private static String getMessageForSuccessfulDelete(HashMap deletedPerson) { return String.format(MESSAGE_DELETE_PERSON_SUCCESS, getMessageForFormattedPersonData(deletedPerson)); } @@ -574,7 +571,7 @@ private static String executeClearAddressBook() { * @return feedback display message for the operation result */ private static String executeListAllPersonsInAddressBook() { - ArrayList toBeDisplayed = getAllPersonsInAddressBook(); + ArrayList> toBeDisplayed = getAllPersonsInAddressBook(); showToUser(toBeDisplayed); return getMessageForPersonsDisplayedSummary(toBeDisplayed); } @@ -629,7 +626,7 @@ private static void showToUser(String... message) { * The list will be indexed, starting from 1. * */ - private static void showToUser(ArrayList persons) { + private static void showToUser(ArrayList> persons) { String listAsString = getDisplayString(persons); showToUser(listAsString); updateLatestViewedPersonListing(persons); @@ -638,10 +635,10 @@ private static void showToUser(ArrayList persons) { /** * Returns the display string representation of the list of persons. */ - private static String getDisplayString(ArrayList persons) { + private static String getDisplayString(ArrayList> persons) { final StringBuilder messageAccumulator = new StringBuilder(); for (int i = 0; i < persons.size(); i++) { - final String[] person = persons.get(i); + final HashMap person = persons.get(i); final int displayIndex = i + DISPLAYED_INDEX_OFFSET; messageAccumulator.append('\t') .append(getIndexedPersonListElementMessage(displayIndex, person)) @@ -657,7 +654,7 @@ private static String getDisplayString(ArrayList persons) { * @param person to show * @return formatted listing message with index */ - private static String getIndexedPersonListElementMessage(int visibleIndex, String[] person) { + private static String getIndexedPersonListElementMessage(int visibleIndex, HashMap person) { return String.format(MESSAGE_DISPLAY_LIST_ELEMENT_INDEX, visibleIndex) + getMessageForFormattedPersonData(person); } @@ -667,7 +664,7 @@ private static String getIndexedPersonListElementMessage(int visibleIndex, Strin * @param person to show * @return formatted message showing internal state */ - private static String getMessageForFormattedPersonData(String[] person) { + private static String getMessageForFormattedPersonData(HashMap person) { return String.format(MESSAGE_DISPLAY_PERSON_DATA, getNameFromPerson(person), getPhoneFromPerson(person), getEmailFromPerson(person)); } @@ -677,7 +674,7 @@ private static String getMessageForFormattedPersonData(String[] person) { * * @param newListing the new listing of persons */ - private static void updateLatestViewedPersonListing(ArrayList newListing) { + private static void updateLatestViewedPersonListing(ArrayList> newListing) { // clone to insulate from future changes to arg list latestPersonListingView = new ArrayList<>(newListing); } @@ -688,7 +685,7 @@ private static void updateLatestViewedPersonListing(ArrayList newListi * @param lastVisibleIndex displayed index from last shown person listing * @return the actual person object in the last shown person listing */ - private static String[] getPersonByLastVisibleIndex(int lastVisibleIndex) { + private static HashMap getPersonByLastVisibleIndex(int lastVisibleIndex) { return latestPersonListingView.get(lastVisibleIndex - DISPLAYED_INDEX_OFFSET); } @@ -728,8 +725,8 @@ private static void createFileIfMissing(String filePath) { * @param filePath file to load from * @return the list of decoded persons */ - private static ArrayList loadPersonsFromFile(String filePath) { - final Optional> successfullyDecoded = decodePersonsFromStrings(getLinesInFile(filePath)); + private static ArrayList> loadPersonsFromFile(String filePath) { + final Optional>> successfullyDecoded = decodePersonsFromStrings(getLinesInFile(filePath)); if (!successfullyDecoded.isPresent()) { showToUser(MESSAGE_INVALID_STORAGE_FILE_CONTENT); exitProgram(); @@ -760,7 +757,7 @@ private static ArrayList getLinesInFile(String filePath) { * * @param filePath file for saving */ - private static void savePersonsToFile(ArrayList persons, String filePath) { + private static void savePersonsToFile(ArrayList> persons, String filePath) { final ArrayList linesToWrite = encodePersonsToStrings(persons); try { Files.write(Paths.get(storageFilePath), linesToWrite); @@ -782,7 +779,7 @@ private static void savePersonsToFile(ArrayList persons, String filePa * * @param person to add */ - private static void addPersonToAddressBook(String[] person) { + private static void addPersonToAddressBook(HashMap person) { ALL_PERSONS.add(person); savePersonsToFile(getAllPersonsInAddressBook(), storageFilePath); } @@ -793,7 +790,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 deletePersonFromAddressBook(HashMap exactPerson) { final boolean changed = ALL_PERSONS.remove(exactPerson); if (changed) { savePersonsToFile(getAllPersonsInAddressBook(), storageFilePath); @@ -804,7 +801,7 @@ private static boolean deletePersonFromAddressBook(String[] exactPerson) { /** * Returns all persons in the address book */ - private static ArrayList getAllPersonsInAddressBook() { + private static ArrayList> getAllPersonsInAddressBook() { return ALL_PERSONS; } @@ -821,7 +818,7 @@ private static void clearAddressBook() { * * @param persons list of persons to initialise the model with */ - private static void initialiseAddressBookModel(ArrayList persons) { + private static void initialiseAddressBookModel(ArrayList> persons) { ALL_PERSONS.clear(); ALL_PERSONS.addAll(persons); } @@ -838,8 +835,8 @@ private static void initialiseAddressBookModel(ArrayList persons) { * * @param person whose name you want */ - private static String getNameFromPerson(String[] person) { - return person[PERSON_DATA_INDEX_NAME]; + private static String getNameFromPerson(HashMap person) { + return person.get(PERSON_PROPERTY_NAME); } /** @@ -847,8 +844,8 @@ private static String getNameFromPerson(String[] person) { * * @param person whose phone number you want */ - private static String getPhoneFromPerson(String[] person) { - return person[PERSON_DATA_INDEX_PHONE]; + private static String getPhoneFromPerson(HashMap person) { + return person.get(PERSON_PROPERTY_PHONE); } /** @@ -856,8 +853,8 @@ private static String getPhoneFromPerson(String[] person) { * * @param person whose email you want */ - private static String getEmailFromPerson(String[] person) { - return person[PERSON_DATA_INDEX_EMAIL]; + private static String getEmailFromPerson(HashMap person) { + return person.get(PERSON_PROPERTY_EMAIL); } /** @@ -868,11 +865,11 @@ private static String getEmailFromPerson(String[] person) { * @param email without data prefix * @return constructed person */ - private static String[] makePersonFromData(String name, String phone, String email) { - final String[] person = new String[PERSON_DATA_COUNT]; - person[PERSON_DATA_INDEX_NAME] = name; - person[PERSON_DATA_INDEX_PHONE] = phone; - person[PERSON_DATA_INDEX_EMAIL] = email; + private static HashMap makePersonFromData(String name, String phone, String email) { + final HashMap person = new HashMap(); + person.put(PERSON_PROPERTY_NAME, name); + person.put(PERSON_PROPERTY_PHONE, phone); + person.put(PERSON_PROPERTY_EMAIL, email); return person; } @@ -882,7 +879,7 @@ private static String[] makePersonFromData(String name, String phone, String ema * @param person to be encoded * @return encoded string */ - private static String encodePersonToString(String[] person) { + private static String encodePersonToString(HashMap person) { return String.format(PERSON_STRING_REPRESENTATION, getNameFromPerson(person), getPhoneFromPerson(person), getEmailFromPerson(person)); } @@ -893,9 +890,9 @@ private static String encodePersonToString(String[] person) { * @param persons to be encoded * @return encoded strings */ - private static ArrayList encodePersonsToStrings(ArrayList persons) { + private static ArrayList encodePersonsToStrings(ArrayList> persons) { final ArrayList encoded = new ArrayList<>(); - for (String[] person : persons) { + for (HashMap person : persons) { encoded.add(encodePersonToString(person)); } return encoded; @@ -915,12 +912,12 @@ private static ArrayList encodePersonsToStrings(ArrayList pers * @return if cannot decode: empty Optional * else: Optional containing decoded person */ - private static Optional decodePersonFromString(String encoded) { + private static Optional> decodePersonFromString(String encoded) { // check that we can extract the parts of a person from the encoded string if (!isPersonDataExtractableFrom(encoded)) { return Optional.empty(); } - final String[] decodedPerson = makePersonFromData( + final HashMap decodedPerson = makePersonFromData( extractNameFromPersonString(encoded), extractPhoneFromPersonString(encoded), extractEmailFromPersonString(encoded) @@ -936,10 +933,10 @@ private static Optional decodePersonFromString(String encoded) { * @return if cannot decode any: empty Optional * else: Optional containing decoded persons */ - private static Optional> decodePersonsFromStrings(ArrayList encodedPersons) { - final ArrayList decodedPersons = new ArrayList<>(); + private static Optional>> decodePersonsFromStrings(ArrayList encodedPersons) { + final ArrayList> decodedPersons = new ArrayList<>(); for (String encodedPerson : encodedPersons) { - final Optional decodedPerson = decodePersonFromString(encodedPerson); + final Optional> decodedPerson = decodePersonFromString(encodedPerson); if (!decodedPerson.isPresent()) { return Optional.empty(); } @@ -1028,10 +1025,10 @@ private static String extractEmailFromPersonString(String encoded) { * * @param person String array representing the person (used in internal data) */ - private static boolean isPersonDataValid(String[] person) { - return isPersonNameValid(person[PERSON_DATA_INDEX_NAME]) - && isPersonPhoneValid(person[PERSON_DATA_INDEX_PHONE]) - && isPersonEmailValid(person[PERSON_DATA_INDEX_EMAIL]); + private static boolean isPersonDataValid(HashMap person) { + return isPersonNameValid(person.get(PERSON_PROPERTY_NAME)) + && isPersonPhoneValid(person.get(PERSON_PROPERTY_PHONE)) + && isPersonEmailValid(person.get(PERSON_PROPERTY_EMAIL)); } /* From 96b0060319d5bd1c61f01ab05cb18e9a862e3d6a Mon Sep 17 00:00:00 2001 From: "DESKTOP-9K2NI32\\Liu Yiming" Date: Fri, 9 Feb 2018 04:01:52 +0800 Subject: [PATCH 4/4] use enum for key of the hashmap --- src/seedu/addressbook/AddressBook.java | 110 ++++++++++++------------- 1 file changed, 54 insertions(+), 56 deletions(-) diff --git a/src/seedu/addressbook/AddressBook.java b/src/seedu/addressbook/AddressBook.java index 7e166cca..f82c1946 100644 --- a/src/seedu/addressbook/AddressBook.java +++ b/src/seedu/addressbook/AddressBook.java @@ -138,13 +138,11 @@ public class AddressBook { /* We use a HashMap to store details of a single person. - * The constants given below are the keys for the different data elements of a person - * used by the internal HashMap storage format. - * For example, a person's name is stored as the "name" element in the HashMap. + * The enum variable given below are the keys for the different data elements of a person + * used by the internal HashMap storage format. + * For example, a person's name is stored as the "PersonaProperty.NAME" element in the HashMap. */ - private static final String PERSON_PROPERTY_NAME = "name"; - private static final String PERSON_PROPERTY_PHONE = "phone"; - private static final String PERSON_PROPERTY_EMAIL = "email"; + private enum PersonProperty {NAME, EMAIL, PHONE}; /** * Offset required to convert between 1-indexing and 0-indexing.COMMAND_ @@ -177,14 +175,14 @@ public class AddressBook { /** * List of all persons in the address book. */ - private static final ArrayList> ALL_PERSONS = new ArrayList<>(); + 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 * those persons from this list. */ - private static ArrayList> latestPersonListingView = getAllPersonsInAddressBook(); + private static ArrayList> latestPersonListingView = getAllPersonsInAddressBook(); // initial view is of all /** @@ -414,7 +412,7 @@ private static String getMessageForInvalidCommandInput(String userCommand, Strin */ private static String executeAddPerson(String commandArgs) { // try decoding a person from the raw args - final Optional> decodeResult = decodePersonFromString(commandArgs); + final Optional> decodeResult = decodePersonFromString(commandArgs); // checks if args are valid (decode result will not be present if the person is invalid) if (!decodeResult.isPresent()) { @@ -422,7 +420,7 @@ private static String executeAddPerson(String commandArgs) { } // add the person as specified - final HashMap personToAdd = decodeResult.get(); + final HashMap personToAdd = decodeResult.get(); addPersonToAddressBook(personToAdd); return getMessageForSuccessfulAddPerson(personToAdd); } @@ -434,7 +432,7 @@ private static String executeAddPerson(String commandArgs) { * @param addedPerson person who was successfully added * @return successful add person feedback message */ - private static String getMessageForSuccessfulAddPerson(HashMap addedPerson) { + private static String getMessageForSuccessfulAddPerson(HashMap addedPerson) { return String.format(MESSAGE_ADDED, getNameFromPerson(addedPerson), getPhoneFromPerson(addedPerson), getEmailFromPerson(addedPerson)); } @@ -448,7 +446,7 @@ private static String getMessageForSuccessfulAddPerson(HashMap ad */ private static String executeFindPersons(String commandArgs) { final Set keywords = extractKeywordsFromFindPersonArgs(commandArgs); - final ArrayList> personsFound = getPersonsWithNameContainingAnyKeyword(keywords); + final ArrayList> personsFound = getPersonsWithNameContainingAnyKeyword(keywords); showToUser(personsFound); return getMessageForPersonsDisplayedSummary(personsFound); } @@ -459,7 +457,7 @@ private static String executeFindPersons(String commandArgs) { * @param personsDisplayed used to generate summary * @return summary message for persons displayed */ - private static String getMessageForPersonsDisplayedSummary(ArrayList> personsDisplayed) { + private static String getMessageForPersonsDisplayedSummary(ArrayList> personsDisplayed) { return String.format(MESSAGE_PERSONS_FOUND_OVERVIEW, personsDisplayed.size()); } @@ -479,9 +477,9 @@ private static Set extractKeywordsFromFindPersonArgs(String findPersonCo * @param keywords for searching * @return list of persons in full model with name containing some of the keywords */ - private static ArrayList> getPersonsWithNameContainingAnyKeyword(Collection keywords) { - final ArrayList> matchedPersons = new ArrayList<>(); - for (HashMap person : getAllPersonsInAddressBook()) { + private static ArrayList> getPersonsWithNameContainingAnyKeyword(Collection keywords) { + final ArrayList> matchedPersons = new ArrayList<>(); + for (HashMap person : getAllPersonsInAddressBook()) { final Set wordsInName = new HashSet<>(splitByWhitespace(getNameFromPerson(person))); if (!Collections.disjoint(wordsInName, keywords)) { matchedPersons.add(person); @@ -504,7 +502,7 @@ private static String executeDeletePerson(String commandArgs) { if (!isDisplayIndexValidForLastPersonListingView(targetVisibleIndex)) { return MESSAGE_INVALID_PERSON_DISPLAYED_INDEX; } - final HashMap targetInModel = getPersonByLastVisibleIndex(targetVisibleIndex); + final HashMap targetInModel = getPersonByLastVisibleIndex(targetVisibleIndex); return deletePersonFromAddressBook(targetInModel) ? getMessageForSuccessfulDelete(targetInModel) // success : MESSAGE_PERSON_NOT_IN_ADDRESSBOOK; // not found } @@ -551,7 +549,7 @@ private static boolean isDisplayIndexValidForLastPersonListingView(int index) { * @param deletedPerson successfully deleted * @return successful delete person feedback message */ - private static String getMessageForSuccessfulDelete(HashMap deletedPerson) { + private static String getMessageForSuccessfulDelete(HashMap deletedPerson) { return String.format(MESSAGE_DELETE_PERSON_SUCCESS, getMessageForFormattedPersonData(deletedPerson)); } @@ -571,7 +569,7 @@ private static String executeClearAddressBook() { * @return feedback display message for the operation result */ private static String executeListAllPersonsInAddressBook() { - ArrayList> toBeDisplayed = getAllPersonsInAddressBook(); + ArrayList> toBeDisplayed = getAllPersonsInAddressBook(); showToUser(toBeDisplayed); return getMessageForPersonsDisplayedSummary(toBeDisplayed); } @@ -626,7 +624,7 @@ private static void showToUser(String... message) { * The list will be indexed, starting from 1. * */ - private static void showToUser(ArrayList> persons) { + private static void showToUser(ArrayList> persons) { String listAsString = getDisplayString(persons); showToUser(listAsString); updateLatestViewedPersonListing(persons); @@ -635,10 +633,10 @@ private static void showToUser(ArrayList> persons) { /** * Returns the display string representation of the list of persons. */ - private static String getDisplayString(ArrayList> persons) { + private static String getDisplayString(ArrayList> persons) { final StringBuilder messageAccumulator = new StringBuilder(); for (int i = 0; i < persons.size(); i++) { - final HashMap person = persons.get(i); + final HashMap person = persons.get(i); final int displayIndex = i + DISPLAYED_INDEX_OFFSET; messageAccumulator.append('\t') .append(getIndexedPersonListElementMessage(displayIndex, person)) @@ -654,7 +652,7 @@ private static String getDisplayString(ArrayList> persons * @param person to show * @return formatted listing message with index */ - private static String getIndexedPersonListElementMessage(int visibleIndex, HashMap person) { + private static String getIndexedPersonListElementMessage(int visibleIndex, HashMap person) { return String.format(MESSAGE_DISPLAY_LIST_ELEMENT_INDEX, visibleIndex) + getMessageForFormattedPersonData(person); } @@ -664,7 +662,7 @@ private static String getIndexedPersonListElementMessage(int visibleIndex, HashM * @param person to show * @return formatted message showing internal state */ - private static String getMessageForFormattedPersonData(HashMap person) { + private static String getMessageForFormattedPersonData(HashMap person) { return String.format(MESSAGE_DISPLAY_PERSON_DATA, getNameFromPerson(person), getPhoneFromPerson(person), getEmailFromPerson(person)); } @@ -674,7 +672,7 @@ private static String getMessageForFormattedPersonData(HashMap pe * * @param newListing the new listing of persons */ - private static void updateLatestViewedPersonListing(ArrayList> newListing) { + private static void updateLatestViewedPersonListing(ArrayList> newListing) { // clone to insulate from future changes to arg list latestPersonListingView = new ArrayList<>(newListing); } @@ -685,7 +683,7 @@ private static void updateLatestViewedPersonListing(ArrayList getPersonByLastVisibleIndex(int lastVisibleIndex) { + private static HashMap getPersonByLastVisibleIndex(int lastVisibleIndex) { return latestPersonListingView.get(lastVisibleIndex - DISPLAYED_INDEX_OFFSET); } @@ -725,8 +723,8 @@ private static void createFileIfMissing(String filePath) { * @param filePath file to load from * @return the list of decoded persons */ - private static ArrayList> loadPersonsFromFile(String filePath) { - final Optional>> successfullyDecoded = decodePersonsFromStrings(getLinesInFile(filePath)); + private static ArrayList> loadPersonsFromFile(String filePath) { + final Optional>> successfullyDecoded = decodePersonsFromStrings(getLinesInFile(filePath)); if (!successfullyDecoded.isPresent()) { showToUser(MESSAGE_INVALID_STORAGE_FILE_CONTENT); exitProgram(); @@ -757,7 +755,7 @@ private static ArrayList getLinesInFile(String filePath) { * * @param filePath file for saving */ - private static void savePersonsToFile(ArrayList> persons, String filePath) { + private static void savePersonsToFile(ArrayList> persons, String filePath) { final ArrayList linesToWrite = encodePersonsToStrings(persons); try { Files.write(Paths.get(storageFilePath), linesToWrite); @@ -779,7 +777,7 @@ private static void savePersonsToFile(ArrayList> persons, * * @param person to add */ - private static void addPersonToAddressBook(HashMap person) { + private static void addPersonToAddressBook(HashMap person) { ALL_PERSONS.add(person); savePersonsToFile(getAllPersonsInAddressBook(), storageFilePath); } @@ -790,7 +788,7 @@ private static void addPersonToAddressBook(HashMap 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(HashMap exactPerson) { + private static boolean deletePersonFromAddressBook(HashMap exactPerson) { final boolean changed = ALL_PERSONS.remove(exactPerson); if (changed) { savePersonsToFile(getAllPersonsInAddressBook(), storageFilePath); @@ -801,7 +799,7 @@ private static boolean deletePersonFromAddressBook(HashMap exactP /** * Returns all persons in the address book */ - private static ArrayList> getAllPersonsInAddressBook() { + private static ArrayList> getAllPersonsInAddressBook() { return ALL_PERSONS; } @@ -818,7 +816,7 @@ private static void clearAddressBook() { * * @param persons list of persons to initialise the model with */ - private static void initialiseAddressBookModel(ArrayList> persons) { + private static void initialiseAddressBookModel(ArrayList> persons) { ALL_PERSONS.clear(); ALL_PERSONS.addAll(persons); } @@ -835,8 +833,8 @@ private static void initialiseAddressBookModel(ArrayList> * * @param person whose name you want */ - private static String getNameFromPerson(HashMap person) { - return person.get(PERSON_PROPERTY_NAME); + private static String getNameFromPerson(HashMap person) { + return person.get(PersonProperty.NAME); } /** @@ -844,8 +842,8 @@ private static String getNameFromPerson(HashMap person) { * * @param person whose phone number you want */ - private static String getPhoneFromPerson(HashMap person) { - return person.get(PERSON_PROPERTY_PHONE); + private static String getPhoneFromPerson(HashMap person) { + return person.get(PersonProperty.PHONE); } /** @@ -853,8 +851,8 @@ private static String getPhoneFromPerson(HashMap person) { * * @param person whose email you want */ - private static String getEmailFromPerson(HashMap person) { - return person.get(PERSON_PROPERTY_EMAIL); + private static String getEmailFromPerson(HashMap person) { + return person.get(PersonProperty.EMAIL); } /** @@ -865,11 +863,11 @@ private static String getEmailFromPerson(HashMap person) { * @param email without data prefix * @return constructed person */ - private static HashMap makePersonFromData(String name, String phone, String email) { - final HashMap person = new HashMap(); - person.put(PERSON_PROPERTY_NAME, name); - person.put(PERSON_PROPERTY_PHONE, phone); - person.put(PERSON_PROPERTY_EMAIL, email); + private static HashMap makePersonFromData(String name, String phone, String email) { + final HashMap person = new HashMap(); + person.put(PersonProperty.NAME, name); + person.put(PersonProperty.PHONE, phone); + person.put(PersonProperty.EMAIL, email); return person; } @@ -879,7 +877,7 @@ private static HashMap makePersonFromData(String name, String pho * @param person to be encoded * @return encoded string */ - private static String encodePersonToString(HashMap person) { + private static String encodePersonToString(HashMap person) { return String.format(PERSON_STRING_REPRESENTATION, getNameFromPerson(person), getPhoneFromPerson(person), getEmailFromPerson(person)); } @@ -890,9 +888,9 @@ private static String encodePersonToString(HashMap person) { * @param persons to be encoded * @return encoded strings */ - private static ArrayList encodePersonsToStrings(ArrayList> persons) { + private static ArrayList encodePersonsToStrings(ArrayList> persons) { final ArrayList encoded = new ArrayList<>(); - for (HashMap person : persons) { + for (HashMap person : persons) { encoded.add(encodePersonToString(person)); } return encoded; @@ -912,12 +910,12 @@ private static ArrayList encodePersonsToStrings(ArrayList> decodePersonFromString(String encoded) { + private static Optional> decodePersonFromString(String encoded) { // check that we can extract the parts of a person from the encoded string if (!isPersonDataExtractableFrom(encoded)) { return Optional.empty(); } - final HashMap decodedPerson = makePersonFromData( + final HashMap decodedPerson = makePersonFromData( extractNameFromPersonString(encoded), extractPhoneFromPersonString(encoded), extractEmailFromPersonString(encoded) @@ -933,10 +931,10 @@ private static Optional> decodePersonFromString(String en * @return if cannot decode any: empty Optional * else: Optional containing decoded persons */ - private static Optional>> decodePersonsFromStrings(ArrayList encodedPersons) { - final ArrayList> decodedPersons = new ArrayList<>(); + private static Optional>> decodePersonsFromStrings(ArrayList encodedPersons) { + final ArrayList> decodedPersons = new ArrayList<>(); for (String encodedPerson : encodedPersons) { - final Optional> decodedPerson = decodePersonFromString(encodedPerson); + final Optional> decodedPerson = decodePersonFromString(encodedPerson); if (!decodedPerson.isPresent()) { return Optional.empty(); } @@ -1025,10 +1023,10 @@ private static String extractEmailFromPersonString(String encoded) { * * @param person String array representing the person (used in internal data) */ - private static boolean isPersonDataValid(HashMap person) { - return isPersonNameValid(person.get(PERSON_PROPERTY_NAME)) - && isPersonPhoneValid(person.get(PERSON_PROPERTY_PHONE)) - && isPersonEmailValid(person.get(PERSON_PROPERTY_EMAIL)); + private static boolean isPersonDataValid(HashMap person) { + return isPersonNameValid(person.get(PersonProperty.NAME)) + && isPersonPhoneValid(person.get(PersonProperty.PHONE)) + && isPersonEmailValid(person.get(PersonProperty.EMAIL)); } /*