-
Notifications
You must be signed in to change notification settings - Fork 454
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
[Lulu Y] iP #473
base: master
Are you sure you want to change the base?
[Lulu Y] iP #473
Changes from 9 commits
d839859
661654c
b0037be
8c89dc5
fad94ab
cf8150a
d66b802
7b3a3ea
1385ef2
0b59ceb
8a580c2
3658c71
aace4c9
a0d5982
96f9d69
2b3acec
d00efa7
ee836cd
70f9441
dd6bf47
e2eb3f8
a0ab049
18b246f
53e21c5
d7cf635
06ed319
c371ad6
056aa5d
7a673ec
4795a70
eae433b
4a41bc0
9e1f4e2
2b25d9b
2f4be6b
eaf0b08
6268a75
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import java.time.LocalDate; | ||
import java.time.LocalDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
|
||
public class Deadline extends Task{ | ||
protected LocalDate time; | ||
|
||
public Deadline(String description, String by) { | ||
super(description); | ||
this.label = "D"; | ||
time = LocalDate.parse(by); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "[D]" + super.toString() + " (by: " + time.format(DateTimeFormatter.ofPattern("MMM d yyyy")) + ")"; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,172 @@ | ||
import java.util.ArrayList; | ||
import java.util.Scanner; | ||
import java.io.FileWriter; | ||
import java.io.File; | ||
import java.io.IOException; | ||
|
||
public class Duke { | ||
public static void main(String[] args) { | ||
String logo = " ____ _ \n" | ||
+ "| _ \\ _ _| | _____ \n" | ||
+ "| | | | | | | |/ / _ \\\n" | ||
+ "| |_| | |_| | < __/\n" | ||
+ "|____/ \\__,_|_|\\_\\___|\n"; | ||
System.out.println("Hello from\n" + logo); | ||
|
||
public static void main(String[] args) throws DukeException, IOException{ | ||
String pathName = "./data/duke.txt"; | ||
File f = new File(pathName); | ||
try { | ||
if(!f.exists()){ | ||
f.createNewFile(); | ||
} | ||
Scanner sc = new Scanner(f); | ||
ArrayList<Task> t = 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. I am not sure I like the name of the ArrayList. I think giving it a clearer name would improve the readability of your code. For example, 'taskList'. |
||
while (sc.hasNext()) { | ||
String task = sc.nextLine(); | ||
String[] taskArr = task.split("//|"); | ||
if (taskArr[0].equals("T")) { | ||
Todo td = new Todo(taskArr[2]); | ||
if (taskArr[1].equals("1")) { | ||
td.markAsDone(); | ||
} | ||
t.add(td); | ||
} | ||
if (taskArr[0].equals("D")) { | ||
Deadline d = new Deadline(taskArr[2], taskArr[3]); | ||
if (taskArr[1].equals("1")) { | ||
d.markAsDone(); | ||
} | ||
t.add(d); | ||
} | ||
if (taskArr[0].equals("E")) { | ||
Event e = new Event(taskArr[2], taskArr[3]); | ||
if (taskArr[1].equals("1")) { | ||
e.markAsDone(); | ||
} | ||
t.add(e); | ||
} | ||
} | ||
int ctr = 0; | ||
Scanner scanner = new Scanner(System.in); | ||
System.out.println("_______________________________________________"); | ||
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. Can perhaps use a function to print the dividers to make the code look more readable! |
||
System.out.println("Hello! I'm Duke\n" + "What can I do for you?"); | ||
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. I think placing a line break before the '+' would improve improve the readability of your code. This is because you add a next line character before the '+', so presenting your code in the same way the output will look would be nice. |
||
System.out.println("_______________________________________________"); | ||
|
||
while (scanner.hasNext()) { | ||
try { | ||
String response = scanner.nextLine(); | ||
if (response.equals("list")) { | ||
System.out.println("_______________________________________________"); | ||
System.out.println("Here are the tasks on your list: "); | ||
for (int i = 0; i < ctr; i++) { | ||
System.out.println((i + 1) + ". " + t.get(i)); | ||
} | ||
System.out.println("_______________________________________________"); | ||
} else if (response.equals("bye")) { | ||
System.out.println("_______________________________________________"); | ||
System.out.println("Bye! Hope to see you again soon!"); | ||
System.out.println("_______________________________________________"); | ||
writeFile(f, t); | ||
System.exit(0); | ||
} else if (response.contains("done")) { | ||
String[] str = response.split(" "); | ||
String task = str[0]; | ||
int num = Integer.parseInt(str[1]); | ||
t.get(num - 1).markAsDone(); | ||
|
||
System.out.println("Nice! I've marked this task as done: \n" + t.get(num - 1).toString()); | ||
System.out.println("_______________________________________________"); | ||
} else if (response.contains("todo")) { | ||
if (response.length() > 4) { | ||
String[] str = response.split(" "); | ||
String task = str[0]; | ||
String command = str[1]; | ||
command = response.substring(response.indexOf(" ") + 1); | ||
Task td = new Todo(command); | ||
t.add(td); | ||
|
||
System.out.println("Got it! I've added this task: \n" + td.toString()); | ||
ctr++; | ||
System.out.println("Now you have " + ctr + " tasks in the list."); | ||
System.out.println("_______________________________________________"); | ||
} else { | ||
throw new DukeException("☹ OOPS!!! The description of a todo cannot be empty."); | ||
} | ||
} else if (response.contains("deadline")) { | ||
int tLabelFirst = response.indexOf(" ") + 1; | ||
int tTimeFirst = response.indexOf("/"); | ||
String tLabel = response.substring(tLabelFirst, tTimeFirst - 1); | ||
String tTime = response.substring(tTimeFirst + 4); | ||
Task td = new Deadline(tLabel, tTime); | ||
t.add(td); | ||
|
||
System.out.println("Got it! I've added this task: \n" + td.toString()); | ||
ctr++; | ||
System.out.println("Now you have " + ctr + " tasks in the list."); | ||
System.out.println("_______________________________________________"); | ||
} else if (response.contains("event")) { | ||
int tLabelFirst = response.indexOf(" ") + 1; | ||
int tTimeFirst = response.indexOf("/"); | ||
String tLabel = response.substring(tLabelFirst, tTimeFirst - 1); | ||
String tTime = response.substring(tTimeFirst + 4); | ||
Task td = new Event(tLabel, tTime); | ||
t.add(td); | ||
|
||
System.out.println("Got it! I've added this task: \n" + td.toString()); | ||
ctr++; | ||
System.out.println("Now you have " + ctr + " tasks in the list."); | ||
System.out.println("_______________________________________________"); | ||
} else if (response.contains("delete")) { | ||
String[] str = response.split(" "); | ||
String task = str[0]; | ||
int num = Integer.parseInt(str[1]); | ||
Task tsk = t.remove(num - 1); | ||
|
||
System.out.println("Noted. I've now removed this task: \n" + tsk); | ||
ctr--; | ||
System.out.println("Now you have " + ctr + " tasks in the list."); | ||
System.out.println("_______________________________________________"); | ||
} else { | ||
throw new DukeException("☹ OOPS!!! I'm sorry, but I don't know what that means :-( " + | ||
"Try todo, event, or deadline"); | ||
} | ||
} catch (DukeException d) { | ||
System.out.println(d.getMessage()); | ||
} | ||
} | ||
}catch(IOException e){ | ||
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. I think you may have missed this. Add a space between the } and catch. |
||
System.out.println("OOPS!!! Directory not found. " + e.getMessage()); | ||
System.exit(0); | ||
} | ||
} | ||
public static void writeFile(File f, ArrayList<Task> tasks) throws IOException { | ||
FileWriter fw = new FileWriter(f); | ||
for(int i = 0; i < tasks.size(); i++){ | ||
Task t = tasks.get(i); | ||
String text = ""; | ||
if(t.label.equals("T")){ | ||
text = text.concat("T|"); | ||
if(t.isDone){ | ||
text = text.concat("1|"); | ||
}else{ | ||
text = text.concat("0|"); | ||
} | ||
text = text.concat(t.description + "\n"); | ||
} | ||
if(t.label.equals("D")){ | ||
text = text.concat("D|"); | ||
if(t.isDone){ | ||
text = text.concat("1|"); | ||
}else{ | ||
text = text.concat("0|"); | ||
} | ||
text = text.concat(t.description + "|" + ((Deadline) t).time + "\n"); | ||
} | ||
if(t.label.equals("E")){ | ||
text = text.concat("E|"); | ||
if(t.isDone){ | ||
text = text.concat("1|"); | ||
}else{ | ||
text = text.concat("0|"); | ||
} | ||
text = text.concat(t.description + "|" + ((Event) t).by + "\n"); | ||
} | ||
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. I think it is better to add a space around every 'if', 'else' and 'for' as this helps the readability of your code. |
||
fw.write(text); | ||
} | ||
fw.close(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
public class DukeException extends Exception{ | ||
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. I think you should add a space after Exception. I noticed that this is a recurring practice for your classes. Adding a space between the class declaration and the { will help to add readability to your code. I suggest you read the part on "White space within a statement" in the Java coding standard 😄 . |
||
public DukeException(String message){ | ||
super(message); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
public class Event extends Task{ | ||
|
||
protected String 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. I think it would be better to keep your class variables as private. Do you have a reason to declare them as protected? |
||
|
||
public Event(String description, String by) { | ||
super(description); | ||
this.label = "E"; | ||
this.by = by; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "[E]" + super.toString() + " (by: " + by + ")"; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
public class Task { | ||
protected String description; | ||
protected boolean isDone; | ||
String label; | ||
|
||
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 String toString(){ | ||
return "[" + getStatusIcon() +"] " + this.description; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
public class Todo extends Task { | ||
|
||
protected String by; | ||
|
||
public Todo(String description) { | ||
super(description); | ||
this.label = "T"; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "[T]" + super.toString(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,56 @@ | ||
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. | ||
_______________________________________________ | ||
Got it! I've added this task: | ||
[T][ ] cook dinner | ||
Now you have 2 tasks in the list. | ||
_______________________________________________ | ||
_______________________________________________ | ||
Here are the tasks on your list: | ||
1. [T][ ] read book | ||
2. [T][ ] cook dinner | ||
_______________________________________________ | ||
Got it! I've added this task: | ||
[D][ ] CS2105 assignment (by: Tuesday) | ||
Now you have 3 tasks in the list. | ||
_______________________________________________ | ||
Got it! I've added this task: | ||
[D][ ] CS2103T task (by: Today) | ||
Now you have 4 tasks in the list. | ||
_______________________________________________ | ||
Nice! I've marked this task as done: | ||
[T][X] read book | ||
_______________________________________________ | ||
_______________________________________________ | ||
Here are the tasks on your list: | ||
1. [T][X] read book | ||
2. [T][ ] cook dinner | ||
3. [D][ ] CS2105 assignment (by: Tuesday) | ||
4. [D][ ] CS2103T task (by: Today) | ||
_______________________________________________ | ||
Got it! I've added this task: | ||
[E][ ] archery course (by: Aug 26 5-7 pm) | ||
Now you have 5 tasks in the list. | ||
_______________________________________________ | ||
Nice! I've marked this task as done: | ||
[D][X] CS2105 assignment (by: Tuesday) | ||
_______________________________________________ | ||
Nice! I've marked this task as done: | ||
[D][X] CS2103T task (by: Today) | ||
_______________________________________________ | ||
_______________________________________________ | ||
Here are the tasks on your list: | ||
1. [T][X] read book | ||
2. [T][ ] cook dinner | ||
3. [D][X] CS2105 assignment (by: Tuesday) | ||
4. [D][X] CS2103T task (by: Today) | ||
5. [E][ ] archery course (by: Aug 26 5-7 pm) | ||
_______________________________________________ | ||
_______________________________________________ | ||
Bye! Hope to see you again soon! | ||
_______________________________________________ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
todo read book | ||
todo cook dinner | ||
list | ||
deadline CS2105 assignment /by Tuesday | ||
deadline CS2103T task /by Today | ||
done 1 | ||
list | ||
event archery course /at Aug 26 5-7 pm | ||
done 3 | ||
done 4 | ||
list | ||
bye |
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.
Could perhaps add header docs to the class!