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

[JThh] ip #364

Open
wants to merge 65 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
65 commits
Select commit Hold shift + click to select a range
556af3f
Add Gradle support
May 24, 2020
41d3174
Init Level 1. Greet, Echo, Exit
Jan 17, 2023
b74f259
Init Level 2. Add, List
Jan 17, 2023
9926150
Finish Level 3. Mark as Done
Jan 17, 2023
f1781e5
Finish Level 3. Mark as Done
Jan 17, 2023
3272357
Finish Level 4. ToDos, Events, Deadlines
Jan 18, 2023
f35eeda
Finish Level 6. Delete
Jan 18, 2023
cef55ac
Finish Level 6. Delete
Jan 18, 2023
89b047b
Finish Level 6. Delete
Jan 18, 2023
8637026
Finish Level 6. Delete
Jan 18, 2023
a01b890
Finish Level 5. Exception
Jan 18, 2023
eb0d5db
Finish Level 5. Exception
Jan 18, 2023
e1aa1ec
Merge remote-tracking branch 'origin/master'
Jan 18, 2023
f1c11d3
Level-7 Finish
Jan 31, 2023
a353a03
Level-8 Finish
Jan 31, 2023
6300a04
Merge branch 'branch-Level-7'
Jan 31, 2023
0792b27
Level-8 Finish
Jan 31, 2023
7105732
Merge branch 'branch-Level-7'
Jan 31, 2023
717395e
Update Duke.java
JThh Jan 31, 2023
dd2153c
Merge branch 'master' into Level-8
JThh Jan 31, 2023
2cc5e4a
Merge pull request #1 from JThh/Level-8
JThh Jan 31, 2023
6de7960
More OOP in-progress
Feb 3, 2023
e6212c5
More OOP
Feb 17, 2023
c221412
resolved conflicts
Feb 17, 2023
28ed102
Merge pull request #3 from JThh/branch-Level-7
JThh Feb 17, 2023
b3a0071
removed unused files
Feb 17, 2023
7a694e6
added gradle support
Feb 17, 2023
b966dc8
Merge branch 'master' into add-gradle-support
Feb 17, 2023
2d9ca5e
Modified tests
Feb 18, 2023
2fe004b
add workflow script and modify test files
Feb 18, 2023
c99555e
add date parsing function and fix unit tests
Feb 19, 2023
a605b0d
finish jar file creation
Feb 19, 2023
88a162a
omit data
Feb 19, 2023
b7554d5
Create gradle.yml
JThh Feb 19, 2023
6d29b0c
add gui init
Feb 19, 2023
b20bdf1
fixed ci tests
Feb 19, 2023
0e2d481
fix gradle
Feb 19, 2023
8c7b0c8
add java doc
Feb 19, 2023
b36e5a0
refactor codes and add command class
Feb 19, 2023
26d5c27
fixes merging conflicts
Feb 19, 2023
0bc35eb
finishes adding find function
Feb 19, 2023
3dc32ba
Merge branch 'master' into branch-A-JavaDoc
Feb 19, 2023
8baab87
Add gui
Feb 19, 2023
1261027
Merge branch 'master' into branch-A-JavaDoc
Feb 19, 2023
b9be6ca
Fix GUI
Feb 19, 2023
962a618
Add Ui png
Feb 19, 2023
f58e500
Fix gui
Feb 20, 2023
8ab92df
Merge branch 'master' into branch-A-JavaDoc
Feb 20, 2023
d36f2ce
Make command class attributes private and methods non-static
Feb 20, 2023
ee750ad
Finish comments
Feb 20, 2023
a3f3019
Add assertions for input sanity check
Feb 20, 2023
51ad510
Refactor codes
Feb 20, 2023
5533235
Refactor codes
Feb 20, 2023
9497575
Fix CI
Feb 20, 2023
aa88a18
Fix CI
Feb 20, 2023
d7ecdad
Add check duplicate functionality
Feb 20, 2023
033f3ee
Fix CI
Feb 20, 2023
503fbb9
Fix CI
Feb 20, 2023
083b67d
Fix CI: another trial
Feb 20, 2023
46b8178
Replace ui png
Feb 20, 2023
9749f0a
Fix CI
Feb 20, 2023
3df55f8
Add streaming behaviors
Feb 20, 2023
4899d04
Add streaming behaviors
Feb 20, 2023
5a6e8fe
Add README.md
Feb 20, 2023
fb6b17f
Merge pull request #4 from JThh/branch-A-CodeQuality
JThh Feb 20, 2023
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
273 changes: 267 additions & 6 deletions src/main/java/Duke.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,271 @@
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import java.io.File;


