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

Critically examined the code and refactored it to improve the code qu… #5

Merged
merged 1 commit into from
Sep 30, 2019
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
2 changes: 1 addition & 1 deletion src/main/java/AddCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public AddCommand (Task task) {
* @return
*/
@Override
public String execute(TaskList taskList, Ui ui, Storage storage) throws IOException {
public String execute(TaskList taskList, Ui ui, Storage storage) throws DukeException {

taskList.add(task);

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public abstract class Command {
* @param storage
* @throws IOException
*/
public abstract String execute (TaskList taskList, Ui ui, Storage storage) throws IOException;
public abstract String execute (TaskList taskList, Ui ui, Storage storage) throws DukeException;

/**
* Checks whether the command is to exit the program
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/DeleteCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public DeleteCommand (int index) {
* @throws IOException
*/
@Override
public String execute(TaskList taskList, Ui ui, Storage storage) throws IOException {
public String execute(TaskList taskList, Ui ui, Storage storage) throws DukeException {

Task task = taskList.delete(index);

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/DoneCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public DoneCommand (int index) {
* @throws IOException
*/
@Override
public String execute(TaskList taskList, Ui ui, Storage storage) throws IOException {
public String execute(TaskList taskList, Ui ui, Storage storage) throws DukeException {

Task task = taskList.done(this.index);

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/Duke.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public String run (String fullCommand) {
Command c = Parser.parse(fullCommand);
this.isExit = c.isExit();
return c.execute(tasks, ui, storage);
} catch (IOException | DukeException e) {
} catch (DukeException e) {
return ui.showError(e.getMessage());
}

Expand Down
4 changes: 4 additions & 0 deletions src/main/java/DukeException.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,8 @@ public static DukeException emptyDescription () {
public static DukeException outOfBounds () {
return new DukeException("☹ OOPS!!! The number you inputted is out of bounds \uD83D\uDE22");
}

public static DukeException couldNotSave () {
return new DukeException("☹ OOPS!!! Your task could not be saved/updated/deleted \uD83D\uDE22");
}
}
2 changes: 1 addition & 1 deletion src/main/java/ExitCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class ExitCommand extends Command {
* @throws IOException
*/
@Override
public String execute(TaskList taskList, Ui ui, Storage storage) throws IOException {
public String execute(TaskList taskList, Ui ui, Storage storage) throws DukeException {
return ui.bye();
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/FindCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public FindCommand(String text) {


@Override
public String execute(TaskList taskList, Ui ui, Storage storage) throws IOException {
public String execute(TaskList taskList, Ui ui, Storage storage) throws DukeException {

Task[] matchedTasks = taskList.find(text);

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/ListCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class ListCommand extends Command {
* @throws IOException
*/
@Override
public String execute(TaskList taskList, Ui ui, Storage storage) throws IOException {
public String execute(TaskList taskList, Ui ui, Storage storage) throws DukeException {

return ui.list(taskList.list);
}
Expand Down
21 changes: 15 additions & 6 deletions src/main/java/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,26 @@ public ArrayList<Task> retrieve () throws FileNotFoundException {
/**
* Saves the Tasks in a file
* @param list The list of tasks to save
* @throws IOException
* @throws DukeException
*/
public void save (ArrayList<Task> list) throws IOException {
public void save (ArrayList<Task> list) throws DukeException {

try {

String s = "";

for (Task t : list) {
s += t.saveFormat() + System.lineSeparator();
}

this.writeToFile(filePath, s);

} catch (IOException e) {

String s = "";
throw DukeException.couldNotSave();

for (Task t : list) {
s += t.saveFormat() + System.lineSeparator();
}

this.writeToFile(filePath, s);
}

}
9 changes: 7 additions & 2 deletions src/main/java/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ public void add (Task task) {
* @param index The Index of the Task
* @return
*/
public Task done (int index) {
public Task done (int index) throws DukeException {

if (index < 0 || index > list.size()) throw DukeException.outOfBounds();

Task task = list.get(index);
task.markAsDone();

Expand All @@ -43,7 +46,9 @@ public Task done (int index) {
* @param index The index of the task
* @return The deleted task
*/
public Task delete (int index) {
public Task delete (int index) throws DukeException {

if (index < 0 || index > list.size()) throw DukeException.outOfBounds();

Task task = list.remove(index);
return task;
Expand Down