Skip to content

Commit

Permalink
Used Polymorphism by adding isMutating() in Command Class
Browse files Browse the repository at this point in the history
  • Loading branch information
leerachel committed Sep 20, 2018
1 parent 4a18d6b commit 2a861d6
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/seedu/addressbook/commands/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,6 @@ public int getTargetIndex() {
public void setTargetIndex(int targetIndex) {
this.targetIndex = targetIndex;
}

public boolean isMutating() { return true; }
}
2 changes: 2 additions & 0 deletions src/seedu/addressbook/commands/FindCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,6 @@ private List<ReadOnlyPerson> getPersonsWithNameContainingAnyKeyword(Set<String>
return matchedPersons;
}

@Override
public boolean isMutating() { return false; }
}
4 changes: 4 additions & 0 deletions src/seedu/addressbook/commands/HelpCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,8 @@ public class HelpCommand extends Command {
public CommandResult execute() {
return new CommandResult(MESSAGE_ALL_USAGES);
}

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

}
3 changes: 3 additions & 0 deletions src/seedu/addressbook/commands/IncorrectCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@ public CommandResult execute() {
return new CommandResult(feedbackToUser);
}

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

}
3 changes: 3 additions & 0 deletions src/seedu/addressbook/commands/ListCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,7 @@ public CommandResult execute() {
List<ReadOnlyPerson> allPersons = addressBook.getAllPersons().immutableListView();
return new CommandResult(getMessageForPersonListShownSummary(allPersons), allPersons);
}

@Override
public boolean isMutating() { return false; }
}
3 changes: 3 additions & 0 deletions src/seedu/addressbook/commands/ViewAllCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,7 @@ public CommandResult execute() {
return new CommandResult(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
}
}

@Override
public boolean isMutating() { return false; }
}
3 changes: 3 additions & 0 deletions src/seedu/addressbook/commands/ViewCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,7 @@ public CommandResult execute() {
}
}

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

}
6 changes: 4 additions & 2 deletions src/seedu/addressbook/logic/Logic.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public CommandResult execute(String userCommandText) throws Exception {
}

/**
* Executes the command, updates storage, and returns the result.
* Executes the command, updates storage if data is mutated, and returns the result.
*
* @param command user command
* @return result of the command
Expand All @@ -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() == true) {
storage.save(addressBook);
}
return result;
}

Expand Down

0 comments on commit 2a861d6

Please sign in to comment.