Choose a reason for hiding this comment

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

remember to separate the files and provide javadoc when you reach the appropriate stages 😄

public class Duke {
public static ArrayList<Task> todos = new ArrayList<>();

Choose a reason for hiding this comment

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

Good naming of arrays and making them plural!

public static File dir = new File("./data/");
public static File file = new File("./data/todo_list.txt");

public static class Task {
protected String description;
protected boolean isDone;

Choose a reason for hiding this comment

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

good job at consistent naming of the booleans!


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

public String getStatusIcon() {
return (isDone ? "X" : " "); // mark done task with X
}

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

public void markAsNotDone() {
this.isDone = false;
}

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

public static class Todo extends Task {
public Todo(String description) {
super(description);
}
@Override
public String toString() {
return "[T]" + super.toString();
}
}

public static class Deadline extends Task {

protected String by;

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

@Override
public String toString() {
return "[D]" + super.toString() + " (by: " + by + ")";

Choose a reason for hiding this comment

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

To keep it simple you can use string formatting to prevent accumulation of '+' in string concatenation

int i = 461012;
System.out.format("The value of i is: %d%n", i);

}
}

public static class Event extends Task {

protected String from;
protected String to;

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

@Override
public String toString() {
return "[E]" + super.toString() + " (from: " + from + " to " + to + ")";
}
}

public static void print(String s) {
System.out.println(s);
}

public static class DukeCommandNotFoundException extends Exception {
public DukeCommandNotFoundException (String msg) {
super(msg);
}
}

public static class DukeEmptyTaskException extends Exception {
public DukeEmptyTaskException (String msg) {
super(msg);
}
}


public static String parse_date(String s) {

Choose a reason for hiding this comment

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

Instead of "parse_date", perhaps you could rename it to "parseDate", which conforms to the coding standard?

DateTimeFormatter read_fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HHmm");
DateTimeFormatter print_fmt = DateTimeFormatter.ofPattern("MMM dd yyyy");
try {
LocalDate lt = LocalDate.parse(s, read_fmt);

Choose a reason for hiding this comment

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

Is it possible to make the argument more expressive?

return lt.format(print_fmt);
} catch (DateTimeParseException e) {
print(e.toString());
}
return s;
}

public static void save_to_file() {
try {
if (!dir.exists()){
while (!dir.mkdirs()) {
print(dir.getName() + " created\n");
}
}

if (file.createNewFile()) {
print(file.getName() + " created\n");
}

FileWriter fw = new FileWriter(file, false);
if (todos.isEmpty()) {
return;
} else {
for (Task t : todos) {
String desc = t.toString() + "\n";
fw.write(desc);
}
}
fw.close();
} catch (IOException e) {
e.getStackTrace();
}
}

public static void process_input(String input) throws DukeCommandNotFoundException, DukeEmptyTaskException {
String trigger = input.split(" ")[0];
int tid = 1;
Task task;
String content = "", ddl = "", from = "", to = "";
switch (trigger) {
case "bye":
print("Bye. Hope to see you again soon!");
System.exit(0);
case "list":
if (todos.isEmpty()) {
print("No items yet.");
} else {
int i = 1;
for (Task t : todos) {
print(i + "." + t.toString());
i++;
}
}
break;
case "mark":
try {
tid = Integer.parseInt(input.split(" ")[1]);
} catch (NumberFormatException | IndexOutOfBoundsException e) {
print(e.toString());
System.exit(1);
}
task = todos.get(tid - 1);
task.markAsDone();
print("Nice! I've marked this task as done:");
print("\t" + task);
break;
case "unmark":
try {
tid = Integer.parseInt(input.split(" ")[1]);
} catch (NumberFormatException | IndexOutOfBoundsException e) {
print(e.toString());
System.exit(1);
}
task = todos.get(tid - 1);
task.markAsNotDone();
print("OK, I've marked this task as not done yet:");
print("\t" + task);
break;
case "deadline":
try {
input = input.split(trigger)[1];
content = input.split("/by")[0].strip();
ddl = input.split("/by")[1].strip();
ddl = parse_date(ddl);
} catch (IndexOutOfBoundsException e) {
print(e.toString());
System.exit(1);
}
task = new Deadline(content, ddl);
todos.add(task);
print("Got it. I've added this task:");
print("\t" + task);
print("Now you have " + todos.size() + " tasks in the list.");
break;
case "event":
if (input.split(trigger).length == 1) {
throw new DukeEmptyTaskException("☹ OOPS!!! The description of a " + trigger + " cannot be empty.");
} else {
input = input.split(trigger)[1].strip();
}
try {
content = input.split("/from")[0].strip();
from = input.split("/from")[1].split("/to")[0].strip();
to = input.split("/from")[1].split("/to")[1].strip();
from = parse_date(from);
to = parse_date(to);
} catch (IndexOutOfBoundsException e) {
print(e.toString());
System.exit(1);
}
task = new Event(content, from, to);
todos.add(task);
print("Got it. I've added this task:");
print("\t" + task);
print("Now you have " + todos.size() + " tasks in the list.");
break;
case "todo":
if (input.split(trigger).length == 0) {
throw new DukeEmptyTaskException("☹ OOPS!!! The description of a " + trigger + " cannot be empty.");
} else {
input = input.split(trigger)[1].strip();
}
task = new Todo(input);
todos.add(task);
print("Got it. I've added this task:");
print("\t" + task);
print("Now you have " + todos.size() + " tasks in the list.");
break;
case "delete":
if (input.split(trigger).length == 1) {
throw new DukeEmptyTaskException("☹ OOPS!!! The description of a " + trigger + " cannot be empty.");
}
try {
tid = Integer.parseInt(input.split(trigger)[1].strip());
task = todos.get(tid - 1);
todos.remove(task);
print("Noted. I've removed this task:");
print("\t" + task);
print("Now you have " + todos.size() + " tasks in the list.");
} catch (NumberFormatException | IndexOutOfBoundsException e) {
print(e.toString());
System.exit(1);
}
break;
default:
throw new DukeCommandNotFoundException("☹ OOPS!!! I'm sorry, but I don't know what that means :-(");
}
save_to_file();
}

public static void main(String[] args) {
String logo = " ____ _ \n"
+ "| _ \\ _ _| | _____ \n"
+ "| | | | | | | |/ / _ \\\n"
+ "| |_| | |_| | < __/\n"
+ "|____/ \\__,_|_|\\_\\___|\n";
System.out.println("Hello from\n" + logo);
String greeting = "Hello! I'm Duke\n" +
" What can I do for you?";
print(greeting);
Scanner sc = new Scanner(System.in);
while (sc.hasNextLine()) {
String input = sc.nextLine();
try {
process_input(input);
} catch (Exception e) {
print(e.toString());
}
}
}
}
37 changes: 30 additions & 7 deletions text-ui-test/EXPECTED.TXT
Original file line number Diff line number Diff line change
@@ -1,7 +1,30 @@
Hello from
____ _
| _ \ _ _| | _____
| | | | | | | |/ / _ \
| |_| | |_| | < __/
|____/ \__,_|_|\_\___|

Hello! I'm Duke
What can I do for you?
Got it. I've added this task:
[T][ ] read book
Now you have 1 tasks in the list.
Nice! I've marked this task as done:
[T][X] read book
Got it. I've added this task:
[D][ ] return book (by: June 6th)
Now you have 2 tasks in the list.
Got it. I've added this task:
[T][ ] borrow book
Now you have 3 tasks in the list.
Got it. I've added this task:
[D][ ] return book (by: Sunday)
Now you have 4 tasks in the list.
Got it. I've added this task:
[E][ ] project meeting (from: Mon 2pm to 4pm)
Now you have 5 tasks in the list.
OK, I've marked this task as not done yet:
[T][ ] read book
Got it. I've added this task:
[D][ ] do homework (by: no idea :-p)
Now you have 6 tasks in the list.
1.[T][ ] read book
2.[D][ ] return book (by: June 6th)
3.[T][ ] borrow book
4.[D][ ] return book (by: Sunday)
5.[E][ ] project meeting (from: Mon 2pm to 4pm)
6.[D][ ] do homework (by: no idea :-p)
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 read book
mark 1
deadline return book /by June 6th
todo borrow book
deadline return book /by Sunday
event project meeting /from Mon 2pm /to 4pm
unmark 1
deadline do homework /by no idea :-p
list