-
Notifications
You must be signed in to change notification settings - Fork 479
[Cloud7050] iP #532
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?
[Cloud7050] iP #532
Changes from 34 commits
28ad2b8
ed6d4d2
e8b3258
cc7b41d
fb5add2
0b33360
1d9a846
304ec50
2b43d5a
a76b0db
1a28c69
6881b0d
f8ea187
dbc3b33
9be3d20
774a366
21d63d0
0f15682
eddd8a2
4fe1224
e2b528a
35067fb
b88606a
4f1f3f6
5610437
811a80c
e945188
ca81c37
373f286
0e01257
becf338
541f6bc
9a7cdee
59bb19f
e161bc5
abb83ad
ed9c0c6
bc86d2e
c6eed14
8d26682
69f990f
e7c1de2
badd237
f2e99fb
3818dd7
5681260
b7153f7
8308678
c90a50a
bde28cb
09b9691
07914d4
996216e
3befadd
a0fdfd2
3349793
910bca7
4d16a80
8a692b5
3a3d1d2
d0d2b64
1f39294
6b463ae
5df2d8e
735bfd5
0baff58
8e5e69d
929e65c
c9fddc3
1013ed1
6a981f3
e3b0f11
d11ff49
0712206
dd837a3
95daf36
0b5a85f
69c2957
cecdeec
d1ba67a
d8bb3d8
b48a507
6f4c459
b5eaec2
1cd0649
8a65ca9
4cebf4f
3e9daf6
e50e606
3828a89
49cffd0
e42feef
50ec32f
86355bf
54b73ea
fdb9b57
9a50a91
9ec23bd
5acbd95
25d48ee
baf4879
70a2ccf
c00a210
0244d3e
02b1845
4533db1
e2d717a
8fd31e0
6dc36eb
b80e03f
c75c6a4
d72feec
df6551d
59c7896
8fdaaf9
22e50cb
aae82a4
3a6835a
749eba0
e8ad23a
2bb8116
af543d1
b64185d
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 |
|---|---|---|
| @@ -1,17 +1,2 @@ | ||
| # IDEA files | ||
| /.idea/ | ||
| /out/ | ||
| /*.iml | ||
|
|
||
| # Gradle build files | ||
| /.gradle/ | ||
| /build/ | ||
| src/main/resources/docs/ | ||
|
|
||
| # MacOS custom attributes files created by Finder | ||
| .DS_Store | ||
| *.iml | ||
| bin/ | ||
|
|
||
| /text-ui-test/ACTUAL.TXT | ||
| text-ui-test/EXPECTED-UNIX.TXT | ||
| tests/ACTUAL.txt |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| { | ||
| "editor.insertSpaces": true, | ||
| "editor.detectIndentation": false, | ||
| "editor.tabSize": 4 | ||
| } |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,24 +1,3 @@ | ||
| # Duke project template | ||
| # The Cloud Chatbot | ||
|
|
||
| This is a project template for a greenfield Java project. It's named after the Java mascot _Duke_. Given below are instructions on how to use it. | ||
|
|
||
| ## Setting up in Intellij | ||
|
|
||
| Prerequisites: JDK 11, update Intellij to the most recent version. | ||
|
|
||
| 1. Open Intellij (if you are not in the welcome screen, click `File` > `Close Project` to close the existing project first) | ||
| 1. Open the project into Intellij as follows: | ||
| 1. Click `Open`. | ||
| 1. Select the project directory, and click `OK`. | ||
| 1. If there are any further prompts, accept the defaults. | ||
| 1. Configure the project to use **JDK 11** (not other versions) as explained in [here](https://www.jetbrains.com/help/idea/sdk.html#set-up-jdk).<br> | ||
| In the same dialog, set the **Project language level** field to the `SDK default` option. | ||
| 3. After that, locate the `src/main/java/Duke.java` file, right-click it, and choose `Run Duke.main()` (if the code editor is showing compile errors, try restarting the IDE). If the setup is correct, you should see something like the below as the output: | ||
| ``` | ||
| Hello from | ||
| ____ _ | ||
| | _ \ _ _| | _____ | ||
| | | | | | | | |/ / _ \ | ||
| | |_| | |_| | < __/ | ||
| |____/ \__,_|_|\_\___| | ||
| ``` | ||
| This is a chatbot named **Cloud**. The name has no relation to the actual cloud. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,196 @@ | ||
| package com.cloud.chatbot; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Scanner; | ||
|
|
||
| import com.cloud.chatbot.annotations.Nullable; | ||
| import com.cloud.chatbot.exceptions.MissingFlagInputException; | ||
| import com.cloud.chatbot.exceptions.MissingInputException; | ||
| import com.cloud.chatbot.todo.Deadline; | ||
| import com.cloud.chatbot.todo.Event; | ||
| import com.cloud.chatbot.todo.Todo; | ||
| import com.cloud.chatbot.token.CommandManager; | ||
| import com.cloud.chatbot.token.FlagManager; | ||
| import com.cloud.chatbot.token.Token; | ||
|
|
||
|
|
||
|
|
||
| /** | ||
| * The chatbot's main class. | ||
| */ | ||
| public final class Cloud { | ||
| private static final Scanner SCANNER = new Scanner(System.in); | ||
| private static final List<Todo> TODOS = new ArrayList<>(); | ||
|
|
||
| private static void handle(String input) { | ||
| CommandManager manager = new CommandManager(input); | ||
| String command; | ||
| try { | ||
| command = manager.getCommand(); | ||
| } catch (MissingInputException e) { | ||
| // Ignore empty inputs | ||
| return; | ||
| } | ||
|
|
||
| switch (CommandType.stringToCommandType(command)) { | ||
| case ADD: { | ||
| @Nullable Todo todo = Cloud.createTodo(manager); | ||
| if (todo == null) { | ||
| break; | ||
| } | ||
|
|
||
| Cloud.TODOS.add(todo); | ||
| Cloud.say(todo.toString(Cloud.TODOS.size())); | ||
| break; | ||
| } | ||
| case LIST: { | ||
| if (Cloud.TODOS.size() <= 0) { | ||
| Cloud.say("Your TODO list is empty."); | ||
| break; | ||
| } | ||
|
|
||
| for (int i = 0; i < Cloud.TODOS.size(); i++) { | ||
| Todo todo = Cloud.TODOS.get(i); | ||
| Cloud.say(todo.toString(i + 1)); | ||
| } | ||
| break; | ||
| } | ||
| case MARK: { | ||
| @Nullable Integer number = Cloud.verifyNumber(manager); | ||
| if (number == null) { | ||
| break; | ||
| } | ||
|
|
||
| Todo todo = Cloud.TODOS.get(number - 1); | ||
| todo.setComplete(true); | ||
| Cloud.say(todo.toString(number)); | ||
| break; | ||
| } | ||
| case UNMARK: { | ||
| @Nullable Integer number = Cloud.verifyNumber(manager); | ||
| if (number == null) { | ||
| break; | ||
| } | ||
|
|
||
| Todo todo = Cloud.TODOS.get(number - 1); | ||
| todo.setComplete(false); | ||
| Cloud.say(todo.toString(number)); | ||
| break; | ||
| } | ||
| case DELETE: { | ||
| @Nullable Integer number = Cloud.verifyNumber(manager); | ||
| if (number == null) { | ||
| break; | ||
| } | ||
|
|
||
| Todo todo = Cloud.TODOS.remove(number - 1); | ||
| Cloud.say("Yeeted:"); | ||
| Cloud.say(todo.toString(number)); | ||
| break; | ||
| } | ||
| case EXIT: { | ||
| Cloud.say("\\o"); | ||
| System.exit(0); | ||
| break; | ||
| } | ||
| case UNKNOWN: | ||
| default: { | ||
| Cloud.say( | ||
| String.format( | ||
| "\"%s\" is not a valid command.", | ||
| command | ||
| ) | ||
| ); | ||
| break; | ||
| } | ||
Cloud7050 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| private static @Nullable Todo createTodo(CommandManager manager) { | ||
| String description; | ||
| try { | ||
| description = manager.getDetails(); | ||
| } catch (MissingInputException e) { | ||
| Cloud.say("Please enter a description for your TODO."); | ||
| return null; | ||
| } | ||
|
|
||
| @Nullable FlagManager managerBy = manager.findFlag("by"); | ||
| @Nullable FlagManager managerFrom = manager.findFlag("from"); | ||
| @Nullable FlagManager managerTo = manager.findFlag("to"); | ||
|
|
||
| try { | ||
| if (managerBy != null) { | ||
| return new Deadline(description, managerBy.getSubInput()); | ||
| } | ||
| if (managerFrom != null && managerTo != null) { | ||
| return new Event(description, managerFrom.getSubInput(), managerTo.getSubInput()); | ||
| } | ||
| } catch (MissingFlagInputException e) { | ||
| Cloud.say( | ||
| String.format( | ||
| "Please enter a description for the \"%s\" flag.", | ||
| e.getFlagText() | ||
| ) | ||
| ); | ||
| return null; | ||
| } | ||
|
|
||
| return new Todo(description); | ||
| } | ||
|
|
||
| private static @Nullable Integer verifyNumber(CommandManager manager) { | ||
|
||
| if (manager.getTokenCount() <= 1) { | ||
| return null; | ||
| } | ||
|
|
||
| Token numberToken = manager.getToken(1); | ||
| if (!numberToken.isInt()) { | ||
| Cloud.say( | ||
| String.format( | ||
| "\"%s\" is not a valid number.", | ||
| numberToken.toString() | ||
| ) | ||
| ); | ||
| return null; | ||
| } | ||
|
|
||
| if (!numberToken.isValidNumber(Cloud.TODOS)) { | ||
| Cloud.say( | ||
| String.format( | ||
| "TODO #%d does not exist.", | ||
| numberToken.toInt() | ||
| ) | ||
| ); | ||
| return null; | ||
| } | ||
Cloud7050 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| return numberToken.toInt(); | ||
| } | ||
|
|
||
| private static void say(String text) { | ||
| System.out.println(text); | ||
| } | ||
|
|
||
| private static void inputMarker() { | ||
| System.out.print("\n>>> "); | ||
| } | ||
|
|
||
| /** | ||
| * The chatbot's main method. | ||
| * | ||
| * @param args Java arguments. | ||
| */ | ||
| public static void main(String[] args) { | ||
| Cloud.say("Cloud online."); | ||
| Cloud.inputMarker(); | ||
|
|
||
| while (Cloud.SCANNER.hasNextLine()) { | ||
| String input = Cloud.SCANNER.nextLine(); | ||
| Cloud.handle(input); | ||
|
|
||
| Cloud.inputMarker(); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| package com.cloud.chatbot; | ||
|
|
||
|
|
||
|
|
||
| /** | ||
| * Represents all possible command types, including unknown commands. | ||
| */ | ||
| public enum CommandType { | ||
| ADD, | ||
| LIST, | ||
| MARK, | ||
| UNMARK, | ||
| DELETE, | ||
| EXIT, | ||
| UNKNOWN; | ||
|
|
||
| /** | ||
| * Returns the matching command type for the specified command. | ||
| * | ||
| * @param command The command. | ||
| */ | ||
| public static CommandType stringToCommandType(String command) { | ||
| switch (command) { | ||
| case "todo": | ||
| case "t": | ||
| case "deadline": | ||
| case "d": | ||
| case "event": | ||
| case "e": | ||
| case "add": | ||
| return CommandType.ADD; | ||
| case "list": | ||
| case "l": | ||
| return CommandType.LIST; | ||
| case "mark": | ||
| case "m": | ||
| return CommandType.MARK; | ||
| case "unmark": | ||
| case "un": | ||
| return CommandType.UNMARK; | ||
| case "delete": | ||
| case "del": | ||
| case "yeet": | ||
| return CommandType.DELETE; | ||
| case "bye": | ||
| case "exit": | ||
| case "quit": | ||
| case "q": | ||
| case "done": | ||
| return CommandType.EXIT; | ||
| default: | ||
| return CommandType.UNKNOWN; | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package com.cloud.chatbot.annotations; | ||
|
|
||
|
|
||
|
|
||
| /** | ||
| * Explicitly marks types as potentially being null, meaning appropriate null | ||
| * checking is in order. | ||
| */ | ||
| public @interface Nullable {} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package com.cloud.chatbot.exceptions; | ||
|
|
||
| import com.cloud.chatbot.token.FlagManager; | ||
|
|
||
|
|
||
|
|
||
| /** | ||
| * Thrown when the user does not specify all required sub inputs for a flag. | ||
| */ | ||
| public class MissingFlagInputException extends MissingInputException { | ||
| private FlagManager manager; | ||
|
|
||
| public MissingFlagInputException(FlagManager _manager) { | ||
| this.manager = _manager; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the text portion of the associated flag. | ||
| */ | ||
| public String getFlagText() { | ||
| return manager.getFlagText(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package com.cloud.chatbot.exceptions; | ||
|
|
||
|
|
||
|
|
||
| /** | ||
| * Thrown when the user does not specify all required inputs. | ||
| */ | ||
| public class MissingInputException extends Exception { | ||
| public MissingInputException() {} | ||
|
|
||
| public MissingInputException(String message) { | ||
| super(message); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| package com.cloud.chatbot.todo; | ||
|
|
||
|
|
||
|
|
||
| /** | ||
| * Represents a deadline TODO, which has an ending timestamp. | ||
| */ | ||
| public class Deadline extends Todo { | ||
| private String timestampEnd; | ||
|
|
||
| public Deadline(String _description, String end) { | ||
| super(_description); | ||
|
|
||
| this.timestampEnd = end; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString(int number) { | ||
| return String.format( | ||
| "%s | BY %s", | ||
| this.getBasicString(number), | ||
| this.getEnd() | ||
| ); | ||
| } | ||
|
|
||
| @Override | ||
| public String getTypeString() { | ||
| return "D"; | ||
| } | ||
|
|
||
| public String getEnd() { | ||
| return this.timestampEnd; | ||
| } | ||
| } |

Uh oh!
There was an error while loading. Please reload this page.