-
Notifications
You must be signed in to change notification settings - Fork 191
[Teo Ziyi Ivy] iP #182
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?
[Teo Ziyi Ivy] iP #182
Changes from 16 commits
a05464f
efd9e5b
99d6469
1814bd1
0e868fe
de9dc0a
0061596
860304e
466cbee
781043a
50e7e6b
a6b0cb4
81aba61
dcd1d91
63405a4
9096123
f6335dc
a5faa2b
8d4fd35
d17bf94
890f18e
43da2b2
e39676b
55b724b
76199bd
a87decb
a5be714
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,21 @@ | ||
| package duke; | ||
|
|
||
| public class Deadline extends Todo{ | ||
|
||
| protected String by; | ||
|
|
||
| public Deadline(String description, String by) { | ||
| super(description); | ||
| this.by = by; | ||
| } | ||
| public String getBy() { | ||
| return by; | ||
| } | ||
| public void setBy(String by) { | ||
| this.by = by; | ||
| } | ||
|
||
|
|
||
| public void printTask() { | ||
| System.out.print("[D][" + getStatusIcon() + "] " + getDescription()); | ||
|
||
| System.out.println("(by:" + by + ")"); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| package duke; | ||
|
|
||
| public class DeadlineException extends Exception{ | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| package duke; | ||
|
|
||
| public class DoneException extends Exception{ | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| package duke; | ||
|
|
||
| import java.util.Scanner; | ||
|
|
||
| public class Duke { | ||
| public static Task[] tasks = new Task[100]; | ||
| public static int taskCount = 0; | ||
|
|
||
| public static void printGreeting() { | ||
| System.out.println("Hello! I'm Duke\n" + "What can I do for you?"); | ||
| } | ||
|
|
||
| public static void printConfused() { | ||
| System.out.println("Could you say that again?"); | ||
| } | ||
|
|
||
| public static void printExit() { | ||
| System.out.println("Bye. Hope to see you again soon!"); | ||
| } | ||
|
|
||
| public static void printGotIt() { | ||
| System.out.println("Got it. I've added this task:"); | ||
| } | ||
|
|
||
| public static void printTaskCount() { | ||
| System.out.println("Now you have " + taskCount + " tasks in the list."); | ||
| } | ||
|
|
||
| public static void printTodoException() { | ||
| System.out.println("The description of todo cannot be empty."); | ||
| } | ||
|
|
||
| public static void printDeadlineException() { | ||
| System.out.println("The description of deadline cannot be empty."); | ||
| } | ||
|
|
||
| public static void printEventException() { | ||
| System.out.println("The description of event cannot be empty."); | ||
| } | ||
|
|
||
| public static void printDoneException() { | ||
| System.out.println("The description of done cannot be empty."); | ||
| } | ||
|
||
|
|
||
| public static void printTaskTypeResponse() { | ||
| //printing different responses depending if its duke.Todo/duke.Deadline/duke.Event | ||
|
||
| printGotIt(); | ||
| tasks[taskCount - 1].printTask(); | ||
| printTaskCount(); | ||
| } | ||
|
|
||
| public static void printList() { | ||
| System.out.println("Here are the tasks in your list:"); | ||
| for (int i = 0; i < taskCount; i += 1) { | ||
| System.out.print((i + 1) + "."); | ||
| tasks[i].printTask(); | ||
| } | ||
| } | ||
|
|
||
| public static void taskDone(String line) throws DoneException { | ||
|
||
| if (line.equals("") || line.equals("done")) { | ||
| throw new DoneException(); | ||
| } else { | ||
| int index = Integer.parseInt(line) - 1; | ||
| tasks[index].setDone(); | ||
| System.out.println("Nice! I've marked this task as done:"); | ||
| tasks[index].printTask(); | ||
| } | ||
| } | ||
|
|
||
| public static void addTodo(String line) throws TodoException { | ||
| if (line.equals("") || line.equals("todo")) { | ||
| throw new TodoException(); | ||
| } else { | ||
| tasks[taskCount] = new Todo(line); | ||
| taskCount = taskCount + 1; | ||
| printTaskTypeResponse(); | ||
| } | ||
| } | ||
|
|
||
| public static void addDeadline(String line) throws DeadlineException { | ||
| if (!line.matches("(.*)/by(.*)")) { | ||
| throw new DeadlineException(); | ||
| } else { | ||
| //extracting the description and date | ||
| String description = line.replaceAll("/.+", ""); | ||
| String by = line.replaceAll(".+/by", ""); | ||
| tasks[taskCount] = new Deadline(description, by); | ||
| taskCount = taskCount + 1; | ||
| printTaskTypeResponse(); | ||
| } | ||
| } | ||
|
|
||
| public static void addEvent(String line) throws EventException { | ||
| if (!line.matches("(.*)/at(.*)")) { | ||
| throw new EventException(); | ||
| } else { | ||
| //extracting the description and date | ||
| String description = line.replaceAll("/.+", ""); | ||
| String by = line.replaceAll(".+/at", ""); | ||
| tasks[taskCount] = new Event(description, by); | ||
| taskCount = taskCount + 1; | ||
| printTaskTypeResponse(); | ||
| } | ||
| } | ||
|
|
||
| public static void readInput(String line) { | ||
| String[] splitLine = line.split(" ", 2); | ||
|
||
| String command = splitLine[0]; | ||
| line = line.replaceAll("^" + command + " ", ""); | ||
| if (command.equals("list")) { | ||
| printList(); | ||
| } else if (command.equals("done")) { | ||
| try { | ||
| //find which task user is done with | ||
| taskDone(line); | ||
| } catch (DoneException e) { | ||
| printDoneException(); | ||
| } | ||
| } else if (command.equals("todo")) { | ||
| try { | ||
| addTodo(line); | ||
| } catch (TodoException e) { | ||
| printTodoException(); | ||
| } | ||
| } else if (command.equals("deadline")) { | ||
| try { | ||
| addDeadline(line); | ||
| } catch (DeadlineException e) { | ||
| printDeadlineException(); | ||
| } | ||
| } else if (command.equals("event")) { | ||
| try { | ||
| addEvent(line); | ||
| } catch (EventException e) { | ||
| printEventException(); | ||
| } | ||
| } else { | ||
| //error with input | ||
| printConfused(); | ||
| } | ||
| } | ||
|
||
|
|
||
| public static void main(String[] args) { | ||
| printGreeting(); | ||
| String line; | ||
| Scanner in = new Scanner(System.in); | ||
| line = in.nextLine(); | ||
| //continue to run program unless types "bye" to exit program | ||
|
||
| while(!line.equals("bye")) { | ||
| readInput(line); | ||
| line = in.nextLine(); | ||
| } | ||
| printExit(); | ||
| } | ||
| } | ||
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package duke; | ||
|
|
||
| public class Event extends Todo{ | ||
| protected String at; | ||
|
|
||
| public Event(String description, String at) { | ||
| super(description); | ||
| this.at = at; | ||
| } | ||
| public String getAt() { | ||
| return at; | ||
| } | ||
| public void setAt(String at) { | ||
| this.at = at; | ||
| } | ||
|
|
||
| public void printTask() { | ||
| System.out.print("[E][" + getStatusIcon() + "] " + getDescription()); | ||
| System.out.println("(at:" + at + ")"); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| package duke; | ||
|
|
||
| public class EventException extends Exception{ | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package duke; | ||
|
|
||
| public class Task { | ||
| protected String description; | ||
| protected boolean isDone; | ||
|
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 your use the |
||
|
|
||
| public Task(String description) { | ||
| this.description = description; | ||
| isDone = false; | ||
| } | ||
|
|
||
| public String getStatusIcon() { | ||
| // mark done task with X | ||
| return (isDone ? "X" : " "); | ||
|
Comment on lines
+13
to
+
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 your comment is indented relative to its position in the code. Good job! |
||
| } | ||
|
|
||
| public String getDescription() { | ||
| return description; | ||
| } | ||
|
|
||
| public void setDone() { | ||
| isDone = true; | ||
| } | ||
|
|
||
| public void printTask() { | ||
| System.out.println(description); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| package duke; | ||
|
|
||
| public class TaskCountException extends Exception{ | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package duke; | ||
|
|
||
| public class Todo extends Task{ | ||
|
|
||
| public Todo(String description) { | ||
| super(description); | ||
| } | ||
|
|
||
| public void printTask() { | ||
| System.out.println("[T][" + getStatusIcon() + "] " + getDescription()); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| package duke; | ||
|
|
||
| public class TodoException extends Exception{ | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,20 @@ | ||
| Hello from | ||
| ____ _ | ||
| | _ \ _ _| | _____ | ||
| | | | | | | | |/ / _ \ | ||
| | |_| | |_| | < __/ | ||
| |____/ \__,_|_|\_\___| | ||
|
|
||
| Hello! I'm Duke | ||
| What can I do for you? | ||
| Could you say that again? | ||
| Got it. I've added this task: | ||
| [T][ ] eat cake | ||
| Now you have 1 tasks in the list. | ||
| Got it. I've added this task: | ||
| [D][ ] eat chocolate cake (by: Friday 2pm) | ||
| Now you have 2 tasks in the list. | ||
| Got it. I've added this task: | ||
| [E][ ] attend cake buffet (at: Saturday 5pm) | ||
| Now you have 3 tasks in the list. | ||
| Nice! I've marked this task as done: | ||
| [T][X] eat cake | ||
| The description of todo cannot be empty. | ||
| Here are the tasks in your list: | ||
| 1.[T][X] eat cake | ||
| 2.[D][ ] eat chocolate cake (by: Friday 2pm) | ||
| 3.[E][ ] attend cake buffet (at: Saturday 5pm) | ||
| Could you say that again? |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| eat cake | ||
| todo eat cake | ||
| deadline eat chocolate cake /by Friday 2pm | ||
| event attend cake buffet /at Saturday 5pm | ||
| done 1 | ||
| todo | ||
| list | ||
| cake | ||
| 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.
Great to see that you organise your types (e.g., classes) into a package for easier management!