-
Notifications
You must be signed in to change notification settings - Fork 361
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
base: master
Are you sure you want to change the base?
[JThh] ip #364
Changes from 20 commits
556af3f
41d3174
b74f259
9926150
f1781e5
3272357
f35eeda
cef55ac
89b047b
8637026
a01b890
eb0d5db
e1aa1ec
f1c11d3
a353a03
6300a04
0792b27
7105732
717395e
dd2153c
2cc5e4a
6de7960
e6212c5
c221412
28ed102
b3a0071
7a694e6
b966dc8
2d9ca5e
2fe004b
c99555e
a605b0d
88a162a
b7554d5
6d29b0c
b20bdf1
0e2d481
8c7b0c8
b36e5a0
26d5c27
0bc35eb
3dc32ba
8baab87
1261027
b9be6ca
962a618
f58e500
8ab92df
d36f2ce
ee750ad
a3f3019
51ad510
5533235
9497575
aa88a18
d7ecdad
033f3ee
503fbb9
083b67d
46b8178
9749f0a
3df55f8
4899d04
5a6e8fe
fb6b17f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
|
||
|
||
public class Duke { | ||
public static ArrayList<Task> todos = new ArrayList<>(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 + ")"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()); | ||
} | ||
} | ||
} | ||
} |
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) |
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 |
There was a problem hiding this comment.
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 😄