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

[Jerry Ho] Duke Increments #451

Open
wants to merge 80 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
80 commits
Select commit Hold shift + click to select a range
b0b06c7
Add Scanner and Implement Greet, Echo, Exit
itsjerryho Aug 20, 2020
a240abd
Import ArrayList
itsjerryho Aug 20, 2020
affd926
Add ability to store text and display back to user when requested
itsjerryho Aug 20, 2020
79d4d42
Add Task Class
itsjerryho Aug 20, 2020
5a1fcf7
Add markAsDone to Task Class
itsjerryho Aug 20, 2020
266db70
Add getDescription to Task Class
itsjerryho Aug 20, 2020
84ccdbd
Change ArrayList from String to Task and modify display of list items
itsjerryho Aug 20, 2020
c0f21c4
Create method printTask and add mark as done feature
itsjerryho Aug 20, 2020
529f823
Remove printTask from Duke.java and Override Task's toString method
itsjerryho Aug 20, 2020
62872cd
Add Deadline class
itsjerryho Aug 20, 2020
1bd86c5
Add ToDo Class
itsjerryho Aug 20, 2020
59b5d8e
Add Event Class
itsjerryho Aug 20, 2020
22a75bb
Integrate Deadline, ToDo and Event to Duke
itsjerryho Aug 20, 2020
2e8dec8
Modify EXPECTED.txt, input.txt to include test case.
itsjerryho Aug 20, 2020
9d1cf01
Add DukeException.java
itsjerryho Aug 20, 2020
a97cea3
Seperate mainLogic from main() and create a createTask method. Add ba…
itsjerryho Aug 20, 2020
e7c27be
Add delete function
itsjerryho Aug 20, 2020
79740e0
Add additional exception handling, fixed early exiting bug after exce…
itsjerryho Aug 20, 2020
4514a22
Add printMessage method to output messages with lines
itsjerryho Aug 27, 2020
f395cb4
Add toText method to Deadline, Event and Task classes
itsjerryho Aug 27, 2020
b046dde
Add writeToFile, saveToDisk and readFileContents method to Duke.java
itsjerryho Aug 27, 2020
b524ea4
Add gitignore for data.txt
itsjerryho Aug 27, 2020
519147a
Add new constructor method to Deadline, Task, Event and ToDo to suppo…
itsjerryho Aug 27, 2020
9182971
Add saveDataToList method to Duke.java
itsjerryho Aug 27, 2020
b5cc47b
Add LocalDate support to Deadline.java and Duke.java
itsjerryho Aug 29, 2020
0a2925a
Merge branch 'branch-Level-7'
itsjerryho Aug 29, 2020
fca52df
Merge branch 'branch-Level-8'
itsjerryho Aug 29, 2020
b3778fe
Modify Deadline.java to make it compatible with DateTime implementati…
itsjerryho Aug 29, 2020
94f6e22
Create Ui.java
itsjerryho Sep 3, 2020
d17fe93
Create TaskList.java
itsjerryho Sep 3, 2020
584720d
Create Storage.java
itsjerryho Sep 3, 2020
c202b68
Create Parser.java
itsjerryho Sep 3, 2020
9bc7fbd
Create Command.java
itsjerryho Sep 3, 2020
6afecbc
Create AdditionalInfo.java
itsjerryho Sep 3, 2020
b15d528
Minor edits after reading Comments on GitHub
itsjerryho Sep 3, 2020
52172c1
Restructure Duke by using OOP principles
itsjerryho Sep 3, 2020
d76908a
Add all classes to Duke package except Duke.java
itsjerryho Sep 3, 2020
e0cafc3
Create Common.java to store commonly used constants
itsjerryho Sep 3, 2020
ea1ad12
Add jUnit test for Parser.java
itsjerryho Sep 3, 2020
2a18cd0
Add method to create folder and/or data file in the event that either…
itsjerryho Sep 3, 2020
2dedee2
Fix bug in the case of invalid commands
itsjerryho Sep 3, 2020
93a7276
Add javadocs to some classes
itsjerryho Sep 5, 2020
cf585d9
Modify files to adhere to Java coding standards.
itsjerryho Sep 6, 2020
e5f245f
Add find command to Duke
itsjerryho Sep 6, 2020
07f06f3
Merge branch 'branch-A-JavaDoc'
itsjerryho Sep 6, 2020
17a6bbf
Merge branch 'branch-A-CodingStandard'
itsjerryho Sep 6, 2020
486b792
Merge branch 'branch-Level-9'
itsjerryho Sep 6, 2020
f285892
Add Gradle support
itsjerryho Sep 6, 2020
eaba12a
Merge branch 'branch-A-Gradle'
itsjerryho Sep 6, 2020
5f5ab83
Add support for Gradle Checkstyle
itsjerryho Sep 8, 2020
6366d0a
Merge branch 'branch-A-CheckStyle'
itsjerryho Sep 8, 2020
2cef4c2
Add DialogBox, Launcher, Main and MainWindow for GUI support
itsjerryho Sep 8, 2020
e92b0ef
Add images and fxml files for DialogBox and MainWindow
itsjerryho Sep 8, 2020
9732544
Add support for Duke GUI
itsjerryho Sep 8, 2020
28aafcc
Make minor edits
itsjerryho Sep 15, 2020
f886b94
Fix bug in Duke.java
itsjerryho Sep 15, 2020
a343896
Make minor changes to build.gradle and fxml files to remove warnings
itsjerryho Sep 15, 2020
c6c5573
Add assertions to TaskList.java
itsjerryho Sep 15, 2020
ff195ae
Add assertion to AdditionalInfo.java
itsjerryho Sep 15, 2020
3d01210
Add assertion to build.gradle
itsjerryho Sep 15, 2020
5a7755e
Create TimedTask
itsjerryho Sep 17, 2020
d13b712
Refactor Code
itsjerryho Sep 17, 2020
48fc970
Refactor Code
itsjerryho Sep 17, 2020
1c07ea6
Add enums for Task class
itsjerryho Sep 17, 2020
72f40c7
Merge pull request #1 from printinghelloworld/branch-A-Assertions
itsjerryho Sep 18, 2020
5922b84
Merge branch 'master' into branch-A-CodeQuality
itsjerryho Sep 18, 2020
fefab25
Merge branch 'branch-A-CodeQuality'
itsjerryho Sep 18, 2020
39df444
Merge branch 'A-Enums'
itsjerryho Sep 18, 2020
5bad734
Implement BetterSearch feature
itsjerryho Sep 18, 2020
690abb3
Merge pull request #3 from printinghelloworld/branch-C-BetterSearch
itsjerryho Sep 18, 2020
97fa23b
Add functionality to delete multiple tasks at one go
itsjerryho Sep 18, 2020
a904225
Merge branch 'branch-C-MassOps'
itsjerryho Sep 18, 2020
c7e4fc8
Minor edits to FindCommand
itsjerryho Sep 18, 2020
32a730f
Edit README.md
itsjerryho Sep 18, 2020
86ad76d
Merge branch 'branch-A-UserGuide'
itsjerryho Sep 18, 2020
4df3c01
Add Javadocs and Checkstyle
itsjerryho Sep 18, 2020
0af3ecf
Increase JUnit testing
itsjerryho Sep 18, 2020
84b52a4
Improve GUI
itsjerryho Sep 18, 2020
656baa6
Final changes to Duke
itsjerryho Sep 18, 2020
498ee0f
Merge branch 'branch-A-BetterGui'
itsjerryho Sep 18, 2020
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ bin/

