-
Notifications
You must be signed in to change notification settings - Fork 191
[Vincent Lau Han Leong] iP #183
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 9 commits
cbf3cd2
91ce339
e4f50de
39dbc7c
b287fd9
b6db002
ab47db6
026be10
5da062e
14a2865
8d6b1e5
42969c1
46c8930
8493d64
974df0a
3dc210f
a9902e7
7c89dda
b9b174f
c254470
80a19f2
c2ec413
bcb0f12
10ef049
861e928
d76441c
f01e19e
7200398
c0a0767
54f9efa
ca92735
45dc063
540a897
31cc2eb
6f942e9
69ce26d
086b7ec
4663af3
266610b
c1dd736
2157f69
4ebd498
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,15 @@ | ||
| public class Deadline extends Task{ | ||
| public static final String BY = "by: "; | ||
| protected String date; | ||
| private static final String IDENTIFIER = "D"; | ||
|
|
||
| public Deadline(String description,String date) { | ||
| super(description); | ||
| this.date = date; | ||
| } | ||
|
|
||
| public String getStatusIconAndDescription() { | ||
| String icon = (isDone ? "X" : " "); | ||
| return addSquareBrackets(IDENTIFIER) + addSquareBrackets(icon) + " " + description + " " + addBrackets(BY + date); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,180 @@ | ||
| import java.util.Scanner; | ||
|
|
||
| public class Duke { | ||
| public static final String DIVIDER = "___________________________________________________________"; | ||
| public static final String INDENTATION = " "; | ||
| public static final String LOGO = " ____ _ \n" | ||
| + "| _ \\ _ _| | _____ \n" | ||
| + "| | | | | | | |/ / _ \\\n" | ||
| + "| |_| | |_| | < __/\n" | ||
| + "|____/ \\__,_|_|\\_\\___|\n"; | ||
| public static final String HELLO_MESSAGE_2 = "Hello! I'm Duke, your friendly neighbourhood task manager"; | ||
| public static final String HELLO_MESSAGE_3 = "What can I do for you? :D"; | ||
| public static final String TASK_COMPLETED_MESSAGE = "You've completed the task! Well done!"; | ||
| public static final String ADDED_TO_LIST = "I've added this to your list :D"; | ||
| public static final String DEADLINE_DESCRIPTION_AND_DATE_SPLITTER = " /by "; | ||
| public static final String EVENT_DESCRIPTION_AND_DATE_TIME_SPLITTER = " /at "; | ||
| public static final String GOODBYE_MESSAGE = "Bye, hope to see you again soon! :)"; | ||
| public static final String TYPE_SUITABLE_COMMAND_MESSAGE = "Sorry, you are using me wrongly. Please type in a suitable command :)"; | ||
| public static final boolean IS_FINE = true; | ||
| public static final int TODO_STARTING_INDEX = 5; | ||
| public static final int DEADLINE_STARTING_INDEX = 9; | ||
| public static final int EVENT_STARTING_INDEX = 6; | ||
| public static Task[] tasks = new Task[100]; | ||
|
|
||
| public static void main(String[] args) { | ||
| String logo = " ____ _ \n" | ||
| + "| _ \\ _ _| | _____ \n" | ||
| + "| | | | | | | |/ / _ \\\n" | ||
| + "| |_| | |_| | < __/\n" | ||
| + "|____/ \\__,_|_|\\_\\___|\n"; | ||
| System.out.println("Hello from\n" + logo); | ||
| printStartingMessage(); | ||
| Scanner in = new Scanner(System.in); | ||
| String input = in.nextLine(); | ||
| while (!input.equals("bye")) { | ||
| processInput(input); | ||
| input = in.nextLine(); | ||
| } | ||
| printGoodbyeMessage(); | ||
| } | ||
|
|
||
| public static void printIndentationAndDivider() { | ||
| System.out.print(INDENTATION); | ||
| System.out.println(DIVIDER); | ||
| } | ||
|
|
||
| public static void printWordsWithIndentation(String words) { | ||
| System.out.print(INDENTATION); | ||
| System.out.println(words); | ||
| } | ||
|
|
||
| private static void printStartingMessage() { | ||
| System.out.println("Hello from\n" + LOGO); | ||
| printIndentationAndDivider(); | ||
| printWordsWithIndentation(HELLO_MESSAGE_2); | ||
| printWordsWithIndentation(HELLO_MESSAGE_3); | ||
| printIndentationAndDivider(); | ||
| System.out.println(); | ||
| } | ||
|
|
||
| private static void printGoodbyeMessage() { | ||
| printIndentationAndDivider(); | ||
| printWordsWithIndentation(GOODBYE_MESSAGE); | ||
| printIndentationAndDivider(); | ||
| } | ||
|
|
||
| private static void processInput(String input) { | ||
| switch(input.split(" ")[0].toLowerCase()) { | ||
|
||
| case "list" : | ||
| executeListCase(); | ||
| break; | ||
| case "done" : | ||
| executeDoneCase(input); | ||
| break; | ||
| case "todo" : | ||
| executeTaskCase(input, TODO_STARTING_INDEX,TaskType.TODO); | ||
| break; | ||
| case "deadline" : | ||
| executeTaskCase(input, DEADLINE_STARTING_INDEX,TaskType.DEADLINE); | ||
| break; | ||
| case "event" : | ||
| executeTaskCase(input, EVENT_STARTING_INDEX,TaskType.EVENT); | ||
| break; | ||
| default : | ||
| printIncorrectInputMessage(); | ||
|
||
| } | ||
| } | ||
|
|
||
| private static void printTaskMessage() { | ||
| printIndentationAndDivider(); | ||
| printWordsWithIndentation(ADDED_TO_LIST); | ||
| printWordsWithIndentation(tasks[Task.getTotalTasks() - 1].getStatusIconAndDescription()); | ||
| printWordsWithIndentation(notifyNumberOfTasks()); | ||
| printIndentationAndDivider(); | ||
| System.out.println(); | ||
| } | ||
|
|
||
| private static void printIncorrectInputMessage() { | ||
| printIndentationAndDivider(); | ||
| printWordsWithIndentation(TYPE_SUITABLE_COMMAND_MESSAGE); | ||
| printIndentationAndDivider(); | ||
| System.out.println(); | ||
| } | ||
|
|
||
| private static void executeTaskCase(String input, int starting_index, TaskType type) { | ||
| String description = input.strip().substring(starting_index); | ||
| boolean isFine = addTask(description, type); | ||
| if(isFine) { | ||
| printTaskMessage(); | ||
| }else { | ||
| printIncorrectInputMessage(); | ||
| } | ||
| } | ||
|
|
||
| private static String[] parseInputForDifferentTask(String input,TaskType type) { | ||
| String[] parsedOutput = {}; | ||
| switch(type) { | ||
| case TODO: | ||
| parsedOutput = new String[]{input}; | ||
| break; | ||
|
|
||
|
||
| case DEADLINE: | ||
| parsedOutput = input.split(DEADLINE_DESCRIPTION_AND_DATE_SPLITTER); | ||
| break; | ||
|
|
||
| case EVENT: | ||
| parsedOutput = input.split(EVENT_DESCRIPTION_AND_DATE_TIME_SPLITTER); | ||
| break; | ||
| } | ||
|
||
| return parsedOutput; | ||
| } | ||
|
|
||
| private static boolean addTask(String input,TaskType type) { | ||
| String[] parsedOutput = parseInputForDifferentTask(input,type); | ||
| switch(type){ | ||
| case TODO: | ||
| tasks[Task.getTotalTasks()] = new ToDo(parsedOutput[0]); | ||
| break; | ||
| case EVENT: | ||
| if(parsedOutput.length < 2) { | ||
| return !IS_FINE; | ||
| } | ||
| tasks[Task.getTotalTasks()] = new Event(parsedOutput[0], parsedOutput[1]); | ||
| break; | ||
| case DEADLINE: | ||
| if(parsedOutput.length < 2) { | ||
| return !IS_FINE; | ||
| } | ||
| tasks[Task.getTotalTasks()] = new Deadline(parsedOutput[0], parsedOutput[1]); | ||
| break; | ||
| } | ||
| Task.setTotalTasks(Task.getTotalTasks() + 1); | ||
| return IS_FINE; | ||
| } | ||
|
|
||
| private static String notifyNumberOfTasks() { | ||
| return "Now you have " + Task.getTotalTasks() + " task(s) in the list"; | ||
| } | ||
|
|
||
| private static void executeDoneCase(String input) { | ||
| if(input.length() != 6) { | ||
| printIncorrectInputMessage(); | ||
| return; | ||
| } | ||
|
||
| int index = Integer.parseInt(input.split(" ")[1]) - 1; | ||
| tasks[index].markAsDone(); | ||
| printTaskCompletedMessage(tasks[index]); | ||
| } | ||
|
|
||
| private static void printTaskCompletedMessage(Task task) { | ||
| printIndentationAndDivider(); | ||
| printWordsWithIndentation(TASK_COMPLETED_MESSAGE); | ||
| printWordsWithIndentation(task.getStatusIconAndDescription()); | ||
| printIndentationAndDivider(); | ||
| System.out.println(); | ||
| } | ||
|
|
||
| private static void executeListCase() { | ||
| printIndentationAndDivider(); | ||
| for (int i = 0; i < Task.getTotalTasks(); i++) { | ||
| printWordsWithIndentation(i + 1 + "." + tasks[i].getStatusIconAndDescription()); | ||
| } | ||
| printIndentationAndDivider(); | ||
| System.out.println(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| public class Event extends Task{ | ||
| public static final String AT = "at: "; | ||
| protected String dateAndTime; | ||
| private static final String IDENTIFIER = "E"; | ||
|
|
||
| public Event(String description, String dateAndTime) { | ||
| super(description); | ||
| this.dateAndTime = dateAndTime; | ||
| } | ||
|
|
||
| public String getStatusIconAndDescription() { | ||
| String icon = (isDone ? "X" : " "); | ||
| return addSquareBrackets(IDENTIFIER) + addSquareBrackets(icon) + " " + description + " " + addBrackets(AT + dateAndTime); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| public class Task { | ||
| protected boolean isDone; | ||
| protected String description; | ||
|
|
||
| private static int totalTasks = 0; | ||
|
|
||
| public Task(String description){ | ||
| this.description = description; | ||
| isDone = false; | ||
| } | ||
|
|
||
| public String getStatusIconAndDescription() { | ||
| String icon = (isDone ? "X" : " "); | ||
| return addSquareBrackets(icon) + " " + description; | ||
| } | ||
|
|
||
| public void markAsDone() { | ||
| isDone = true; | ||
| } | ||
|
|
||
| public void setDescription(String description) { | ||
| this.description = description; | ||
| } | ||
|
|
||
| public static int getTotalTasks() { | ||
| return totalTasks; | ||
| } | ||
|
|
||
| public static void setTotalTasks(int totalTasks) { | ||
| Task.totalTasks = totalTasks; | ||
| } | ||
|
|
||
| protected static String addSquareBrackets(String s) { | ||
| return "[" + s + "]"; | ||
| } | ||
| protected static String addBrackets(String s) { | ||
| return "(" + s + ")"; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| public enum TaskType { | ||
| TODO,DEADLINE,EVENT; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| public class ToDo extends Task{ | ||
| private static final String IDENTIFIER = "T"; | ||
|
|
||
| public ToDo(String description) { | ||
| super(description); | ||
| } | ||
|
|
||
| public String getStatusIconAndDescription() { | ||
| String icon = (isDone ? "X" : " "); | ||
| return addSquareBrackets(IDENTIFIER) + addSquareBrackets(icon) + " " + description; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| todo borrow book | ||
| DeadLine return book /by Sunday | ||
| Event project meeting /at 4th Sep 2-3pm | ||
| DEADLINE /by Monday | ||
| done | ||
| list | ||
| 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.
A minor code improvement: add a space before the "{"