Skip to content

Commit

Permalink
Standardise error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
simonteozw committed Sep 4, 2020
1 parent 6dd68d1 commit 4e3158d
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 24 deletions.
13 changes: 13 additions & 0 deletions src/main/java/duke/Duke.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package duke;

import java.io.IOException;

public class Duke {

private TaskList taskList;
Expand All @@ -12,6 +14,17 @@ public class Duke {
* @return A string representing Duke's response.
*/
public String getResponse(String input) {
if (taskList.isEmpty()) {
try {
taskList.initialise();
} catch (IOException e) {
return e.toString();
} catch (InvalidTypeException e) {
return e.toString();
} catch (InvalidDataException e) {
return e.toString();
}
}
return parser.allocate(input, taskList);
}

Expand Down
10 changes: 8 additions & 2 deletions src/main/java/duke/Parser.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package duke;

import java.io.IOException;

/**
* Parser class to handle user inputs.
*/
Expand Down Expand Up @@ -51,8 +53,12 @@ public static String allocate(String input, TaskList tl) {

switch (arr[0]) {
case "bye":
tl.saveData();
return Ui.GOODBYE;
try {
tl.saveData();
return Ui.GOODBYE;
} catch (IOException e) {
return e.toString();
}
case "list":
return tl.display();
case "find":
Expand Down
12 changes: 4 additions & 8 deletions src/main/java/duke/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public void writeData(ArrayList<Task> items) throws IOException {
*
* @return An ArrayList of tasks.
*/
public ArrayList<Task> readData() {
public ArrayList<Task> readData() throws IOException, InvalidTypeException, InvalidDataException {
boolean pathExists = Files.exists(PATH);

ArrayList<Task> res = new ArrayList<>();
Expand All @@ -112,17 +112,13 @@ public ArrayList<Task> readData() {
res.add(stringToTask(str));
}
} catch (IOException e) {
e.printStackTrace();
Ui.addLine(this.IO_MESSAGE);
throw e;
} catch (InvalidTypeException e) {
System.out.println(e.toString());
Ui.addLine(this.INVALID_MESSAGE);
throw e;
} catch (InvalidDataException e) {
System.out.println(e.toString());
Ui.addLine(this.INVALID_MESSAGE);
throw e;
}
}

return res;
}
}
22 changes: 19 additions & 3 deletions src/main/java/duke/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,33 @@ public class TaskList {
*/
public TaskList() {
this.storage = new Storage();
this.items = storage.readData();
this.items = new ArrayList<>();
}

public void initialise() throws IOException, InvalidTypeException, InvalidDataException {
try {
this.items = this.storage.readData();
} catch (IOException e) {
throw e;
} catch (InvalidTypeException e) {
throw e;
} catch (InvalidDataException e) {
throw e;
}
}

public boolean isEmpty() {
return this.items.isEmpty();
}

/**
* Exit function for TaskList class, writes data to .txt file.
*/
public void saveData() {
public void saveData() throws IOException {
try {
storage.writeData(this.items);
} catch (IOException e) {
e.printStackTrace();
throw e;
}
}

Expand Down
11 changes: 0 additions & 11 deletions src/main/java/duke/Ui.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,4 @@ public class Ui {

static final String WELCOME = OPENING + "\nHello I am Duke!\nWhat can I help you with?";
static final String GOODBYE = "Goodbye. See you soon!";
private static final String LINE = "--------------------------------------------------";

/**
* Adds line under given input for aesthetics.
*
* @param input Input for which line shall be printed under.
*/
public static void addLine(String input) {
System.out.println(input);
System.out.println(LINE);
}
}

0 comments on commit 4e3158d

Please sign in to comment.