Skip to content
This repository has been archived by the owner on Feb 6, 2020. It is now read-only.

Commit

Permalink
Merge pull request #4 from shawnkoh/branch-A-CodeQuality
Browse files Browse the repository at this point in the history
Refactor the codebase to remove unnecessary runtime exceptions
  • Loading branch information
shawnkoh authored Sep 5, 2019
2 parents 42f1f3c + 8bea6e1 commit 03b2595
Show file tree
Hide file tree
Showing 23 changed files with 247 additions and 206 deletions.
17 changes: 6 additions & 11 deletions src/main/java/duke/Duke.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
import duke.parser.Parser;
import duke.storage.Storage;
import duke.task.TaskList;
import duke.ui.Ui;

import java.io.IOException;

/**
* A task management application.
Expand All @@ -16,20 +17,14 @@ public class Duke {
private boolean isActive;
private Storage storage;
private TaskList tasks;
private Ui ui;

/**
* Returns a {@link Duke}.
*/
public Duke() {
public Duke() throws DukeException, IOException {
isActive = true;
ui = new Ui();
storage = new Storage(STORAGE_PATHNAME);
try {
tasks = new TaskList(storage.load());
} catch (DukeException e) {
tasks = new TaskList();
}
tasks = new TaskList(storage.load());
}

/**
Expand All @@ -38,10 +33,10 @@ public Duke() {
* @param input The input to parse and execute
* @return The response from Duke.
*/
public String getResponse(String input) {
public String getResponse(String input) throws IOException {
try {
Command c = Parser.parse(input);
c.setData(tasks, ui, storage);
c.setData(tasks, storage);
CommandResult result = c.execute();
isActive = !c.isExit();
return result.feedback;
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/duke/Main.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package duke;

import duke.exception.DukeException;
import duke.ui.MainWindow;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
Expand All @@ -13,6 +14,9 @@ public class Main extends Application {

private Duke duke = new Duke();

public Main() throws IOException, DukeException {
}

@Override
public void start(Stage primaryStage) {
try {
Expand Down
18 changes: 14 additions & 4 deletions src/main/java/duke/command/AddCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

import duke.exception.DukeException;
import duke.task.Task;
import duke.ui.Ui;

import java.io.IOException;

import static java.lang.System.lineSeparator;
import static java.util.Objects.requireNonNull;

/**
* Represents a {@link Command} to add a {@link Task}.
Expand All @@ -12,20 +16,26 @@ public class AddCommand extends Command {

private final Task toAdd;

/**
* Creates a {@link Command} that will create a {@link Task} when executed.
*
* @param task the Task to be created
*/
public AddCommand(Task task) {
this.toAdd = task;
this.toAdd = requireNonNull(task);
}

@Override
public CommandResult execute() throws DukeException {
public CommandResult execute() throws IOException {
tasks.add(toAdd);
String data = toAdd.stringify();
if (tasks.size() > 1) {
data = lineSeparator() + data;
}
storage.append(data);
return new CommandResult("Got it. I've added this task:"
+ lineSeparator() + ui.INDENT + toAdd
+ lineSeparator() + "Now you have " + tasks.size() + " tasks in the list.");
+ lineSeparator() + Ui.INDENT + toAdd
+ lineSeparator() + "Now you have " + tasks.size() + " tasks in the list."
);
}
}
18 changes: 9 additions & 9 deletions src/main/java/duke/command/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@
import duke.exception.DukeException;
import duke.storage.Storage;
import duke.task.TaskList;
import duke.ui.Ui;

import java.io.IOException;

import static java.util.Objects.requireNonNull;

/**
* Represents a user command.
*/
public abstract class Command {

protected TaskList tasks;
protected Ui ui;
protected Storage storage;
protected boolean isExit;

Expand All @@ -21,19 +23,17 @@ public abstract class Command {
*
* @throws DukeException If the command failed to execute.
*/
public abstract CommandResult execute() throws DukeException;
public abstract CommandResult execute() throws DukeException, IOException;

/**
* Sets the data to be used by the {@link Command}.
*
* @param tasks List of tasks.
* @param ui UI interface.
* @param tasks List of tasks.
* @param storage Storage.
*/
public void setData(TaskList tasks, Ui ui, Storage storage) {
this.tasks = tasks;
this.ui = ui;
this.storage = storage;
public void setData(TaskList tasks, Storage storage) {
this.tasks = requireNonNull(tasks);
this.storage = requireNonNull(storage);
}

/**
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/duke/command/DeadlineCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@
public class DeadlineCommand extends AddCommand {

public static final String COMMAND_WORD = "deadline";
public static final String MESSAGE_USAGE = COMMAND_WORD
+ ": Adds a task with a deadline" + System.lineSeparator()
+ "Usage: " + COMMAND_WORD + " <description> /by <dueDate>";

public DeadlineCommand(String description, Date by) {
super(new Deadline(description, by));
public DeadlineCommand(String description, Date dueDate) {
super(new Deadline(description, dueDate));
}
}
22 changes: 18 additions & 4 deletions src/main/java/duke/command/DeleteCommand.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package duke.command;

import duke.exception.DukeException;
import duke.task.Task;
import duke.ui.Ui;

import java.io.IOException;

import static java.lang.System.lineSeparator;

Expand All @@ -11,19 +13,31 @@
public class DeleteCommand extends Command {

public static final String COMMAND_WORD = "delete";
public static final String MESSAGE_USAGE = COMMAND_WORD
+ ": Deletes the task with the specified ID" + System.lineSeparator()
+ "Usage: " + COMMAND_WORD + " <id>";
private final int toDelete;

/**
* Creates a {@link Command} that deletes a {@link Task} when executed.
*
* @param id the id of the {@link Task} to be deleted.
*/
public DeleteCommand(int id) {
assert id >= 0 && id < tasks.size();
this.toDelete = id;
}

@Override
public CommandResult execute() throws DukeException {
public CommandResult execute() throws IOException {
if (toDelete < 0 || toDelete >= tasks.size()) {
return new IncorrectCommand(MESSAGE_USAGE).execute();
}
final Task task = tasks.delete(toDelete);
storage.save(tasks.stringify());
return new CommandResult("Noted. I've removed this task:"
+ lineSeparator() + ui.INDENT + task
+ lineSeparator() + "Now you have " + tasks.size() + " tasks in the list.");
+ lineSeparator() + Ui.INDENT + task
+ lineSeparator() + "Now you have " + tasks.size() + " tasks in the list."
);
}
}
28 changes: 18 additions & 10 deletions src/main/java/duke/command/DoneCommand.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package duke.command;

import duke.exception.DukeException;
import duke.task.Task;
import duke.ui.Ui;

import java.io.IOException;

import static java.lang.System.lineSeparator;

Expand All @@ -11,24 +13,30 @@
public class DoneCommand extends Command {

public static final String COMMAND_WORD = "done";
public static final String MESSAGE_USAGE = COMMAND_WORD
+ ": Marks the task with the specified ID as complete"
+ lineSeparator() + "Usage: " + COMMAND_WORD + " <id>";

private final int toMarkAsDone;

/**
* Creates a {@link Command} that marks a {@link Task} as complete when executed.
*
* @param id the id of the task to be marked as complete
*/
public DoneCommand(int id) {
assert id >= 0 && id < tasks.size();
this.toMarkAsDone = id;
}

@Override
public CommandResult execute() throws DukeException {
try {
Task task = tasks.get(toMarkAsDone);
task.markAsDone();
storage.save(tasks.stringify());
return new CommandResult("Nice! I've marked this task as done:"
+ lineSeparator() + ui.INDENT + task);
} catch (IndexOutOfBoundsException e) {
throw new DukeException("Marking task with ID " + toMarkAsDone + " failed: Invalid ID");
public CommandResult execute() throws IOException {
if (toMarkAsDone < 0 || toMarkAsDone >= tasks.size()) {
return new IncorrectCommand(MESSAGE_USAGE).execute();
}
Task task = tasks.get(toMarkAsDone);
task.markAsDone();
storage.save(tasks.stringify());
return new CommandResult("Nice! I've marked this task as done:" + lineSeparator() + Ui.INDENT + task);
}
}
7 changes: 5 additions & 2 deletions src/main/java/duke/command/EventCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@
public class EventCommand extends AddCommand {

public static final String COMMAND_WORD = "event";
public static final String MESSAGE_USAGE = COMMAND_WORD
+ ": Creates a task with a date" + System.lineSeparator()
+ "Usage: " + COMMAND_WORD + " <description> /at <date>";

public EventCommand(String description, Date at) {
super(new Event(description, at));
public EventCommand(String description, Date date) {
super(new Event(description, date));
}
}
6 changes: 5 additions & 1 deletion src/main/java/duke/command/FindCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@
import duke.task.TaskList;

import static java.lang.System.lineSeparator;
import static java.util.Objects.requireNonNull;

public class FindCommand extends Command {

public static final String COMMAND_WORD = "find";
public static final String MESSAGE_USAGE = COMMAND_WORD
+ ": Finds and displays a list of Tasks that match the keyword"
+ lineSeparator() + "Usage: " + COMMAND_WORD + " <keyword>";
private final String keyword;

public FindCommand(String keyword) {
this.keyword = keyword;
this.keyword = requireNonNull(keyword);
}

@Override
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/duke/command/IncorrectCommand.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
package duke.command;

import static java.util.Objects.requireNonNull;

/**
* Represents a {@link Command} to inform the user that he has entered an incorrect command.
*/
public class IncorrectCommand extends Command {

private final String message;

public IncorrectCommand(String message) {
this.message = requireNonNull(message);
}

@Override
public CommandResult execute() {
return new CommandResult("☹ OOPS!!! I'm sorry, but I don't know what that means :-(");
return new CommandResult(message);
}
}
3 changes: 2 additions & 1 deletion src/main/java/duke/command/ListCommand.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package duke.command;

import duke.task.Task;
import duke.ui.Ui;

import static java.lang.System.lineSeparator;

Expand All @@ -20,7 +21,7 @@ public CommandResult execute() {
StringBuilder feedback = new StringBuilder("Here are the tasks in your list:");
for (int i = 1; i <= tasks.size(); i++) {
Task task = tasks.get(i - 1);
feedback.append(lineSeparator() + i + ". " + task);
feedback.append(lineSeparator() + Ui.INDENT + i + ". " + task);
}
return new CommandResult(feedback.toString());
}
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/duke/command/ToDoCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@

import duke.task.ToDo;

import static java.lang.System.lineSeparator;

/**
* Represents a {@link Command} to add a {@link ToDo}.
*/
public class ToDoCommand extends AddCommand {

public static final String COMMAND_WORD = "todo";
public static final String MESSAGE_USAGE = COMMAND_WORD
+ ": Create a new task" + lineSeparator()
+ "Usage: " + COMMAND_WORD + " <description>";

public ToDoCommand(String description) {
super(new ToDo(description));
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/duke/command/UnknownCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package duke.command;

/**
* Represents a {@link Command} to inform the user that he has entered an unknown command.
*/
public class UnknownCommand extends Command {

@Override
public CommandResult execute() {
return new CommandResult("☹ OOPS!!! I'm sorry, but I don't know what that means :-(");
}
}
Loading

0 comments on commit 03b2595

Please sign in to comment.