Skip to content

Commit

Permalink
Merge pull request #3 from muller317/branch-A-CodeQuality
Browse files Browse the repository at this point in the history
Refactor command classes to reduce code duplication
  • Loading branch information
muller317 authored Sep 15, 2024
2 parents 590eeec + c6079d0 commit e648bfc
Show file tree
Hide file tree
Showing 13 changed files with 116 additions and 53 deletions.
3 changes: 3 additions & 0 deletions src/main/java/MainWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public class MainWindow extends AnchorPane {
private Image userImage = new Image(this.getClass().getResourceAsStream("/images/DaUser.png"));
private Image mullerImage = new Image(this.getClass().getResourceAsStream("/images/DaMuller.png"));

/**
* Starts of the program with greetings and a GUI.
*/
@FXML
public void initialize() {
String welcomeMessage = "Hello! I'm Muller!\nWhat can I do for you?";
Expand Down
1 change: 0 additions & 1 deletion src/main/java/muller/Muller.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ public String getResponse(String input) {
// Programmer-level assumption: input should not be null or empty
assert input != null : "Input should not be null";
assert !input.trim().isEmpty() : "Input should not be empty";

Parser parser = new Parser();
try {
Command command = parser.parse(input);
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/muller/command/AddCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public AddCommand(String[] commandInputs, String taskType) {

@Override
public String execute(TaskList tasks, Ui ui, Storage storage) throws MullerException {
super.assertionTest(tasks, ui, storage);
if (commandInputs.length < 2) {
CommandUtil.assertionTest(tasks, ui, storage);
if (CommandUtil.isInputComplete(commandInputs)) {
throw new MullerException(taskType.equals("T") ? "Todo what?"
: taskType.equals("D") ? "Deadline for what?" : "Event for what?");
}
Expand Down Expand Up @@ -65,7 +65,7 @@ public String execute(TaskList tasks, Ui ui, Storage storage) throws MullerExcep
*/
private Task parseDeadline(String details) throws MullerException {
String[] detailParts = details.split("/by", 2);
if (detailParts.length < 2) {
if (CommandUtil.isInputComplete(detailParts)) {
throw new MullerException("Oops, you didn't specify the deadline!");
}
Task task = new Task(detailParts[0].trim());
Expand All @@ -83,12 +83,12 @@ private Task parseDeadline(String details) throws MullerException {
* @throws MullerException If the date format is invalid.
*/
private Task parseEvent(String details) throws MullerException {
String[] detailParts = details.split("/from", 2);
if (detailParts.length < 2) {
String[] detailParts = details.split("/from", 2); //A String array that separates event name and relevant dates.
if (CommandUtil.isInputComplete(detailParts)) { //Checks if dates are specified.
throw new MullerException("Oops, you didn't specify the start date!");
}
String[] dateParts = detailParts[1].split("/to", 2);
if (dateParts.length < 2) {
String[] dateParts = detailParts[1].split("/to", 2); // A String array that separates start date and end date.
if (CommandUtil.isInputComplete(dateParts)) { //Checks if end date/start date is specified.
throw new MullerException("You missed out either the start or end date!");
}
Task task = new Task(detailParts[0].trim());
Expand Down
13 changes: 0 additions & 13 deletions src/main/java/muller/command/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,6 @@ public abstract class Command {
* @throws MullerException If an error occurs during command execution.
*/
public abstract String execute(TaskList tasks, Ui ui, Storage storage) throws MullerException;

/**
* Checks if the input task, ui, storage are not null.
*
* @param tasks
* @param ui
* @param storage
*/
public void assertionTest(TaskList tasks, Ui ui, Storage storage) {
assert tasks != null : "TaskList should not be null";
assert ui != null : "Ui should not be null";
assert storage != null : "Storage should not be null";
}
}


89 changes: 89 additions & 0 deletions src/main/java/muller/command/CommandUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package muller.command;

import muller.storage.Storage;
import muller.task.TaskList;
import muller.ui.Ui;

/**
* Utility class for common validation methods.
*/
public class CommandUtil {
/**
* Checks if the input task, ui, storage are not null.
*
* @param tasks
* @param ui
* @param storage
*/
public static void assertionTest(TaskList tasks, Ui ui, Storage storage) {
assert tasks != null : "TaskList should not be null";
assert ui != null : "Ui should not be null";
assert storage != null : "Storage should not be null";
}

/**
* Checks if the input task index is valid.
*
* @param index
* @param taskListSize
* @return
*/
public static boolean isValidTaskIndex(int index, int taskListSize) {
return index >= 0 && index < taskListSize;
}

/**
* Check if the input is complete for different tasks.
*
* @param commandInputs
* @return
*/
public static boolean isInputComplete(String[] commandInputs) {
return commandInputs.length < 2;
}

/**
* Checks if the input string is a numeric value.
*
* @param str The input string.
* @return True if the string is numeric, false otherwise.
*/
public static boolean isNumeric(String str) {
try {
Integer.parseInt(str);
return true;
} catch (NumberFormatException e) {
return false;
}
}

/**
* Check the command to delete tasks is complete
*
* @param commandInputs
* @return
*/
public static boolean isDeleteCommandValid(String[] commandInputs) {
return commandInputs.length < 2 || !isNumeric(commandInputs[1]);
}

/**
* Check if the input is complete for different tasks.
*
* @param commandInputs
* @return
*/
public static boolean isFindCommandValid(String[] commandInputs) {
return commandInputs.length < 2 || commandInputs[1].trim().isEmpty();
}

/**
* Check the command to delete tasks is complete
*
* @param commandInputs
* @return
*/
public static boolean isMarkCommandValid(String[] commandInputs) {
return commandInputs.length < 2 || !isNumeric(commandInputs[1]);
}
}
24 changes: 5 additions & 19 deletions src/main/java/muller/command/DeleteCommand.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package muller.command;

import muller.Muller;
import muller.storage.Storage;
import muller.task.Task;
import muller.task.TaskList;
Expand All @@ -19,17 +18,19 @@ public class DeleteCommand extends Command {
* @throws MullerException If the input is not a valid task number.
*/
public DeleteCommand(String[] inputs) throws MullerException {
if (inputs.length < 2 || !isNumeric(inputs[1])) {
if (CommandUtil.isDeleteCommandValid(inputs)) {
throw new MullerException("Pick a valid task number to delete!");
}

//Resize the index to make sure align with 0-indexed list.
this.index = Integer.parseInt(inputs[1]) - 1;
}

@Override
public String execute(TaskList tasks, Ui ui, Storage storage) throws MullerException {
super.assertionTest(tasks, ui, storage);
CommandUtil.assertionTest(tasks, ui, storage);
try {
if (index < 0 || index >= tasks.getSize()) {
if (CommandUtil.isValidTaskIndex(index, tasks.getSize())) {
throw new MullerException("Invalid task number!");
}
Task deletedTask = tasks.get(index);
Expand All @@ -40,19 +41,4 @@ public String execute(TaskList tasks, Ui ui, Storage storage) throws MullerExcep
return e.getMessage();
}
}

/**
* Checks if the input string is a numeric value.
*
* @param str The input string.
* @return True if the string is numeric, false otherwise.
*/
private boolean isNumeric(String str) {
try {
Integer.parseInt(str);
return true;
} catch (NumberFormatException e) {
return false;
}
}
}
2 changes: 1 addition & 1 deletion src/main/java/muller/command/ExitCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
public class ExitCommand extends Command {
@Override
public String execute(TaskList tasks, Ui ui, Storage storage) {
super.assertionTest(tasks, ui, storage);
CommandUtil.assertionTest(tasks, ui, storage);
return "Bye. Hope to see you again soon!";
}
}
4 changes: 2 additions & 2 deletions src/main/java/muller/command/FindCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ public class FindCommand extends Command {
* @throws MullerException If the keyword is missing.
*/
public FindCommand(String[] inputs) throws MullerException {
if (inputs.length < 2 || inputs[1].trim().isEmpty()) {
if (CommandUtil.isFindCommandValid(inputs)) {
throw new MullerException("Please provide a keyword to search for!");
}
this.keyword = inputs[1].trim();
}

@Override
public String execute(TaskList tasks, Ui ui, Storage storage) {
super.assertionTest(tasks, ui, storage);
CommandUtil.assertionTest(tasks, ui, storage);
List<Task> matchingTasks = tasks.findTasksByKeyword(keyword);
return ui.showMatchingTasks(matchingTasks);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/muller/command/ListCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
public class ListCommand extends Command {
@Override
public String execute(TaskList tasks, Ui ui, Storage storage) {
super.assertionTest(tasks, ui, storage);
CommandUtil.assertionTest(tasks, ui, storage);
return ui.showTaskList(tasks);
}
}
4 changes: 2 additions & 2 deletions src/main/java/muller/command/MarkCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ public class MarkCommand extends Command {
* @throws MullerException If the input is not a valid task number.
*/
public MarkCommand(String[] inputs) throws MullerException {
if (inputs.length < 2 || !isNumeric(inputs[1])) {
if (CommandUtil.isMarkCommandValid(inputs)) {
throw new MullerException("Pick a valid task number to mark!");
}
this.index = Integer.parseInt(inputs[1]) - 1;
}

@Override
public String execute(TaskList tasks, Ui ui, Storage storage) throws MullerException {
super.assertionTest(tasks, ui, storage);
CommandUtil.assertionTest(tasks, ui, storage);
tasks.get(index).markAsDone();
storage.saveTasks(tasks);
return ui.showTaskMarked(tasks, index);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/muller/command/OnCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class OnCommand extends Command {
* @throws MullerException If the input date format is invalid.
*/
public OnCommand(String[] inputs) throws MullerException {
if (inputs.length < 2) {
if (CommandUtil.isInputComplete(inputs)) {
throw new MullerException("Specify a date (e.g., 'on 2019-10-15')!");
}
try {
Expand All @@ -34,7 +34,7 @@ public OnCommand(String[] inputs) throws MullerException {

@Override
public String execute(TaskList tasks, Ui ui, Storage storage) {
super.assertionTest(tasks, ui, storage);
CommandUtil.assertionTest(tasks, ui, storage);
return ui.showTaskOnDate(tasks, date);
}
}
4 changes: 2 additions & 2 deletions src/main/java/muller/command/UnmarkCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ public class UnmarkCommand extends Command {
* @throws MullerException If the input is not a valid task number.
*/
public UnmarkCommand(String[] inputs) throws MullerException {
if (inputs.length < 2 || !isNumeric(inputs[1])) {
if (CommandUtil.isMarkCommandValid(inputs)) {
throw new MullerException("Pick a valid task number to unmark!");
}
this.index = Integer.parseInt(inputs[1]) - 1;
}

@Override
public String execute(TaskList tasks, Ui ui, Storage storage) throws MullerException {
super.assertionTest(tasks, ui, storage);
CommandUtil.assertionTest(tasks, ui, storage);
tasks.get(index).markAsNotDone();
storage.saveTasks(tasks);
return ui.showTaskUnMarked(tasks, index);
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/muller/ui/Ui.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.time.LocalDate;
import java.util.List;
import java.util.Scanner;

import muller.command.MullerException;
import muller.task.Task;
Expand Down Expand Up @@ -54,7 +53,7 @@ public String showTaskMarked(TaskList tasks, int index) {
try {
return ("Nice! I've marked this task as done:\n"
+ " " + tasks.get(index));
} catch (MullerException e){
} catch (MullerException e) {
return e.getMessage();
}
}
Expand All @@ -69,7 +68,7 @@ public String showTaskUnMarked(TaskList tasks, int index) {
try {
return ("OK, I've marked this task as not done yet:\n"
+ " " + tasks.get(index));
} catch (MullerException e){
} catch (MullerException e) {
return e.getMessage();
}
}
Expand Down

0 comments on commit e648bfc

Please sign in to comment.