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

Refactor logic components #60

Merged
Merged
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
3 changes: 1 addition & 2 deletions src/main/java/seedu/address/logic/Logic.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import javafx.collections.ObservableList;
import seedu.address.commons.core.GuiSettings;
import seedu.address.logic.commands.CommandResult;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.ReadOnlyAddressBook;
Expand All @@ -21,7 +20,7 @@ public interface Logic {
* @throws CommandException If an error occurs during command execution.
* @throws ParseException If an error occurs during parsing.
*/
CommandResult execute(String commandText) throws CommandException, ParseException;
String execute(String commandText) throws CommandException, ParseException;

/**
* Returns the AddressBook.
Expand Down
10 changes: 3 additions & 7 deletions src/main/java/seedu/address/logic/LogicManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import seedu.address.commons.core.GuiSettings;
import seedu.address.commons.core.LogsCenter;
import seedu.address.logic.commands.Command;
import seedu.address.logic.commands.CommandResult;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.logic.parser.AddressBookParser;
import seedu.address.logic.parser.exceptions.ParseException;
Expand All @@ -31,24 +30,21 @@ public class LogicManager implements Logic {

private final Model model;
private final Storage storage;
private final AddressBookParser addressBookParser;

/**
* Constructs a {@code LogicManager} with the given {@code Model} and {@code Storage}.
*/
public LogicManager(Model model, Storage storage) {
this.model = model;
this.storage = storage;
addressBookParser = new AddressBookParser();
}

@Override
public CommandResult execute(String commandText) throws CommandException, ParseException {
public String execute(String commandText) throws CommandException, ParseException {
logger.info("----------------[USER COMMAND][" + commandText + "]");

CommandResult commandResult;
Command command = addressBookParser.parseCommand(commandText);
commandResult = command.execute(model);
Command command = AddressBookParser.parseCommand(commandText);
String commandResult = command.execute(model);

try {
storage.saveAddressBook(model.getAddressBook());
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/seedu/address/logic/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ public class Messages {
public static final String MESSAGE_DUPLICATE_FIELDS =
"Multiple values specified for the following single-valued field(s): ";

public static final String MESSAGE_SHOWING_HELP = "Opened help window.";
public static final String MESSAGE_EXIT_ACKNOWLEDGEMENT = "Exiting Address Book as requested ...";


/**
* Returns an error message indicating the duplicate prefixes.
*/
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/seedu/address/logic/commands/AddCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ public AddCommand(Person person) {
}

@Override
public CommandResult execute(Model model) throws CommandException {
public String execute(Model model) throws CommandException {
requireNonNull(model);

if (model.hasPerson(toAdd)) {
throw new CommandException(MESSAGE_DUPLICATE_PERSON);
}

model.addPerson(toAdd);
return new CommandResult(String.format(MESSAGE_SUCCESS, Messages.format(toAdd)));
return String.format(MESSAGE_SUCCESS, Messages.format(toAdd));
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/seedu/address/logic/commands/ClearCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ public class ClearCommand extends Command {


@Override
public CommandResult execute(Model model) {
public String execute(Model model) {
requireNonNull(model);
model.setAddressBook(new AddressBook());
return new CommandResult(MESSAGE_SUCCESS);
return MESSAGE_SUCCESS;
}
}
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/logic/commands/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ public abstract class Command {
* @return feedback message of the operation result for display
* @throws CommandException If an error occurs during command execution.
*/
public abstract CommandResult execute(Model model) throws CommandException;
public abstract String execute(Model model) throws CommandException;

}
82 changes: 0 additions & 82 deletions src/main/java/seedu/address/logic/commands/CommandResult.java

This file was deleted.

4 changes: 2 additions & 2 deletions src/main/java/seedu/address/logic/commands/DeleteCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public DeleteCommand(Index targetIndex) {
}

@Override
public CommandResult execute(Model model) throws CommandException {
public String execute(Model model) throws CommandException {
requireNonNull(model);
List<Person> lastShownList = model.getFilteredPersonList();

Expand All @@ -42,7 +42,7 @@ public CommandResult execute(Model model) throws CommandException {

Person personToDelete = lastShownList.get(targetIndex.getZeroBased());
model.deletePerson(personToDelete);
return new CommandResult(String.format(MESSAGE_DELETE_PERSON_SUCCESS, Messages.format(personToDelete)));
return String.format(MESSAGE_DELETE_PERSON_SUCCESS, Messages.format(personToDelete));
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/seedu/address/logic/commands/EditCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public EditCommand(Index index, EditPersonDescriptor editPersonDescriptor) {
}

@Override
public CommandResult execute(Model model) throws CommandException {
public String execute(Model model) throws CommandException {
requireNonNull(model);
List<Person> lastShownList = model.getFilteredPersonList();

Expand All @@ -82,7 +82,7 @@ public CommandResult execute(Model model) throws CommandException {

model.setPerson(personToEdit, editedPerson);
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);
return new CommandResult(String.format(MESSAGE_EDIT_PERSON_SUCCESS, Messages.format(editedPerson)));
return String.format(MESSAGE_EDIT_PERSON_SUCCESS, Messages.format(editedPerson));
}

/**
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/seedu/address/logic/commands/ExitCommand.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package seedu.address.logic.commands;

import seedu.address.logic.Messages;
import seedu.address.model.Model;

/**
Expand All @@ -9,11 +10,9 @@ public class ExitCommand extends Command {

public static final String COMMAND_WORD = "exit";

public static final String MESSAGE_EXIT_ACKNOWLEDGEMENT = "Exiting Address Book as requested ...";

@Override
public CommandResult execute(Model model) {
return new CommandResult(MESSAGE_EXIT_ACKNOWLEDGEMENT, false, true);
public String execute(Model model) {
return Messages.MESSAGE_EXIT_ACKNOWLEDGEMENT;
}

}
5 changes: 2 additions & 3 deletions src/main/java/seedu/address/logic/commands/FindCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,10 @@ public FindCommand(NameContainsKeywordsPredicate predicate) {
}

@Override
public CommandResult execute(Model model) {
public String execute(Model model) {
requireNonNull(model);
model.updateFilteredPersonList(predicate);
return new CommandResult(
String.format(Messages.MESSAGE_PERSONS_LISTED_OVERVIEW, model.getFilteredPersonList().size()));
return String.format(Messages.MESSAGE_PERSONS_LISTED_OVERVIEW, model.getFilteredPersonList().size());
}

@Override
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/seedu/address/logic/commands/HelpCommand.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package seedu.address.logic.commands;

import seedu.address.logic.Messages;
import seedu.address.model.Model;

/**
Expand All @@ -12,10 +13,8 @@ public class HelpCommand extends Command {
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Shows program usage instructions.\n"
+ "Example: " + COMMAND_WORD;

public static final String SHOWING_HELP_MESSAGE = "Opened help window.";

@Override
public CommandResult execute(Model model) {
return new CommandResult(SHOWING_HELP_MESSAGE, true, false);
public String execute(Model model) {
return Messages.MESSAGE_SHOWING_HELP;
}
}
4 changes: 2 additions & 2 deletions src/main/java/seedu/address/logic/commands/ListCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ public class ListCommand extends Command {


@Override
public CommandResult execute(Model model) {
public String execute(Model model) {
requireNonNull(model);
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);
return new CommandResult(MESSAGE_SUCCESS);
return MESSAGE_SUCCESS;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class AddressBookParser {
* @return the command based on the user input
* @throws ParseException if the user input does not conform the expected format
*/
public Command parseCommand(String userInput) throws ParseException {
public static Command parseCommand(String userInput) throws ParseException {
final Matcher matcher = BASIC_COMMAND_FORMAT.matcher(userInput.trim());
if (!matcher.matches()) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, HelpCommand.MESSAGE_USAGE));
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/seedu/address/ui/CommandBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import javafx.fxml.FXML;
import javafx.scene.control.TextField;
import javafx.scene.layout.Region;
import seedu.address.logic.commands.CommandResult;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.logic.parser.exceptions.ParseException;

Expand Down Expand Up @@ -79,7 +78,7 @@ public interface CommandExecutor {
*
* @see seedu.address.logic.Logic#execute(String)
*/
CommandResult execute(String commandText) throws CommandException, ParseException;
String execute(String commandText) throws CommandException, ParseException;
}

}
29 changes: 14 additions & 15 deletions src/main/java/seedu/address/ui/MainWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import seedu.address.commons.core.GuiSettings;
import seedu.address.commons.core.LogsCenter;
import seedu.address.logic.Logic;
import seedu.address.logic.commands.CommandResult;
import seedu.address.logic.Messages;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.logic.parser.exceptions.ParseException;

Expand Down Expand Up @@ -172,25 +172,24 @@ public PersonListPanel getPersonListPanel() {
*
* @see seedu.address.logic.Logic#execute(String)
*/
private CommandResult executeCommand(String commandText) throws CommandException, ParseException {
private String executeCommand(String commandText) throws CommandException, ParseException {
String commandResult;
try {
CommandResult commandResult = logic.execute(commandText);
logger.info("Result: " + commandResult.getFeedbackToUser());
resultDisplay.setFeedbackToUser(commandResult.getFeedbackToUser());

if (commandResult.isShowHelp()) {
handleHelp();
}

if (commandResult.isExit()) {
handleExit();
}

return commandResult;
commandResult = logic.execute(commandText);
logger.info("Result: " + commandResult);
resultDisplay.setFeedbackToUser(commandResult);
} catch (CommandException | ParseException e) {
logger.info("An error occurred while executing command: " + commandText);
resultDisplay.setFeedbackToUser(e.getMessage());
throw e;
}

if (commandResult.equals(Messages.MESSAGE_SHOWING_HELP)) {
handleHelp();
}
if (commandResult.equals(Messages.MESSAGE_EXIT_ACKNOWLEDGEMENT)) {
handleExit();
}
return commandResult;
}
}
5 changes: 2 additions & 3 deletions src/test/java/seedu/address/logic/LogicManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import org.junit.jupiter.api.io.TempDir;

import seedu.address.logic.commands.AddCommand;
import seedu.address.logic.commands.CommandResult;
import seedu.address.logic.commands.ListCommand;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.logic.parser.exceptions.ParseException;
Expand Down Expand Up @@ -96,8 +95,8 @@ public void getFilteredPersonList_modifyList_throwsUnsupportedOperationException
*/
private void assertCommandSuccess(String inputCommand, String expectedMessage,
Model expectedModel) throws CommandException, ParseException {
CommandResult result = logic.execute(inputCommand);
assertEquals(expectedMessage, result.getFeedbackToUser());
String result = logic.execute(inputCommand);
assertEquals(expectedMessage, result);
assertEquals(expectedModel, model);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ public void execute_personAcceptedByModel_addSuccessful() throws Exception {
ModelStubAcceptingPersonAdded modelStub = new ModelStubAcceptingPersonAdded();
Person validPerson = new PersonBuilder().build();

CommandResult commandResult = new AddCommand(validPerson).execute(modelStub);
String commandResult = new AddCommand(validPerson).execute(modelStub);

assertEquals(String.format(AddCommand.MESSAGE_SUCCESS, Messages.format(validPerson)),
commandResult.getFeedbackToUser());
commandResult);
assertEquals(Arrays.asList(validPerson), modelStub.personsAdded);
}

Expand Down
Loading