-
Notifications
You must be signed in to change notification settings - Fork 223
[{Shi Zheng}] iP #258
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?
[{Shi Zheng}] iP #258
Changes from 6 commits
1e3655a
49ee517
5ebbd23
1675a38
80e2e5c
dd46e6e
6663e80
5c10cf2
5d6c844
7672a13
a0a85c8
e295557
8b15dd1
33d72f8
84ce18f
ec32cf3
7559aec
20d5fc5
f50d7d8
7baa86b
5032f29
53fbf79
09a6d02
66a7bff
58bb224
6e4fa72
97d2238
fb11842
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 |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| public class Deadline extends Task { | ||
|
|
||
| public Deadline(String description) { | ||
| super(description); | ||
| } | ||
|
|
||
| public Deadline(String description, boolean b) { | ||
| super(description,b); | ||
|
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. For greater readability, perhaps you could have a whitespace after the comma? |
||
| } | ||
|
|
||
| public String toString() { | ||
| return "[D]" + super.toString(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,95 @@ | ||
| import java.util.Scanner; | ||
| import java.util.ArrayList; | ||
|
|
||
| public class Duke { | ||
|
|
||
| /** | ||
| * main method to run the program | ||
| * | ||
| * @param args command line arguments taken in | ||
| */ | ||
|
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. Minor nitpick but perhaps consider starting Javadoc sentences with an uppercase letter and ending with a full stop to keep within the coding standard. |
||
| public static void main(String[] args) { | ||
| String logo = " ____ _ \n" | ||
| + "| _ \\ _ _| | _____ \n" | ||
| + "| | | | | | | |/ / _ \\\n" | ||
| + "| |_| | |_| | < __/\n" | ||
| + "|____/ \\__,_|_|\\_\\___|\n"; | ||
| System.out.println("Hello from\n" + logo); | ||
| Scanner sc = new Scanner(System.in); | ||
| System.out.println("Hello! I'm Duke\n"+ "What can I do for you?"); | ||
| ArrayList<Task> items = new ArrayList<>(); | ||
|
|
||
| while (true) { | ||
| String input = sc.nextLine(); | ||
| try { | ||
| errorHandling(input); | ||
| if (input.equals("bye")) { | ||
| bye(); | ||
| break; | ||
| } else if (input.contains("done")) { | ||
| System.out.println("Nice! I've marked this task as done: "); | ||
|
|
||
| int taskNum = Integer.parseInt(input.substring(5)); | ||
| String name = items.get(taskNum - 1).getDescription(); | ||
|
|
||
| Task type = items.get(taskNum - 1); | ||
| items.remove(taskNum - 1); | ||
| if (type instanceof Todo) { | ||
| Todo markDone = new Todo(name, true); | ||
| items.add(taskNum - 1, markDone); | ||
| System.out.println(markDone); | ||
| } else if (type instanceof Deadline) { | ||
| Deadline markDone = new Deadline(name, true); | ||
| items.add(taskNum - 1, markDone); | ||
| System.out.println(markDone); | ||
| } else { | ||
| Event markDone = new Event(name, true); | ||
| items.add(taskNum - 1, markDone); | ||
| System.out.println(markDone); | ||
| } | ||
|
|
||
| } else if (input.equals("list")) { | ||
| int n = 1; | ||
| System.out.println("Here are the tasks in your list:"); | ||
| for (Task item : items) { | ||
| System.out.println(n + ". " + item); | ||
| n++; | ||
| } | ||
|
|
||
| } else if (input.length() >= 8 && input.substring(0,8).equals("deadline")) { | ||
| items.add(new Deadline(input.substring(8))); | ||
| } else if (input.length() >= 4 && input.substring(0,4).equals("todo")) { | ||
| items.add(new Todo(input.substring(4))); | ||
| } else if (input.length() >= 5 && input.substring(0,5).equals("event")) { | ||
| items.add(new Event(input.substring(5))); | ||
| } else if (input.length() >= 6 && input.substring(0,6).equals("delete")) { | ||
| int taskToDelete = Integer.parseInt(input.substring(7)); | ||
| Task toRemove = items.get(taskToDelete - 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. Perhaps int taskToDelete could be indexOfTaskToDelete even though it may be quite long. |
||
| items.remove(taskToDelete - 1); | ||
| System.out.println("Noted. I've removed this task:\n " | ||
| + toRemove + "\nNow you have " + items.size() + " tasks in the list."); | ||
|
|
||
| } else { | ||
| items.add(new Task(input)); | ||
| } | ||
| } catch (Exception e) { | ||
| System.out.println(e); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * prints Goodbye message. | ||
| */ | ||
| public static void bye() { | ||
| System.out.println("Bye. Hope to see you again soon!"); | ||
| } | ||
|
|
||
| public static void errorHandling(String input) throws DukeException { | ||
|
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. Following the coding guidelines, method names should be verbs. Perhaps handleError() would be a better method 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. Same sentiment as the above! |
||
| if (input.length() == 4 && input.equals("todo")) { | ||
| throw new DukeException(" ☹ OOPS!!! The description of a todo cannot be empty."); | ||
| } else if (input.length() == 8 && input.equals("deadline")) { | ||
| throw new DukeException(" ☹ OOPS!!! The description of a deadline cannot be empty."); | ||
| } else if (input.length() == 5 && input.equals("event")) { | ||
| throw new DukeException(" ☹ OOPS!!! The description of a event cannot be empty."); | ||
| } else if (input.length() >= 4 && input.substring(0,4).equals("blah")) { | ||
| throw new DukeException("☹ OOPS!!! I'm sorry, but I don't know what that means :-("); | ||
| } else if (input.isBlank()) { | ||
| throw new DukeException(" ☹ OOPS!!! A Task cannot be empty."); | ||
| } else {} | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| public class DukeException extends Exception { | ||
|
|
||
| public DukeException(String e) { | ||
| super(e); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| public class Event extends Task { | ||
| public Event(String description) { | ||
|
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 there be an empty line before this constructor? |
||
| super(description); | ||
| } | ||
|
|
||
| public Event(String description, boolean b) { | ||
| super(description,b); | ||
|
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 use isDone instead of b, so that it is more descriptive? |
||
| } | ||
|
|
||
| public String toString() { | ||
| return "[E]" + super.toString(); | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| public class Task { | ||
| protected String description; | ||
| protected boolean isDone; | ||
|
|
||
| public Task(String description) { | ||
| this.description = description; | ||
| this.isDone = false; | ||
| } | ||
|
|
||
| public Task(String description, boolean b) { | ||
| this.description = description; | ||
| this.isDone = b; | ||
| } | ||
|
|
||
| public String getStatusIcon() { | ||
| return (isDone ? "X" : " "); // mark done task with X | ||
| } | ||
|
|
||
| public String getDescription() { | ||
| return this.description; | ||
| } | ||
|
|
||
| public Task changeStatus(Task t) { | ||
|
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. Since this method is a setter, maybe setStatusTrue() would be better? It would explain to readers what status you are changing to as well! |
||
| return new Task(t.getDescription(), true); | ||
| } | ||
|
|
||
| public String toString() { | ||
| return "[" + getStatusIcon() + "]" + getDescription(); | ||
|
|
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| public class Todo extends Task { | ||
| public Todo(String description) { | ||
| super(description); | ||
| } | ||
|
|
||
| public Todo(String description, boolean b) { | ||
| super(description, b); | ||
| } | ||
|
|
||
| public String toString() { | ||
| return "[T]" + super.toString(); | ||
| } | ||
| } | ||
|
|
||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| todo homework1 | ||
| event lecture 1 (10.00am tmr) | ||
| deadline OP1cs2101 (16 Feb) | ||
|
|
||
| done 1 | ||
| done 2 | ||
| done 3 |
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.
instead of using b, you could just use isDone.