Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
yadobler committed Sep 11, 2024
2 parents c4f8ad2 + 1532de3 commit 1a12e7f
Show file tree
Hide file tree
Showing 17 changed files with 233 additions and 153 deletions.
18 changes: 10 additions & 8 deletions src/main/java/yappingbot/Launcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
*/
public class Launcher {

public static String savefilePath;
// default savefile path is './savefile'
private static String savefilePath = "./savefile";

/**
* MainGuiApplication entry point. Parses arguments and launches YappingBot appropriately.
Expand All @@ -25,11 +26,13 @@ public static void main(String[] args) {
// NOTE: args DOES NOT INCLUDE FILENAME AT ARGS[0]
boolean isUsingGui = true;
boolean stopTakingInputs = false;
String customSavefilePath = "";
String[] jfxArgs = new String[0];

// loops through arguments to parse them.
// any arguments preceeding a '--' are not processed and directly passed on to javaFX
for (int i = 0; i < args.length; i++) {
if (stopTakingInputs) {
// -- has been detected. Stop and just copy everything to be passed to javaFX
jfxArgs = Arrays.copyOfRange(args, i + 1, args.length);
break;
} else {
Expand All @@ -39,12 +42,14 @@ public static void main(String[] args) {
continue;
case "-s":
case "--savefile":
// peek the next arguemnt to get the savefile name
int savefilePathIndex = i + 1;
if (savefilePathIndex >= args.length
|| args[savefilePathIndex].startsWith("-")) {
System.out.printf("Error: %s missing argument: savefile path!\n", args[i]);
} else {
customSavefilePath = args[savefilePathIndex];
savefilePath = args[savefilePathIndex];
// only advance the pointer if there was a valid value for this flag
i = i + 1;
}
continue;
Expand All @@ -57,11 +62,10 @@ public static void main(String[] args) {
}
}

Launcher.savefilePath = customSavefilePath.isEmpty() ? "./savefile" : customSavefilePath;
if (isUsingGui) {
launchGui(jfxArgs);
} else {
launchCli(jfxArgs);
launchCli();
}
}

Expand All @@ -77,10 +81,8 @@ public static void launchGui(String[] args) {

/**
* Static method to launchGui MainGuiApplication with JavaFX GUI.
*
* @param args String ArrayList of arguments passed in via CLI when launching this app.
*/
public static void launchCli(String[] args) {
public static void launchCli() {
YappingBot yp = new YappingBot(new UiCli(), new Storage(savefilePath));
yp.start();
}
Expand Down
29 changes: 19 additions & 10 deletions src/main/java/yappingbot/YappingBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import yappingbot.commands.CommandDispatcher;
import yappingbot.commands.Parser;
import yappingbot.exceptions.YappingBotException;
import yappingbot.exceptions.YappingBotInvalidSaveFileException;
import yappingbot.exceptions.YappingBotExceptionList;
import yappingbot.exceptions.YappingBotSaveFileNotFoundException;
import yappingbot.exceptions.YappingBotUnknownCommandException;
import yappingbot.storage.Storage;
Expand Down Expand Up @@ -46,15 +46,12 @@ public YappingBot(Ui ui, Storage storage) {
* load the list from disk.
*/
private void init() {
ui.print(ReplyTextMessages.GREETING_TEXT);
userList = new TaskList();
try {
ArrayList<String> s = storage.loadListFromFile();
YappingBotInvalidSaveFileException e = userList.generateFromRaw(s);
// Lord forgive me for returning execptions without throwing them is so
// illegal.
if (e != null) {
ui.printError(e);
}
YappingBotExceptionList e = userList.generateFromRaw(s);
e.checkExceptions();
} catch (YappingBotSaveFileNotFoundException e) {
ui.printError(e);
}
Expand All @@ -71,6 +68,7 @@ private void saveAndCleanup() {
} catch (YappingBotException e) {
ui.printError(String.format(ReplyTextMessages.SAVE_FILE_ERROR_1s, e.getMessage()));
}
ui.print(ReplyTextMessages.EXIT_TEXT);
}

/**
Expand All @@ -80,48 +78,61 @@ private void mainLoop() {
while (ui.hasInputLines()) {
String userInput = ui.getNextInputLine().trim();
String[] userInputSlices = userInput.split(" ");
int taskListIndexPtr; // task list pointer

try {
int taskListIndexPtr; // task list pointer
switch (parser.parseCommand(userInputSlices[0])) {
case EXIT:
// exits the function, ending the main loop
return;
case RESET_LIST:
// resets any filter on the list
userList = commandDispatch.resetView(userList, false);
break;
case LIST:
// lists out all tasks that fits current filter (if any)
commandDispatch.printUserList(userList);
break;
case MARK:
// marks a task as Done, given the index
Parser.checkMinimumArgsAvailable(userInputSlices, 1);
taskListIndexPtr = Parser.parseTaskNumberSelected(userInputSlices[1]);
commandDispatch.changeTaskListStatus(taskListIndexPtr, true, userList);
break;
case UNMARK:
// unmarks a task as Done, given the index
Parser.checkMinimumArgsAvailable(userInputSlices, 1);
taskListIndexPtr = Parser.parseTaskNumberSelected(userInputSlices[1]);
commandDispatch.changeTaskListStatus(taskListIndexPtr, false, userList);
break;
case DELETE:
// deletes a task from list, given the index
Parser.checkMinimumArgsAvailable(userInputSlices, 1);
taskListIndexPtr = Parser.parseTaskNumberSelected(userInputSlices[1]);
commandDispatch.deleteTask(taskListIndexPtr, userList);
break;
case TODO:
// creates a new to-do task
commandDispatch.createNewTask(userInputSlices, TaskTypes.TODO, userList);
break;
case EVENT:
// creates a new event task
commandDispatch.createNewTask(userInputSlices, TaskTypes.EVENT, userList);
break;
case DEADLINE:
// creates a new deadline task
commandDispatch.createNewTask(userInputSlices, TaskTypes.DEADLINE, userList);
break;
case FIND:
// creates a filtered list view with tasks matching the given criteria
Parser.checkMinimumArgsAvailable(userInputSlices, 1);
String searchString = userInput.substring(userInput.indexOf(" ") + 1);
userList = commandDispatch.findStringInTasks(searchString, userList);
break;
case UNKNOWN:
default:
// catch-all for malformed commands marked UNKNOWN, or any uncaught and
// unimplemented comannd
throw new YappingBotUnknownCommandException();
}
} catch (YappingBotException e) {
Expand All @@ -137,9 +148,7 @@ private void mainLoop() {
*/
public void start() {
init();
ui.print(ReplyTextMessages.GREETING_TEXT);
mainLoop();
saveAndCleanup();
ui.print(ReplyTextMessages.EXIT_TEXT);
}
}
2 changes: 1 addition & 1 deletion src/main/java/yappingbot/commands/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public CommandTypes parseCommand(String commandString)
/**
* Parses any text that must be interpreted as an integer.
*
* @param userInputSlice String of text to be converted to integer.
* @param userInputSlices String of text to be converted to integer.
* @return integer of the parsed integer.
* @throws YappingBotInvalidTaskNumberException Exception if given String is not a valid
* integer.
Expand Down
33 changes: 33 additions & 0 deletions src/main/java/yappingbot/exceptions/YappingBotExceptionList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package yappingbot.exceptions;

import java.util.ArrayList;

import yappingbot.stringconstants.ReplyTextMessages;

/**
* Array container for multiple YappingBotExceptions that are to be collected and thrown together
* at the end.
*/
public class YappingBotExceptionList extends ArrayList<YappingBotException> {

/**
* Checks if any exceptions were added, and collates them into a single generic
* YappingBotException.
*
* @throws YappingBotException only if exceptions are present and added.
*/
public void checkExceptions() throws YappingBotException {
if (!isEmpty()) {
StringBuilder sb = new StringBuilder();
for (YappingBotException e : this) {
sb.append(e.getErrorMessage());
sb.append("\n---\n");
}
throw new YappingBotException(String.format(
ReplyTextMessages.MULTIPLE_EXCEPTIONS_1d_1s,
this.size(),
sb
));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ public class YappingBotInvalidTaskNumberException extends YappingBotException {
/**
* Constructs Exception when a String, expected to be an index, is unable to be parsed.
*
* @param userInputSlice The offending text that cannot be resolved into an integer.
* @param userInputSlices The offending text that cannot be resolved into an integer.
*/

public YappingBotInvalidTaskNumberException(String userInputSlice) {
super(String.format(ReplyTextMessages.SELECT_TASK_NOT_INT_TEXT_1s, userInputSlice));
public YappingBotInvalidTaskNumberException(String userInputSlices) {
super(String.format(ReplyTextMessages.SELECT_TASK_NOT_INT_TEXT_1s, userInputSlices));
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package yappingbot.exceptions;

import yappingbot.stringconstants.ReplyTextMessages;

/**
* YappingBotException for indices that are out of bounds.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,5 @@ public final class ReplyTextMessages {
public static final String FIND_STRING_FOUND_1d_1s = "Found %d tasks with '%s' (Type reset "
+ "to return to default view)";

public static final String MULTIPLE_EXCEPTIONS_1d_1s = "%d warnings were generated!:\n%s";
}
29 changes: 10 additions & 19 deletions src/main/java/yappingbot/tasks/tasklist/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,16 @@
import java.util.function.Consumer;

import yappingbot.exceptions.YappingBotException;
import yappingbot.exceptions.YappingBotInvalidSaveFileException;
import yappingbot.exceptions.YappingBotExceptionList;
import yappingbot.exceptions.YappingBotOobException;
import yappingbot.stringconstants.ReplyTextMessages;
import yappingbot.tasks.Task;
import yappingbot.ui.Ui;

/**
* TaskList container to hold valid Tasks.
*/
public class TaskList implements Iterable<Task> {
protected ArrayList<Task> tasks;
protected final ArrayList<Task> tasks;
protected int size;

/**
Expand All @@ -29,33 +28,25 @@ public TaskList() {
}

/**
* Creates a task list populated with values from the given ArrayList of Strings.
* Creates a task list populated with values from the given ArrayList of Strings. Exceptions
* caught here are returned instead of thrown so that the loop can continue without having
* throws that interrupt the process.
*
* @param tasksRaw ArrayList of Strings, each a line that denotes a serialized task.
* @return YappingBotException for any caught errors.
*/
public YappingBotInvalidSaveFileException generateFromRaw(ArrayList<String> tasksRaw) {
public YappingBotExceptionList generateFromRaw(ArrayList<String> tasksRaw) {
assert tasksRaw != null;

ArrayList<Exception> errorLists = new ArrayList<>();
YappingBotExceptionList exceptions = new YappingBotExceptionList();
for (String taskIndividualRaw : tasksRaw) {
String[] s = taskIndividualRaw.split(":");
try {
this.addTask(parseSingleTask(s));
} catch (YappingBotException e) {
errorLists.add(e);
}
}

if (!errorLists.isEmpty()) {
StringBuilder sb = new StringBuilder();
for (Exception e : errorLists) {
// TODO: replace with exception collector class
sb.append(e.toString());
sb.append('\n');
exceptions.add(e);
}
return new YappingBotInvalidSaveFileException(sb.toString());
}
return null;
return exceptions;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
public class TaskListFilterView extends TaskList {
final TaskList parentTaskList;
final ArrayList<Integer> indexInParentTaskList;
String filterString;
final String filterString;

private TaskListFilterView(TaskList parentTaskList, String filterString) {
this.parentTaskList = parentTaskList;
Expand Down
22 changes: 14 additions & 8 deletions src/main/java/yappingbot/ui/Ui.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ public interface Ui {
void printfError(String formattedString, Object ... o);


/**
* Pushes String input into the input buffer to be processed by Yappingbot.
*
* @param input String to be inputted
*/
void pushInputLine(String input);

/**
* Peeks into input buffer and returns true if a next line is available, or <b>blocks</b>
* until a line is present in the input buffer, or the input stream is closed.
Expand All @@ -70,20 +77,19 @@ public interface Ui {
*/
String getNextInputLine();


/**
* Returns the output lines in the buffer to be printed.
*
* @return String to be outputted or printed:
*/
String getNextOutputLine() throws YappingBotIoException;

/**
* Peeks into output buffer and returns true if a next line is available, or <b>blocks</b>
* until a line is present in the output buffer, or the output stream is closed.
*
* @return boolean true if line is available, or false if output is closed.
*/
boolean hasOutputLines();

/**
* Returns the output lines in the buffer to be printed.
*
* @return String to be outputted or printed:
*/
String getNextOutputLine() throws YappingBotIoException;
}

Loading

0 comments on commit 1a12e7f

Please sign in to comment.