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

[W6.1][W09-B1] Fan Yuting #867

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
5 changes: 5 additions & 0 deletions src/seedu/addressbook/commands/AddCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ public ReadOnlyPerson getPerson() {
return toAdd;
}

@Override
public boolean isMutating() {
return true;
}

@Override
public CommandResult execute() {
try {
Expand Down
5 changes: 5 additions & 0 deletions src/seedu/addressbook/commands/ClearCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
6 changes: 6 additions & 0 deletions src/seedu/addressbook/commands/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
4 changes: 4 additions & 0 deletions src/seedu/addressbook/commands/DeleteCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ public DeleteCommand(int targetVisibleIndex) {
super(targetVisibleIndex);
}

@Override
public boolean isMutating() {
return true;
}

@Override
public CommandResult execute() {
Expand Down
5 changes: 5 additions & 0 deletions src/seedu/addressbook/commands/ExitCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions src/seedu/addressbook/commands/FindCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ public Set<String> getKeywords() {
return new HashSet<>(keywords);
}

@Override
public boolean isMutating() {
return false;
}

@Override
public CommandResult execute() {
final List<ReadOnlyPerson> personsFound = getPersonsWithNameContainingAnyKeyword(keywords);
Expand Down
5 changes: 5 additions & 0 deletions src/seedu/addressbook/commands/HelpCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions src/seedu/addressbook/commands/IncorrectCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions src/seedu/addressbook/commands/ListCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
4 changes: 4 additions & 0 deletions src/seedu/addressbook/commands/ViewAllCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ public ViewAllCommand(int targetVisibleIndex) {
super(targetVisibleIndex);
}

@Override
public boolean isMutating() {
return false;
}

@Override
public CommandResult execute() {
Expand Down
4 changes: 4 additions & 0 deletions src/seedu/addressbook/commands/ViewCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ public ViewCommand(int targetVisibleIndex) {
super(targetVisibleIndex);
}

@Override
public boolean isMutating() {
return false;
}

@Override
public CommandResult execute() {
Expand Down
12 changes: 11 additions & 1 deletion src/seedu/addressbook/data/person/Address.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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.
*/
Expand Down
12 changes: 11 additions & 1 deletion src/seedu/addressbook/data/person/Email.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "[email protected]";
public static final String MESSAGE_EMAIL_CONSTRAINTS =
Expand All @@ -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.
*/
Expand Down
8 changes: 7 additions & 1 deletion src/seedu/addressbook/data/person/Name.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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.
*/
Expand Down
11 changes: 11 additions & 0 deletions src/seedu/addressbook/data/person/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
11 changes: 10 additions & 1 deletion src/seedu/addressbook/data/person/Phone.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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.
*/
Expand Down
9 changes: 9 additions & 0 deletions src/seedu/addressbook/data/person/Printable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package seedu.addressbook.data.person;

public interface Printable {

default String getPrintableString() {
return null;
}

}
4 changes: 3 additions & 1 deletion src/seedu/addressbook/logic/Logic.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down