Skip to content
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

Open
wants to merge 42 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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);
}
}
184 changes: 143 additions & 41 deletions src/main/java/Duke.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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()) {
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();
}

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;

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_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();
}
}
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);
}
}
20 changes: 12 additions & 8 deletions src/main/java/Task.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
public class Task {
private boolean isDone;
private String description;
protected boolean isDone;
protected String description;

private static int totalTasks = 0;

Expand All @@ -9,18 +9,15 @@ public Task(String description){
isDone = false;
}

public String getStatusIcon() {
return (isDone ? "X" : " ");
public String getStatusIconAndDescription() {
String icon = (isDone ? "X" : " ");
return addSquareBrackets(icon) + " " + description;
}

public void markAsDone() {
isDone = true;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}
Expand All @@ -32,4 +29,11 @@ public static int getTotalTasks() {
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;
}
}