-
Notifications
You must be signed in to change notification settings - Fork 438
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
base: master
Are you sure you want to change the base?
Changes from 27 commits
b0b06c7
a240abd
affd926
79d4d42
5a1fcf7
266db70
84ccdbd
c0f21c4
529f823
62872cd
1bd86c5
59b5d8e
22a75bb
2e8dec8
9d1cf01
a97cea3
e7c27be
79740e0
4514a22
f395cb4
b046dde
b524ea4
519147a
9182971
b5cc47b
0a2925a
fca52df
b3778fe
94f6e22
d17fe93
584720d
c202b68
9bc7fbd
6afecbc
b15d528
52172c1
d76908a
e0cafc3
ea1ad12
2a18cd0
2dedee2
93a7276
cf585d9
e5f245f
07f06f3
17a6bbf
486b792
f285892
eaba12a
5f5ab83
6366d0a
2cef4c2
e92b0ef
9732544
28aafcc
f886b94
a343896
c6c5573
ff195ae
3d01210
5a7755e
d13b712
48fc970
1c07ea6
72f40c7
5922b84
fefab25
39df444
5bad734
690abb3
97fa23b
a904225
c7e4fc8
32a730f
86ad76d
4df3c01
0af3ecf
84b52a4
656baa6
498ee0f
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 |
---|---|---|
|
@@ -15,3 +15,4 @@ bin/ | |
|
||
/text-ui-test/ACTUAL.txt | ||
text-ui-test/EXPECTED-UNIX.TXT | ||
data/data.txt |
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 + ")"; | ||
} | ||
} |
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; | ||
|
||
public class Duke { | ||
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 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" + | ||
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. The "+" should be in the next line instead. |
||
"____________________________________________________________\n",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. 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 ++) { | ||
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. 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) { | ||
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. Perhaps the method name could have been a verb instead? Eg. parseCommand. 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. 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) { | ||
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. 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<>(); | ||
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. A better practice might be to split the instantiation of "lst". After your import statements, it could be List lst; |
||
|
||
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); | ||
} | ||
} |
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. A spacing after "Exception" will look nicer! |
||
DukeException(String s) { | ||
super(s); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
public class Event extends Task { | ||
protected String at; | ||
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. 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 + ")"; | ||
} | ||
} |
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; | ||
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. 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); | ||
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 like how you use String.format() here. I think it looks cleaner than using "+" for the strings concatenation 😄 |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
public class ToDo extends Task{ | ||
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. 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(); | ||
} | ||
} |
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) |
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 |
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.
I think the import statements can be grouped based on their package and add one blank line between the groups to enhance readability?