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

[Cui Minjing] iP #13

Open
wants to merge 59 commits into
base: master
Choose a base branch
from
Open

Conversation

Cuiminjing
Copy link

No description provided.

@Cuiminjing Cuiminjing changed the title improve code [Cui Minjing]ip Aug 27, 2021
@Cuiminjing Cuiminjing changed the title [Cui Minjing]ip [Cui Minjing] iP Aug 27, 2021
public class Duke {
public static void main(String[] args) {

public static final String Line = " ____________________________________________________________";

Choose a reason for hiding this comment

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

Please refactor this attribute name as fully capitalized. All constants' names should be capitalized.

@@ -0,0 +1,21 @@
public class Task {
protected String content;
protected boolean Done;

Choose a reason for hiding this comment

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

Done could be renamed to sound like a boolean. For example, isDone

String[] number = command.split(" ");
int taskIndex = Integer.parseInt(number[1]) - 1;
tasks[taskIndex].complete();
System.out.println(Line + "\n" + " Nice! I've marked this task as done:" + "\n " + tasks[taskIndex].toString() + "\n" + Line);

Choose a reason for hiding this comment

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

Please make this line shorter to improve readability. The same can be done for other long lines in this method.

System.out.println(" What can I do for you?");
System.out.println(Line);
}
public static void main(String[] args) {

Choose a reason for hiding this comment

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

The implementation for this method is quite long. Perhaps it could be refactored further? For example, the handling of each task (todo / deadline / event) could be made different methods. You may also consider making the "happy path" more prominent.

Comment on lines 13 to 15
for(int i=0; i<taskSum; i++){
now = tasks[i];
System.out.println(" " + (i+1) + "." + now.toString());

Choose a reason for hiding this comment

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

You might like to insert spaces before and after arithmetic operators too improve readability

break;
case "bye":
break;
default:

Choose a reason for hiding this comment

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

In the default case, you could either use an explicit //Fallthrough comment or include a break statement for consistent style

@giterator
Copy link

Apart from refactoring the main() method in Duke.java, just a few nits to fix.

Copy link

@Rakesh12000 Rakesh12000 left a comment

Choose a reason for hiding this comment

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

First PR done.

@@ -2,24 +2,24 @@

public class Duke {

public static final String Line = " ____________________________________________________________";
private static Task[] tasks = new Task[110];
private static int taskSum = 0;

public static void printList() {
Task now;

Choose a reason for hiding this comment

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

Could use a more appropriate variable name like task instead of now

int first = command.indexOf(" ");
String item = command.substring(first,command.length());
tasks[taskSum] = new Todo(item);
taskSum = taskSum + 1;

Choose a reason for hiding this comment

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

Could be simplified to taskSum++;

}
System.out.println("____________________________________________________________");
System.out.println(Line);
}
public static void bye() {

Choose a reason for hiding this comment

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

Correct use of casing for method bye

@@ -14,4 +14,8 @@ public void complete(){
public String getInformation() {

Choose a reason for hiding this comment

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

Correct use of camelCase.

Copy link

@xseh xseh left a comment

Choose a reason for hiding this comment

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

Overall good job! Please consider using more SLAP to make the code better to understand and simply some of the logic (Practice KISSing) if possible.

Task now;
System.out.println(Line);
System.out.println(" Here are the tasks in your list:");
for(int i=0; i<taskSum; i++){
Copy link

Choose a reason for hiding this comment

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

There should be a spacing between the brackets ( and { in the for statement. The same concept applies to the rest of the code.

Comment on lines 64 to 148
if(command.contains("done")){
String[] number = command.split(" ");
int taskIndex = Integer.parseInt(number[1]) - 1;
tasks.get(taskIndex).complete();
System.out.println(Line + "\n" + " Nice! I've marked this task as done:" + "\n " + tasks.get(taskIndex).toString() + "\n" + Line);
}
else if(command.contains("delete")){
String[] number = command.split(" ");
int taskIndex = Integer.parseInt(number[1]) - 1;
Task temp = tasks.get(taskIndex);
System.out.println(Line + "\n" + " Noted. I've removed this task: " + "\n " + tasks.get(taskIndex).toString() + "\n" + " Now you have " + taskSum + " tasks in the list");
System.out.println(Line);
tasks.remove(taskIndex);
taskSum--;
}
else if(command.contains("todo")){
int first = command.indexOf(" ");
int check = command.indexOf("d");
if(check == command.length()-2){
System.out.println(Line);
System.out.println(" ☹ OOPS!!! The description of a todo cannot be empty.");
System.out.println(Line);
continue;
}
String item = command.substring(first,command.length());
Task temp = new Todo(item);
tasks.add(temp);
taskSum++;
System.out.println(Line + "\n" + " Got it. I've added this task: " + "\n" + " [T][ ] " + item + "\n" + " Now you have " + taskSum + " tasks in the list");
System.out.println(Line);

}
else if(command.contains("deadline")){
int first = command.indexOf(" ");
int check = command.indexOf("deadline");
if(check == command.length()-8){
System.out.println(Line);
System.out.println(" ☹ OOPS!!! The description of a deadline cannot be empty.");
System.out.println(Line);
continue;
}
int itemEnd = command.indexOf("/");
String item = command.substring(first,itemEnd);
String by = command.substring(itemEnd + 1,command.length());
Task temp = new Deadline(item,by);
tasks.add(temp);
taskSum++;
System.out.println(Line + "\n" + " Got it. I've added this task: " + "\n" + " [D][ ] " + item + " (" + by + ")" + "\n" + " Now you have " + taskSum + " tasks in the list");
System.out.println(Line);
}
else if(command.contains("event")){
int first = command.indexOf(" ");
int check = command.indexOf("event");
if(check == command.length()-5){
System.out.println(Line);
System.out.println(" ☹ OOPS!!! The description of a event cannot be empty.");
System.out.println(Line);
continue;
}
int itemEnd = command.indexOf("/");
String item = command.substring(first,itemEnd);
String at = command.substring(itemEnd + 1,command.length());
Task temp = new Event(item,at);
tasks.add(temp);
taskSum++;
System.out.println(Line + "\n" + " Got it. I've added this task: " + "\n" + " [E][ ] " + item + " (" + at + ")" + "\n" + " Now you have " + taskSum + " tasks in the list");
System.out.println(Line);
}
else{
switch (command){
case "list":
printList();
break;
case "bye":
break;
default:
System.out.println(Line);
System.out.println(" ☹ OOPS!!! I'm sorry, but I don't know what that means :-(");
System.out.println(Line);
}
}
}while(!command.equals("bye"));
bye();
}
}
Copy link

Choose a reason for hiding this comment

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

The logic can be further broken down into simpler function, therefore, consider using more SLAP.

Can consider using the switch (instead of if-else) statement too to simplify the code.

Function is too bulky and long (more than 30 LOC), consider breaking it down into smaller parts.

Can consider making the happy path prominent (i.e. make the successful execution path obvious).


public Todo(String content) {
super(content);

Copy link

Choose a reason for hiding this comment

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

Remove unnecessary whitespace within the code

greeting();
String command;
Scanner in = new Scanner(System.in);
String f = "data/lines.txt";
Copy link

Choose a reason for hiding this comment

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

If the string is a constant, the variable name must be all uppercase (and underscore to separate the words) instead. A better variable name would also make understanding the code easier.

try {
writeToFile(f);
} catch (IOException e) {
System.out.println("Something went wrong: " + e.getMessage());
Copy link

Choose a reason for hiding this comment

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

Avoid the use of magic strings. In this case, the error message can be extracted into a constant variable with a suitable name.

}
else if(command.contains("delete")){
String[] number = command.split(" ");
int taskIndex = Integer.parseInt(number[1]) - 1;
Copy link

Choose a reason for hiding this comment

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

Avoid the use of magic literals. Using a suitable variable name for the constant numbers (which describes the value) would help the understanding of the program.

Comment on lines 97 to 98
int first = command.indexOf(" ");
int check = command.indexOf("deadline");
Copy link

Choose a reason for hiding this comment

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

Consider using a better variable name that is more descriptive. e.g. indexOfDeadline

}
else{
switch (command){
case "list":
Copy link

Choose a reason for hiding this comment

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

Case/default clauses must be on the same indentation as the switch statement.

System.out.println(Line);
}
}
}while(!command.equals("bye"));
Copy link

Choose a reason for hiding this comment

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

There should be a spacing between the bracket } and while clause

Task temp = new Todo(item);
tasks.add(temp);
taskSum++;
System.out.println(Line + "\n" + " Got it. I've added this task: " + "\n" + " [T][ ] " + item + "\n" + " Now you have " + taskSum + " tasks in the list");
Copy link

Choose a reason for hiding this comment

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

Line is too long. Consider separating the line into multiple lines.

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