Skip to content

[ChinTaiAnn] ip#76

Open
chintaiann wants to merge 16 commits into
nus-cs2113-AY2122S2:masterfrom
chintaiann:master
Open

[ChinTaiAnn] ip#76
chintaiann wants to merge 16 commits into
nus-cs2113-AY2122S2:masterfrom
chintaiann:master

Conversation

@chintaiann

Copy link
Copy Markdown

No description provided.

@suenalaba suenalaba left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

You can try refactoring your code, to make it for readable. Avoid too many commands within a block.

Comment thread src/main/java/Duke.java Outdated
System.out.println(("What can i do for you?"));
System.out.println("---------------------");
Task[] taskList = new Task[100];
Boolean run = true;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Naming error. Boolean not does not sound boolean

Comment thread src/main/java/Duke.java Outdated
Comment on lines +21 to +41
else if (anyString.matches("mark \\d+")){
String[] arrOfStr = anyString.split(" ", 0);
String indexValue = arrOfStr[1];
int indexValue2 = Integer.parseInt(indexValue) - 1;
taskList[indexValue2].markTask();

}

else if (anyString.startsWith("todo")) {
String desc = anyString.substring(5);
taskList[arrayIndex] = new Task(desc);
arrayIndex++;
}

else if (anyString.startsWith("deadline")){
int index = anyString.indexOf("/");
String desc = anyString.substring(9,index);
index++;
String by = anyString.substring(index);
taskList[arrayIndex] = new Deadline(desc,by);
arrayIndex++;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Can refactor code to make it more readable.

Comment thread src/main/java/Event.java Outdated

@Override
public String toString() {
return "[E]" + (super.isDone ? "[X]" + super.description : "[ ]"+ super.description) + "(" + timing + ")" ; // mark done task with X

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Not readable code. Can try refactoring.

Comment thread src/main/java/Duke.java Outdated
int arrayIndex = 0;
while (run) {
Scanner sc = new Scanner(System.in);
String anyString = sc.nextLine();

@suenalaba suenalaba Feb 4, 2022

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Inappropriate naming. Should name sc to something more appropriate

Comment thread src/main/java/Task.java
Comment on lines +10 to +13
public String toString() {
return "[T]" + (isDone ? "[X]"+ this.description : "[ ]"+ this.description); // mark done task with X
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Should comment above similar to an indentation.

@thernturnaround thernturnaround left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code looks mostly fine, but adding javadocs would make the code more readable and remove superfluous comments.

Comment thread src/main/java/Duke.java Outdated
+ "| |_| | |_| | < __/\n"
+ "|____/ \\__,_|_|\\_\\___|\n";
System.out.println("Hello from\n" + logo);
System.out.println("---------------------");

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Extra space on indentation

Comment thread src/main/java/Task.java Outdated
}

public String toString() {
return "[T]" + (isDone ? "[X]"+ this.description : "[ ]"+ this.description); // mark done task with X

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Could add new lines to improve readability, and comment could be incorporated as part of javadocs

Suggested change
return "[T]" + (isDone ? "[X]"+ this.description : "[ ]"+ this.description); // mark done task with X
/*
* @return (what it returns)
*/
return "[T]"
+ ( (isDone ? "[X]"+ this.description) : "[ ]"+ this.description);

Comment thread src/main/java/Duke.java Outdated
arrayIndex++;
}

else if (anyString.startsWith("deadline")){

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Whitespace consistency

Suggested change
else if (anyString.startsWith("deadline")){
else if (anyString.startsWith("deadline")) {

Comment thread src/main/java/Duke.java Outdated
arrayIndex++;
}

else if (anyString.startsWith("event")){

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Whitespace consistency

Suggested change
else if (anyString.startsWith("event")){
else if (anyString.startsWith("event")) {

Comment thread src/main/java/Duke.java Outdated
System.out.println("Bye! Hope to see you again.");
run = false;
}
else if (anyString.matches("mark \\d+")){

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Whitespace consistency

Suggested change
else if (anyString.matches("mark \\d+")){
else if (anyString.matches("mark \\d+")) {

Comment thread src/main/java/Event.java Outdated

@Override
public String toString() {
return "[E]" + (super.isDone ? "[X]" + super.description : "[ ]"+ super.description) + "(" + timing + ")" ; // mark done task with X

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Could add new lines to improve readability, and comment could be incorporated as part of javadocs

Suggested change
return "[E]" + (super.isDone ? "[X]" + super.description : "[ ]"+ super.description) + "(" + timing + ")" ; // mark done task with X
/*
* @return (what it returns)
*/
return "[E]"
+ (super.isDone ? "[X]" + super.description : "[ ]"+ super.description)
+ "(" + timing + ")" ;

Comment thread src/main/java/Deadline.java Outdated
@Override
public String toString() {
// return "[D]" + super.toString() + "(" + by + ")";
return "[D]" + (super.isDone ? "[X]" + super.description : "[ ]"+ super.description) + "(" + by + ")" ; // mark done task with X

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Could add new lines to improve readability, and comment could be incorporated as part of javadocs.
Could also clean up line 13

Suggested change
return "[D]" + (super.isDone ? "[X]" + super.description : "[ ]"+ super.description) + "(" + by + ")" ; // mark done task with X
/*
* @return (what it returns)
*/
return "[D]"
+ (super.isDone ? "[X]" + super.description : "[ ]"+ super.description)
+ "(" + by + ")" ;

@xseh xseh left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Take note of the coding standards, naming convention and improve readability by refactoring and avoiding magic literals.

Comment thread src/main/java/duke/Duke.java Outdated
Comment on lines +8 to +272
public class Duke {
public static void main(String[] args) throws IndexOutOfBoundsException{
System.out.println("---------------------");
System.out.println(("Hello! I'm Duke"));
System.out.println(("What can i do for you?"));
System.out.println("---------------------");

ArrayList<Task> taskList = new ArrayList<Task>();
//file organization
try {
File myTasks = new File("tasks.txt");
if (myTasks.createNewFile()){
System.out.println("File is created!");
}else{
System.out.println("File exists, reading it to taskList!");
Scanner s = new Scanner(myTasks); // create a Scanner using the File as the source
while (s.hasNext()){
String nextString = s.nextLine();
String[] splitString = nextString.split("\\|");
String typeTask = splitString[0].replaceAll(" ","");
String marked = splitString[1].replaceAll(" ","");
String desc = splitString[2];
System.out.println(typeTask);
System.out.println(marked);
System.out.println(desc);
String todo = "T";
String deadline = "D";
String event = "E";
String toMark = "1";
if (typeTask.equals(todo)) {
taskList.add(new Todo(desc));
if (marked.equals(toMark)) {
taskList.get(taskList.size()-1).markTask();
}
}

else if (typeTask.equals(deadline)) {
String deadlineDate = splitString[3];
taskList.add(new Deadline(desc,deadlineDate));
if (marked.equals(toMark)) {
taskList.get(taskList.size()-1).markTask();
}
}

else if (typeTask.equals(event)) {
String eventDate = splitString[3];
taskList.add(new Event(desc,eventDate));
if (marked.equals(toMark)) {
taskList.get(taskList.size()-1).markTask();
}
}
}
s.close();
}

} catch (IOException e) {
e.printStackTrace();
}

String filePath = "tasks.txt";
boolean continueProgram = true;

//using Arraylist instead.
boolean continueProgram = true;

DukeException dukeEx = new DukeException();
while (continueProgram) {
Scanner sc = new Scanner(System.in);
String inputString = sc.nextLine();
if (inputString.equals("bye")) {
System.out.println("Bye! Hope to see you again.");
continueProgram = false;
}


//deleting task
else if (inputString.matches("delete \\d+")){
try {
String[] arrOfStr = inputString.split(" ", 0);
String indexValue = arrOfStr[1];
int indexValue2 = Integer.parseInt(indexValue) - 1;
if (indexValue2 > taskList.size()-1 || indexValue2 < 0){
throw new IndexOutOfBoundsException();
}

Task removedTask = taskList.get(indexValue2);
taskList.remove(indexValue2);
System.out.println("Noted. I've removed this task:");
System.out.println(removedTask.toString());
System.out.println("Now you have " + taskList.size() + " tasks in the list.");

updateFile(filePath,taskList);


}

catch (IndexOutOfBoundsException e){
dukeEx.wrongIndex();
}
}

//marking a task by index
else if (inputString.matches("mark \\d+")){

try {
String[] arrOfStr = inputString.split(" ", 0);
String indexValue = arrOfStr[1];
int indexValue2 = Integer.parseInt(indexValue) - 1;
//index given is out of array size
if (indexValue2 > taskList.size()-1 || indexValue2 < 0){
throw new IndexOutOfBoundsException();
}


taskList.get(indexValue2).markTask();
updateFile(filePath,taskList);
}

catch (IndexOutOfBoundsException e){
dukeEx.wrongIndex();
}
}

//unmarking a task by index
else if (inputString.matches("unmark \\d+")){

try {
String[] arrOfStr = inputString.split(" ", 0);
String indexValue = arrOfStr[1];
int indexValue2 = Integer.parseInt(indexValue) - 1;
if (indexValue2 > taskList.size()-1 || indexValue2 < 0){
throw new IndexOutOfBoundsException();
}
taskList.get(indexValue2).unmarkTask();
updateFile(filePath,taskList);

}

catch (IndexOutOfBoundsException e){
dukeEx.wrongIndex();
}
}

else if (inputString.startsWith("todo")) {

try {
String desc = inputString.substring(5);
if (desc.isEmpty() || desc.isBlank()) {
throw new IndexOutOfBoundsException();
}
System.out.println("Got it. I've added this task:");


taskList.add(new Todo(desc));
System.out.println(taskList.get(taskList.size()-1).toString());
System.out.println("Now you have " + taskList.size() + " tasks in the list.");
String textToAdd = "T" + "|" + " 0 " + "|" + desc;
try {
appendData(filePath,textToAdd);
}
catch (IOException e) {
System.out.println("Failed to write event data to text file.");
}

}

catch (IndexOutOfBoundsException e){
dukeEx.missingDescription("todo");
}
}

else if (inputString.startsWith("deadline")){

try {

int index = inputString.indexOf("/");
String desc = inputString.substring(9,index);
if (desc.isEmpty() || desc.isBlank()) {
throw new IndexOutOfBoundsException();
}
index++;
String by = inputString.substring(index);
if (by.isEmpty() || by.isBlank()) {
throw new IndexOutOfBoundsException();
}
// taskList[arrayIndex] = new Deadline(desc,by);
taskList.add(new Deadline(desc,by));
System.out.println("Got it. I've added this task:");

System.out.println(taskList.get(taskList.size()-1).toString());
System.out.println("Now you have " + taskList.size() + " tasks in the list.");
String textToAdd = "D" + "|" + " 0 " + "|" + desc + "|" + by;
try {
appendData(filePath,textToAdd);
}
catch (IOException e) {
System.out.println("Failed to write deadline data to text file.");
}

}

catch (IndexOutOfBoundsException e){
dukeEx.missingDescription("deadline");
}

}

else if (inputString.startsWith("event")){

try {

int index = inputString.indexOf("/");
String desc = inputString.substring(6,index);
if (desc.isEmpty() || desc.isBlank()) {
throw new IndexOutOfBoundsException();
}
index++;
String by = inputString.substring(index);
if (by.isEmpty() || by.isBlank()) {
throw new IndexOutOfBoundsException();
}
// taskList[arrayIndex] = new Event(desc,by);
taskList.add(new Event(desc,by));
System.out.println("Got it. I've added this task:");
System.out.println(taskList.get(taskList.size()-1).toString());
System.out.println("Now you have " + taskList.size() + " tasks in the list.");

String textToAdd = "E" + "|" + " 0 " + "|" + desc + "|" + by;
try {
appendData(filePath,textToAdd);
}
catch (IOException e) {
System.out.println("Failed to write event data to text file.");
}
}

catch (IndexOutOfBoundsException e){
dukeEx.missingDescription("event");
}




}

else if (inputString.equals("list")){
System.out.println("Here are the tasks in your list : ");
for (int i = 0; i<taskList.size(); i++) {
System.out.println(i+1 + "." + taskList.get(i).toString());
}
}

else {
System.out.println("☹ OOPS!!! I'm sorry, but I don't know what that means :-(");
continue;
}




}



}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Avoid long methods (> 30 LOCs) to improve code readability.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Avoid deep nesting (> 3 levels of indentation) in the code to improve readability.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Use more SLAP

Comment thread src/main/java/duke/Duke.java Outdated
Comment on lines +10 to +13
System.out.println("---------------------");
System.out.println(("Hello! I'm Duke"));
System.out.println(("What can i do for you?"));
System.out.println("---------------------");

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Avoid magic literals for improved readability. For example, the string "---------------------" can be stored in the variable MESSAGE_BORDER.

Comment thread src/main/java/duke/Duke.java Outdated
File myTasks = new File("tasks.txt");
if (myTasks.createNewFile()){
System.out.println("File is created!");
}else{

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

According to the Java coding standard, there should be blank spaces between }else{.

Comment thread src/main/java/duke/Duke.java Outdated
}
}

else if (typeTask.equals(deadline)) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

According to the Java coding standard, if-else statement should be in the form:

if (condition) {
    statements;
} else if (condition) {
    statements;
} else {
    statements;
}

Comment thread src/main/java/duke/Duke.java Outdated
}

else if (typeTask.equals(deadline)) {
String deadlineDate = splitString[3];

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Avoid magic indexes for better readability. For example, splitString[3] can be changed to splitString[DATE_INDEX].

Comment thread src/main/java/duke/Duke.java Outdated
}


taskList.get(indexValue2).markTask();

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Consider using a more descriptive name to improve code readability.

Comment thread src/main/java/duke/Duke.java Outdated
if (by.isEmpty() || by.isBlank()) {
throw new IndexOutOfBoundsException();
}
// taskList[arrayIndex] = new Deadline(desc,by);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Consider removing dead code

Comment thread src/main/java/duke/Duke.java Outdated
throw new IndexOutOfBoundsException();
}
// taskList[arrayIndex] = new Deadline(desc,by);
taskList.add(new Deadline(desc,by));

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

According to the Java coding standards, there should be a white space between the variables desc,by

Comment thread src/main/java/duke/Duke.java Outdated

else if (inputString.equals("list")){
System.out.println("Here are the tasks in your list : ");
for (int i = 0; i<taskList.size(); i++) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

According to the Java coding standards, the loop structure should resemble this (take note of the white spaces)

for (i = 0; i < 100; i++) {
    sum += value[i];
}

Comment thread src/main/java/duke/Duke.java Outdated
throw new IndexOutOfBoundsException();
}
taskList.get(indexValue2).unmarkTask();
updateFile(filePath,taskList);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

According to the Java coding standards,

plural form should be used on names representing a collection of objects

chintaiann and others added 4 commits February 24, 2022 21:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants