-
Notifications
You must be signed in to change notification settings - Fork 191
[Silin Chen] iP #192
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?
[Silin Chen] iP #192
Changes from 14 commits
8bf8991
5876d3a
02672d8
d75ba2c
c77d04f
308eacf
97a6781
64e85b9
48ce882
653a21d
7f61f2d
dfcf04b
ccd2556
b4c1c68
7f4ad3f
06d40a3
d308886
2c8103d
2a89619
d41de47
3e52e94
8320297
fd35808
c492a3b
e61603d
1e115d9
2faad66
bb57228
ebba67b
999995d
4168e98
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| package duke.command; | ||
|
|
||
| import duke.task.Deadline; | ||
| import duke.task.Event; | ||
| import duke.task.Task; | ||
| import duke.task.ToDo; | ||
|
|
||
| import java.util.Scanner; | ||
|
|
||
|
|
||
| public class Duke { | ||
| public static void listOut(Task[] tasks, int taskNumber) { | ||
| for (int i = 0; i < taskNumber; i++) { | ||
| System.out.println((i + 1) + "." + tasks[i]); | ||
| } | ||
| } | ||
|
|
||
| public static void markDone(String line, Task[] tasks) { | ||
| try { | ||
| int index = Integer.parseInt(line.substring(5)) - 1; | ||
| tasks[index].setDone(); | ||
| System.out.println("Nice! I've marked this task as done:"); | ||
| System.out.println(" " + tasks[index].getStatusIcon() + " " + tasks[index].getDescription()); | ||
| } catch (StringIndexOutOfBoundsException e) { | ||
| System.out.println("OOPS!!! The description of a mark-done cannot be empty."); | ||
| } | ||
| } | ||
|
|
||
| public static boolean addTodo(String line, Task[] tasks, int taskNumber) { | ||
| try { | ||
| tasks[taskNumber] = new ToDo(line.substring(5)); | ||
| taskNumber++; | ||
| System.out.println("Got it. I've added this task:"); | ||
| System.out.println(" " + tasks[taskNumber - 1]); | ||
| System.out.println("Now you have " + taskNumber + " tasks in the list"); | ||
| return true; | ||
| } catch (StringIndexOutOfBoundsException e) { | ||
| System.out.println("OOPS!!! The description of a todo cannot be empty."); | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| public static boolean addDeadline(String line, Task[] tasks, int taskNumber) { | ||
| String[] words = line.split(" "); | ||
| int index = 0; | ||
| String deadlineDescription = ""; | ||
| String by = ""; | ||
| for (int i = 0; i < words.length; i++) { | ||
| if (words[i].equals("/by")) { | ||
| index = i; | ||
| break; | ||
| } | ||
| } | ||
| if (index == 0) { | ||
| System.out.println("OOPS!!! The description of a deadline cannot be empty."); | ||
| return false; | ||
| } | ||
| for (int i = 1; i < index; i++) { | ||
| deadlineDescription = deadlineDescription + words[i] + " "; | ||
| } | ||
| for (int i = index + 1; i < words.length; i++) { | ||
| by = by + words[i] + " "; | ||
| } | ||
| tasks[taskNumber] = new Deadline(deadlineDescription, by); | ||
| taskNumber++; | ||
| System.out.println("Got it. I've added this task:"); | ||
| System.out.println(" " + tasks[taskNumber - 1]); | ||
| System.out.println("Now you have " + taskNumber + " tasks in the list"); | ||
| return true; | ||
|
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. You may want to consider splitting this method into multiple smaller methods that do smaller tasks and call it from this method. This goes the same for the other tasks-related methods. |
||
| } | ||
|
|
||
| public static boolean addEvent(String line, Task[] tasks, int taskNumber) { | ||
| String[] words = line.split(" "); | ||
| int index = 0; | ||
| String eventDescription = ""; | ||
| String at = ""; | ||
| for (int i = 0; i < words.length; i++) { | ||
| if (words[i].equals("/at")) { | ||
| index = i; | ||
| break; | ||
| } | ||
| } | ||
| if (index == 0) { | ||
| System.out.println("OOPS!!! The description of a deadline cannot be empty."); | ||
| return false; | ||
| } | ||
| for (int i = 1; i < index; i++) { | ||
| eventDescription = eventDescription + words[i] + " "; | ||
| } | ||
| for (int i = index + 1; i < words.length; i++) { | ||
| at = at + words[i] + " "; | ||
| } | ||
| tasks[taskNumber] = new Event(eventDescription, at); | ||
| taskNumber++; | ||
| System.out.println("Got it. I've added this task:"); | ||
| System.out.println(" " + tasks[taskNumber - 1]); | ||
|
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 the magic literal 1 be declared as a constant instead so that it is easier for you and other programmers to refer to your code in the future? This applies to all magic literals across the code. |
||
| System.out.println("Now you have " + taskNumber + " tasks in the list"); | ||
| return true; | ||
| } | ||
|
|
||
| public static void sayHi() { | ||
| System.out.println("Hello I'm duke.command.Duke"); | ||
| System.out.println("What can I do for you?"); | ||
| } | ||
|
|
||
| public static void sayBye() { | ||
| System.out.println("Bye. Hope to see you again soon!"); | ||
| } | ||
|
|
||
| public static void running() { | ||
| Task[] tasks = new Task[100]; | ||
| int taskNumber = 0; | ||
| String line; | ||
| Scanner in = new Scanner(System.in); | ||
| while (true) { | ||
| line = in.nextLine(); | ||
| if (line.equals("bye")) { | ||
| return; | ||
|
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. In general, we do not use a return statement in a void function. If you want to break out of the current loop, use the "break" statement instead. |
||
| } | ||
| if (line.equals("list")) { | ||
| listOut(tasks, taskNumber); | ||
| } else if (line.startsWith("done")) { | ||
| markDone(line, tasks); | ||
| } else if (line.startsWith("todo")) { | ||
| if (addTodo(line, tasks, taskNumber)) { | ||
| taskNumber++; | ||
| } | ||
| } else if (line.startsWith("deadline")) { | ||
| if (addDeadline(line, tasks, taskNumber)) { | ||
| taskNumber++; | ||
| } | ||
| } else if (line.startsWith("event")) { | ||
| if (addEvent(line, tasks, taskNumber)) { | ||
| taskNumber++; | ||
| } | ||
| } else { | ||
| System.out.println("☹ OOPS!!! I'm sorry, but I don't know what that means :-("); | ||
| } | ||
| } | ||
|
Comment on lines
+115
to
+38
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. You could consider splitting this functionality into another method for better readability and reusability of your code. |
||
| } | ||
|
|
||
| public static void main(String[] args) { | ||
| sayHi(); | ||
| running(); | ||
| sayBye(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package duke.task; | ||
|
|
||
| public class Deadline extends Task { | ||
|
|
||
| protected String by; | ||
|
|
||
| public Deadline(String description, String by) { | ||
| super(description); | ||
| this.by = by; | ||
| } | ||
|
|
||
| @Override | ||
|
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. Good use of the override statement. Well done! |
||
| public String toString() { | ||
| return "[D]" + super.toString() + "(by: " + by + ")"; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package duke.task; | ||
|
|
||
| public class Event extends Task { | ||
|
|
||
| protected String at; | ||
|
|
||
| public Event(String description, String at) { | ||
| super(description); | ||
| this.at = at; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "[E]" + super.toString() + "(at: " + at + ")"; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package duke.task; | ||
|
|
||
| public class Task { | ||
| protected String description; | ||
| protected boolean isDone; | ||
|
|
||
| public Task(String description) { | ||
| this.description = description; | ||
| this.isDone = false; | ||
| } | ||
|
|
||
| public void setDone() { | ||
| this.isDone = true; | ||
| } | ||
|
|
||
| public String getDescription() { | ||
| return this.description; | ||
| } | ||
|
|
||
| public String getStatusIcon() { | ||
| return (isDone ? "[X]" : "[ ]"); // mark done task with X | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return this.getStatusIcon() + " " + this.getDescription(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package duke.task; | ||
|
|
||
| public class ToDo 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. Do take note of the spacing for each bracket and curly braces for the consistency of the whole program. |
||
| public ToDo(String description) { | ||
| super(description); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "[T]" + super.toString(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,17 @@ | ||
| Hello from | ||
| ____ _ | ||
| | _ \ _ _| | _____ | ||
| | | | | | | | |/ / _ \ | ||
| | |_| | |_| | < __/ | ||
| |____/ \__,_|_|\_\___| | ||
|
|
||
| Hello I'm duke.command.Duke | ||
| What can I do for you? | ||
| Got it. I've added this task: | ||
| [T][ ] do wct | ||
| Now you have 1 tasks in the list | ||
| Got it. I've added this task: | ||
| [D][ ] inquiry (by: now ) | ||
| Now you have 2 tasks in the list | ||
| Got it. I've added this task: | ||
| [E][ ] wct (at: tomorrow 2pm ) | ||
| Now you have 3 tasks in the list | ||
| Nice! I've marked this task as done: | ||
| [X] do wct | ||
| 1.[T][X] do wct | ||
| 2.[D][ ] inquiry (by: now ) | ||
| 3.[E][ ] wct (at: tomorrow 2pm ) | ||
| Bye. Hope to see you again soon! |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| todo do wct | ||
| deadline inquiry /by now | ||
| event wct /at tomorrow 2pm | ||
| done 1 | ||
| list | ||
| bye |
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.
Consider using a clearer naming convention for your method such as printTasks().