-
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 15 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
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,227 @@ | ||
| package duke; | ||
|
|
||
| import task.Deadline; | ||
| import task.Event; | ||
| import task.Task; | ||
| import task.TaskType; | ||
| import task.ToDo; | ||
|
|
||
| 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, I don't know what you mean. " + | ||
| "Please give me a suitable command :)"; | ||
| 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 final String PROMPT_TASK_DESCRIPTION = "Please tell me what do you need to do :)"; | ||
| public static final String INPUT_MORE_THAN_NUMBER_OF_TASKS = "Woah, you don't have THAT many things to do!"; | ||
| public static final String LIST_IS_EMPTY = "You're a free man :)"; | ||
| public static final String MISSING_ARGUMENTS_FOR_EVENT_AND_DEADLINE = "Sorry," + | ||
| " you're missing some arguments, do type 'help' if you're unsure :)"; | ||
| public static final String PROMPT_TASK_NUMBER = "Please tell me which task you finished :)"; | ||
| public static final String PROMPT_SENSIBLE_INDEX = "Please give a number between 1-100 :)"; | ||
| public static Task[] tasks = new Task[100]; | ||
|
|
||
| public static void main(String[] args) { | ||
| 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": | ||
| try { | ||
| executeDoneCase(input); | ||
|
|
||
| } catch (NullPointerException error) { | ||
| printErrorMessage(INPUT_MORE_THAN_NUMBER_OF_TASKS); | ||
|
|
||
| } catch (DukeException error) { | ||
| printErrorMessage(PROMPT_TASK_NUMBER); | ||
|
|
||
| } catch (ArrayIndexOutOfBoundsException error) { | ||
| printErrorMessage(PROMPT_SENSIBLE_INDEX); | ||
|
|
||
| } catch (NumberFormatException error) { | ||
| printErrorMessage(PROMPT_SENSIBLE_INDEX); | ||
|
|
||
| } | ||
| break; | ||
| case "todo": | ||
| try { | ||
| executeTaskCase(input, TODO_STARTING_INDEX, TaskType.TODO); | ||
|
|
||
| } catch (StringIndexOutOfBoundsException error) { | ||
| printErrorMessage(PROMPT_TASK_DESCRIPTION); | ||
| } | ||
| break; | ||
| case "deadline": | ||
| try { | ||
| executeTaskCase(input, DEADLINE_STARTING_INDEX,TaskType.DEADLINE); | ||
|
|
||
| } catch (StringIndexOutOfBoundsException error) { | ||
| printErrorMessage(PROMPT_TASK_DESCRIPTION); | ||
|
|
||
| } | ||
| break; | ||
| case "event": | ||
| try { | ||
| executeTaskCase(input, EVENT_STARTING_INDEX,TaskType.EVENT); | ||
|
|
||
| } catch (StringIndexOutOfBoundsException error) { | ||
| printErrorMessage(PROMPT_TASK_DESCRIPTION); | ||
| } | ||
| break; | ||
| default: | ||
| printErrorMessage(TYPE_SUITABLE_COMMAND_MESSAGE); | ||
| } | ||
| } | ||
|
|
||
| private static void printTaskMessage() { | ||
| printIndentationAndDivider(); | ||
| printWordsWithIndentation(ADDED_TO_LIST); | ||
| printWordsWithIndentation(tasks[Task.getTotalTasks() - 1].getStatusIconAndDescription()); | ||
| printWordsWithIndentation(notifyNumberOfTasks()); | ||
| printIndentationAndDivider(); | ||
| System.out.println(); | ||
| } | ||
|
|
||
| private static void printErrorMessage(String error_message) { | ||
| printIndentationAndDivider(); | ||
| printWordsWithIndentation(error_message); | ||
| printIndentationAndDivider(); | ||
| System.out.println(); | ||
| } | ||
|
|
||
| private static void executeTaskCase(String input, int starting_index, TaskType type) { | ||
| String description = input.strip().substring(starting_index); | ||
| try { | ||
| addTask(description, type); | ||
| printTaskMessage(); | ||
|
|
||
| } catch (DukeException error) { | ||
| printErrorMessage(MISSING_ARGUMENTS_FOR_EVENT_AND_DEADLINE); | ||
| } | ||
| } | ||
|
|
||
| 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 void addTask(String input,TaskType type) throws StringIndexOutOfBoundsException, DukeException { | ||
|
||
| String[] parsedOutput = parseInputForDifferentTask(input,type); | ||
| switch(type){ | ||
| case TODO: | ||
| tasks[Task.getTotalTasks()] = new ToDo(parsedOutput[0]); | ||
| break; | ||
| case EVENT: | ||
| if(parsedOutput.length < 2) { | ||
|
||
| throw new DukeException(); | ||
| } | ||
| tasks[Task.getTotalTasks()] = new Event(parsedOutput[0], parsedOutput[1]); | ||
| break; | ||
| case DEADLINE: | ||
| if(parsedOutput.length < 2) { | ||
| throw new DukeException(); | ||
| } | ||
| tasks[Task.getTotalTasks()] = new Deadline(parsedOutput[0], parsedOutput[1]); | ||
| break; | ||
| } | ||
| Task.setTotalTasks(Task.getTotalTasks() + 1); | ||
| } | ||
|
|
||
| private static String notifyNumberOfTasks() { | ||
| return "Now you have " + Task.getTotalTasks() + " task(s) in the list"; | ||
| } | ||
|
|
||
| private static void executeDoneCase(String input) throws NullPointerException,DukeException, | ||
| ArrayIndexOutOfBoundsException, NumberFormatException{ | ||
| if(input.length() < 6) { | ||
| throw new DukeException(); | ||
| } | ||
| 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(); | ||
| if(Task.getTotalTasks() == 0) { | ||
| printWordsWithIndentation(LIST_IS_EMPTY); | ||
| } | ||
| 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,4 @@ | ||
| package duke; | ||
|
|
||
| public class DukeException extends Exception{ | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package task; | ||
|
|
||
| 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 |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package task; | ||
|
|
||
| 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,41 @@ | ||
| package task; | ||
|
|
||
| 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,5 @@ | ||
| package task; | ||
|
|
||
| public enum TaskType { | ||
| TODO,DEADLINE,EVENT; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package task; | ||
|
|
||
| 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; | ||
| } | ||
| } |
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.
Suggest to keep consistent the new line convention throughout the code.