-
Notifications
You must be signed in to change notification settings - Fork 270
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
[Mark Biju George] iP #306
base: master
Are you sure you want to change the base?
Changes from 11 commits
d839859
88c8d20
6a2ea62
8d817b0
b8c4536
0eaf074
a5fd6ec
0f0b793
3a748c6
f1e27b0
3edf9ae
2466584
987ae8f
91a36da
83901c6
ab034ad
edcb4fc
eb2a949
cd19b3c
a61bb16
4440da8
3430097
a502950
5a5a5a0
c6b7c86
e11f89b
9c12c96
5f2d63d
8255963
f3bb3d2
53ed0bf
dc7bd54
690237a
6cb9827
cbc97e1
df4e00f
8ae349f
3e5b53a
5dc99cf
7b9e93d
c13f1a0
134e81c
7cab92e
be9a892
d93a64d
84784f9
ad26dcd
9124b44
0613f16
354d8b1
aacf706
6deb38d
0411ea5
046ca8f
0cf6b0c
dd9d9a3
d84593a
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 @@ | ||
[T][X] eat chicken |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import java.time.LocalDate; | ||
import java.time.format.DateTimeFormatter; | ||
|
||
public class Deadline extends Task { | ||
|
||
protected LocalDate by; | ||
protected String time; | ||
|
||
public Deadline(String description, LocalDate by, String time) { | ||
super(description); | ||
this.by = by; | ||
this.time = time; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
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 the use of toString() to print details of the task. The method name itself is already specific to its action and it is a built-in method in all java objects. |
||
return "[D]" + super.toString() + " (by: " + by.format(DateTimeFormatter.ofPattern("MMM d yyyy")) +" " + time + ")"; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,182 @@ | ||
import java.io.File; | ||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
import java.time.LocalDate; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Scanner; | ||
|
||
public class Duke { | ||
public static void main(String[] args) { | ||
String logo = " ____ _ \n" | ||
+ "| _ \\ _ _| | _____ \n" | ||
+ "| | | | | | | |/ / _ \\\n" | ||
+ "| |_| | |_| | < __/\n" | ||
+ "|____/ \\__,_|_|\\_\\___|\n"; | ||
System.out.println("Hello from\n" + logo); | ||
|
||
private static void writeToFile(String filePath, String textToAdd) throws IOException { | ||
FileWriter fw = new FileWriter(filePath); | ||
fw.write(textToAdd); | ||
fw.close(); | ||
} | ||
|
||
private static void appendToFile(String filePath, String textToAppend) throws IOException { | ||
FileWriter fw = new FileWriter(filePath, true); // create a FileWriter in append mode | ||
fw.write(textToAppend); | ||
fw.close(); | ||
} | ||
|
||
|
||
public static void main(String[] args) throws DukeException, IOException { | ||
|
||
Scanner myObj = new Scanner(System.in); | ||
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. Would "scanner" be a better variable name here? |
||
int counter = 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. How about a more intuitive name here? Suggestion: "numOfTasksInList". |
||
ArrayList<Task> tasks = new ArrayList<Task>(); | ||
File f = new File("data/file.txt"); | ||
File f1 = new File("data"); | ||
String file1 = "data/file.txt"; | ||
// System.out.println("full path: " + f.getAbsolutePath()); | ||
System.out.println("Hello! I'm 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. Would you consider creating a constant final String for these messages to be printed? |
||
System.out.println("What can I do for you?"); | ||
|
||
if(!f.exists()) { | ||
if (!f1.exists()) { | ||
f1.mkdir(); | ||
} | ||
f.createNewFile(); | ||
} | ||
|
||
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. Perhaps, parsing user inputs could be done in another class called Parser for more abstraction and OOP. 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, this code block is too long, could work towards better abstraction overall. |
||
String input = myObj.nextLine(); | ||
if (input.equals("bye")) { | ||
System.out.println("Bye. Hope to see you again soon!"); | ||
break; | ||
} else if (input.equals("list")){ | ||
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. As per java Coding Standards
Perhaps you can add a white space between the condition and the open parenthesis to be consistent with the rest of the code. I notice many similar issues throughout the rest of the code. |
||
System.out.println("Here are the tasks in your list:"); | ||
for(int i = 0; i < counter; i++) { | ||
System.out.println( Integer.toString(i + 1) + "." + tasks.get(i)); | ||
} | ||
} else if(input.length() > 3 && input.substring(0,4).equals("mark")){ | ||
try { | ||
int toBeMarked = Integer.parseInt(input.substring(5)); | ||
Task currentTask = tasks.get(toBeMarked - 1); | ||
currentTask.markAsDone(); | ||
System.out.println("Nice! I've marked this task as done:"); | ||
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, printing of messages could be done in another Ui class for better abstraction and OOP. |
||
System.out.println(currentTask); | ||
writeToFile(file1, tasks.get(0).toString()); | ||
for(int i = 1; i < tasks.size(); i++) { | ||
appendToFile(file1, tasks.get(i).toString()); | ||
} | ||
} catch (StringIndexOutOfBoundsException e) { | ||
throw new DukeException("☹ OOPS!!! Please tell me which task to mark as done!"); | ||
} catch (IOException e) { | ||
System.out.println("Something went wrong: " + e.getMessage()); | ||
} | ||
|
||
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. Are the empty lines used to differentiate between the next condition in the user input processing? This is a pretty good style as it notifies readers/reviewers of the change in logic. |
||
} else if(input.length() > 5 && input.substring(0,6).equals("unmark")){ | ||
try { | ||
int toBeUnmarked = Integer.parseInt(input.substring(7)); | ||
Task currentTask = tasks.get(toBeUnmarked - 1); | ||
currentTask.markAsUndone(); | ||
System.out.println("OK, I've marked this task as not done yet: "); | ||
System.out.println(currentTask); | ||
writeToFile(file1, tasks.get(0).toString()); | ||
for(int i = 1; i < tasks.size(); i++) { | ||
appendToFile(file1, tasks.get(i).toString()); | ||
} | ||
} catch (StringIndexOutOfBoundsException e) { | ||
throw new DukeException("☹ OOPS!!! Please tell me which task you want me to mark as not done!"); | ||
} catch (IOException e) { | ||
System.out.println("Something went wrong: " + e.getMessage()); | ||
} | ||
|
||
} else if(input.length() > 3 && input.substring(0, 4).equals("todo")) { | ||
try { | ||
String[] arrOfStr = input.split(" ", 2); | ||
tasks.add(new Todo(arrOfStr[1])); | ||
counter = counter + 1; | ||
System.out.println("Got it. I've added this task:"); | ||
System.out.println(tasks.get(counter - 1)); | ||
appendToFile(file1, tasks.get(counter - 1).toString()); | ||
if (counter == 1) { | ||
System.out.println("Now you have " + String.valueOf(counter) + " task in the list."); | ||
} else { | ||
System.out.println("Now you have " + String.valueOf(counter) + " tasks in the list."); | ||
} | ||
|
||
} catch(ArrayIndexOutOfBoundsException e) { | ||
throw new DukeException("☹ OOPS!!! The description of a todo cannot be empty."); | ||
} catch (IOException e) { | ||
System.out.println("Something went wrong: " + e.getMessage()); | ||
} | ||
|
||
} else if(input.length() > 7 && (input.substring(0, 8).equals("deadline"))) { | ||
try { | ||
String[] arrOfStr = input.split(" ", 2); | ||
String[] arrOfStr2 = arrOfStr[1].split("/", 2); | ||
String[] arrOfStr3 = arrOfStr2[1].split(" ", 3); | ||
|
||
tasks.add(new Deadline(arrOfStr2[0], LocalDate.parse(arrOfStr3[1]), arrOfStr3[2])); | ||
counter = counter + 1; | ||
System.out.println("Got it. I've added this task:"); | ||
System.out.println(tasks.get(counter - 1)); | ||
appendToFile(file1, tasks.get(counter - 1).toString()); | ||
if (counter == 1) { | ||
System.out.println("Now you have " + String.valueOf(counter) + " task in the list."); | ||
} else { | ||
System.out.println("Now you have " + String.valueOf(counter) + " tasks in the list."); | ||
} | ||
|
||
} catch(ArrayIndexOutOfBoundsException e) { | ||
throw new DukeException("☹ OOPS!!! Please provide a deadline for your task."); | ||
|
||
} catch (IOException e) { | ||
System.out.println("Something went wrong: " + e.getMessage()); | ||
} | ||
|
||
} else if(input.length() > 4 && input.substring(0, 5).equals("event")) { | ||
try { | ||
String[] arrOfStr = input.split(" ", 2); | ||
String[] arrOfStr2 = arrOfStr[1].split("/", 2); | ||
String[] arrOfStr3 = arrOfStr2[1].split(" ", 2); | ||
|
||
tasks.add(new Event(arrOfStr2[0], arrOfStr3[1])); | ||
counter = counter + 1; | ||
System.out.println("Got it. I've added this task:"); | ||
System.out.println(tasks.get(counter - 1)); | ||
appendToFile(file1, tasks.get(counter - 1).toString()); | ||
if (counter == 1) { | ||
System.out.println("Now you have " + String.valueOf(counter) + " task in the list."); | ||
} else { | ||
System.out.println("Now you have " + String.valueOf(counter) + " tasks in the list."); | ||
} | ||
|
||
} catch(ArrayIndexOutOfBoundsException e) { | ||
throw new DukeException("☹ OOPS!!! Please provide a timing for your event."); | ||
} catch (IOException e) { | ||
System.out.println("Something went wrong: " + e.getMessage()); | ||
} | ||
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 this might have been missed during checks, did you made a mistake and left out a empty white line to separate the if-else logic blocks? |
||
|
||
} else if(input.length() > 5 && input.substring(0, 6).equals("delete")) { | ||
try { | ||
int toBeDeleted = Integer.parseInt(input.substring(7)); | ||
|
||
System.out.println("Noted. I've removed this task:"); | ||
System.out.println(tasks.get(toBeDeleted- 1)); | ||
counter = counter - 1; | ||
tasks.remove(toBeDeleted- 1); | ||
if (counter == 1) { | ||
System.out.println("Now you have " + String.valueOf(counter) + " task in the list."); | ||
} else { | ||
System.out.println("Now you have " + String.valueOf(counter) + " tasks in the list."); | ||
} | ||
writeToFile(file1, tasks.get(0).toString()); | ||
for(int i = 1; i < tasks.size(); i++) { | ||
appendToFile(file1, tasks.get(i).toString()); | ||
} | ||
} catch (StringIndexOutOfBoundsException e) { | ||
throw new DukeException("☹ OOPS!!! Please tell me which task you want me to mark as not done!"); | ||
|
||
} catch (IOException e) { | ||
System.out.println("Something went wrong: " + e.getMessage()); | ||
} | ||
} else { | ||
throw new DukeException("☹ OOPS!!! I'm sorry, but I don't know what that means :-("); | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
public class DukeException extends Exception { | ||
public DukeException(String errorMessage) { | ||
super(errorMessage); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
public class Event extends Task { | ||
|
||
protected String at; | ||
|
||
public Event(String description, String by) { | ||
super(description); | ||
this.at = by; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "[E]" + super.toString() + " (at: " + at + ")"; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
public class Task { | ||
protected String description; | ||
protected boolean isDone; | ||
|
||
public Task(String description) { | ||
this.description = description; | ||
this.isDone = false; | ||
} | ||
|
||
void markAsDone() { | ||
this.isDone = true; | ||
} | ||
|
||
void markAsUndone() { | ||
this.isDone = false; | ||
} | ||
|
||
public String getStatusIcon() { | ||
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 the way you name this method. It is short, direct and natural. Much better than getIsDone() which is what I named mine. 😅 |
||
return isDone | ||
? "X" | ||
: " "; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "[" + this.getStatusIcon() + "] " + description; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
public class Todo extends Task{ | ||
|
||
public Todo(String description) { | ||
super(description); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "[T]" + super.toString(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,16 @@ | ||
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 task 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. | ||
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) | ||
Bye. Hope to see you again soon! |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
todo borrow book | ||
deadline return book /by Sunday | ||
event project meeting /at Mon 2-4pm | ||
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.
Perhaps this variable name can be more specific? One might mistake 'by' as 'added by' instead of 'complete by'.