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

[W5.7][T12-3]Thaddeus Lim #437

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/UserGuide.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Choose a reason for hiding this comment

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

Good job updating the user doc



Choose a reason for hiding this comment

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

Stray line break? Try to avoid these spaces by checking diff while submitting a PR.

== Exiting the program : `exit`

Exits the program. +
Expand Down
21 changes: 21 additions & 0 deletions src/seedu/addressbook/commands/CountCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package seedu.addressbook.commands;

/**
* Shows total number of persons in address book.
*/
public class CountCommand extends Command {

public static final String COMMAND_WORD = "count";

Choose a reason for hiding this comment

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

Try to avoid public class member variables


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
public CommandResult execute() {
return new CommandResult(String.format(MESSAGE_COUNTED, addressBook.getSumPersons()));
}

}
1 change: 1 addition & 0 deletions src/seedu/addressbook/commands/HelpCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand Down
29 changes: 29 additions & 0 deletions src/seedu/addressbook/data/AddressBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public class AddressBook {

private final UniquePersonList allPersons;

private int sumPersons = 0;

/**
* Creates an empty address book.
*/
Expand All @@ -27,6 +29,21 @@ public AddressBook() {
*/
public AddressBook(UniquePersonList persons) {
this.allPersons = new UniquePersonList(persons);
sumPersons = getSize(persons);
}

/**
* Returns the number of elements in the container behind an iterable.
*
*/
public static <T> int getSize(Iterable<T> persons) {
int sumElements = 0;

for (T element : persons) {
sumElements++;
}

return sumElements;
}

/**
Expand All @@ -36,6 +53,7 @@ public AddressBook(UniquePersonList persons) {
*/
public void addPerson(Person toAdd) throws DuplicatePersonException {
allPersons.add(toAdd);
sumPersons++;
}

/**
Expand All @@ -52,20 +70,31 @@ public boolean containsPerson(ReadOnlyPerson key) {
*/
public void removePerson(ReadOnlyPerson toRemove) throws PersonNotFoundException {
allPersons.remove(toRemove);
sumPersons--;
}

/**
* Clears all persons and tags from the address book.
*/
public void clear() {
allPersons.clear();
sumPersons = 0;
}

/**
* Returns a new UniquePersonList of all persons in the address book at the time of the call.
*/
public UniquePersonList getAllPersons() {
return new UniquePersonList(allPersons);

}

/**
* Returns total number of persons in the address book at the time of the call.
*/
public int getSumPersons() {
return this.sumPersons;

}

@Override
Expand Down
4 changes: 4 additions & 0 deletions src/seedu/addressbook/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

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

Expand Down
5 changes: 5 additions & 0 deletions test/expected.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 contacts in the address book.
|| Example: count
|| help: Shows program usage instructions.
|| Example: help
|| exit: Exits the program.
Expand Down Expand Up @@ -233,6 +235,9 @@
||
|| 2 persons listed!
|| ===================================================
|| Enter command: || [Command entered: count]
|| Total number of persons in address book: 5
|| ===================================================
|| Enter command: || [Command entered: delete]
|| Invalid command format!
|| delete: Deletes the person identified by the index number used in the last person listing.
Expand Down
9 changes: 9 additions & 0 deletions test/input.txt
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,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
##########################################################
Expand Down
9 changes: 8 additions & 1 deletion test/java/seedu/addressbook/common/UtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

public class UtilsTest {


@Test
public void elementsAreUnique() throws Exception {
// empty list
Expand All @@ -36,11 +35,19 @@ public void elementsAreUnique() throws Exception {
assertNotUnique(null, "a", "b", null);
}

@Test
public void isAnyNull() throws Exception {

Choose a reason for hiding this comment

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

This commit shouldn't be part of this PR. In general, a PR should not contain unmerged code from other PRs. Try to limit each PR to one enhancement, which means the PR should be based on a branch that starts off from the master branch that is in sync with the upstream master branch

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)));
}

private void assertNotUnique(Object... objects) {
assertFalse(Utils.elementsAreUnique(Arrays.asList(objects)));
}

}
7 changes: 7 additions & 0 deletions test/java/seedu/addressbook/data/AddressBookTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}