/text-ui-test/ACTUAL.txt
text-ui-test/EXPECTED-UNIX.TXT
data/data.txt
32 changes: 32 additions & 0 deletions src/main/java/Deadline.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class Deadline extends Task {

protected LocalDate date;
protected String time;

public Deadline(String description, LocalDate by, String time) {
super(description);
this.date = by;
this.time = time;
}

public Deadline(String description, String by, boolean isDone) {
super(description, isDone);
this.by = by;
}

@Override
public String toText() {
String str = super.toText("D");
str += "| " + this.by;
return str;
}

@Override
public String toString() {
String dateOutput = date.format(DateTimeFormatter.ofPattern("MMM d yyyy"));
return "[D]" + super.toString() + " (by: " + dateOutput + " " + time + ")";
}
}
203 changes: 197 additions & 6 deletions src/main/java/Duke.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,201 @@
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Scanner;
import java.util.ArrayList;
import java.io.File;

Choose a reason for hiding this comment

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

I think the import statements can be grouped based on their package and add one blank line between the groups to enhance readability?


public class Duke {
Copy link

Choose a reason for hiding this comment

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

I have noticed that your Duke class may have too many methods (a little cluttered), and seeing that you have not merged "branch-A-OOP" into master, I am not sure whether you have create new classes to adhere to OOP principles?

public static void invalidInput() throws DukeException {
throw new DukeException("I'm sorry, but I don't know what that means :-(");
}

public static void printMessage(String s) {
String output = String.format("____________________________________________________________\n%s\n" +
Copy link

Choose a reason for hiding this comment

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

The "+" should be in the next line instead.

"____________________________________________________________\n",s);

Choose a reason for hiding this comment

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

I think there should be a space after the comma?

System.out.println(output);
}

private static void writeToFile(String filePath, String textToAdd) throws IOException {
FileWriter fw = new FileWriter(filePath);
fw.write(textToAdd);
fw.close();
}

public static void saveToDisk(ArrayList<Task> lst) {
String filePath = "./data/data.txt";
String info = "";
for (int i = 0; i < lst.size(); i ++) {
Copy link

Choose a reason for hiding this comment

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

There should not be a spacing between "i" and "++".

Task currentTask = lst.get(i);
if (i == lst.size() - 1){
info += currentTask.toText();
}
else {
info += currentTask.toText() + System.lineSeparator();
}
}
try {
writeToFile(filePath, info);
} catch (IOException e) {
System.out.println("Error saving data to disk");
e.printStackTrace();
}
}

public static void saveDataToList(String content, ArrayList<Task> lst) {
Scanner s = new Scanner(content);
while (s.hasNext()) {
String line = s.nextLine();
String[] arr = line.split("\\|");
String type = arr[0].trim();
int status = Integer.parseInt(arr[1].trim());
String description = arr[2].trim();
if (type.equals("T")) {
lst.add(new ToDo(description, status == 1));
} else {
String additionalInfo = arr[3].trim();
if (type.equals("D")) {
lst.add(new Deadline(description, additionalInfo, status == 1));
} else {
lst.add(new Event(description, additionalInfo, status == 1));
}
}
}
}
public static String readFileContents(String filePath) throws FileNotFoundException {
File f = new File(filePath);
String content = "";
Scanner s = new Scanner(f);
while (s.hasNext()) {
content += s.nextLine() + System.lineSeparator();
}
return content;
}
public static Task createTask(String firstWord, String input) throws DukeException {
Task newTask;
if (firstWord.equals("todo")) {
String[] arr = input.split("todo ");
if (arr.length == 1) {
throw new DukeException("The description of a todo cannot be empty.");
} else {
String tsk = arr[1].trim();
newTask = new ToDo(tsk);
}
} else if (firstWord.equals("deadline")) {
String[] arr = input.split("deadline ");
if (arr.length <= 1) {
throw new DukeException("The description of a deadline cannot be empty.");
} else {
String[] split = input.split("/by ");
if (split.length == 1) {
throw new DukeException("By when??? You didn't include your deadline.");
} else {
String tsk = split[0].split("deadline")[1].trim();
String deadline = split[1].trim();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d/M/yyyy");
String dateString = deadline.split(" ")[0];
String time = deadline.split(" ")[1];
LocalDate date = LocalDate.parse(dateString, formatter);
newTask = new Deadline(tsk, date, time);
}
}
} else {
String[] arr = input.split("event ");
if (arr.length == 1) {
throw new DukeException("The description of an event cannot be empty.");
} else {
String[] split = input.split("/at ");
if (split.length == 1) {
throw new DukeException("At??? You didn't include the time of the event.");
} else {
String at = split[1].trim();
String tsk = split[0].split("event ")[1].trim();
newTask = new Event(tsk, at);
}
}
}
return newTask;
}
public static void mainLogic(Scanner sc, ArrayList<Task> lst) {
Copy link

Choose a reason for hiding this comment

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

Perhaps the method name could have been a verb instead? Eg. parseCommand.

Choose a reason for hiding this comment

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

Yes, I also found this in other classes i.e. method invalidInput() in Duke class perhaps can also be changed to a verb?


while (true) {
Copy link

Choose a reason for hiding this comment

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

It might not be too good of practice to use "while (true)", as it is hard to tell what is your terminating condition (and there is a chance of an infinite loop)?

String input = sc.nextLine();
String firstWord = input.split(" ")[0];
String output = "";
if (input.equals("bye")) {
saveToDisk(lst);
output = "Bye. Take care!";
} else if (input.equals("list")) {
output += "Here are the tasks in your list:";
for (int i = 0; i < lst.size(); i ++) {
Task currentTask = lst.get(i);
String num = Integer.toString(i + 1);
output += "\n" + num + "." + currentTask;
}
} else if (firstWord.matches("done|delete")) {
String[] splitted = input.split("\\s+");
int taskIndex = Integer.parseInt(splitted[1]) - 1;
Task selectedTask = lst.get(taskIndex);
if (firstWord.equals("done")) {
selectedTask.markAsDone();
output += "Nice! I've marked this task as done:\n " + selectedTask;
} else {
lst.remove(taskIndex);
output += "Noted. I've removed this task:\n " + selectedTask;
}
output += "\nNow you have " + lst.size() + " tasks in the list.";
} else if (firstWord.matches("todo|deadline|event")) {
Task newTask;
try {
newTask = createTask(firstWord, input);
lst.add(newTask);
output += "Got it. I've added this task:\n " + newTask;
output += "\nNow you have " + lst.size() + " tasks in the list.";
printMessage(output);
} catch (DukeException e) {
output += "☹ OOPS!!! " + e.getMessage();
printMessage(output);
} finally {
mainLogic(sc, lst);
}
} else {
try {
invalidInput();
} catch (DukeException e) {
output += "☹ OOPS!!! " + e.getMessage();
printMessage(output);
} finally {
mainLogic(sc, lst);
}
}
printMessage(output);
}
}
public static void main(String[] args) {
String logo = " ____ _ \n"
+ "| _ \\ _ _| | _____ \n"
+ "| | | | | | | |/ / _ \\\n"
+ "| |_| | |_| | < __/\n"
+ "|____/ \\__,_|_|\\_\\___|\n";
System.out.println("Hello from\n" + logo);
String greetings = "Hello! I'm Duke, your personal assistant.\nWhat can I do for you?";
printMessage(greetings);

ArrayList<Task> lst = new ArrayList<>();
Copy link

Choose a reason for hiding this comment

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

A better practice might be to split the instantiation of "lst".

After your import statements, it could be List lst;
Then in your Duke constructor: this.lst = new ArrayList<>();


String filePath = "./data/data.txt";
try {
File f = new File(filePath);
if (f.exists()) {
String content = readFileContents(filePath);
saveDataToList(content, lst);
} else {
f.createNewFile();
}
} catch (FileNotFoundException e) {
System.out.println("Unable to read contents. File not found");
} catch (IOException e) {
System.out.println("Unable to create new file.");
e.printStackTrace();
}

Scanner sc = new Scanner(System.in);
mainLogic(sc, lst);
}
}
5 changes: 5 additions & 0 deletions src/main/java/DukeException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
public class DukeException extends Exception{
Copy link

Choose a reason for hiding this comment

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

A spacing after "Exception" will look nicer!

DukeException(String s) {
super(s);
}
}
25 changes: 25 additions & 0 deletions src/main/java/Event.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
public class Event extends Task {
protected String at;

Choose a reason for hiding this comment

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

Maybe the attribute name "at" can be changed to a noun like "date" or "eventDate" to enhance readability?


public Event(String description, String at) {
super(description);
this.at = at;
}

public Event(String description, String at, boolean isDone) {
super(description, isDone);
this.at = at;
}

@Override
public String toText() {
String str = super.toText("E");
str += "| " + this.at;
return str;
}

@Override
public String toString() {
return "[E]" + super.toString() + " (at: " + at + ")";
}
}
40 changes: 40 additions & 0 deletions src/main/java/Task.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
public class Task {
protected String description;
protected boolean isDone;

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

public Task(String description, boolean status) {
this.description = description;
this.isDone = status;
}

public String getStatusIcon() {
return (isDone ? "✓" : "✘"); //return tick or X symbols
}

public void markAsDone() {
this.isDone = true;
}

public String getDescription() {
return this.description;
}

@Override
public String toString() {
return "[" + this.getStatusIcon() + "] " + this.getDescription();
}

public String toText() {
return toText("T");
}

public String toText(String type) {
int doneInt = this.isDone ? 1 : 0;
Copy link

Choose a reason for hiding this comment

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

It might be better to use Enum to represent 1 and 0, as it might look like "magic numbers".

return String.format("%s | %d | %s", type, doneInt, this.description);

Choose a reason for hiding this comment

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

I like how you use String.format() here. I think it looks cleaner than using "+" for the strings concatenation 😄

}
}
14 changes: 14 additions & 0 deletions src/main/java/ToDo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
public class ToDo extends Task{
Copy link

Choose a reason for hiding this comment

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

Same as mentioned in DukeException class.

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

public ToDo(String description, boolean status) {
super(description, status);
}

@Override
public String toString() {
return "[T]" + super.toString();
}
}
32 changes: 25 additions & 7 deletions text-ui-test/EXPECTED.TXT
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
Hello from
____ _
| _ \ _ _| | _____
| | | | | | | |/ / _ \
| |_| | |_| | < __/
|____/ \__,_|_|\_\___|

