Skip to content

Commit

Permalink
Ui: remove Earl specific behaviour
Browse files Browse the repository at this point in the history
Moved welcome and goodbye message definitions into the Earl class
instead.

Add HelpHandler

A help command is invoked when the user types "help", or an invalid
input, enumerating the list of available commands.
  • Loading branch information
yisiox committed Feb 17, 2024
1 parent ef760d6 commit 9e29122
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 52 deletions.
18 changes: 12 additions & 6 deletions src/main/java/earl/Earl.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package earl;

import earl.exceptions.EarlException;
import earl.exceptions.ParserException;
import earl.exceptions.StorageException;
import earl.logic.Handler;
import earl.util.Storage;
Expand All @@ -15,6 +14,15 @@
*/
public class Earl {

private static final String[] GREETING_MESSAGE = {
"Greetings, I, Earl, extend my esteemed salutations.",
"Pray, how may I be of service to you on this auspicious day?"
};
private static final String[] GOODBYE_MESSAGE = {
"Farewell, dear interlocutor!",
"Until our paths intertwine again, I bid you adieu."
};

private final Storage storage;
private final TaskList tasks;
private final Ui ui;
Expand All @@ -25,14 +33,14 @@ public class Earl {
* @param filePath a path to the text file storing past data
*/
public Earl(String filePath) {
ui = new Ui();
ui = new Ui(GREETING_MESSAGE);
storage = new Storage(filePath);
tasks = new TaskList(storage.load(TaskStorageParser::parse));
if (!storage.wasLoadSuccessful()) {
ui.buildResponse("Storage hath succumb to corruption... ",
"initiating an unfortunate state of emptiness.");
}
ui.showGreeting();
ui.completeResponse();
}

/** Main function for TUI mode. */
Expand Down Expand Up @@ -81,15 +89,13 @@ private void processUserInput(String input) {
handler.handle(tasks, ui);
} catch (EarlException e) {
ui.makeResponse(e.getMessage());
} catch (ParserException e) {
ui.makeResponse("Input defies parsing: " + e.getMessage());
}
}

private void saveAndExit() {
try {
storage.save(tasks.getAsStorageStringStream());
ui.showGoodbye();
ui.makeResponse(GOODBYE_MESSAGE);
} catch (StorageException e) {
ui.makeResponse("Alas, a grievous misfortune occurred "
+ "during the endeavour to save.");
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/earl/exceptions/ParserException.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,4 @@ public class ParserException extends Exception {
public ParserException() {
super();
}
public ParserException(String message) {
super(message);
}
}
6 changes: 6 additions & 0 deletions src/main/java/earl/logic/HandlerType.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ public Handler createHandler(String args) {
public Handler createHandler(String args) {
return new FindHandler(args);
}
},
HELP {
@Override
public Handler createHandler(String args) {
return new HelpHandler(args);
}
};

public abstract Handler createHandler(String args);
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/earl/logic/HelpHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package earl.logic;

import earl.util.TaskList;
import earl.util.Ui;

/**
* Class responsible for displaying list of available commands.
*/
public class HelpHandler extends Handler {

public HelpHandler(String args) {
super(args);
}

@Override
public void handle(TaskList tasks, Ui ui) {
if (!args.isEmpty()) {
ui.buildResponse("Input defies parsing: " + args);
}
ui.buildResponse("Pray, issue a command forthwith.");
ui.buildResponse("The sanctioned commands unfold:");
for (HandlerType type : HandlerType.values()) {
ui.buildResponse("-" + type.name().toLowerCase());
}
ui.completeResponse();
}
}
13 changes: 6 additions & 7 deletions src/main/java/earl/logic/TaskHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@ protected String[] addTask(TaskList tasks, TaskType type, String... data)
try {
Task newTask = type.createTask(data);
tasks.add(newTask);

String first = "A new " + type.name() + " " + newTask.toString();
String second = "hath been appended to the "
+ "roster of responsibilities.";
String third = "The ledger of tasks bears witness to "
+ tasks.getSize() + " endeavour(s).";
return new String[]{first, second, third};
return new String[]{
"A new " + type.name() + " " + newTask.toString(),
"hath been appended to the roster of responsibilities.",
"The ledger of tasks bears witness to "
+ tasks.getSize() + " endeavour(s)."
};
} catch (IndexOutOfBoundsException e) {
throw new EarlException("An error befalls.");
} catch (TimeException e) {
Expand Down
26 changes: 4 additions & 22 deletions src/main/java/earl/util/Ui.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,17 @@ public class Ui {
private static final String PADDING = " ".repeat(4);
private static final String DIVIDER = "_".repeat(60);
private static final String NEWLINE = System.lineSeparator();
private static final String GREETING_MESSAGE =
"Greetings, I, Earl, extend my esteemed salutations." + NEWLINE
+ PADDING + "Pray, how may I be of service to you"
+ " on this auspicious day?";
private static final String GOODBYE_MESSAGE =
"Farewell, dear interlocutor!" + NEWLINE
+ PADDING + "Until our paths intertwine again, I bid you adieu.";

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

private String[] response; // the lines of the previous response

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

private void setPrevResponse(String... arr) {
Expand Down Expand Up @@ -88,17 +81,6 @@ public String appendNewline(String line) {
return line + NEWLINE;
}

/** Displays the initial greeting. */
public void showGreeting() {
// response is built in case of any preceding error messages
buildResponse(GREETING_MESSAGE);
completeResponse();
}

public void showGoodbye() {
makeResponse(GOODBYE_MESSAGE);
}

/**
* Returns the previous response made for GUI interactions.
*
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/earl/util/parsers/InputParser.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package earl.util.parsers;

import earl.exceptions.ParserException;
import earl.logic.Handler;
import earl.logic.HandlerType;

Expand All @@ -14,19 +13,22 @@ public class InputParser implements Parser<Handler> {
*
* @param input text input by the user
* @return a {@code Handler} object of the relevant type
* @throws ParserException if user input is of unexpected format
*/
public static Handler parse(String input) throws ParserException {
public static Handler parse(String input) {
try {
// all valid input is expected to be of the format
// <command> [<arg1>, <arg2>, ...]
String[] data = input.split("\\s+", 2);
assert data.length > 0;
String command = data[0].toUpperCase();
if (command.isEmpty()) {
return HandlerType.HELP.createHandler(input);
}
String args = (data.length > 1) ? data[1] : "";
HandlerType handlerType = HandlerType.valueOf(command);
return handlerType.createHandler(args);
} catch (Exception e) {
throw new ParserException(input);
return HandlerType.HELP.createHandler(input);
}
}
}
14 changes: 4 additions & 10 deletions src/test/java/earl/util/parsers/InputParserTest.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package earl.util.parsers;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.fail;

import org.junit.jupiter.api.Test;

import earl.exceptions.ParserException;
import earl.logic.Handler;
import earl.logic.HelpHandler;
import earl.logic.TodoHandler;

class InputParserTest {
Expand All @@ -21,12 +19,8 @@ void parse_normalInput_handlerReturnedSuccess() throws Exception {
}

@Test
void parse_invalidInput_exceptionThrown() {
try {
InputParser.parse("no ip");
fail();
} catch (ParserException e) {
assertEquals("no ip", e.getMessage());
}
void parse_invalidInput_returnHelpHandler() {
Handler handler = InputParser.parse("no ip");
assertInstanceOf(HelpHandler.class, handler);
}
}

0 comments on commit 9e29122

Please sign in to comment.