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

[Mark Biju George] iP #306

Open
wants to merge 57 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
d839859
Add Gradle support
May 24, 2020
88c8d20
Update Duke.java
markbiju16 Jan 20, 2022
6a2ea62
Update Duke.java
markbiju16 Jan 20, 2022
8d817b0
Update Duke.java
markbiju16 Jan 20, 2022
b8c4536
Level-4
markbiju16 Jan 21, 2022
0eaf074
Level-4 upd
markbiju16 Jan 22, 2022
a5fd6ec
A-TextUiTesting
markbiju16 Jan 22, 2022
0f0b793
Level-5
markbiju16 Jan 26, 2022
3a748c6
Update Duke.java
markbiju16 Jan 26, 2022
f1e27b0
Level-7
markbiju16 Feb 1, 2022
3edf9ae
Level-8
markbiju16 Feb 2, 2022
2466584
Merge branch 'branch-Level-8'
markbiju16 Feb 2, 2022
987ae8f
A-MoreOOP
markbiju16 Feb 5, 2022
91a36da
update A-MoreOOP
markbiju16 Feb 5, 2022
83901c6
Delete AddCommand.java
markbiju16 Feb 11, 2022
ab034ad
A-Packages
markbiju16 Feb 11, 2022
edcb4fc
A-JUnit
markbiju16 Feb 12, 2022
eb2a949
Delete file.txt
markbiju16 Feb 12, 2022
cd19b3c
A-Jar
markbiju16 Feb 12, 2022
a61bb16
branch-A-JavaDoc
markbiju16 Feb 12, 2022
4440da8
branch-A-CodingStandard
markbiju16 Feb 13, 2022
3430097
Level-9
markbiju16 Feb 14, 2022
a502950
Merge branch 'branch-A-CodingStandard'
markbiju16 Feb 14, 2022
5a5a5a0
Merge branch 'branch-Level-9'
markbiju16 Feb 14, 2022
c6b7c86
Merge branch 'add-gradle-support'
markbiju16 Feb 15, 2022
e11f89b
Level-10
markbiju16 Feb 19, 2022
9c12c96
Update Storage.java
markbiju16 Feb 19, 2022
5f2d63d
update Ui
markbiju16 Feb 19, 2022
8255963
Update tasks.txt
markbiju16 Feb 19, 2022
f3bb3d2
Update build.gradle
markbiju16 Feb 19, 2022
53ed0bf
update A-Jar
markbiju16 Feb 19, 2022
dc7bd54
update Unmark
markbiju16 Feb 19, 2022
690237a
Update find
markbiju16 Feb 19, 2022
6cb9827
Update delete
markbiju16 Feb 19, 2022
cbc97e1
Add assertions
markbiju16 Feb 21, 2022
df4e00f
Update Ui.java
markbiju16 Feb 21, 2022
8ae349f
Merge branch 'branch-Level-10'
markbiju16 Feb 21, 2022
3e5b53a
Improve code quality
markbiju16 Feb 21, 2022
5dc99cf
Update Deadline.java
markbiju16 Feb 21, 2022
7b9e93d
Update Deadline.java
markbiju16 Feb 21, 2022
c13f1a0
Merge branch 'master' into branch-A-Assertions
markbiju Feb 21, 2022
134e81c
Merge pull request #2 from markbiju/branch-A-Assertions
markbiju Feb 21, 2022
7cab92e
Merge branch 'master' into branch-A-CodeQuality
markbiju Feb 21, 2022
be9a892
Merge pull request #3 from markbiju/branch-A-CodeQuality
markbiju Feb 21, 2022
d93a64d
Update deadline saving
markbiju16 Feb 21, 2022
84784f9
Merge branch 'master' of https://github.com/markbiju/ip
markbiju16 Feb 21, 2022
ad26dcd
Update deadline saving
markbiju16 Feb 21, 2022
9124b44
BCD-Extension
markbiju16 Feb 21, 2022
0613f16
Update README.md
markbiju16 Feb 21, 2022
354d8b1
Ui.png
markbiju16 Feb 21, 2022
aacf706
Update README.md
markbiju16 Feb 21, 2022
6deb38d
Update README.md
markbiju16 Feb 21, 2022
0411ea5
Update README.md
markbiju16 Feb 21, 2022
046ca8f
Update SnoozeCommand.java
markbiju16 Feb 21, 2022
0cf6b0c
Update README.md
markbiju Feb 22, 2022
dd9d9a3
Update Parser.java
markbiju16 Feb 24, 2022
d84593a
Update Parser.java
markbiju16 Feb 24, 2022
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
1 change: 1 addition & 0 deletions data/file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[T][X] eat chicken
19 changes: 19 additions & 0 deletions src/main/java/Deadline.java
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;

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'.

this.time = time;
}

@Override
public String toString() {

Choose a reason for hiding this comment

The 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 + ")";
}
}
186 changes: 179 additions & 7 deletions src/main/java/Duke.java
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);

Choose a reason for hiding this comment

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

Would "scanner" be a better variable name here?

int counter = 0;

Choose a reason for hiding this comment

The 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");

Choose a reason for hiding this comment

The 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) {
Copy link

Choose a reason for hiding this comment

The 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.

Copy link

Choose a reason for hiding this comment

The 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")){

Choose a reason for hiding this comment

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

As per java Coding Standards

Java reserved words should be followed by a white space.

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:");
Copy link

Choose a reason for hiding this comment

The 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());
}

Choose a reason for hiding this comment

The 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());
}

Choose a reason for hiding this comment

The 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 :-(");
}
}
}
}
5 changes: 5 additions & 0 deletions src/main/java/DukeException.java
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);
}
}
14 changes: 14 additions & 0 deletions src/main/java/Event.java
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 + ")";
}
}
28 changes: 28 additions & 0 deletions src/main/java/Task.java
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() {

Choose a reason for hiding this comment

The 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;
}
}
11 changes: 11 additions & 0 deletions src/main/java/Todo.java
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();
}
}
23 changes: 16 additions & 7 deletions text-ui-test/EXPECTED.TXT
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!
5 changes: 5 additions & 0 deletions text-ui-test/input.txt
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