Skip to content
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
cbf3cd2
Add Greet
vincentlauhl Aug 18, 2021
91ce339
Add echo and method for printing output
vincentlauhl Aug 25, 2021
e4f50de
Add list keeping and list printing
vincentlauhl Aug 25, 2021
39dbc7c
Add Task class and Mark as Done feature
vincentlauhl Aug 25, 2021
b287fd9
Change taskList to tasks
vincentlauhl Aug 25, 2021
b6db002
Change constant to upper case
vincentlauhl Aug 27, 2021
ab47db6
Add Event, ToDo, Deadline class and TaskType enum.
vincentlauhl Sep 1, 2021
026be10
Add text UI testing
vincentlauhl Sep 1, 2021
5da062e
Update text ui test and done refactoring
vincentlauhl Sep 1, 2021
14a2865
Add exception handling and exception class
vincentlauhl Sep 8, 2021
8d6b1e5
Change input and expected text for ui test
vincentlauhl Sep 8, 2021
42969c1
Move classes into packages and update runtest.bat
vincentlauhl Sep 8, 2021
46c8930
Merge branch 'branch-Level-5'
vincentlauhl Sep 8, 2021
8493d64
Add package duke and update readme and runtest.bat
vincentlauhl Sep 10, 2021
974df0a
Merge branch 'branch-A-Packages'
vincentlauhl Sep 10, 2021
3dc210f
Change array to array list, tidy up code, and update text ui test.
vincentlauhl Sep 15, 2021
a9902e7
Add delete task and update text ui test
vincentlauhl Sep 15, 2021
7c89dda
Add save to file after termination and read from file when the progra…
vincentlauhl Sep 15, 2021
b9b174f
Merge branch 'branch-Level-6'
vincentlauhl Sep 15, 2021
c254470
Merge branch 'branch-Level-7'
vincentlauhl Sep 15, 2021
80a19f2
Add help message and add jar file
vincentlauhl Sep 15, 2021
c2ec413
Add classes for OOP with main code untouched
vincentlauhl Sep 25, 2021
bcb0f12
Update the code to have OOP style
vincentlauhl Sep 25, 2021
10ef049
Add date and time for event and deadline and some constants
vincentlauhl Sep 26, 2021
861e928
Add find function to find tasks containining the description
vincentlauhl Sep 26, 2021
d76441c
Add javadocs to most classes and methods
vincentlauhl Sep 27, 2021
f01e19e
Merge pull request #1 from vincentlauhl/branch-Level-8
vincentlauhl Sep 27, 2021
7200398
Merge branch 'master' into branch-Level-9
vincentlauhl Sep 27, 2021
c0a0767
Merge pull request #2 from vincentlauhl/branch-Level-9
vincentlauhl Sep 27, 2021
54f9efa
Merge branch 'master' into branch-A-JavaDoc
vincentlauhl Sep 27, 2021
ca92735
Merge pull request #3 from vincentlauhl/branch-A-JavaDoc
vincentlauhl Sep 27, 2021
45dc063
Update ReadMe file
vincentlauhl Sep 29, 2021
540a897
Do a minor change on the ReadMe file
vincentlauhl Sep 29, 2021
31cc2eb
Update ReadMe to correct GitHub Page ReadMe
vincentlauhl Sep 29, 2021
6f942e9
Correct GitHub Page ReadMe
vincentlauhl Sep 29, 2021
69ce26d
Correct GitHub Page not bolding words
vincentlauhl Sep 29, 2021
086b7ec
Correct GitHub Page not bolding words attempt 2.
vincentlauhl Sep 29, 2021
4663af3
Update doc/ReadMe and revert the other ReadMe. Update help mesage
vincentlauhl Sep 29, 2021
266610b
Slight change to doc/ReadMe
vincentlauhl Sep 29, 2021
c1dd736
Update code to adhere to coding standards. Update error message.
vincentlauhl Sep 29, 2021
2157f69
Include error handling for general errors in reading file
vincentlauhl Sep 29, 2021
4ebd498
Correction on reused code format
vincentlauhl Oct 5, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/main/java/Deadline.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
public class Deadline extends Task{

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 "{"

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);
}
}
182 changes: 176 additions & 6 deletions src/main/java/Duke.java
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()) {
Copy link

Choose a reason for hiding this comment

The 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();
Copy link

Choose a reason for hiding this comment

The 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();
}

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;

Choose a reason for hiding this comment

The 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_DESCRIPTION_AND_DATE_SPLITTER);
break;

case EVENT:
parsedOutput = input.split(EVENT_DESCRIPTION_AND_DATE_TIME_SPLITTER);
break;
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps you could add a default branch? The default branch can be used to raise a suitable error as I think the execution should not reach the default branch. I also noticed this same issue in your other switch statements.

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;
}
Copy link

@rizemon rizemon Sep 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good use of this guard clause and I am able to see the "good path" of the method.

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();
}
}
15 changes: 15 additions & 0 deletions src/main/java/Event.java
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);
}
}
39 changes: 39 additions & 0 deletions src/main/java/Task.java
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 + ")";
}
}
3 changes: 3 additions & 0 deletions src/main/java/TaskType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
public enum TaskType {
TODO,DEADLINE,EVENT;
}
12 changes: 12 additions & 0 deletions src/main/java/ToDo.java
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;
}
}
51 changes: 51 additions & 0 deletions text-ui-test/EXPECTED.TXT
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,54 @@ Hello from
| |_| | |_| | < __/
|____/ \__,_|_|\_\___|

___________________________________________________________
Hello! I'm Duke, your friendly neighbourhood task manager
What can I do for you? :D
___________________________________________________________

___________________________________________________________
I've added this to your list :D
[T][ ] borrow book
Now you have 1 task(s) in the list
___________________________________________________________

___________________________________________________________
I've added this to your list :D
[D][ ] return book (by: Sunday)
Now you have 2 task(s) in the list
___________________________________________________________

___________________________________________________________
I've added this to your list :D
[E][ ] project meeting (at: 4th Sep 2-3pm)
Now you have 3 task(s) in the list
___________________________________________________________

___________________________________________________________
Sorry, you are using me wrongly. Please type in a suitable command :)
___________________________________________________________

___________________________________________________________
Sorry, you are using me wrongly. Please type in a suitable command :)
___________________________________________________________

___________________________________________________________
1.[T][ ] borrow book
2.[D][ ] return book (by: Sunday)
3.[E][ ] project meeting (at: 4th Sep 2-3pm)
___________________________________________________________

___________________________________________________________
You've completed the task! Well done!
[T][X] borrow book
___________________________________________________________

___________________________________________________________
1.[T][X] borrow book
2.[D][ ] return book (by: Sunday)
3.[E][ ] project meeting (at: 4th Sep 2-3pm)
___________________________________________________________

___________________________________________________________
Bye, hope to see you again soon! :)
___________________________________________________________
9 changes: 9 additions & 0 deletions text-ui-test/input.txt
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