Hello! I'm Duke
What can I do for you?
Got it. I've added this task:
[T][✘] borrow book
Now you have 1 tasks in the list.
Got it. I've added this task:
[D][✘] return book (by: Sunday)
Now you have 2 tasks in the list.
Got it. I've added this task:
[E][✘] project meeting (at: Mon 2-4pm)
Now you have 3 tasks in the list.
Nice! I've marked this task as done:
[D][ ✓] return book (by: Sunday)
Nice! I've marked this task as done:
[T][ ✓] borrow book
Here are the tasks in your list:
1.[T][ ✓] borrow book
2.[D][ ✓] return book (by: Sunday)
3.[E][✘] project meeting (at: Mon 2-4pm)
Nice! I've marked this task as done:
[E][ ✓] project meeting (at: Mon 2-4pm)
Here are the tasks in your list:
1.[T][ ✓] borrow book
2.[D][ ✓] return book (by: Sunday)
3.[E][ ✓] project meeting (at: Mon 2-4pm)
8 changes: 8 additions & 0 deletions text-ui-test/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
todo borrow book
deadline return book /by Sunday
event project meeting /at Mon 2-4pm
done 2
done 1
list
done 3
list
4 changes: 2 additions & 2 deletions text-ui-test/runtest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ then
fi

# compile the code into the bin folder, terminates if error occurred
if ! javac -cp ../src -Xlint:none -d ../bin ../src/main/java/Duke.java
if ! javac -cp ../src -Xlint:none -d ../bin ../src/main/java/*.java
then
echo "********** BUILD FAILURE **********"
exit 1
Expand All @@ -35,4 +35,4 @@ then
else
echo "Test result: FAILED"
exit 1
fi
fi