-
Notifications
You must be signed in to change notification settings - Fork 452
[Tin Jingyao] iP #507
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?
[Tin Jingyao] iP #507
Changes from 7 commits
556af3f
1541204
18f1898
ec9fd31
400cf62
7bc53d7
6f5be36
ddb1d97
841c78b
129758c
c351d1d
a69c99c
462fb51
4d4f45d
fbdbd16
304acb1
609dc79
fe3f213
86fba2c
982ba6d
7338f37
aef9014
8009da8
b99000a
3bda679
a2c8035
6741565
ce8111d
217e318
7554d3d
cfabe0d
5d7b0ab
1297ade
ddf41bb
dafecc7
14da0b9
4cd2cf6
3c0576a
7f02321
a883516
3574c8a
c8e2fdc
0d08193
a2eb1a6
b8d38f9
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,10 +1,208 @@ | ||
| import java.util.ArrayList; | ||
| import java.util.Locale; | ||
| import java.util.Scanner; | ||
|
|
||
| public class Duke { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could split the class into various classes, i.e. TaskList, Event, Deadline, instead of putting them into one class There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could also add JavaDocs for documentation purposes |
||
|
|
||
| private static final ArrayList<Task> LIST_OF_THINGS = new ArrayList<>(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe you could consider extracting this out as a separate TaskList class, which would handle adding tasks, etc. |
||
|
|
||
| private abstract static class Task { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe you could create Task as a separate class, instead of nesting it in the Duke class. |
||
| private final String task_name; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be camelcase formatting for variables |
||
| private boolean isCompleted; | ||
|
|
||
| private static class TodoTask extends Task { | ||
|
|
||
| public TodoTask(String name) { | ||
| super(name); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| if (isCompleted()) { | ||
| return String.format("[T][X] %s", getTaskName()); | ||
| } else { | ||
| return String.format("[T][ ] %s", getTaskName()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static class DeadlineTask extends Task { | ||
|
|
||
| private final String deadline; | ||
|
|
||
| public DeadlineTask(String name, String date) { | ||
| super(name); | ||
| deadline = date; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| if (isCompleted()) { | ||
| return String.format("[D][X] %s (By: %s)", getTaskName(), deadline); | ||
| } else { | ||
| return String.format("[D][ ] %s (By: %s)", getTaskName(), deadline); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static class EventTask extends Task { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like how you named the different Tasks clearly! 👍 |
||
|
|
||
| private final String datetime; | ||
|
|
||
| public EventTask(String name, String date) { | ||
| super(name); | ||
| datetime = date; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| if (isCompleted()) { | ||
| return String.format("[E][X] %s (At: %s)", getTaskName(), datetime); | ||
| } else { | ||
| return String.format("[E][ ] %s (At: %s)", getTaskName(), datetime); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public Task(String name) { | ||
| task_name = name; | ||
| isCompleted = false; | ||
| } | ||
|
|
||
| public String getTaskName() { | ||
| return task_name; | ||
| } | ||
|
|
||
| public void markComplete() { | ||
| isCompleted = true; | ||
| } | ||
|
|
||
| public void markIncomplete() { | ||
| isCompleted = false; | ||
| } | ||
|
|
||
| public boolean isCompleted() { | ||
| return isCompleted; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| if (isCompleted) { | ||
| return String.format("[X] %s", task_name); | ||
| } else { | ||
| return String.format("[ ] %s", task_name); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public static boolean isMarkCommand(String str) { | ||
| if (str.length() < 6) { | ||
| return false; | ||
| } | ||
| if (!str.startsWith("mark")) { | ||
| return false; | ||
| } | ||
| try { | ||
| Integer.parseInt(str.substring(5)); | ||
| return true; | ||
| } catch (NumberFormatException nfe) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| public static boolean isUnmarkCommand(String str) { | ||
| if (str.length() < 8) { | ||
| return false; | ||
| } | ||
| if (!str.startsWith("unmark")) { | ||
| return false; | ||
| } | ||
| try { | ||
| Integer.parseInt(str.substring(7)); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps you could consider using str.split(" ") rather than str.substring(7). May improve reusability of code instead of having to change the number according to the length of the command? |
||
| return true; | ||
| } catch (NumberFormatException nfe) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
|
|
||
| public static void main(String[] args) { | ||
| String logo = " ____ _ \n" | ||
| + "| _ \\ _ _| | _____ \n" | ||
| + "| | | | | | | |/ / _ \\\n" | ||
| + "| |_| | |_| | < __/\n" | ||
| + "|____/ \\__,_|_|\\_\\___|\n"; | ||
| System.out.println("Hello from\n" + logo); | ||
| System.out.println("Hi, I'm Duke. What can I do for you?"); | ||
| Scanner keyboard = new Scanner(System.in); | ||
| String message = keyboard.nextLine(); | ||
| while (true) { | ||
| if (message.equals("bye")) { | ||
| System.out.println("Bye! See you next time!"); | ||
| break; | ||
| } else if (message.equals("list")) { | ||
| StringBuilder output_message = new StringBuilder(); | ||
| for (int i = 0; i < LIST_OF_THINGS.size(); i++) { | ||
| output_message.append(String.format("%d. %s", i + 1, LIST_OF_THINGS.get(i))); | ||
| output_message.append("\n"); | ||
| } | ||
| System.out.println(output_message); | ||
| } else if (isMarkCommand(message)) { | ||
| int index = Integer.parseInt(message.substring(5)); | ||
| if (index > LIST_OF_THINGS.size() || index < 1) { | ||
| System.out.printf("Sorry, task %d does not exist!%n", index); | ||
| } else { | ||
| Task task = LIST_OF_THINGS.get(index - 1); | ||
| task.markComplete(); | ||
| System.out.printf("Good Job! This task has been marked as done:%n%s%n", task); | ||
| } | ||
| } else if (isUnmarkCommand(message)) { | ||
| int index = Integer.parseInt(message.substring(7)); | ||
| if (index > LIST_OF_THINGS.size() || index < 1) { | ||
| System.out.printf("Sorry, task %d does not exist!%n", index); | ||
| } else { | ||
| Task task = LIST_OF_THINGS.get(index - 1); | ||
| task.markIncomplete(); | ||
| System.out.printf("Get it done! This task has been marked as undone:%n%s%n", task); | ||
| } | ||
| } else if (message.startsWith("deadline")) { | ||
| int slash_char_pos = message.indexOf("/by"); | ||
| if (slash_char_pos < 13) { | ||
| System.out.println("Deadline tasks require a task name and a deadline specified by /by"); | ||
| } else { | ||
| String task_name = message.substring(9, slash_char_pos); | ||
| String deadline = message.substring(slash_char_pos + 4); | ||
| Task this_task = new Task.DeadlineTask(task_name, deadline); | ||
| LIST_OF_THINGS.add(this_task); | ||
| System.out.printf("I've added this task:%n%s%n", this_task); | ||
| } | ||
| } else if (message.startsWith(("event"))) { | ||
| int slash_char_pos = message.indexOf("/at"); | ||
| if (slash_char_pos < 10) { | ||
| System.out.println("Event tasks require a task name and a datetime specified by /at"); | ||
| } else { | ||
| String task_name = message.substring(6, slash_char_pos); | ||
| String date = message.substring(slash_char_pos + 4); | ||
| Task this_task = new Task.EventTask(task_name, date); | ||
| LIST_OF_THINGS.add(this_task); | ||
| System.out.printf("I've added this task:%n%s%n", this_task); | ||
| } | ||
| } else if (message.startsWith(("todo"))) { | ||
| String task_name = message.substring(5); | ||
| Task this_task = new Task.TodoTask(task_name); | ||
| LIST_OF_THINGS.add(this_task); | ||
| System.out.printf("I've added this task:%n%s%n", this_task); | ||
| } else if (message.startsWith("delete")) { | ||
| try { | ||
| int task_index = Integer.parseInt(message.substring(7)) - 1; | ||
| Task removed = LIST_OF_THINGS.remove(task_index); | ||
| System.out.printf("Noted. I've removed this task:%n%s%nNow you have %d tasks in the list%n", | ||
| removed, LIST_OF_THINGS.size()); | ||
| } catch (StringIndexOutOfBoundsException e) { | ||
| System.out.println("Please indicate which task you would like to delete"); | ||
| } catch (NumberFormatException e) { | ||
| System.out.println("You need to enter a number!"); | ||
| } catch (IndexOutOfBoundsException e) { | ||
| System.out.printf("You only have %d tasks!%n", LIST_OF_THINGS.size()); | ||
| } | ||
| } else { | ||
| System.out.println("Sorry, I don't know what that means :("); | ||
| } | ||
| message = keyboard.nextLine(); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,3 @@ | ||
| Hello from | ||
| ____ _ | ||
| | _ \ _ _| | _____ | ||
| | | | | | | | |/ / _ \ | ||
| | |_| | |_| | < __/ | ||
| |____/ \__,_|_|\_\___| | ||
|
|
||
| Hi, I'm Duke. What can I do for you? | ||
| I've added this task: | ||
| [D][ ] CS2013 (By: 1 September) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| deadline CS2013 /by 1 September | ||
| list | ||
| mark 1 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| @ECHO OFF | ||
| @ECHO OFF | ||
|
|
||
| REM create bin directory if it doesn't exist | ||
| if not exist ..\bin mkdir ..\bin | ||
|
|
||
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.
Perhaps apply OOP to split the Duke class into different smaller classes?