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.4d][W15-B4]Muhammad Nur Kamal #480

Open
wants to merge 3 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
6 changes: 5 additions & 1 deletion src/seedu/addressbook/commands/AddCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,9 @@ public CommandResult execute() {
return new CommandResult(MESSAGE_DUPLICATE_PERSON);
}
}


@Override
public boolean isMutating() {
return true;
}
}
5 changes: 5 additions & 0 deletions src/seedu/addressbook/commands/ClearCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,9 @@ public CommandResult execute() {
addressBook.clear();
return new CommandResult(MESSAGE_SUCCESS);
}

@Override
public boolean isMutating() {
return true;
}
}
7 changes: 7 additions & 0 deletions src/seedu/addressbook/commands/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,11 @@ public int getTargetIndex() {
public void setTargetIndex(int targetIndex) {
this.targetIndex = targetIndex;
}

/**
* Checks if command is able to modify data by Command type.
*/
public boolean isMutating() {
return false;
}
}
4 changes: 4 additions & 0 deletions src/seedu/addressbook/commands/DeleteCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,8 @@ public CommandResult execute() {
}
}

@Override
public boolean isMutating() {
return true;
}
}
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
8 changes: 7 additions & 1 deletion test/java/seedu/addressbook/logic/LogicTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import seedu.addressbook.data.person.*;
import seedu.addressbook.data.tag.Tag;
import seedu.addressbook.data.tag.UniqueTagList;
import seedu.addressbook.parser.Parser;
import seedu.addressbook.storage.StorageFile;

import java.util.*;
Expand Down Expand Up @@ -76,9 +77,14 @@ private void assertCommandBehavior(String inputCommand,
AddressBook expectedAddressBook,
boolean isRelevantPersonsExpected,
List<? extends ReadOnlyPerson> lastShownList) throws Exception {

Command command = new Parser().parseCommand(inputCommand);
//Execute the command
CommandResult r = logic.execute(inputCommand);

//Guarantees saveFile is saved for non-mutating methods
if (!command.isMutating()) {
saveFile.save(addressBook);
}

//Confirm the result contains the right data
assertEquals(expectedMessage, r.feedbackToUser);
Expand Down