From a645e1f97629177c1c173a994943611cd764c7b7 Mon Sep 17 00:00:00 2001 From: leeyimin Date: Wed, 24 Aug 2016 01:23:14 +0800 Subject: [PATCH 1/7] Unindent main path in processProgramArgs --- .settings/org.eclipse.jdt.core.prefs | 1 + src/seedu/addressbook/AddressBook.java | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs index 3a215370..a698e596 100644 --- a/.settings/org.eclipse.jdt.core.prefs +++ b/.settings/org.eclipse.jdt.core.prefs @@ -1,5 +1,6 @@ eclipse.preferences.version=1 org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve org.eclipse.jdt.core.compiler.compliance=1.8 diff --git a/src/seedu/addressbook/AddressBook.java b/src/seedu/addressbook/AddressBook.java index a43b1690..92acab28 100644 --- a/src/seedu/addressbook/AddressBook.java +++ b/src/seedu/addressbook/AddressBook.java @@ -258,15 +258,15 @@ private static void processProgramArgs(String[] args) { if (args.length >= 2) { showToUser(MESSAGE_INVALID_PROGRAM_ARGS); exitProgram(); + return; } if (args.length == 1) { setupGivenFileForStorage(args[0]); + return; } - if(args.length == 0) { - setupDefaultFileForStorage(); - } + setupDefaultFileForStorage(); } /** From d8e85833a9257466aa8ba1cc30d047e2b5e31e1f Mon Sep 17 00:00:00 2001 From: leeyimin Date: Wed, 24 Aug 2016 01:27:14 +0800 Subject: [PATCH 2/7] Rename showResultToUser to showResultToUserWithDivider --- src/seedu/addressbook/AddressBook.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/seedu/addressbook/AddressBook.java b/src/seedu/addressbook/AddressBook.java index 92acab28..8cbf2490 100644 --- a/src/seedu/addressbook/AddressBook.java +++ b/src/seedu/addressbook/AddressBook.java @@ -206,7 +206,7 @@ public static void main(String[] args) { String userCommand = getUserInput(); echoUserCommand(userCommand); String feedback = executeCommand(userCommand); - showResultToUser(feedback); + showResultToUserWithDivider(feedback); } } @@ -221,7 +221,7 @@ private static void showWelcomeMessage() { showToUser(DIVIDER, DIVIDER, VERSION, MESSAGE_WELCOME, DIVIDER); } - private static void showResultToUser(String result) { + private static void showResultToUserWithDivider(String result) { showToUser(result, DIVIDER); } From d2def04123dc1230f03cce934f612584af1d6bf3 Mon Sep 17 00:00:00 2001 From: leeyimin Date: Wed, 24 Aug 2016 01:46:13 +0800 Subject: [PATCH 3/7] Add type parameter to arraylists created in getLinesInFile and splitByWhitespace --- src/seedu/addressbook/AddressBook.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/seedu/addressbook/AddressBook.java b/src/seedu/addressbook/AddressBook.java index 8cbf2490..5b3ce745 100644 --- a/src/seedu/addressbook/AddressBook.java +++ b/src/seedu/addressbook/AddressBook.java @@ -721,7 +721,7 @@ private static ArrayList loadPersonsFromFile(String filePath) { private static ArrayList getLinesInFile(String filePath) { ArrayList lines = null; try { - lines = new ArrayList(Files.readAllLines(Paths.get(filePath))); + lines = new ArrayList<>(Files.readAllLines(Paths.get(filePath))); } catch (FileNotFoundException fnfe) { showToUser(String.format(MESSAGE_ERROR_MISSING_STORAGE_FILE, filePath)); exitProgram(); @@ -1180,7 +1180,7 @@ private static String removePrefixSign(String s, String sign) { * @return split by whitespace */ private static ArrayList splitByWhitespace(String toSplit) { - return new ArrayList(Arrays.asList(toSplit.trim().split("\\s+"))); + return new ArrayList(Arrays.asList(toSplit.trim().split("\\s+"))); } } \ No newline at end of file From de1e735d5d24892cbdf51881f32abc46ce02c8a0 Mon Sep 17 00:00:00 2001 From: leeyimin Date: Wed, 24 Aug 2016 01:50:56 +0800 Subject: [PATCH 4/7] Remove deletePersonFromAddressBook(int index) --- src/seedu/addressbook/AddressBook.java | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/src/seedu/addressbook/AddressBook.java b/src/seedu/addressbook/AddressBook.java index 5b3ce745..3ff4ada3 100644 --- a/src/seedu/addressbook/AddressBook.java +++ b/src/seedu/addressbook/AddressBook.java @@ -765,17 +765,6 @@ private static void addPersonToAddressBook(String[] person) { savePersonsToFile(getAllPersonsInAddressBook(), storageFilePath); } - /** - * Deletes a person from the address book, target is identified by it's absolute index in the full list. - * Saves changes to storage file. - * - * @param index absolute index of person to delete (index within {@link #ALL_PERSONS}) - */ - private static void deletePersonFromAddressBook(int index) { - ALL_PERSONS.remove(index); - savePersonsToFile(getAllPersonsInAddressBook(), storageFilePath); - } - /** * Deletes the specified person from the addressbook if it is inside. Saves any changes to storage file. * From 2af04745aaf7d10c1ff74d96950b602905b25527 Mon Sep 17 00:00:00 2001 From: leeyimin Date: Wed, 24 Aug 2016 03:10:31 +0800 Subject: [PATCH 5/7] Add Person --- src/seedu/addressbook/AddressBook.java | 205 ++++++------------------- src/seedu/addressbook/Person.java | 90 +++++++++++ test/runtests.bat | 2 +- test/runtests.sh | 2 +- 4 files changed, 140 insertions(+), 159 deletions(-) create mode 100644 src/seedu/addressbook/Person.java diff --git a/src/seedu/addressbook/AddressBook.java b/src/seedu/addressbook/AddressBook.java index 3ff4ada3..ee029129 100644 --- a/src/seedu/addressbook/AddressBook.java +++ b/src/seedu/addressbook/AddressBook.java @@ -87,12 +87,10 @@ public class AddressBook { private static final String MESSAGE_USING_DEFAULT_FILE = "Using default storage file : " + DEFAULT_STORAGE_FILEPATH; // These are the prefix strings to define the data type of a command parameter - private static final String PERSON_DATA_PREFIX_PHONE = "p/"; - private static final String PERSON_DATA_PREFIX_EMAIL = "e/"; + protected static final String PERSON_DATA_PREFIX_PHONE = "p/"; + protected static final String PERSON_DATA_PREFIX_EMAIL = "e/"; + - private static final String PERSON_STRING_REPRESENTATION = "%1$s " // name - + PERSON_DATA_PREFIX_PHONE + "%2$s " // phone - + PERSON_DATA_PREFIX_EMAIL + "%3$s"; // email private static final String COMMAND_ADD_WORD = "add"; private static final String COMMAND_ADD_DESC = "Adds a person to the address book."; private static final String COMMAND_ADD_PARAMETERS = "NAME " @@ -131,20 +129,6 @@ 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. - */ - 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; - /** * Offset required to convert between 1-indexing and 0-indexing.COMMAND_ */ @@ -166,8 +150,8 @@ public class AddressBook { private static final Scanner SCANNER = new Scanner(System.in); /* * ==============NOTE TO STUDENTS====================================================================== - * Note that the type of the variable below can also be declared as List, as follows: - * private static final List ALL_PERSONS = new ArrayList<>() + * Note that the type of the variable below can also be declared as List, as follows: + * private static final List ALL_PERSONS = new ArrayList<>() * That is because List is an interface implemented by the ArrayList class. * In this code we use ArrayList instead because we wanted to to stay away from advanced concepts * such as interface inheritance. @@ -176,7 +160,7 @@ 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<>(); /** @@ -184,7 +168,7 @@ public class AddressBook { * 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. @@ -387,7 +371,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()) { @@ -395,7 +379,7 @@ private static String executeAddPerson(String commandArgs) { } // add the person as specified - final String[] personToAdd = decodeResult.get(); + final Person personToAdd = decodeResult.get(); addPersonToAddressBook(personToAdd); return getMessageForSuccessfulAddPerson(personToAdd); } @@ -407,9 +391,9 @@ 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(Person addedPerson) { return String.format(MESSAGE_ADDED, - getNameFromPerson(addedPerson), getPhoneFromPerson(addedPerson), getEmailFromPerson(addedPerson)); + addedPerson.getName(), addedPerson.getPhone(), addedPerson.getEmail()); } /** @@ -421,7 +405,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); } @@ -432,7 +416,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()); } @@ -452,10 +436,10 @@ 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()) { - final Set wordsInName = new HashSet<>(splitByWhitespace(getNameFromPerson(person))); + private static ArrayList getPersonsWithNameContainingAnyKeyword(Collection keywords) { + final ArrayList matchedPersons = new ArrayList<>(); + for (Person person : getAllPersonsInAddressBook()) { + final Set wordsInName = new HashSet<>(splitByWhitespace(person.getName())); if (!Collections.disjoint(wordsInName, keywords)) { matchedPersons.add(person); } @@ -477,7 +461,7 @@ private static String executeDeletePerson(String commandArgs) { if (!isDisplayIndexValidForLastPersonListingView(targetVisibleIndex)) { return MESSAGE_INVALID_PERSON_DISPLAYED_INDEX; } - final String[] targetInModel = getPersonByLastVisibleIndex(targetVisibleIndex); + final Person targetInModel = getPersonByLastVisibleIndex(targetVisibleIndex); return deletePersonFromAddressBook(targetInModel) ? getMessageForSuccessfulDelete(targetInModel) // success : MESSAGE_PERSON_NOT_IN_ADDRESSBOOK; // not found } @@ -524,7 +508,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(Person deletedPerson) { return String.format(MESSAGE_DELETE_PERSON_SUCCESS, getMessageForFormattedPersonData(deletedPerson)); } @@ -544,7 +528,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); } @@ -599,7 +583,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); @@ -608,10 +592,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 Person person = persons.get(i); final int displayIndex = i + DISPLAYED_INDEX_OFFSET; messageAccumulator.append('\t') .append(getIndexedPersonListElementMessage(displayIndex, person)) @@ -627,7 +611,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, Person person) { return String.format(MESSAGE_DISPLAY_LIST_ELEMENT_INDEX, visibleIndex) + getMessageForFormattedPersonData(person); } @@ -637,9 +621,9 @@ 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(Person person) { return String.format(MESSAGE_DISPLAY_PERSON_DATA, - getNameFromPerson(person), getPhoneFromPerson(person), getEmailFromPerson(person)); + person.getName(), person.getPhone(), person.getEmail()); } /** @@ -647,7 +631,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); } @@ -658,14 +642,14 @@ 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 Person getPersonByLastVisibleIndex(int lastVisibleIndex) { return latestPersonListingView.get(lastVisibleIndex - DISPLAYED_INDEX_OFFSET); } /** * @return unmodifiable list view of the last person listing view */ - private static ArrayList getLatestPersonListingView() { + private static ArrayList getLatestPersonListingView() { return latestPersonListingView; } @@ -705,8 +689,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(); @@ -738,7 +722,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); @@ -760,7 +744,7 @@ private static void savePersonsToFile(ArrayList persons, String filePa * * @param person to add */ - private static void addPersonToAddressBook(String[] person) { + private static void addPersonToAddressBook(Person person) { ALL_PERSONS.add(person); savePersonsToFile(getAllPersonsInAddressBook(), storageFilePath); } @@ -771,7 +755,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(Person exactPerson) { final boolean changed = ALL_PERSONS.remove(exactPerson); if (changed) { savePersonsToFile(getAllPersonsInAddressBook(), storageFilePath); @@ -782,7 +766,7 @@ private static boolean deletePersonFromAddressBook(String[] exactPerson) { /** * @return unmodifiable list view of all persons in the address book */ - private static ArrayList getAllPersonsInAddressBook() { + private static ArrayList getAllPersonsInAddressBook() { return ALL_PERSONS; } @@ -799,7 +783,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); } @@ -811,55 +795,14 @@ private static void initialiseAddressBookModel(ArrayList persons) { * =========================================== */ - /** - * @param person whose name you want - * @return person's name - */ - private static String getNameFromPerson(String[] person) { - return person[PERSON_DATA_INDEX_NAME]; - } - - /** - * @param person whose phone number you want - * @return person's phone number - */ - private static String getPhoneFromPerson(String[] person) { - return person[PERSON_DATA_INDEX_PHONE]; - } - - /** - * @param person whose email you want - * @return person's email - */ - private static String getEmailFromPerson(String[] person) { - return person[PERSON_DATA_INDEX_EMAIL]; - } - - /** - * Create a person for use in the internal data. - * - * @param name of person - * @param phone without data prefix - * @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; - return person; - } - /** * Encodes a person into a decodable and readable string representation. * * @param person to be encoded * @return encoded string */ - private static String encodePersonToString(String[] person) { - return String.format(PERSON_STRING_REPRESENTATION, - getNameFromPerson(person), getPhoneFromPerson(person), getEmailFromPerson(person)); + private static String encodePersonToString(Person person) { + return person.toString(); } /** @@ -868,9 +811,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 (Person person : persons) { encoded.add(encodePersonToString(person)); } return encoded; @@ -889,18 +832,18 @@ 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 Person decodedPerson = new Person( extractNameFromPersonString(encoded), extractPhoneFromPersonString(encoded), extractEmailFromPersonString(encoded) ); // check that the constructed person is valid - return isPersonDataValid(decodedPerson) ? Optional.of(decodedPerson) : Optional.empty(); + return decodedPerson.isValid() ? Optional.of(decodedPerson) : Optional.empty(); } /** @@ -910,10 +853,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(); } @@ -998,58 +941,6 @@ private static String extractEmailFromPersonString(String encoded) { } } - /** - * Validates a person's data fields - * - * @param person String array representing the person (used in internal data) - * @return whether the given person has valid 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]); - } - - /* - * ==============NOTE TO STUDENTS====================================== - * Note the use of 'regular expressions' in the method below. - * Regular expressions can be very useful in checking if a a string - * follows a sepcific format. - * ==================================================================== - */ - /** - * Validates string as a legal person name - * - * @param name to be validated - * @return whether arg is a valid person name - */ - private static boolean isPersonNameValid(String name) { - return name.matches("(\\w|\\s)+"); // name is nonempty mixture of alphabets and whitespace - //TODO: implement a more permissive validation - } - - /** - * Validates string as a legal person phone number - * - * @param phone to be validated - * @return whether arg is a valid person phone number - */ - private static boolean isPersonPhoneValid(String phone) { - return phone.matches("\\d+"); // phone nonempty sequence of digits - //TODO: implement a more permissive validation - } - - /** - * Validates string as a legal person email - * - * @param email to be validated - * @return whether arg is a valid person email - */ - private static boolean isPersonEmailValid(String email) { - return email.matches("\\S+@\\S+\\.\\S+"); // email is [non-whitespace]@[non-whitespace].[non-whitespace] - //TODO: implement a more permissive validation - } - /* * =============================================== @@ -1171,5 +1062,5 @@ private static String removePrefixSign(String s, String sign) { private static ArrayList splitByWhitespace(String toSplit) { return new ArrayList(Arrays.asList(toSplit.trim().split("\\s+"))); } - -} \ No newline at end of file + +} diff --git a/src/seedu/addressbook/Person.java b/src/seedu/addressbook/Person.java new file mode 100644 index 00000000..4a10bbf5 --- /dev/null +++ b/src/seedu/addressbook/Person.java @@ -0,0 +1,90 @@ +/** + * + */ +package seedu.addressbook; + +/** + * @author YiMin + * + */ +public class Person { + private static final String PERSON_STRING_REPRESENTATION = "%1$s " // name + + AddressBook.PERSON_DATA_PREFIX_PHONE + "%2$s " // phone + + AddressBook.PERSON_DATA_PREFIX_EMAIL + "%3$s"; // email + + private final String name_; + private final String email_; + private final String phone_; + + public Person(String name, String phone, String email){ + name_=name; + phone_=phone; + email_=email; + } + + /** + * @return the name_ + */ + public String getName() { + return name_; + } + + /** + * @return the email_ + */ + public String getEmail() { + return email_; + } + + /** + * @return the phone_ + */ + public String getPhone() { + return phone_; + } + + /** + * Validates the person's data fields + * + * @return whether the person has valid data + */ + public boolean isValid() { + return isNameValid() && isPhoneValid() && isEmailValid(); + } + + /** + * Validates name as a legal person name + * + * @return whether name is a valid person name + */ + private boolean isNameValid() { + return name_.matches("(\\w|\\s)+"); // name is nonempty mixture of alphabets and whitespace + //TODO: implement a more permissive validation + } + + /** + * Validates phone as a legal person phone number + *= + * @return whether phone is a valid person phone number + */ + private boolean isPhoneValid() { + return phone_.matches("\\d+"); // phone nonempty sequence of digits + //TODO: implement a more permissive validation + } + + /** + * Validates email as a legal person email + * + * @return whether email is a valid person email + */ + private boolean isEmailValid() { + return email_.matches("\\S+@\\S+\\.\\S+"); // email is [non-whitespace]@[non-whitespace].[non-whitespace] + //TODO: implement a more permissive validation + } + + @Override + public String toString(){ + return String.format(PERSON_STRING_REPRESENTATION, + name_,email_,phone_); + } +} diff --git a/test/runtests.bat b/test/runtests.bat index 2ee2491c..6761cd3b 100644 --- a/test/runtests.bat +++ b/test/runtests.bat @@ -1,6 +1,6 @@ @ECHO OFF REM compile the code into the bin folder -javac ..\src\seedu\addressbook\Addressbook.java -d ..\bin +javac ..\src\seedu\addressbook\Addressbook.java ..\src\seedu\addressbook\Person.java -d ..\bin REM run the program, feed commands from input.txt file and redirect the output to the actual.txt java -classpath ..\bin seedu.addressbook.AddressBook < input.txt > actual.txt diff --git a/test/runtests.sh b/test/runtests.sh index b78e7dee..93e93c59 100755 --- a/test/runtests.sh +++ b/test/runtests.sh @@ -10,7 +10,7 @@ then fi # compile the code into the bin folder -javac ../src/seedu/addressbook/AddressBook.java -d ../bin +javac ../src/seedu/addressbook/AddressBook.java ../src/seedu/addressbook/Person.java -d ../bin # run the program, feed commands from input.txt file and redirect the output to the actual.txt java -classpath ../bin seedu.addressbook.AddressBook < input.txt > actual.txt From 73830b43515f001b8cf0bc59fdcd01301acd4b37 Mon Sep 17 00:00:00 2001 From: leeyimin Date: Wed, 24 Aug 2016 03:14:00 +0800 Subject: [PATCH 6/7] Inline processProgramArgs --- src/seedu/addressbook/AddressBook.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/seedu/addressbook/AddressBook.java b/src/seedu/addressbook/AddressBook.java index ee029129..21dbabe7 100644 --- a/src/seedu/addressbook/AddressBook.java +++ b/src/seedu/addressbook/AddressBook.java @@ -184,7 +184,16 @@ public class AddressBook { */ public static void main(String[] args) { showWelcomeMessage(); - processProgramArgs(args); + + if (args.length >= 2) { + showToUser(MESSAGE_INVALID_PROGRAM_ARGS); + exitProgram(); + } + if (args.length == 1) { + setupGivenFileForStorage(args[0]); + } + setupDefaultFileForStorage(); + loadDataFromStorage(); while (true) { String userCommand = getUserInput(); From 2b28585de37776909d1c9b9aeb161e4f45d555e7 Mon Sep 17 00:00:00 2001 From: leeyimin Date: Wed, 24 Aug 2016 03:19:25 +0800 Subject: [PATCH 7/7] Lower code quality of setupDefaultFileForStorage --- src/seedu/addressbook/AddressBook.java | 29 ++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/src/seedu/addressbook/AddressBook.java b/src/seedu/addressbook/AddressBook.java index 21dbabe7..15da3733 100644 --- a/src/seedu/addressbook/AddressBook.java +++ b/src/seedu/addressbook/AddressBook.java @@ -292,9 +292,34 @@ private static void exitProgram() { * Exits program if the file cannot be created. */ private static void setupDefaultFileForStorage() { - showToUser(MESSAGE_USING_DEFAULT_FILE); + String[] message1 = { MESSAGE_USING_DEFAULT_FILE }; + for (String m1 : message1) { + System.out.println(LINE_PREFIX + m1); + } storageFilePath = DEFAULT_STORAGE_FILEPATH; - createFileIfMissing(storageFilePath); + String filePath = storageFilePath; + final File storageFile = new File(filePath); + if (storageFile.exists()) return; + String[] message = { String.format(MESSAGE_ERROR_MISSING_STORAGE_FILE, filePath) }; + + for (String m : message) { + System.out.println(LINE_PREFIX + m); + } + + try { + storageFile.createNewFile(); + String[] message2 = { String.format(MESSAGE_STORAGE_FILE_CREATED, filePath) }; + for (String m2 : message2) { + System.out.println(LINE_PREFIX + m2); + } + } catch (IOException ioe) { + String[] message2 = { String.format(MESSAGE_ERROR_CREATING_STORAGE_FILE, filePath) }; + for (String m2 : message2) { + System.out.println(LINE_PREFIX + m2); + } + showToUser(MESSAGE_GOODBYE, DIVIDER, DIVIDER); + System.exit(0); + } } /**