-
Notifications
You must be signed in to change notification settings - Fork 76
[Shi Haochen] iP #77
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 Haochen] iP #77
Changes from 3 commits
cd6d347
13ec220
18f70b5
71a2724
69f1c6c
4d8d6ec
faddc9e
b800f93
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,13 @@ | ||
| public class Deadline extends Task{ | ||
| protected String by; | ||
|
|
||
| public Deadline(String description, String by) throws DukeException{ | ||
| super(description); | ||
| this.by = by; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "[D]" + super.toString() + " (by: " + by + ")"; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,92 @@ | ||
| import java.util.Scanner; | ||
| import java.util.List; | ||
| import java.util.ArrayList; | ||
| public class Duke { | ||
| protected final static String line = "_______________________________________________"; | ||
|
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. follow the correct naming standard for constants |
||
| private static void print(String message){ | ||
| System.out.println(line); | ||
| System.out.println(message); | ||
| System.out.println(line); | ||
| } | ||
| public static void main(String[] args) { | ||
| String logo = " ____ _ \n" | ||
| + "| _ \\ _ _| | _____ \n" | ||
| + "| | | | | | | |/ / _ \\\n" | ||
| + "| |_| | |_| | < __/\n" | ||
| + "|____/ \\__,_|_|\\_\\___|\n"; | ||
| System.out.println("Hello from\n" + logo); | ||
| String greet = "Hello! I'm Elwin\n" + "What can I do for you?"; | ||
|
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. Can use capital letters for constant eg. GREET |
||
| //String line = "_______________________________________________"; | ||
|
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. remove unecessary code |
||
| String exit = "Bye. Hope to see you again soon!"; | ||
|
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 line is used only once, should it be extracted out and just print it directly when needed? |
||
| String listRequirements = "Here are the tasks in your list:"; | ||
|
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 job! Use of camelCase |
||
| String markRequirements = "Nice! I've marked this task as done:"; | ||
| String unmarkRequirements = "OK, I've marked this task as not done yet:"; | ||
| String markFormat = "^mark \\d+$"; | ||
| String unmarkFormat = "^unmark \\d+$"; | ||
|
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 have some confusion regarding these strings. Can you provide some clarification on their meaning? |
||
| System.out.println(line); | ||
| System.out.println(greet); | ||
| System.out.println(line); | ||
| Scanner scanner = new Scanner(System.in); | ||
| List<Task> todoList = new ArrayList<>(); | ||
| while(true){ | ||
| String input = scanner.nextLine(); | ||
| if(input.equals("bye")){ | ||
|
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. Remember to add spacing for if else class statements eg. if (something) { |
||
| break; | ||
| }else if(input.equals("list")){ | ||
| System.out.println(line); | ||
| System.out.println(listRequirements); | ||
| for(int i = 0; i < todoList.size(); i++){ | ||
| System.out.println(i+1 + "." + todoList.get(i)); | ||
| } | ||
| System.out.println(line); | ||
| }else if(input.matches(markFormat)) { | ||
| int index = Integer.parseInt(input.substring(5)); | ||
| try{ | ||
| todoList.get(index-1).unmarkAsDone(); | ||
| }catch(IndexOutOfBoundsException e){ | ||
| print("☹ OOPS!!! The task number is not in the list."); | ||
| continue; | ||
| } | ||
| print(markRequirements+"\n "+todoList.get(index-1)); | ||
| }else if(input.matches(unmarkFormat)) { | ||
| int index = Integer.parseInt(input.substring(7)); | ||
| try{ | ||
| todoList.get(index-1).unmarkAsDone(); | ||
| }catch(IndexOutOfBoundsException e){ | ||
| print("☹ OOPS!!! The task number is not in the list."); | ||
| continue; | ||
| } | ||
| print(unmarkRequirements+"\n "+todoList.get(index-1)); | ||
| }else if(input.split(" ")[0].equals("todo")){ | ||
| Todo todo; | ||
| try{ | ||
| todo = new Todo(input.substring(4, input.length())); | ||
| }catch(DukeException e){ | ||
| continue; | ||
| } | ||
| todoList.add(todo); | ||
| print("Got it. I've added this task:\n "+todo+"\nNow you have " + todoList.size() + " tasks in the list."); | ||
| }else if(input.split(" ")[0].equals("deadline")){ | ||
| Deadline deadline; | ||
| try { | ||
| deadline = new Deadline(input.split("/")[0].substring(9, input.split("/")[0].length() - 1), input.split("/")[1].replace("by ", "")); | ||
| }catch (DukeException e){ | ||
| continue; | ||
| } | ||
| todoList.add(deadline); | ||
| print("Got it. I've added this task:\n "+deadline+"\nNow you have " + todoList.size() + " tasks in the list."); | ||
| }else if(input.split(" ")[0].equals("event")){ | ||
| Event event; | ||
| try{ | ||
| event = new Event(input.split("/")[0].substring(5, input.split("/")[0].length()-1), input.split("/")[1], input.split("/")[2]); | ||
| }catch(DukeException e){ | ||
| continue; | ||
| }catch(StringIndexOutOfBoundsException e){ | ||
| print("☹ OOPS!!! The description of a event should use \\ mark start time and end time."); | ||
| continue; | ||
| } | ||
| todoList.add(event); | ||
| print("Got it. I've added this task:\n "+event+"\nNow you have " + todoList.size() + " tasks in the list."); | ||
| }else{ | ||
| print("☹ OOPS!!! I'm sorry, but I don't know what that means :-("); | ||
| } | ||
| } | ||
| scanner.close(); | ||
| System.out.println(exit); | ||
| System.out.println(line); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| public class DukeException extends Exception{ | ||
| protected String line = "_______________________________________________"; | ||
| public DukeException(String message){ | ||
| super(message); | ||
| System.out.println(line); | ||
| System.out.println(message); | ||
| System.out.println(line); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| public class Event extends Task{ | ||
| protected String by; | ||
| protected String from; | ||
| public Event(String description, String from, String by) throws DukeException{ | ||
| super(description); | ||
| if(!from.contains("from ")){ | ||
| throw new DukeException("☹ OOPS!!! The description of a event must contain \"from\" time."); | ||
| }else if(!by.contains("to ")){ | ||
| throw new DukeException("☹ OOPS!!! The description of a event must contain \"to\" time."); | ||
| } | ||
| this.from = from; | ||
| this.by = by; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "[E]" + super.toString() + " (from: " + this.from + " to: " + this.by + ")"; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| public class Task{ | ||
| protected String description; | ||
| protected boolean isDone; | ||
|
|
||
| public Task(String description) throws DukeException { | ||
| if(description.isBlank()){ | ||
| throw new DukeException("☹ OOPS!!! The description of a task cannot be empty."); | ||
| } | ||
| this.description = description; | ||
| this.isDone = false; | ||
| } | ||
|
|
||
| public String getStatusIcon() { | ||
| return (isDone ? "X" : " "); // mark done task with X | ||
| } | ||
|
|
||
| public void markAsDone() { | ||
| this.isDone = true; | ||
| } | ||
|
|
||
| public void unmarkAsDone(){ | ||
| this.isDone = false; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "[" + getStatusIcon() + "]" + this.description; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| public class Todo extends Task{ | ||
| public Todo(String description) throws DukeException{ | ||
| super(description); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "[T]" + super.toString(); | ||
| } | ||
| } |
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.
Good job! names representing packages are in lower case