Skip to content

Commit

Permalink
Modify Ui to be responsible to format exceptions
Browse files Browse the repository at this point in the history
To be consistent across operating systems, Ui now checks for the
operating system's line separator and standardises all tabbing
to 4 spaces.
  • Loading branch information
yisiox committed Feb 12, 2024
1 parent bc647c6 commit 8c55e57
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 52 deletions.
22 changes: 13 additions & 9 deletions src/main/java/earl/logic/DeadlineHandler.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package earl.logic;

import earl.exceptions.EarlException;
import earl.tasks.Task;
import earl.tasks.TaskType;
import earl.util.TaskList;
import earl.util.Ui;
Expand All @@ -20,18 +21,21 @@ public DeadlineHandler(String args) {
public void handle(TaskList tasks, Ui ui) throws EarlException {
try {
String[] data = args.split("\\s+/by\\s+");
tasks.add(TaskType.DEADLINE.createTask(data));
ui.makeResponse("Added new deadline.",
"\t" + tasks.get(tasks.getSize() - 1),
"There are " + tasks.getSize() + " task(s) tracked.");
Task added = tasks.add(TaskType.DEADLINE.createTask(data));
ui.buildResponse("Added new deadline.");
ui.buildResponse(ui.leftPad(added.toString()));
ui.buildResponse("There are " + tasks.getSize()
+ " task(s) tracked.");
ui.completeResponse();
} catch (IndexOutOfBoundsException e) {
throw new EarlException(
"Error, invalid deadline format.\n"
+ "\tExample use:\n\t"
+ "\tdeadline <task_name> /by <end>");
ui.appendNewline("Error, invalid deadline format.")
+ ui.appendNewline("Example use:")
+ ui.leftPad("deadline <name> /by <due>"));
} catch (Exception e) {
throw new EarlException("Error, unknown use of deadline.\n"
+ e.getMessage());
throw new EarlException(
ui.appendNewline("Error, unknown use of deadline.")
+ ui.leftPad(e.getMessage()));
}
}
}
5 changes: 3 additions & 2 deletions src/main/java/earl/logic/DeleteHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ public void handle(TaskList tasks, Ui ui) throws EarlException {
} catch (EarlException e) {
throw e;
} catch (Exception e) {
throw new EarlException("Error, unknown use of delete.\n"
+ "\t" + e.getMessage());
throw new EarlException(
ui.appendNewline("Error, unknown use of delete.")
+ ui.leftPad(e.getMessage()));
}
}
}
24 changes: 15 additions & 9 deletions src/main/java/earl/logic/EventHandler.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package earl.logic;

import earl.exceptions.EarlException;
import earl.tasks.Task;
import earl.tasks.TaskType;
import earl.util.TaskList;
import earl.util.Ui;
Expand All @@ -19,17 +20,22 @@ public EventHandler(String args) {
public void handle(TaskList tasks, Ui ui) throws EarlException {
try {
String[] data = args.split("\\s+/(from|to)\\s+");
tasks.add(TaskType.EVENT.createTask(data));
ui.makeResponse("Added new event.",
"\t" + tasks.get(tasks.getSize() - 1),
"There are " + tasks.getSize() + " task(s) tracked.");
Task added = tasks.add(TaskType.EVENT.createTask(data));
ui.buildResponse("Added new event.");
ui.buildResponse(ui.leftPad(added.toString()));
ui.buildResponse("There are " + tasks.getSize()
+ " task(s) tracked.");
ui.completeResponse();
} catch (IndexOutOfBoundsException e) {
throw new EarlException("Error, invalid event format.\n"
+ "\tExample use:\n\t"
+ "\tevent <task_name> /from <start> /to <end>");
throw new EarlException(
ui.appendNewline("Error, invalid event format.")
+ ui.appendNewline("Example use:")
+ ui.leftPad("event <name>"
+ " /from <start> /to <end>"));
} catch (Exception e) {
throw new EarlException("Error, unknown use of event.\n"
+ e.getMessage());
throw new EarlException(
ui.appendNewline("Error, unknown use of event.")
+ ui.leftPad(e.getMessage()));
}
}
}
12 changes: 6 additions & 6 deletions src/main/java/earl/logic/MarkHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,20 @@ public void handle(TaskList tasks, Ui ui) throws EarlException {
}
for (int idx : indices) {
boolean success = tasks.get(idx).markAsDone();
String feedback = idx + 1 + "." + tasks.get(idx);
if (!success) {
addDisplayEntry(idx + 1 + "." + tasks.get(idx)
+ " already marked as done.");
continue;
feedback = feedback + " already marked as done.";
}
addDisplayEntry(idx + 1 + "." + tasks.get(idx));
addDisplayEntry(feedback);
}
addDisplayEntry("Item(s) marked as done.");
ui.makeResponse(getDisplay());
} catch (EarlException e) {
throw e;
} catch (Exception e) {
throw new EarlException("Error, unknown use of mark.\n"
+ "\t" + e.getMessage());
throw new EarlException(
ui.appendNewline("Error, unknown use of mark.")
+ ui.leftPad(e.getMessage()));
}
}
}
21 changes: 13 additions & 8 deletions src/main/java/earl/logic/TodoHandler.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package earl.logic;

