-
Notifications
You must be signed in to change notification settings - Fork 193
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
[Vincent Lau Han Leong] iP #183
base: master
Are you sure you want to change the base?
Changes from 1 commit
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 |
---|---|---|
|
@@ -3,8 +3,36 @@ | |
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_PROMPT = " /by "; | ||
public static final String EVENT_PROMPT = " /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) { | ||
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); | ||
|
@@ -15,55 +43,129 @@ public static void printWordsWithIndentation(String words) { | |
System.out.println(words); | ||
} | ||
|
||
public static void main(String[] args) { | ||
String logo = " ____ _ \n" | ||
+ "| _ \\ _ _| | _____ \n" | ||
+ "| | | | | | | |/ / _ \\\n" | ||
+ "| |_| | |_| | < __/\n" | ||
+ "|____/ \\__,_|_|\\_\\___|\n"; | ||
System.out.println("Hello from\n" + logo); | ||
private static void printGoodbyeMessage() { | ||
printIndentationAndDivider(); | ||
printWordsWithIndentation("Hello! I'm Duke, your friendly neighbourhood task manager"); | ||
printWordsWithIndentation("What can I do for you? :D"); | ||
printWordsWithIndentation(GOODBYE_MESSAGE); | ||
printIndentationAndDivider(); | ||
} | ||
|
||
private static void processInput(String input) { | ||
switch(input.split(" ")[0].toLowerCase()) { | ||
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 create a variable with a meaningful name to simplify the expression in the switch statement? |
||
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(); | ||
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 like how the Single Level of Abstraction Principle is being applied here and I find it very easy to read and understand what you are trying to do. |
||
} | ||
} | ||
|
||
private static void printTaskMessage() { | ||
printIndentationAndDivider(); | ||
printWordsWithIndentation(ADDED_TO_LIST); | ||
printWordsWithIndentation(tasks[Task.getTotalTasks() - 1].getStatusIconAndDescription()); | ||
printWordsWithIndentation(notifyNumberOfTasks()); | ||
printIndentationAndDivider(); | ||
System.out.println(); | ||
} | ||
|
||
Scanner in = new Scanner(System.in); | ||
String input = in.nextLine(); | ||
while (!input.equals("bye")) { | ||
switch(input.split(" ")[0]) { | ||
case "list" : | ||
printIndentationAndDivider(); | ||
for (int i = 0; i < Task.getTotalTasks(); i++) { | ||
String description = tasks[i].getDescription(); | ||
String statusIcon = tasks[i].getStatusIcon(); | ||
printWordsWithIndentation(i + 1 + "." + "[" + statusIcon + "] " + description); | ||
} | ||
printIndentationAndDivider(); | ||
System.out.println(); | ||
break; | ||
case "done" : | ||
int index = Integer.parseInt(input.split(" ")[1]) - 1; | ||
tasks[index].markAsDone(); | ||
printIndentationAndDivider(); | ||
printWordsWithIndentation("You've completed the task! Well done!"); | ||
printWordsWithIndentation("[" + tasks[index].getStatusIcon() + "] " + tasks[index].getDescription()); | ||
printIndentationAndDivider(); | ||
break; | ||
default : | ||
printIndentationAndDivider(); | ||
printWordsWithIndentation("added: " + input); | ||
printIndentationAndDivider(); | ||
System.out.println(); | ||
tasks[Task.getTotalTasks()] = new Task(input); | ||
Task.setTotalTasks(Task.getTotalTasks() + 1); | ||
break; | ||
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; | ||
|
||
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 delete the empty line between each case in the switch function. It was not shown in the lectures. Also, I think it is better to keep the consistency of format (since you did not add any empty line before or after this). |
||
case DEADLINE: | ||
parsedOutput = input.split(DEADLINE_PROMPT); | ||
break; | ||
|
||
case EVENT: | ||
parsedOutput = input.split(EVENT_PROMPT); | ||
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; | ||
} | ||
input = in.nextLine(); | ||
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) { | ||
int index = Integer.parseInt(input.split(" ")[1]) - 1; | ||
tasks[index].markAsDone(); | ||
printIndentationAndDivider(); | ||
printWordsWithIndentation(TASK_COMPLETED_MESSAGE); | ||
printWordsWithIndentation(tasks[index].getStatusIconAndDescription()); | ||
printIndentationAndDivider(); | ||
} | ||
|
||
private static void executeListCase() { | ||
printIndentationAndDivider(); | ||
for (int i = 0; i < Task.getTotalTasks(); i++) { | ||
printWordsWithIndentation(i + 1 + "." + tasks[i].getStatusIconAndDescription()); | ||
} | ||
printIndentationAndDivider(); | ||
System.out.println(); | ||
} | ||
|
||
private static void printStartingMessage() { | ||
System.out.println("Hello from\n" + LOGO); | ||
printIndentationAndDivider(); | ||
printWordsWithIndentation("Bye, hope to see you again soon! :)"); | ||
printWordsWithIndentation(HELLO_MESSAGE_2); | ||
printWordsWithIndentation(HELLO_MESSAGE_3); | ||
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,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; | ||
} | ||
} |
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 "{"