Skip to content

Commit

Permalink
Merge pull request #138 from AY2021S1-CS2103T-T10-3/feature/69
Browse files Browse the repository at this point in the history
Make commands print out something sensible when they're executed
  • Loading branch information
seowalex authored Oct 25, 2020
2 parents 7954d4a + 092616d commit e5ab0fe
Show file tree
Hide file tree
Showing 28 changed files with 311 additions and 220 deletions.
17 changes: 0 additions & 17 deletions src/main/java/chopchop/commons/core/Messages.java

This file was deleted.

27 changes: 18 additions & 9 deletions src/main/java/chopchop/logic/commands/AddIngredientCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ public class AddIngredientCommand extends Command implements Undoable {
+ ARG_QUANTITY + " 3 "
+ ARG_EXPIRY + " 2020-10-05";

public static final String MESSAGE_ADD_INGREDIENT_SUCCESS = "Ingredient added: %s";
public static final String MESSAGE_COMBINE_INGREDIENT_SUCCESS = "Ingredient updated: %s";
public static final String MESSAGE_UNDO_SUCCESS = "Ingredient updated: %s";

private final Ingredient ingredient;
private Ingredient existingIngredient;
private Ingredient combinedIngredient;
Expand All @@ -54,27 +50,40 @@ public CommandResult execute(Model model, HistoryManager historyManager) throws
this.combinedIngredient = this.existingIngredient.combine(this.ingredient);
model.setIngredient(this.existingIngredient, this.combinedIngredient);

return new CommandResult(String.format(MESSAGE_COMBINE_INGREDIENT_SUCCESS, this.combinedIngredient));
} catch (IncompatibleIngredientsException e) {
throw new CommandException(e.toString());
return CommandResult.error(e.toString());
}

return CommandResult.message("Updated ingredient '%s'", this.combinedIngredient.getName());

} else {

model.addIngredient(this.ingredient);
return new CommandResult(String.format(MESSAGE_ADD_INGREDIENT_SUCCESS, this.ingredient));
return CommandResult.message("Added ingredient '%s'", this.ingredient.getName());
}
}

@Override
public CommandResult undo(Model model) {
requireNonNull(model);

String action = "";
Ingredient ingr = null;

if (this.existingIngredient == null && this.combinedIngredient == null) {

model.deleteIngredient(this.ingredient);
return new CommandResult(String.format(MESSAGE_UNDO_SUCCESS, this.ingredient));

ingr = this.ingredient;
action = "removed";
} else {
model.setIngredient(this.combinedIngredient, this.existingIngredient);
return new CommandResult(String.format(MESSAGE_UNDO_SUCCESS, this.existingIngredient));

ingr = this.existingIngredient;
action = "updated";
}

return CommandResult.message("Undo: %s ingredient '%s'", action, ingr.getName());
}