import earl.exceptions.EarlException;
import earl.tasks.Task;
import earl.tasks.TaskType;
import earl.util.TaskList;
import earl.util.Ui;
Expand All @@ -18,16 +19,20 @@ public TodoHandler(String args) {
@Override
public void handle(TaskList tasks, Ui ui) throws EarlException {
try {
tasks.add(TaskType.TODO.createTask(args));
ui.makeResponse("Added new todo.",
"\t" + tasks.get(tasks.getSize() - 1),
"There are " + tasks.getSize() + " task(s) tracked.");
Task added = tasks.add(TaskType.TODO.createTask(args));
ui.buildResponse("Added new todo.");
ui.buildResponse(ui.leftPad(added.toString()));
ui.buildResponse("There are " + tasks.getSize()
+ " task(s) tracked.");
ui.completeResponse();
} catch (IndexOutOfBoundsException e) {
throw new EarlException("Error, missing task name.\n"
+ "\tExample use:\n\ttodo <task_name>");
throw new EarlException(
ui.appendNewline("Error, missing task name. Example use:")
+ ui.leftPad("todo <name>"));
} catch (Exception e) {
throw new EarlException("Error, unknown use of todo.\n"
+ e.getMessage());
throw new EarlException(
ui.appendNewline("Error, unknown use of todo.")
+ ui.leftPad(e.getMessage()));
}
}
}
12 changes: 6 additions & 6 deletions src/main/java/earl/logic/UnmarkHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,20 @@ public void handle(TaskList tasks, Ui ui) throws EarlException {
}
for (int idx : indices) {
boolean success = tasks.get(idx).markUndone();
String feedback = idx + 1 + "." + tasks.get(idx);
if (!success) {
addDisplayEntry(idx + 1 + "." + tasks.get(idx)
+ " already marked as not done.");
continue;
feedback = feedback + " already marked as not done.";
}
addDisplayEntry(idx + 1 + "." + tasks.get(idx));
addDisplayEntry(feedback);
}
addDisplayEntry("Item(s) marked as not done.");
ui.makeResponse(getDisplay());
} catch (EarlException e) {
throw e;
} catch (Exception e) {
throw new EarlException("Error, unknown use of mark.\n"
+ "\t" + e.getMessage());
throw new EarlException(
ui.appendNewline("Error, unknown use of unmark.")
+ ui.leftPad(e.getMessage()));
}
}
}
3 changes: 2 additions & 1 deletion src/main/java/earl/util/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ public Task get(int i) {
return tasks.get(i);
}

public void add(Task task) {
public Task add(Task task) {
tasks.add(task);
return task;
}

public Task delete(int idx) {
Expand Down
48 changes: 37 additions & 11 deletions src/main/java/earl/util/Ui.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package earl.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
Expand All @@ -9,19 +11,23 @@ public class Ui {

private static final String PADDING = " ".repeat(4);
private static final String DIVIDER = "_".repeat(60);
private static final String GREETING_MESSAGE = "Hello! I'm Earl\n"
private static final String NEWLINE = System.lineSeparator();
private static final String GREETING_MESSAGE = "Hello! I'm Earl" + NEWLINE
+ PADDING + "What can I do for you?";

private static final String GOODBYE_MESSAGE = "Goodbye! See you soon.";

private final Scanner sc;
private final List<String> delayedResponse;

private String[] prevResponse;
private String[] response;

/** Class constructor. */
public Ui() {
sc = new Scanner(System.in);
prevResponse = new String[0];
response = new String[1];
response[0] = GREETING_MESSAGE;
delayedResponse = new ArrayList<>();
}

/** Displays horizontal divider. */
Expand All @@ -36,6 +42,21 @@ private static void printStrings(String... arr) {
}
}

private void setPrevResponse(String... arr) {
response = arr;
}

/** Adds strings to be displayed later. */
public void buildResponse(String... arr) {
delayedResponse.addAll(List.of(arr));
}

/** Formats and prints the built response for the user to read. */
public void completeResponse() {
makeResponse(delayedResponse.toArray(String[]::new));
delayedResponse.clear();
}

/**
* Formats and prints the arguments for the user to read.
* <p>
Expand All @@ -52,14 +73,19 @@ public void makeResponse(String... arr) {
printDivider();
}

private void setPrevResponse(String... arr) {
prevResponse = arr;
}

public String getUserInput() {
return sc.nextLine();
}

/** Left pads the input string with the class's set padding. */
public String leftPad(String line) {
return PADDING + line;
}

public String appendNewline(String line) {
return line + NEWLINE;
}

public void showGreeting() {
makeResponse(GREETING_MESSAGE);
}
Expand All @@ -75,11 +101,11 @@ public void showGoodbye() {
* text based UI
*/
public String getResponse() {
StringBuilder res = new StringBuilder(PADDING + DIVIDER + "\n");
for (String line: prevResponse) {
res.append(PADDING).append(line).append("\n");
StringBuilder res = new StringBuilder(PADDING + DIVIDER + NEWLINE);
for (String line: response) {
res.append(PADDING).append(line).append(NEWLINE);
}
res.append(PADDING).append(DIVIDER).append("\n");
res.append(PADDING).append(DIVIDER).append(NEWLINE);
return res.toString();
}
}

0 comments on commit 8c55e57

Please sign in to comment.