From 13a0c6cf80b3437ba1ffebcb004a40eb0617bf3f Mon Sep 17 00:00:00 2001 From: Thaddeus Lim Date: Thu, 13 Sep 2018 09:01:28 +0800 Subject: [PATCH 01/10] Updated User Guide Updated User Guide on Count Command --- docs/UserGuide.adoc | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/docs/UserGuide.adoc b/docs/UserGuide.adoc index 4abb17e3e..986793382 100644 --- a/docs/UserGuide.adoc +++ b/docs/UserGuide.adoc @@ -156,6 +156,11 @@ Views all details of the 1st person in the results of the `find` command. Clears all entries from the address book. + Format: `clear` +== Counting total number of persons in the address book : `count` +Counts total number of persons in the address book. +Format: `count` + + == Exiting the program : `exit` Exits the program. + From 788a0b8c71dab22f8ea5d238d3baf0b8fe72d0e9 Mon Sep 17 00:00:00 2001 From: Thaddeus Lim Date: Thu, 13 Sep 2018 09:01:59 +0800 Subject: [PATCH 02/10] Added Command(Count) Added new command 'count' under commands --- src/seedu/addressbook/commands/CountCommand.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 src/seedu/addressbook/commands/CountCommand.java diff --git a/src/seedu/addressbook/commands/CountCommand.java b/src/seedu/addressbook/commands/CountCommand.java new file mode 100644 index 000000000..af5456055 --- /dev/null +++ b/src/seedu/addressbook/commands/CountCommand.java @@ -0,0 +1,15 @@ +package seedu.addressbook.commands; +/** + * Shows total number of persons in address book. + */ +public class CountCommand extends Command { + public static final String COMMAND_WORD = "count"; + public static final String MESSAGE_USAGE = COMMAND_WORD + ": Counts the total number of persons " + + "in the address book.\n" + + "Example: " + COMMAND_WORD; + public static final String MESSAGE_COUNTED = "Total number of persons in your address book is: %1$s"; + @Override + public CommandResult execute() { + return new CommandResult(String.format(MESSAGE_COUNTED, addressBook.getSumPersons())); + } +} \ No newline at end of file From c692f3fe2524fbbdf851badc0ba1e08e3a671a73 Mon Sep 17 00:00:00 2001 From: Thaddeus Lim Date: Thu, 13 Sep 2018 09:02:34 +0800 Subject: [PATCH 03/10] Updated Help Command Added new help command for 'count' --- src/seedu/addressbook/commands/HelpCommand.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/seedu/addressbook/commands/HelpCommand.java b/src/seedu/addressbook/commands/HelpCommand.java index 9be217d89..dde380a10 100644 --- a/src/seedu/addressbook/commands/HelpCommand.java +++ b/src/seedu/addressbook/commands/HelpCommand.java @@ -21,6 +21,7 @@ public CommandResult execute() { + "\n" + ListCommand.MESSAGE_USAGE + "\n" + ViewCommand.MESSAGE_USAGE + "\n" + ViewAllCommand.MESSAGE_USAGE + + "\n" + CountCommand.MESSAGE_USAGE + "\n" + HelpCommand.MESSAGE_USAGE + "\n" + ExitCommand.MESSAGE_USAGE ); From b251f4ef48690a7b3761a5db4ccac56735c2aaaa Mon Sep 17 00:00:00 2001 From: Thaddeus Lim Date: Thu, 13 Sep 2018 09:03:12 +0800 Subject: [PATCH 04/10] Updated AddressBook data Added new Field 'sumPersons' to keep track of total person count --- src/seedu/addressbook/data/AddressBook.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/seedu/addressbook/data/AddressBook.java b/src/seedu/addressbook/data/AddressBook.java index 537d35c89..0b9d347bf 100644 --- a/src/seedu/addressbook/data/AddressBook.java +++ b/src/seedu/addressbook/data/AddressBook.java @@ -12,6 +12,7 @@ public class AddressBook { private final UniquePersonList allPersons; + private int sumPersons = 0; /** * Creates an empty address book. @@ -36,6 +37,7 @@ public AddressBook(UniquePersonList persons) { */ public void addPerson(Person toAdd) throws DuplicatePersonException { allPersons.add(toAdd); + sumPersons++; } /** @@ -52,6 +54,7 @@ public boolean containsPerson(ReadOnlyPerson key) { */ public void removePerson(ReadOnlyPerson toRemove) throws PersonNotFoundException { allPersons.remove(toRemove); + sumPersons--; } /** @@ -59,6 +62,7 @@ public void removePerson(ReadOnlyPerson toRemove) throws PersonNotFoundException */ public void clear() { allPersons.clear(); + sumPersons = 0; } /** @@ -66,6 +70,12 @@ public void clear() { */ public UniquePersonList getAllPersons() { return new UniquePersonList(allPersons); + + } + + public int getSumPersons() { + return this.sumPersons; + } @Override From 2ce28767a9b18f23219ab3e684cbe403677c0d0c Mon Sep 17 00:00:00 2001 From: Thaddeus Lim Date: Thu, 13 Sep 2018 09:06:38 +0800 Subject: [PATCH 05/10] Updated Parser (Count) Imported CountCommand --- src/seedu/addressbook/parser/Parser.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/seedu/addressbook/parser/Parser.java b/src/seedu/addressbook/parser/Parser.java index abddb3f45..cd77f7f10 100644 --- a/src/seedu/addressbook/parser/Parser.java +++ b/src/seedu/addressbook/parser/Parser.java @@ -22,6 +22,7 @@ import seedu.addressbook.commands.ListCommand; import seedu.addressbook.commands.ViewAllCommand; import seedu.addressbook.commands.ViewCommand; +import seedu.addressbook.commands.CountCommand; import seedu.addressbook.data.exception.IllegalValueException; /** @@ -94,6 +95,9 @@ public Command parseCommand(String userInput) { case ViewAllCommand.COMMAND_WORD: return prepareViewAll(arguments); + case CountCommand.COMMAND_WORD: + return new CountCommand(); + case ExitCommand.COMMAND_WORD: return new ExitCommand(); From bb36418b925f6ba519ebed808e768d140ef71a83 Mon Sep 17 00:00:00 2001 From: Thaddeus Lim Date: Thu, 13 Sep 2018 09:07:16 +0800 Subject: [PATCH 06/10] Updated tests(Count) Updated tests to test for the new Count method. --- test/expected.txt | 2 ++ test/java/seedu/addressbook/common/UtilsTest.java | 7 +++++++ test/java/seedu/addressbook/data/AddressBookTest.java | 7 +++++++ 3 files changed, 16 insertions(+) diff --git a/test/expected.txt b/test/expected.txt index 56fe5fcac..f4b970679 100644 --- a/test/expected.txt +++ b/test/expected.txt @@ -25,6 +25,8 @@ || viewall: Views the non-private details of the person identified by the index number in the last shown person listing. || Parameters: INDEX || Example: viewall 1 +|| count: Counts the total number of persons in the address book. +|| Example: count || help: Shows program usage instructions. || Example: help || exit: Exits the program. diff --git a/test/java/seedu/addressbook/common/UtilsTest.java b/test/java/seedu/addressbook/common/UtilsTest.java index 23ea62b45..025ad4ee0 100644 --- a/test/java/seedu/addressbook/common/UtilsTest.java +++ b/test/java/seedu/addressbook/common/UtilsTest.java @@ -36,6 +36,13 @@ public void elementsAreUnique() throws Exception { assertNotUnique(null, "a", "b", null); } + @Test + public void isAnyNull() throws Exception { + assertFalse(Utils.isAnyNull()); + assertFalse(Utils.isAnyNull(1, 2, 3)); + assertTrue(Utils.isAnyNull(1, null, 2, 3)); + } + private void assertAreUnique(Object... objects) { assertTrue(Utils.elementsAreUnique(Arrays.asList(objects))); } diff --git a/test/java/seedu/addressbook/data/AddressBookTest.java b/test/java/seedu/addressbook/data/AddressBookTest.java index 193229774..cb649968a 100644 --- a/test/java/seedu/addressbook/data/AddressBookTest.java +++ b/test/java/seedu/addressbook/data/AddressBookTest.java @@ -135,4 +135,11 @@ public void getAllPersons() throws Exception { assertTrue(isIdentical(allPersons, personsToCheck)); } + + @Test + public void count() { + assertTrue(defaultAddressBook.getSumPersons() == 2); + defaultAddressBook.clear(); + assertTrue(defaultAddressBook.getSumPersons()) == 0; + } } From 227bbff266a48e57b06f48c202adf4dbe78ba44b Mon Sep 17 00:00:00 2001 From: Thaddeus Lim Date: Thu, 13 Sep 2018 09:44:58 +0800 Subject: [PATCH 07/10] Corrected a code error Corrected a syntax error at line 143 --- test/java/seedu/addressbook/data/AddressBookTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/java/seedu/addressbook/data/AddressBookTest.java b/test/java/seedu/addressbook/data/AddressBookTest.java index cb649968a..e459f5cb4 100644 --- a/test/java/seedu/addressbook/data/AddressBookTest.java +++ b/test/java/seedu/addressbook/data/AddressBookTest.java @@ -140,6 +140,6 @@ public void getAllPersons() throws Exception { public void count() { assertTrue(defaultAddressBook.getSumPersons() == 2); defaultAddressBook.clear(); - assertTrue(defaultAddressBook.getSumPersons()) == 0; + assertTrue(defaultAddressBook.getSumPersons() == 0); } } From 6ecc154ac14e94009be792647cc56b6b511ffecd Mon Sep 17 00:00:00 2001 From: Thaddeus Lim Date: Thu, 13 Sep 2018 11:49:08 +0800 Subject: [PATCH 08/10] Minor Changes Small changes in attempt to pass Travis test --- src/seedu/addressbook/commands/CountCommand.java | 8 ++++++-- src/seedu/addressbook/data/AddressBook.java | 16 ++++++++++++++++ test/expected.txt | 3 +++ test/input.txt | 16 ++++++++++++++++ .../java/seedu/addressbook/common/UtilsTest.java | 1 + 5 files changed, 42 insertions(+), 2 deletions(-) diff --git a/src/seedu/addressbook/commands/CountCommand.java b/src/seedu/addressbook/commands/CountCommand.java index af5456055..826fcf229 100644 --- a/src/seedu/addressbook/commands/CountCommand.java +++ b/src/seedu/addressbook/commands/CountCommand.java @@ -3,13 +3,17 @@ * Shows total number of persons in address book. */ public class CountCommand extends Command { + public static final String COMMAND_WORD = "count"; - public static final String MESSAGE_USAGE = COMMAND_WORD + ": Counts the total number of persons " + + public static final String MESSAGE_USAGE = COMMAND_WORD + ": Counts the total number of contacts " + "in the address book.\n" + "Example: " + COMMAND_WORD; - public static final String MESSAGE_COUNTED = "Total number of persons in your address book is: %1$s"; + public static final String MESSAGE_COUNTED = "Total number of persons in address book: %1$s"; + @Override public CommandResult execute() { return new CommandResult(String.format(MESSAGE_COUNTED, addressBook.getSumPersons())); } + } \ No newline at end of file diff --git a/src/seedu/addressbook/data/AddressBook.java b/src/seedu/addressbook/data/AddressBook.java index 0b9d347bf..e757a99be 100644 --- a/src/seedu/addressbook/data/AddressBook.java +++ b/src/seedu/addressbook/data/AddressBook.java @@ -12,6 +12,7 @@ public class AddressBook { private final UniquePersonList allPersons; + private int sumPersons = 0; /** @@ -27,9 +28,24 @@ public AddressBook() { * @param persons external changes to this will not affect this address book */ public AddressBook(UniquePersonList persons) { + sumPersons = getSize(persons); this.allPersons = new UniquePersonList(persons); } + /** + * Returns the number of elements in the container behind an iterable. + * + * + */ + public static int getSize(Iterable persons) { + int sumElements = 0; + for (T elem : persons) { + sumElements++; + } + return sumElements; + } + + /** * Adds a person to the address book. * diff --git a/test/expected.txt b/test/expected.txt index f4b970679..7e3421d3d 100644 --- a/test/expected.txt +++ b/test/expected.txt @@ -235,6 +235,9 @@ || || 2 persons listed! || =================================================== +|| Enter command: || [Command entered: count] +|| There are 5 people in the address book. +|| =================================================== || Enter command: || [Command entered: delete] || Invalid command format! || delete: Deletes the person identified by the index number used in the last person listing. diff --git a/test/input.txt b/test/input.txt index eb8df81f8..dc036f7fb 100644 --- a/test/input.txt +++ b/test/input.txt @@ -1,3 +1,10 @@ +########################################################## +# count number of persons before anyone is added +########################################################## + + # should be 0 + count + ########################################################## # invalid command ########################################################## @@ -106,6 +113,15 @@ # find multiple with some keywords find Charlie Betsy +########################################################## +# test count command +########################################################## + + # should only count the number of people inputted into the + + # address book + count + ########################################################## # test delete person command ########################################################## diff --git a/test/java/seedu/addressbook/common/UtilsTest.java b/test/java/seedu/addressbook/common/UtilsTest.java index 025ad4ee0..01f221c96 100644 --- a/test/java/seedu/addressbook/common/UtilsTest.java +++ b/test/java/seedu/addressbook/common/UtilsTest.java @@ -50,4 +50,5 @@ private void assertAreUnique(Object... objects) { private void assertNotUnique(Object... objects) { assertFalse(Utils.elementsAreUnique(Arrays.asList(objects))); } + } From 53634cdb8e963a0be447cd2b95b8e87b1a1b2e04 Mon Sep 17 00:00:00 2001 From: Thaddeus Lim Date: Thu, 13 Sep 2018 11:59:58 +0800 Subject: [PATCH 09/10] Minor Changes Minor changes to code format --- src/seedu/addressbook/commands/CountCommand.java | 2 ++ src/seedu/addressbook/data/AddressBook.java | 9 +++++++-- test/java/seedu/addressbook/common/UtilsTest.java | 3 +-- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/seedu/addressbook/commands/CountCommand.java b/src/seedu/addressbook/commands/CountCommand.java index 826fcf229..3b77e130a 100644 --- a/src/seedu/addressbook/commands/CountCommand.java +++ b/src/seedu/addressbook/commands/CountCommand.java @@ -1,4 +1,5 @@ package seedu.addressbook.commands; + /** * Shows total number of persons in address book. */ @@ -9,6 +10,7 @@ public class CountCommand extends Command { public static final String MESSAGE_USAGE = COMMAND_WORD + ": Counts the total number of contacts " + "in the address book.\n" + "Example: " + COMMAND_WORD; + public static final String MESSAGE_COUNTED = "Total number of persons in address book: %1$s"; @Override diff --git a/src/seedu/addressbook/data/AddressBook.java b/src/seedu/addressbook/data/AddressBook.java index e757a99be..6be3aab42 100644 --- a/src/seedu/addressbook/data/AddressBook.java +++ b/src/seedu/addressbook/data/AddressBook.java @@ -28,8 +28,8 @@ public AddressBook() { * @param persons external changes to this will not affect this address book */ public AddressBook(UniquePersonList persons) { - sumPersons = getSize(persons); this.allPersons = new UniquePersonList(persons); + sumPersons = getSize(persons); } /** @@ -39,9 +39,11 @@ public AddressBook(UniquePersonList persons) { */ public static int getSize(Iterable persons) { int sumElements = 0; - for (T elem : persons) { + + for (T element : persons) { sumElements++; } + return sumElements; } @@ -89,6 +91,9 @@ public UniquePersonList getAllPersons() { } + /** + * Returns total number of persons in the address book at the time of the call. + */ public int getSumPersons() { return this.sumPersons; diff --git a/test/java/seedu/addressbook/common/UtilsTest.java b/test/java/seedu/addressbook/common/UtilsTest.java index 01f221c96..04194cd6d 100644 --- a/test/java/seedu/addressbook/common/UtilsTest.java +++ b/test/java/seedu/addressbook/common/UtilsTest.java @@ -10,7 +10,6 @@ public class UtilsTest { - @Test public void elementsAreUnique() throws Exception { // empty list @@ -50,5 +49,5 @@ private void assertAreUnique(Object... objects) { private void assertNotUnique(Object... objects) { assertFalse(Utils.elementsAreUnique(Arrays.asList(objects))); } - + } From dac73de316bdc802a642ae9bf5963f385879c6a9 Mon Sep 17 00:00:00 2001 From: Thaddeus Lim Date: Thu, 13 Sep 2018 13:12:53 +0800 Subject: [PATCH 10/10] Change to Test Cases Edited test cases to pass --- src/seedu/addressbook/commands/CountCommand.java | 2 +- src/seedu/addressbook/data/AddressBook.java | 2 -- test/expected.txt | 4 ++-- test/input.txt | 7 ------- 4 files changed, 3 insertions(+), 12 deletions(-) diff --git a/src/seedu/addressbook/commands/CountCommand.java b/src/seedu/addressbook/commands/CountCommand.java index 3b77e130a..13dffdeb8 100644 --- a/src/seedu/addressbook/commands/CountCommand.java +++ b/src/seedu/addressbook/commands/CountCommand.java @@ -10,7 +10,7 @@ public class CountCommand extends Command { public static final String MESSAGE_USAGE = COMMAND_WORD + ": Counts the total number of contacts " + "in the address book.\n" + "Example: " + COMMAND_WORD; - + public static final String MESSAGE_COUNTED = "Total number of persons in address book: %1$s"; @Override diff --git a/src/seedu/addressbook/data/AddressBook.java b/src/seedu/addressbook/data/AddressBook.java index 6be3aab42..92aa5fe1d 100644 --- a/src/seedu/addressbook/data/AddressBook.java +++ b/src/seedu/addressbook/data/AddressBook.java @@ -35,7 +35,6 @@ public AddressBook(UniquePersonList persons) { /** * Returns the number of elements in the container behind an iterable. * - * */ public static int getSize(Iterable persons) { int sumElements = 0; @@ -47,7 +46,6 @@ public static int getSize(Iterable persons) { return sumElements; } - /** * Adds a person to the address book. * diff --git a/test/expected.txt b/test/expected.txt index 7e3421d3d..9eced4c54 100644 --- a/test/expected.txt +++ b/test/expected.txt @@ -25,7 +25,7 @@ || viewall: Views the non-private details of the person identified by the index number in the last shown person listing. || Parameters: INDEX || Example: viewall 1 -|| count: Counts the total number of persons in the address book. +|| count: Counts the total number of contacts in the address book. || Example: count || help: Shows program usage instructions. || Example: help @@ -236,7 +236,7 @@ || 2 persons listed! || =================================================== || Enter command: || [Command entered: count] -|| There are 5 people in the address book. +|| Total number of persons in address book: 5 || =================================================== || Enter command: || [Command entered: delete] || Invalid command format! diff --git a/test/input.txt b/test/input.txt index dc036f7fb..6a5abb076 100644 --- a/test/input.txt +++ b/test/input.txt @@ -1,10 +1,3 @@ -########################################################## -# count number of persons before anyone is added -########################################################## - - # should be 0 - count - ########################################################## # invalid command ##########################################################