-
Notifications
You must be signed in to change notification settings - Fork 452
[Samuel Pang Shao Herng] ip #517
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?
Changes from 11 commits
556af3f
c79efa6
327c569
a4346b2
df16435
fd542b8
5d410b8
53a12ae
a003efa
738c9a8
17bb08c
9512919
83446a2
d777be2
1a2c997
0b16da0
32500d1
70bb7e5
12680d8
5130f46
f110900
507e8b8
d99e1ff
1fdadeb
27252f0
371855c
6027556
dd53432
0524628
ef6a4b7
7a9cd02
91ff3bb
9f1df79
52505dc
56949df
ab7eb48
ecbc9fd
ed9edd3
cc0eb58
1bf6279
3b79a44
0517320
07e59f3
18a86c2
ada09a8
0854fc2
dd4a604
4bd20cd
69abf4d
8b10487
8feb9d5
f7a0309
60ac1a3
63089dd
d86b26c
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 |
|---|---|---|
|
|
@@ -3,6 +3,9 @@ | |
| /out/ | ||
| /*.iml | ||
|
|
||
| # .class files | ||
| /.class/ | ||
|
|
||
| # Gradle build files | ||
| /.gradle/ | ||
| /build/ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| public class Deadline extends Task { | ||
|
|
||
| protected String by; | ||
|
|
||
| public Deadline(String description, String by) { | ||
| super(description); | ||
| this.by = by; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "[D]" + super.toString() + " (by: " + this.by + ")"; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,177 @@ | ||
| import java.util.ArrayList; | ||
| import java.util.Scanner; | ||
|
|
||
| public class Duke { | ||
| private static final String horizontalBorder = "_________________________________\n"; | ||
| private static final String INVALID_TODO_INPUT = " ☹ OOPS!!! The description of a todo cannot be empty.\n"; | ||
| private static final String INVALID_DEADLINE_INPUT = "☹ OOPS!!! Please use proper deadline formatting: deadline {task} /by {time}\n"; | ||
| private static final String INVALID_EVENT_INPUT = "☹ OOPS!!! Please use proper event formatting: event {task} /at {time}\n"; | ||
| private static final String INVALID_ACCESS_EMPTY_TASKLIST = "☹ OOPS!!! Task does not exist. Initialise a task first, then try again\n"; | ||
| private static final String INVALID_USER_INPUT = "☹ OOPS!!! Please use one of these keywords: {deadline, event, todo} followed by \\\"by\\\" and \\\"at\\\" for deadline and event tasks respectively.\n"; | ||
|
|
||
| private Tasklist tasklist; | ||
|
|
||
| private 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. It does not appear that this constructor is invoked so you could look into omitting this constructor. |
||
| this.tasklist = new Tasklist(); | ||
| } | ||
|
|
||
| private Duke(Tasklist tasklist){ | ||
| this.tasklist = tasklist; | ||
| } | ||
|
|
||
| private static String welcomeMessage(){ | ||
| return horizontalBorder + "Hello! I'm Duke\nWhat can I do for you?\n" + horizontalBorder; | ||
| } | ||
|
|
||
| private static String byeMessage(){ | ||
| return horizontalBorder + "Bye. Hope to see you again soon!\n" + horizontalBorder; | ||
| } | ||
|
|
||
| private String listContents(){ | ||
| return horizontalBorder + this.tasklist + horizontalBorder; | ||
| } | ||
|
|
||
| public String addTaskMessage(String taskString) { | ||
| return horizontalBorder + "Got it. I've added this task:\n" + taskString + "\n" + "Now you have " + this.tasklist.getCount() + " tasks in the list.\n" + horizontalBorder; | ||
|
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. Line length for this line is pretty long so you could look into using line wrapping such as splitting at the addition operators. |
||
| } | ||
|
|
||
| public String taskNotFoundMessage(){ | ||
| return "☹ OOPS!!! Task does not exist. Try another number between 1 and " + this.tasklist.getCount() + "\n"; | ||
| } | ||
|
|
||
| private String markDoneMessage(int position) throws DukeException{ | ||
| boolean isTaskMarked = this.tasklist.markTaskAtPos(position); | ||
| if (isTaskMarked) { | ||
| Task currentTask = this.tasklist.getTask(position); | ||
| return horizontalBorder + "Nice! I've marked this task as done:\n" + currentTask + "\n" + horizontalBorder; | ||
| } else if (this.tasklist.getCount() == 0) { | ||
| throw new DukeException(INVALID_ACCESS_EMPTY_TASKLIST); | ||
| } else { | ||
| throw new DukeException(taskNotFoundMessage()); | ||
| } | ||
| } | ||
|
|
||
| private String unmarkDoneMessage(int position) throws DukeException { | ||
| boolean isTaskUnmarked = this.tasklist.unmarkTaskAtPos(position); | ||
| if (isTaskUnmarked) { | ||
| Task currentTask = this.tasklist.getTask(position); | ||
| return horizontalBorder + "OK, I've marked this task as not done yet:\n" + currentTask + "\n" + horizontalBorder; | ||
| } else if (this.tasklist.getCount() == 0) { | ||
| throw new DukeException(INVALID_ACCESS_EMPTY_TASKLIST); | ||
| } else { | ||
| throw new DukeException(taskNotFoundMessage()); | ||
| } | ||
| } | ||
|
|
||
| private String deleteTaskMessage(int position) throws DukeException { | ||
| try { | ||
| Task deletedTask = this.tasklist.deleteTaskAtPos(position); | ||
| return horizontalBorder + "Noted. I've removed this task:\n" + deletedTask + "\n" + "" + "Now you have " + this.tasklist.getCount() + " tasks in the list.\n" + horizontalBorder; | ||
| } catch (IndexOutOfBoundsException e){ | ||
| if (this.tasklist.getCount() == 0){ | ||
| throw new DukeException(INVALID_ACCESS_EMPTY_TASKLIST); | ||
| } else { | ||
| throw new DukeException(taskNotFoundMessage()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private boolean isInteger(String value) { | ||
| try { | ||
| Integer.parseInt(value); | ||
| return true; | ||
| } catch (NumberFormatException e) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| private String makeToDoFromInput(String input) throws DukeException { | ||
| String description = input.substring("todo".length()).strip(); | ||
| if (!description.equals("")) { | ||
| ToDo newToDo = new ToDo(description); | ||
| this.tasklist.add(newToDo); | ||
| return addTaskMessage(newToDo.toString()); | ||
| } else { | ||
| throw new DukeException(INVALID_TODO_INPUT); | ||
| } | ||
| } | ||
|
|
||
| private String makeDeadlineFromInput(String input) throws DukeException { | ||
| String[] stringArray = input.substring("deadline".length()).strip().split("/by"); | ||
| if (stringArray.length > 1) { | ||
| Deadline newDeadline = new Deadline(stringArray[0].strip(), stringArray[1].strip()); | ||
| this.tasklist.add(newDeadline); | ||
| return addTaskMessage(newDeadline.toString()); | ||
| } else { | ||
| throw new DukeException(INVALID_DEADLINE_INPUT); | ||
| } | ||
| } | ||
|
|
||
| private String makeEventFromInput(String input) throws DukeException{ | ||
| String[] stringArray = input.substring("event".length()).strip().split("/at"); | ||
| if (stringArray.length > 1) { | ||
| Event newEvent = new Event(stringArray[0].strip(), stringArray[1].strip()); | ||
| this.tasklist.add(newEvent); | ||
| return addTaskMessage(newEvent.toString()); | ||
| } else { | ||
| throw new DukeException(INVALID_EVENT_INPUT); | ||
| } | ||
| } | ||
|
|
||
| public void run(){ | ||
| System.out.println(welcomeMessage()); | ||
| Scanner scan = new Scanner(System.in); | ||
| String s = scan.nextLine(); | ||
| boolean exitNow = false; | ||
|
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 using a variable name which sounds more like a Boolean such as isExit. |
||
| while(!exitNow) { | ||
| try { | ||
| String[] commandList = s.strip().split(" "); | ||
| String command = commandList[0].toLowerCase(); | ||
| if (command.equals("bye") && commandList.length == 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. You can consider using switch statements here. |
||
| exitNow = true; | ||
| System.out.println(byeMessage()); | ||
| } else if (command.equals("list") && commandList.length == 1) { | ||
| System.out.println(listContents()); | ||
| } else if (command.equals("mark") && commandList.length > 1 && isInteger(commandList[1])) { | ||
| int taskIndexNum = Integer.parseInt(commandList[1]); | ||
| System.out.println(markDoneMessage(taskIndexNum)); | ||
| } else if (command.equals("unmark") && commandList.length > 1 && isInteger(commandList[1])) { | ||
| int taskIndexNum = Integer.parseInt(commandList[1]); | ||
| System.out.println(unmarkDoneMessage(taskIndexNum)); | ||
| } else if (command.equals("deadline")) { | ||
| System.out.println(makeDeadlineFromInput(s)); | ||
| } else if (command.equals("event")) { | ||
| System.out.println(makeEventFromInput(s)); | ||
| } else if (command.equals("todo")) { | ||
| System.out.println(makeToDoFromInput(s)); | ||
| } else if (command.equals("delete") && commandList.length > 1 && isInteger(commandList[1])){ | ||
| int taskIndexNum = Integer.parseInt(commandList[1]); | ||
| System.out.println(deleteTaskMessage(taskIndexNum)); | ||
| } else if (!s.strip().equals("")) { | ||
| System.out.println(horizontalBorder + INVALID_USER_INPUT + horizontalBorder); | ||
| } | ||
| } catch (DukeException e) { | ||
| System.out.println(horizontalBorder + e.getMessage() + horizontalBorder); | ||
| } finally { | ||
| if (!exitNow) { | ||
| s = scan.nextLine(); | ||
| } | ||
| } | ||
| } | ||
| scan.close(); | ||
| } | ||
|
|
||
|
|
||
| public static void main(String[] args) { | ||
| String logo = " ____ _ \n" | ||
| + "| _ \\ _ _| | _____ \n" | ||
| + "| | | | | | | |/ / _ \\\n" | ||
| + "| |_| | |_| | < __/\n" | ||
| + "|____/ \\__,_|_|\\_\\___|\n"; | ||
| System.out.println("Hello from\n" + logo); | ||
| Duke sampleDuke = new Duke(new Tasklist()); | ||
| sampleDuke.run(); | ||
| // ArrayList<String> a = new ArrayList<>(); | ||
| // a.add("lol"); | ||
| // a.add("gan"); | ||
| // a.add("lasso"); | ||
| // a.remove(1); | ||
| // for (int i = 0; i < a.size(); i++){ | ||
| // System.out.println(a.get(i)); | ||
| // } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| public class DukeException extends Exception{ | ||
| public DukeException(String message){ | ||
| super(message); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| 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: " + this.at + " )"; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
|
|
||
| public class Task { | ||
| protected String description; | ||
| protected boolean isDone; | ||
|
|
||
| public Task(String description) { | ||
| this.description = description; | ||
| this.isDone = false; | ||
| } | ||
|
|
||
| public String getStatusIcon() { | ||
| return (this.isDone ? "X" : " "); | ||
| } | ||
|
|
||
| public void markAsDone() { | ||
| this.isDone = true; | ||
| } | ||
|
|
||
| public void unmark() { | ||
| this.isDone = false; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString(){ | ||
| return "[" + getStatusIcon() + "] " + this.description; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| import java.util.ArrayList; | ||
|
|
||
| public class Tasklist { | ||
| private ArrayList<Task> taskArray; | ||
| private int count = 0; | ||
|
|
||
| public Tasklist(){ | ||
| this.taskArray = new ArrayList<>(); | ||
| } | ||
|
|
||
| public void add(Task task){ | ||
| this.taskArray.add(task); | ||
| this.count += 1; | ||
| } | ||
|
|
||
| public Task getTask(int position) throws IndexOutOfBoundsException{ | ||
| return taskArray.get(position - 1); | ||
| } | ||
|
|
||
| public int getCount(){ | ||
| return this.count; | ||
| } | ||
|
|
||
| public boolean markTaskAtPos(int position){ | ||
| try { | ||
| Task currTask = getTask(position); | ||
| currTask.markAsDone(); | ||
| return true; | ||
| } catch (IndexOutOfBoundsException e) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| public boolean unmarkTaskAtPos(int position){ | ||
| try { | ||
| Task currTask = getTask(position); | ||
| currTask.unmark(); | ||
| return true; | ||
| } catch (IndexOutOfBoundsException e) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| public Task deleteTaskAtPos(int position) throws IndexOutOfBoundsException { | ||
| Task deletedTask = getTask(position); | ||
| this.taskArray.remove(position - 1); | ||
| this.count -= 1; | ||
| return deletedTask; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString(){ | ||
| String stringedList = ""; | ||
| for (int i = 0; i < this.count; i++) { | ||
| stringedList += (i + 1) + ". " + getTask(i + 1).toString() + "\n"; | ||
| } | ||
| return "Here are the tasks in your list:\n" + stringedList; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| public class ToDo extends Task{ | ||
|
|
||
| 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,51 @@ | ||
| Hello from | ||
| ____ _ | ||
| | _ \ _ _| | _____ | ||
| | | | | | | | |/ / _ \ | ||
| | |_| | |_| | < __/ | ||
| |____/ \__,_|_|\_\___| | ||
| _________________________________ | ||
| Hello! I'm Duke | ||
| What can I do for you? | ||
| _________________________________ | ||
|
|
||
| _________________________________ | ||
| Got it. I've added this task: | ||
| [E][ ] Tuition (at: Mon 2-3pm ) | ||
| Now you have 1 tasks in the list. | ||
| _________________________________ | ||
|
|
||
| _________________________________ | ||
| Got it. I've added this task: | ||
| [T][ ] Eat dinner | ||
| Now you have 2 tasks in the list. | ||
| _________________________________ | ||
|
|
||
| _________________________________ | ||
| Got it. I've added this task: | ||
| [D][ ] Math assignment submission (by: Friday) | ||
| Now you have 3 tasks in the list. | ||
| _________________________________ | ||
|
|
||
| _________________________________ | ||
| ☹ OOPS!!! Please use one of these keywords: {deadline, event, todo} followed by \"by\" and \"at\" for deadline and event tasks respectively. | ||
| _________________________________ | ||
|
|
||
| _________________________________ | ||
| Nice! I've marked this task as done: | ||
| [T][X] Eat dinner | ||
| _________________________________ | ||
|
|
||
| _________________________________ | ||
| ☹ OOPS!!! Task does not exist. Try another number between 1 and 3 | ||
| _________________________________ | ||
|
|
||
| _________________________________ | ||
| Here are the tasks in your list: | ||
| 1. [E][ ] Tuition (at: Mon 2-3pm ) | ||
| 2. [T][X] Eat dinner | ||
| 3. [D][ ] Math assignment submission (by: Friday) | ||
| _________________________________ | ||
|
|
||
| _________________________________ | ||
| ☹ OOPS!!! Please use proper deadline formatting: deadline {task} /by {time} | ||
| _________________________________ | ||
|
|
||
| _________________________________ | ||
| Bye. Hope to see you again soon! | ||
| _________________________________ | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| event Tuition /at Mon 2-3pm | ||
| todo Eat dinner | ||
| deadline Math assignment submission /by Friday | ||
| make up bed | ||
| mark 2 | ||
| unmark 0 | ||
| list | ||
| deadline Math assignment 2 | ||
| 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.
This variable is only used within the class so it could use the private access modifier instead