@Override
Expand Down
10 changes: 3 additions & 7 deletions src/main/java/chopchop/logic/commands/AddRecipeCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ public class AddRecipeCommand extends Command implements Undoable {
+ ARG_STEP + " Chop tomatoes. "
+ ARG_STEP + " Add sugar to it and mix well.";

public static final String MESSAGE_ADD_RECIPE_SUCCESS = "Recipe added: %s";
public static final String MESSAGE_DUPLICATE_RECIPE = "Recipe '%s' already exists in the recipe book";
public static final String MESSAGE_UNDO_SUCCESS = "Recipe removed: %s";

private final Recipe recipe;

/**
Expand All @@ -47,19 +43,19 @@ public CommandResult execute(Model model, HistoryManager historyManager) throws
requireNonNull(model);

if (model.hasRecipe(this.recipe)) {
throw new CommandException(String.format(MESSAGE_DUPLICATE_RECIPE, this.recipe.getName()));
return CommandResult.error("Recipe '%s' already exists", this.recipe.getName());
}

model.addRecipe(this.recipe);
return new CommandResult(String.format(MESSAGE_ADD_RECIPE_SUCCESS, this.recipe));
return CommandResult.message("Added recipe '%s'", this.recipe.getName());
}

@Override
public CommandResult undo(Model model) {
requireNonNull(model);

model.deleteRecipe(this.recipe);
return new CommandResult(String.format(MESSAGE_UNDO_SUCCESS, this.recipe));
return CommandResult.message("Undo: removed recipe '%s'", this.recipe.getName());
}

@Override
Expand Down
62 changes: 62 additions & 0 deletions src/main/java/chopchop/logic/commands/Command.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
// Command.java

package chopchop.logic.commands;

import chopchop.commons.util.Result;
import chopchop.logic.commands.exceptions.CommandException;
import chopchop.logic.history.HistoryManager;
import chopchop.logic.parser.ItemReference;
import chopchop.model.Model;
import chopchop.model.ingredient.Ingredient;
import chopchop.model.recipe.Recipe;

/**
* Represents a command with hidden internal logic and the ability to be executed.
*/
public abstract class Command {

/**
* Executes the command and returns the result message.
*
Expand All @@ -17,4 +24,59 @@ public abstract class Command {
* @throws CommandException If an error occurs during command execution.
*/
public abstract CommandResult execute(Model model, HistoryManager historyManager) throws CommandException;


/**
* Resolves the ingredient reference, or returns an error message.
*
* @param ref the reference to resolve
* @param model the model to use
* @return the found ingredient, or an error message
*/
public Result<Ingredient> resolveIngredientReference(ItemReference ref, Model model) {

if (ref.isIndexed()) {
var lastShownList = model.getFilteredIngredientList();

if (ref.getZeroIndex() >= lastShownList.size()) {
return Result.error("Ingredient index '%d' is out of range (should be between 1 and %d)",
ref.getOneIndex(), lastShownList.size()
);
}

return Result.of(lastShownList.get(ref.getZeroIndex()));

} else {

return Result.ofOptional(model.findIngredientWithName(ref.getName()),
String.format("No ingredient named '%s'", ref.getName()));
}
}

/**
* Resolves the recipe reference, or returns an error message.
*
* @param ref the reference to resolve
* @param model the model to use
* @return the found recipe, or an error message
*/
public Result<Recipe> resolveRecipeReference(ItemReference ref, Model model) {

if (ref.isIndexed()) {
var lastShownList = model.getFilteredRecipeList();

if (ref.getZeroIndex() >= lastShownList.size()) {
return Result.error("Recipe index '%d' is out of range (should be between 1 and %d)",
ref.getOneIndex(), lastShownList.size()
);
}

return Result.of(lastShownList.get(ref.getZeroIndex()));

} else {

return Result.ofOptional(model.findRecipeWithName(ref.getName()),
String.format("No recipe named '%s'", ref.getName()));
}
}
}
111 changes: 86 additions & 25 deletions src/main/java/chopchop/logic/commands/CommandResult.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// CommandResult.java

package chopchop.logic.commands;

import static java.util.Objects.requireNonNull;
Expand All @@ -8,54 +10,113 @@
* Represents the result of a command execution.
*/
public class CommandResult {
private final String feedbackToUser;

/** Help information should be shown to the user. */
private final String message;
private final boolean isError;
private final boolean showHelp;

/** The application should exit. */
private final boolean exit;
private final boolean shouldExit;

/**
* Constructs a {@code CommandResult} with the specified fields.
*/
public CommandResult(String feedbackToUser, boolean showHelp, boolean exit) {
this.feedbackToUser = requireNonNull(feedbackToUser);
private CommandResult(String message, boolean isError, boolean shouldExit, boolean showHelp) {
requireNonNull(message);

this.message = message;
this.isError = isError;
this.showHelp = showHelp;
this.exit = exit;
this.shouldExit = shouldExit;
}

/**
* Constructs a {@code CommandResult} with the specified {@code feedbackToUser},
* and other fields set to their default value.
* Returns true if the application should exit after this command
*/
public CommandResult(String feedbackToUser) {
this(feedbackToUser, false, false);
public boolean shouldExit() {
return this.shouldExit;
}

public String getFeedbackToUser() {
return this.feedbackToUser;
/**
* Returns true if the app should open the help window
*/
public boolean shouldShowHelp() {
return this.showHelp;
}

public boolean isShowHelp() {
return this.showHelp;
/**
* Returns true if the message should be styled as an error
*/
public boolean isError() {
return this.isError;
}

public boolean isExit() {
return this.exit;
/**
* Returns true if !isError()
*/
public boolean didSucceed() {
return !this.isError;
}

/**
* Returns the message
*/
public String getMessage() {
return this.message;
}

@Override
public boolean equals(Object other) {
return other == this
|| (other instanceof CommandResult
&& this.feedbackToUser.equals(((CommandResult) other).feedbackToUser)
&& this.showHelp == ((CommandResult) other).showHelp
&& this.exit == ((CommandResult) other).exit);
public boolean equals(Object obj) {
if (obj == this) {
return true;
} else if (!(obj instanceof CommandResult)) {
return false;
}

var cr = (CommandResult) obj;

return this.message.equals(cr.message)
&& this.isError == cr.isError
&& this.showHelp == cr.showHelp
&& this.shouldExit == cr.shouldExit;
}

@Override
public int hashCode() {
return Objects.hash(this.feedbackToUser, this.showHelp, this.exit);
return Objects.hash(this.message, this.isError, this.showHelp, this.shouldExit);
}

/**
* Constructs a new command result that only shows a message.
*
* @param message the message to show
*/
public static CommandResult message(String message, Object... args) {
return new CommandResult(String.format(message, args),
/* isError: */ false, /* shouldExit: */ false, /* showHelp: */ false);
}

/**
* Constructs a new command result that shows an error.
*
* @param error the error to show
*/
public static CommandResult error(String error, Object... args) {
return new CommandResult(String.format(error, args),
/* isError: */ true, /* shouldExit: */ false, /* showHelp: */ false);
}

/**
* Constructs a new command result that shows help
*/
public static CommandResult help() {
return new CommandResult("", /* isError: */ false, /* shouldExit: */ false,
/* showHelp: */ true);
}

/**
* Constructs a new command result that quits.
*/
public static CommandResult exit() {
return new CommandResult("", /* isError: */ false, /* shouldExit: */ true,
/* showHelp: */ false);
}
}
Loading

0 comments on commit e5ab0fe

Please sign in to comment.