Skip to content
Open
Show file tree
Hide file tree
Changes from 16 commits
Commits
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
10 changes: 0 additions & 10 deletions src/main/java/Duke.java

This file was deleted.

21 changes: 21 additions & 0 deletions src/main/java/duke/Deadline.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package duke;
Copy link

Choose a reason for hiding this comment

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

Great to see that you organise your types (e.g., classes) into a package for easier management!


public class Deadline extends Todo{
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 leave a space here before { for layout consistency. Similar styles also happen in line 3 of DeadlineException class.

protected String by;

public Deadline(String description, String by) {
super(description);
this.by = by;
}
public String getBy() {
return by;
}
public void setBy(String by) {
this.by = by;
}
Copy link

Choose a reason for hiding this comment

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

Precise setter name, good job!


public void printTask() {
System.out.print("[D][" + getStatusIcon() + "] " + getDescription());
Copy link

Choose a reason for hiding this comment

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

According to https://nus-cs2113-ay2122s1.github.io/website/se-book-adapted/chapters/codeQuality.html#avoid-magic-numbers, perhaps you could spend some time trying to avoid all sorts of magic strings (there are also found elsewhere such E in Event class) to further improve readability?

System.out.println("(by:" + by + ")");
}
}
4 changes: 4 additions & 0 deletions src/main/java/duke/DeadlineException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package duke;

public class DeadlineException extends Exception{
}
4 changes: 4 additions & 0 deletions src/main/java/duke/DoneException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package duke;

public class DoneException extends Exception{
}
156 changes: 156 additions & 0 deletions src/main/java/duke/Duke.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
package duke;

import java.util.Scanner;

public class Duke {
public static Task[] tasks = new Task[100];
public static int taskCount = 0;

public static void printGreeting() {
System.out.println("Hello! I'm Duke\n" + "What can I do for you?");
}

public static void printConfused() {
System.out.println("Could you say that again?");
}

public static void printExit() {
System.out.println("Bye. Hope to see you again soon!");
}

public static void printGotIt() {
System.out.println("Got it. I've added this task:");
}

public static void printTaskCount() {
System.out.println("Now you have " + taskCount + " tasks in the list.");
}

public static void printTodoException() {
System.out.println("The description of todo cannot be empty.");
}

public static void printDeadlineException() {
System.out.println("The description of deadline cannot be empty.");
}

public static void printEventException() {
System.out.println("The description of event cannot be empty.");
}

public static void printDoneException() {
System.out.println("The description of done cannot be empty.");
}
Copy link

Choose a reason for hiding this comment

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

Great to see that you have created many helper print functions here! Perhaps you could also extract these final strings (also found in other parts of the code such as line 53) out as constants and put them into a new Message class later on?


public static void printTaskTypeResponse() {
//printing different responses depending if its duke.Todo/duke.Deadline/duke.Event
Copy link

Choose a reason for hiding this comment

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

You could consider removing this comment and use JavaDoc comments for non-private classes / methods and non-trivial private methods in an appropriate way, as per https://nus-cs2113-ay2122s1.github.io/website/se-book-adapted/chapters/documentation.html#javadoc. The same applies to line 85.

printGotIt();
tasks[taskCount - 1].printTask();
printTaskCount();
}

public static void printList() {
System.out.println("Here are the tasks in your list:");
for (int i = 0; i < taskCount; i += 1) {
System.out.print((i + 1) + ".");
tasks[i].printTask();
}
}

public static void taskDone(String line) throws DoneException {
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 rephrase the method name here so that it starts with a verb according to https://se-education.org/guides/conventions/java/index.html#naming.

if (line.equals("") || line.equals("done")) {
throw new DoneException();
} else {
int index = Integer.parseInt(line) - 1;
tasks[index].setDone();
System.out.println("Nice! I've marked this task as done:");
tasks[index].printTask();
}
}

public static void addTodo(String line) throws TodoException {
if (line.equals("") || line.equals("todo")) {
throw new TodoException();
} else {
tasks[taskCount] = new Todo(line);
taskCount = taskCount + 1;
printTaskTypeResponse();
}
}

public static void addDeadline(String line) throws DeadlineException {
if (!line.matches("(.*)/by(.*)")) {
throw new DeadlineException();
} else {
//extracting the description and date
String description = line.replaceAll("/.+", "");
String by = line.replaceAll(".+/by", "");
tasks[taskCount] = new Deadline(description, by);
taskCount = taskCount + 1;
printTaskTypeResponse();
}
}

public static void addEvent(String line) throws EventException {
if (!line.matches("(.*)/at(.*)")) {
throw new EventException();
} else {
//extracting the description and date
String description = line.replaceAll("/.+", "");
String by = line.replaceAll(".+/at", "");
tasks[taskCount] = new Event(description, by);
taskCount = taskCount + 1;
printTaskTypeResponse();
}
}

public static void readInput(String line) {
String[] splitLine = line.split(" ", 2);
Copy link

@zikunz zikunz Sep 14, 2021

Choose a reason for hiding this comment

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

Perhaps you could also spend some time to avoid having magic numbers as well.

String command = splitLine[0];
line = line.replaceAll("^" + command + " ", "");
if (command.equals("list")) {
printList();
} else if (command.equals("done")) {
try {
//find which task user is done with
taskDone(line);
} catch (DoneException e) {
printDoneException();
}
} else if (command.equals("todo")) {
try {
addTodo(line);
} catch (TodoException e) {
printTodoException();
}
} else if (command.equals("deadline")) {
try {
addDeadline(line);
} catch (DeadlineException e) {
printDeadlineException();
}
} else if (command.equals("event")) {
try {
addEvent(line);
} catch (EventException e) {
printEventException();
}
} else {
//error with input
printConfused();
}
}
Copy link

Choose a reason for hiding this comment

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

This method exceeds 30 LOC, perhaps you could shorten it (e.g., by extracting lines such as 121 - 125, 133 - 137 into different methods on their own).


public static void main(String[] args) {
printGreeting();
String line;
Scanner in = new Scanner(System.in);
line = in.nextLine();
//continue to run program unless types "bye" to exit program
Copy link

Choose a reason for hiding this comment

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

Lines 149 is pretty obvious, perhaps you could consider to delete it.

while(!line.equals("bye")) {
readInput(line);
line = in.nextLine();
}
printExit();
}
}
Copy link

Choose a reason for hiding this comment

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

Perhaps some statements can be abstracted to better follow SLAP (https://nus-cs2113-ay2122s1.github.io/website/se-book-adapted/chapters/codeQuality.html#slap-hard).

21 changes: 21 additions & 0 deletions src/main/java/duke/Event.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package duke;

public class Event extends Todo{
protected String at;

public Event(String description, String at) {
super(description);
this.at = at;
}
public String getAt() {
return at;
}
public void setAt(String at) {
this.at = at;
}

public void printTask() {
System.out.print("[E][" + getStatusIcon() + "] " + getDescription());
System.out.println("(at:" + at + ")");
}
}
4 changes: 4 additions & 0 deletions src/main/java/duke/EventException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package duke;

public class EventException extends Exception{
}
28 changes: 28 additions & 0 deletions src/main/java/duke/Task.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package duke;

public class Task {
protected String description;
protected boolean isDone;
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 your use the is prefix here to make your boolean attribute sound like valid booleans.


public Task(String description) {
this.description = description;
isDone = false;
}

public String getStatusIcon() {
// mark done task with X
return (isDone ? "X" : " ");
Comment on lines +13 to +
Copy link

@zikunz zikunz Sep 14, 2021

Choose a reason for hiding this comment

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

I like how your comment is indented relative to its position in the code. Good job!

}

public String getDescription() {
return description;
}

public void setDone() {
isDone = true;
}

public void printTask() {
System.out.println(description);
}
}
4 changes: 4 additions & 0 deletions src/main/java/duke/TaskCountException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package duke;

public class TaskCountException extends Exception{
}
12 changes: 12 additions & 0 deletions src/main/java/duke/Todo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package duke;

public class Todo extends Task{

public Todo(String description) {
super(description);
}

public void printTask() {
System.out.println("[T][" + getStatusIcon() + "] " + getDescription());
}
}
4 changes: 4 additions & 0 deletions src/main/java/duke/TodoException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package duke;

public class TodoException extends Exception{
}
27 changes: 20 additions & 7 deletions text-ui-test/EXPECTED.TXT
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
Hello from
____ _
| _ \ _ _| | _____
| | | | | | | |/ / _ \
| |_| | |_| | < __/
|____/ \__,_|_|\_\___|

Hello! I'm Duke
What can I do for you?
Could you say that again?
Got it. I've added this task:
[T][ ] eat cake
Now you have 1 tasks in the list.
Got it. I've added this task:
[D][ ] eat chocolate cake (by: Friday 2pm)
Now you have 2 tasks in the list.
Got it. I've added this task:
[E][ ] attend cake buffet (at: Saturday 5pm)
Now you have 3 tasks in the list.
Nice! I've marked this task as done:
[T][X] eat cake
The description of todo cannot be empty.
Here are the tasks in your list:
1.[T][X] eat cake
2.[D][ ] eat chocolate cake (by: Friday 2pm)
3.[E][ ] attend cake buffet (at: Saturday 5pm)
Could you say that again?
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 @@
eat cake
todo eat cake
deadline eat chocolate cake /by Friday 2pm
event attend cake buffet /at Saturday 5pm
done 1
todo
list
cake
bye
6 changes: 3 additions & 3 deletions text-ui-test/runtest.bat
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ REM create bin directory if it doesn't exist
if not exist ..\bin mkdir ..\bin

REM delete output from previous run
if exist ACTUAL.TXT del ACTUAL.TXT
del ACTUAL.TXT

REM compile the code into the bin folder
javac -cp ..\src\main\java -Xlint:none -d ..\bin ..\src\main\java\*.java
javac -cp ..\src\main\java -Xlint:none -d ..\bin ..\src\main\java\duke\*.java
IF ERRORLEVEL 1 (
echo ********** BUILD FAILURE **********
exit /b 1
)
REM no error here, errorlevel == 0

REM run the program, feed commands from input.txt file and redirect the output to the ACTUAL.TXT
java -classpath ..\bin Duke < input.txt > ACTUAL.TXT
java -classpath ..\bin duke.Duke < input.txt > ACTUAL.TXT

REM compare the output to the expected output
FC ACTUAL.TXT EXPECTED.TXT