From ecf0e75e0344195a9a2b3b9d6ebfe6c49b996968 Mon Sep 17 00:00:00 2001 From: huyidi <1013792433@qq.com> Date: Sun, 10 Feb 2019 20:15:05 +0800 Subject: [PATCH] Added a command to output total number of contacts in the addressbook Simple feature but straightforward and useful to manage memory space --- src/seedu/addressbook/commands/Command.java | 1 + .../addressbook/commands/TotalCommand.java | 30 +++++++++++++++++++ src/seedu/addressbook/parser/Parser.java | 4 +++ 3 files changed, 35 insertions(+) create mode 100644 src/seedu/addressbook/commands/TotalCommand.java diff --git a/src/seedu/addressbook/commands/Command.java b/src/seedu/addressbook/commands/Command.java index 2ff8f1575..59418b10e 100644 --- a/src/seedu/addressbook/commands/Command.java +++ b/src/seedu/addressbook/commands/Command.java @@ -60,6 +60,7 @@ protected ReadOnlyPerson getTargetPerson() throws IndexOutOfBoundsException { return relevantPersons.get(getTargetIndex() - DISPLAYED_INDEX_OFFSET); } + public int getTargetIndex() { return targetIndex; } diff --git a/src/seedu/addressbook/commands/TotalCommand.java b/src/seedu/addressbook/commands/TotalCommand.java new file mode 100644 index 000000000..3e7a9399c --- /dev/null +++ b/src/seedu/addressbook/commands/TotalCommand.java @@ -0,0 +1,30 @@ +package seedu.addressbook.commands; + +import seedu.addressbook.data.person.ReadOnlyPerson; + +import java.util.List; + + +/** + * Lists all persons in the address book to the user. + */ +public class TotalCommand extends Command { + + public static final String COMMAND_WORD = "total"; + + public static final String MESSAGE_USAGE = COMMAND_WORD + + ": Total number of persons in the address book.\n" + + "Example: " + COMMAND_WORD; + + + @Override + public CommandResult execute() { + List totalList = addressBook.getAllPersons().immutableListView(); + int TOTALNUMBER = totalList.size(); + + return new CommandResult(TOTALNUMBER+" "+ + "IS THE TOTAL NUMBER OF EXISTING CONTACTS"); + } +} + + diff --git a/src/seedu/addressbook/parser/Parser.java b/src/seedu/addressbook/parser/Parser.java index abddb3f45..ba2f84ce1 100644 --- a/src/seedu/addressbook/parser/Parser.java +++ b/src/seedu/addressbook/parser/Parser.java @@ -20,6 +20,7 @@ import seedu.addressbook.commands.HelpCommand; import seedu.addressbook.commands.IncorrectCommand; import seedu.addressbook.commands.ListCommand; +import seedu.addressbook.commands.TotalCommand; import seedu.addressbook.commands.ViewAllCommand; import seedu.addressbook.commands.ViewCommand; import seedu.addressbook.data.exception.IllegalValueException; @@ -88,6 +89,9 @@ public Command parseCommand(String userInput) { case ListCommand.COMMAND_WORD: return new ListCommand(); + case TotalCommand.COMMAND_WORD: + return new TotalCommand(); + case ViewCommand.COMMAND_WORD: return prepareView(arguments);