-
Notifications
You must be signed in to change notification settings - Fork 197
[zavsky] iP #197
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
base: master
Are you sure you want to change the base?
[zavsky] iP #197
Changes from 10 commits
f306814
cf970a8
2bbab8c
6c70db6
c4c4faf
77839ab
6c39cff
eda5180
b7a4a53
5ad547a
e35e895
cc18725
4eef65c
351be6f
9d41e65
0f90bf2
409731a
6402578
4442464
9913a79
27dc91e
5be9c7e
e16fc55
5fbe970
7a3dbfa
61a0382
dc65ce7
1a92be7
22f2a2f
ad398f7
9f85920
99947ed
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,3 +15,5 @@ bin/ | |
|
|
||
| /text-ui-test/ACTUAL.TXT | ||
| text-ui-test/EXPECTED-UNIX.TXT | ||
|
|
||
| *.class | ||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| package bluebird; | ||
| public class Bluebird { | ||
|
|
||
| public Bluebird() { | ||
|
|
||
| } | ||
|
|
||
| public void greetHello() { | ||
| clearScreen(); | ||
| System.out.println("Hello, I am Bluebird"); | ||
| System.out.println("What can I do for you?"); | ||
| System.out.println(""); | ||
| } | ||
|
|
||
| public void greetGoodbye() { | ||
| clearScreen(); | ||
| System.out.println("CAWWW"); | ||
| System.out.println(""); | ||
| } | ||
|
|
||
| public static void clearScreen() { | ||
| System.out.print("\033[H\033[2J"); | ||
| System.out.flush(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| package bluebird; | ||
|
|
||
| import java.security.SecureRandom; | ||
|
|
||
| import bluebird.commands.*; | ||
| import bluebird.exceptions.IndexOutOfBoundsException; | ||
|
|
||
| public class CommandParser { | ||
| private final TaskManager taskManager; | ||
| private final UIHandler ui; | ||
|
|
||
| private static final SecureRandom random = new SecureRandom(); | ||
|
|
||
| public CommandParser(TaskManager taskManager, UIHandler ui) { | ||
| this.taskManager = taskManager; | ||
| this.ui = ui; | ||
| } | ||
|
|
||
| public Command parseInput(String input) { | ||
| String[] parts = input.split(" ", 2); | ||
| String command = parts[0].toLowerCase(); | ||
| String arguments = parts.length > 1 ? parts[1] : ""; | ||
|
|
||
| switch (command) { | ||
| case "list": | ||
| case "l": | ||
| return new ListCommand(taskManager, ui); | ||
| case "add": | ||
| case "a": | ||
| return parseAddCommand(arguments); | ||
| case "mark": | ||
| case "m": | ||
| return parseMarkCommand(arguments, true); | ||
| case "unmark": | ||
| case "u": | ||
| return parseMarkCommand(arguments, false); | ||
| case "delete": | ||
| case "d": | ||
| return parseDeleteCommand(arguments); | ||
| //case "undo": | ||
| //case "x": | ||
| // implement undo last functionality | ||
| case "help": | ||
| case "h": | ||
| return new HelpCommand(ui); | ||
| case "exit": | ||
| case "e": | ||
| //bb.greetGoodbye(); | ||
| return new ExitCommand(); | ||
| default: | ||
| System.out.println("Unknown command."); | ||
| return new HelpCommand(ui); | ||
| } | ||
| } | ||
|
|
||
| private Command parseAddCommand(String arguments) { | ||
| String trimmedArgs = arguments.trim(); | ||
| String[] parts = trimmedArgs.split(" ", 2); | ||
| String taskType = parts[0].toLowerCase(); | ||
|
|
||
| if (!isValidTaskType(taskType)) { | ||
| String input = ui.getUserInput("Add event, deadline or todo? ").trim(); | ||
| parts = input.split(" ", 2); | ||
| taskType = parts[0].toLowerCase(); | ||
|
|
||
| if (!isValidTaskType(taskType)) { | ||
| return new Command(); | ||
| } | ||
| } | ||
|
|
||
| String details = (parts.length > 1) ? parts[1] : ui.getUserInput("Enter details: "); | ||
|
|
||
| if (details.trim().isEmpty()) { | ||
| return new Command(); | ||
| } | ||
|
|
||
| return new AddCommand(taskManager, ui, taskType, details); | ||
|
|
||
| // System.out.println(randomEnum(TaskType.class).name()); | ||
| // return new Command(); | ||
| } | ||
|
|
||
| /** | ||
| * Taken from stackoverflow.com/questions/1972392/pick-a-random-value-from-an-enum | ||
| * from users Eldelshell and zuddduz. | ||
| * @return | ||
| */ | ||
| public static <T extends Enum<?>> T randomEnum(Class<T> clazz) { | ||
| int x = random.nextInt(clazz.getEnumConstants().length); | ||
| return clazz.getEnumConstants()[x]; | ||
| } | ||
|
||
|
|
||
| enum TaskType { | ||
| DEADLINE, D, | ||
| EVENT, E, | ||
| TODO, T | ||
| } | ||
|
||
|
|
||
| private boolean isValidTaskType(String args) { | ||
| try { | ||
| TaskType.valueOf(args.toUpperCase()); | ||
| return true; | ||
| } catch (IllegalArgumentException e) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| private Command parseMarkCommand(String arguments, boolean markAsDone) { | ||
| try { | ||
| String trimmedArgs = arguments.trim(); | ||
| if (trimmedArgs.isEmpty()) { | ||
| ui.displayTasks(taskManager.getTasks()); | ||
| trimmedArgs = ui.getUserInput("Enter task number: "); | ||
| } | ||
| return new MarkCommand(taskManager, ui, parseTaskIndex(trimmedArgs), markAsDone); | ||
| } catch (IndexOutOfBoundsException e) { | ||
| ui.showError("Index provided is out of bounds"); | ||
| return new Command(); | ||
| } | ||
| } | ||
|
|
||
| private Command parseDeleteCommand(String arguments) { | ||
| try { | ||
| String trimmedArgs = arguments.trim(); | ||
| if (trimmedArgs.isEmpty()) { | ||
| ui.displayTasks(taskManager.getTasks()); | ||
| trimmedArgs = ui.getUserInput("Enter task number to delete: "); | ||
| } | ||
| return new DeleteCommand(taskManager, ui, parseTaskIndex(trimmedArgs)); | ||
| } catch (IndexOutOfBoundsException e) { | ||
| ui.showError("Index provided is out of bounds"); | ||
| return new Command(); | ||
| } | ||
| } | ||
|
|
||
| private int parseTaskIndex(String input) throws IndexOutOfBoundsException { | ||
| int index = Integer.parseInt(input.trim()) - 1; | ||
| // check index out-of-bounds | ||
| if (index < 0 || index >= taskManager.getTasks().size()) { | ||
| throw new IndexOutOfBoundsException(); | ||
| } | ||
| return index; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Like the initiative to do short form commands for each command. However, these are considered magic strings. Perhaps you could put them in a constant EXIT_COMMAND/EXIT_COMMAND_SHORT or use a enum to represent the commands.