diff --git a/src/seedu/addressbook/commands/AddCommand.java b/src/seedu/addressbook/commands/AddCommand.java index b0f202d42..dfd692e97 100644 --- a/src/seedu/addressbook/commands/AddCommand.java +++ b/src/seedu/addressbook/commands/AddCommand.java @@ -57,6 +57,11 @@ public ReadOnlyPerson getPerson() { return toAdd; } + @Override + public boolean isMutating() { + return true; + } + @Override public CommandResult execute() { try { diff --git a/src/seedu/addressbook/commands/ClearCommand.java b/src/seedu/addressbook/commands/ClearCommand.java index 330146aaa..cb93824fd 100644 --- a/src/seedu/addressbook/commands/ClearCommand.java +++ b/src/seedu/addressbook/commands/ClearCommand.java @@ -11,6 +11,11 @@ public class ClearCommand extends Command { public static final String MESSAGE_SUCCESS = "Address book has been cleared!"; + @Override + public boolean isMutating() { + return true; + } + @Override public CommandResult execute() { addressBook.clear(); diff --git a/src/seedu/addressbook/commands/Command.java b/src/seedu/addressbook/commands/Command.java index ccd4fcbf4..643d0539e 100644 --- a/src/seedu/addressbook/commands/Command.java +++ b/src/seedu/addressbook/commands/Command.java @@ -26,6 +26,12 @@ public Command(int targetIndex) { protected Command() { } + /** + * Checks whether the command type mutates the data + * @return true if the data is mutated, false otherwise + */ + public abstract boolean isMutating(); + /** * Constructs a feedback message to summarise an operation that displayed a listing of persons. * diff --git a/src/seedu/addressbook/commands/DeleteCommand.java b/src/seedu/addressbook/commands/DeleteCommand.java index 1dd78f85e..7fc9adf43 100644 --- a/src/seedu/addressbook/commands/DeleteCommand.java +++ b/src/seedu/addressbook/commands/DeleteCommand.java @@ -24,6 +24,10 @@ public DeleteCommand(int targetVisibleIndex) { super(targetVisibleIndex); } + @Override + public boolean isMutating() { + return true; + } @Override public CommandResult execute() { diff --git a/src/seedu/addressbook/commands/ExitCommand.java b/src/seedu/addressbook/commands/ExitCommand.java index 0585451f1..c116a61f5 100644 --- a/src/seedu/addressbook/commands/ExitCommand.java +++ b/src/seedu/addressbook/commands/ExitCommand.java @@ -11,6 +11,11 @@ public class ExitCommand extends Command { + "Example: " + COMMAND_WORD; public static final String MESSAGE_EXIT_ACKNOWEDGEMENT = "Exiting Address Book as requested ..."; + @Override + public boolean isMutating() { + return false; + } + @Override public CommandResult execute() { return new CommandResult(MESSAGE_EXIT_ACKNOWEDGEMENT); diff --git a/src/seedu/addressbook/commands/FindCommand.java b/src/seedu/addressbook/commands/FindCommand.java index c8e9a380f..8251a1afd 100644 --- a/src/seedu/addressbook/commands/FindCommand.java +++ b/src/seedu/addressbook/commands/FindCommand.java @@ -30,6 +30,11 @@ public Set getKeywords() { return new HashSet<>(keywords); } + @Override + public boolean isMutating() { + return false; + } + @Override public CommandResult execute() { final List personsFound = getPersonsWithNameContainingAnyKeyword(keywords); diff --git a/src/seedu/addressbook/commands/HelpCommand.java b/src/seedu/addressbook/commands/HelpCommand.java index ef2ed7d0e..05fcf3072 100644 --- a/src/seedu/addressbook/commands/HelpCommand.java +++ b/src/seedu/addressbook/commands/HelpCommand.java @@ -21,6 +21,11 @@ public class HelpCommand extends Command { + "\n" + HelpCommand.MESSAGE_USAGE + "\n" + ExitCommand.MESSAGE_USAGE; + @Override + public boolean isMutating() { + return false; + } + @Override public CommandResult execute() { return new CommandResult(MESSAGE_ALL_USAGES); diff --git a/src/seedu/addressbook/commands/IncorrectCommand.java b/src/seedu/addressbook/commands/IncorrectCommand.java index 81abba7a1..08cead05e 100644 --- a/src/seedu/addressbook/commands/IncorrectCommand.java +++ b/src/seedu/addressbook/commands/IncorrectCommand.java @@ -12,6 +12,11 @@ public IncorrectCommand(String feedbackToUser){ this.feedbackToUser = feedbackToUser; } + @Override + public boolean isMutating() { + return false; + } + @Override public CommandResult execute() { return new CommandResult(feedbackToUser); diff --git a/src/seedu/addressbook/commands/ListCommand.java b/src/seedu/addressbook/commands/ListCommand.java index cb604a8e9..fdc5b8f7d 100644 --- a/src/seedu/addressbook/commands/ListCommand.java +++ b/src/seedu/addressbook/commands/ListCommand.java @@ -16,6 +16,10 @@ public class ListCommand extends Command { + "Displays all persons in the address book as a list with index numbers.\n\t" + "Example: " + COMMAND_WORD; + @Override + public boolean isMutating() { + return false; + } @Override public CommandResult execute() { diff --git a/src/seedu/addressbook/commands/ViewAllCommand.java b/src/seedu/addressbook/commands/ViewAllCommand.java index ed2c16e83..2e2957784 100644 --- a/src/seedu/addressbook/commands/ViewAllCommand.java +++ b/src/seedu/addressbook/commands/ViewAllCommand.java @@ -24,6 +24,10 @@ public ViewAllCommand(int targetVisibleIndex) { super(targetVisibleIndex); } + @Override + public boolean isMutating() { + return false; + } @Override public CommandResult execute() { diff --git a/src/seedu/addressbook/commands/ViewCommand.java b/src/seedu/addressbook/commands/ViewCommand.java index 1058c4b52..9b67b0dc7 100644 --- a/src/seedu/addressbook/commands/ViewCommand.java +++ b/src/seedu/addressbook/commands/ViewCommand.java @@ -24,6 +24,10 @@ public ViewCommand(int targetVisibleIndex) { super(targetVisibleIndex); } + @Override + public boolean isMutating() { + return false; + } @Override public CommandResult execute() { diff --git a/src/seedu/addressbook/data/person/Address.java b/src/seedu/addressbook/data/person/Address.java index 8ac726444..03ec8d4ae 100644 --- a/src/seedu/addressbook/data/person/Address.java +++ b/src/seedu/addressbook/data/person/Address.java @@ -6,7 +6,7 @@ * Represents a Person's address in the address book. * Guarantees: immutable; is valid as declared in {@link #isValidAddress(String)} */ -public class Address { +public class Address implements Printable { public static final String EXAMPLE = "123, some street"; public static final String MESSAGE_ADDRESS_CONSTRAINTS = "Person addresses can be in any format"; @@ -28,6 +28,16 @@ public Address(String address, boolean isPrivate) throws IllegalValueException { this.value = address; } + @Override + public String getPrintableString() { + if (isPrivate) { + return "Address: private"; + } else { + return "Address: " + value; + } + } + + /** * Returns true if a given string is a valid person email. */ diff --git a/src/seedu/addressbook/data/person/Email.java b/src/seedu/addressbook/data/person/Email.java index c946f1eb3..2ab6a0905 100644 --- a/src/seedu/addressbook/data/person/Email.java +++ b/src/seedu/addressbook/data/person/Email.java @@ -6,7 +6,7 @@ * Represents a Person's email in the address book. * Guarantees: immutable; is valid as declared in {@link #isValidEmail(String)} */ -public class Email { +public class Email implements Printable{ public static final String EXAMPLE = "valid@e.mail"; public static final String MESSAGE_EMAIL_CONSTRAINTS = @@ -30,6 +30,16 @@ public Email(String email, boolean isPrivate) throws IllegalValueException { this.value = email; } + @Override + public String getPrintableString() { + if (isPrivate) { + return "Email: private"; + } else { + return "Email: " + value; + } + } + + /** * Checks if a given string is a valid person email. */ diff --git a/src/seedu/addressbook/data/person/Name.java b/src/seedu/addressbook/data/person/Name.java index 487b7ad9c..5028d0e5c 100644 --- a/src/seedu/addressbook/data/person/Name.java +++ b/src/seedu/addressbook/data/person/Name.java @@ -9,7 +9,7 @@ * Represents a Person's name in the address book. * Guarantees: immutable; is valid as declared in {@link #isValidName(String)} */ -public class Name { +public class Name implements Printable{ public static final String EXAMPLE = "John Doe"; public static final String MESSAGE_NAME_CONSTRAINTS = "Person names should be spaces or alphanumeric characters"; @@ -30,6 +30,12 @@ public Name(String name) throws IllegalValueException { this.fullName = name; } + @Override + public String getPrintableString() { + return "Name: " + fullName; + } + + /** * Returns true if a given string is a valid person name. */ diff --git a/src/seedu/addressbook/data/person/Person.java b/src/seedu/addressbook/data/person/Person.java index cf6211841..af4985cec 100644 --- a/src/seedu/addressbook/data/person/Person.java +++ b/src/seedu/addressbook/data/person/Person.java @@ -34,6 +34,17 @@ public Person(ReadOnlyPerson source) { this(source.getName(), source.getPhone(), source.getEmail(), source.getAddress(), source.getTags()); } + /** + * Returns a concatenated version of the printable strings of each object. + */ + String getPrintableString(Printable... printables){ + String returnString = ""; + for (Printable p : printables) { + returnString += " " + p; + } + return returnString; + } + @Override public Name getName() { return name; diff --git a/src/seedu/addressbook/data/person/Phone.java b/src/seedu/addressbook/data/person/Phone.java index b5a556de4..234853ba9 100644 --- a/src/seedu/addressbook/data/person/Phone.java +++ b/src/seedu/addressbook/data/person/Phone.java @@ -6,7 +6,7 @@ * Represents a Person's phone number in the address book. * Guarantees: immutable; is valid as declared in {@link #isValidPhone(String)} */ -public class Phone { +public class Phone implements Printable { public static final String EXAMPLE = "123456789"; public static final String MESSAGE_PHONE_CONSTRAINTS = "Person phone numbers should only contain numbers"; @@ -29,6 +29,15 @@ public Phone(String phone, boolean isPrivate) throws IllegalValueException { this.value = phone; } + @Override + public String getPrintableString() { + if (isPrivate) { + return "Phone: private"; + } else { + return "Phone: " + value; + } + } + /** * Checks if a given string is a valid person phone number. */ diff --git a/src/seedu/addressbook/data/person/Printable.java b/src/seedu/addressbook/data/person/Printable.java new file mode 100644 index 000000000..b636e9936 --- /dev/null +++ b/src/seedu/addressbook/data/person/Printable.java @@ -0,0 +1,9 @@ +package seedu.addressbook.data.person; + +public interface Printable { + + default String getPrintableString() { + return null; + } + +} diff --git a/src/seedu/addressbook/logic/Logic.java b/src/seedu/addressbook/logic/Logic.java index 17afd61a0..fe2bd529d 100644 --- a/src/seedu/addressbook/logic/Logic.java +++ b/src/seedu/addressbook/logic/Logic.java @@ -85,7 +85,9 @@ public CommandResult execute(String userCommandText) throws Exception { private CommandResult execute(Command command) throws Exception { command.setData(addressBook, lastShownList); CommandResult result = command.execute(); - storage.save(addressBook); + if (command.isMutating()) { + storage.save(addressBook); + } return result; }