Skip to content

[Shulong] iP#467

Open
DreamerDragon wants to merge 48 commits into
nus-cs2103-AY2021S1:masterfrom
DreamerDragon:master
Open

[Shulong] iP#467
DreamerDragon wants to merge 48 commits into
nus-cs2103-AY2021S1:masterfrom
DreamerDragon:master

Conversation

@DreamerDragon
Copy link
Copy Markdown

Working in process

Copy link
Copy Markdown

@dhafinrazaq dhafinrazaq left a comment

Choose a reason for hiding this comment

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

Hi, those are my PR review (on coding standard)!
If I mentioned "Reference", it means it was taken from the Java coding standard from CS2103T website.

Comment thread src/main/java/Duke.java Outdated
int location = 0;
boolean exist = false;
for (int i = 0; i < string.length(); i++){
if(string.charAt(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.

Should there be whitespace after "if"? I noticed the same issue in several other places too.
Reference: "Java reserved words should be followed by a white space." from CS2103 Java coding standard web page

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks Dahfin, I will change that!

Comment thread src/main/java/Duke.java Outdated
// Find out the position of the first '/'
public int firstSlashTracker (String string){
int location = 0;
boolean exist = false;
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 the name of the boolean be "isExist"? I also noticed the same issue in other boolean variables too.
Reference: "Boolean variables/methods should be named to sound like booleans"

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

good point!

Comment thread src/main/java/Duke.java Outdated
@@ -1,10 +1,268 @@
import java.util.ArrayList;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hi, should there be a package statement above this line?
Reference: "Every class should be part of some package."

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

so how does your package look like?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

it actually depends on how the directory in your project looks like. one of my package statements is "package command"

Comment thread src/main/java/Duke.java Outdated
System.out.println("oops, the deadline description cannot be empty");
}else {
int number = firstSlashTracker(order);
Task item = new Task(1, 0,order.substring(8, number) + "(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.

Should the plus sign at the end of the line be placed below instead (i.e. after the break)?
Reference: "Break before an operator."

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

ohh I will take note !

Comment thread src/main/java/Task.java Outdated
@@ -0,0 +1,10 @@
public class Task {
public int status;
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 this be declared as non-public? I also see the same issue on some other variables.
Reference: "Class variables should never be declared public unless the class is a data class with no behavior."

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

behaviour here means methods?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I'm not 100% sure, but I think it means methods

Copy link
Copy Markdown

@yuxuanxc yuxuanxc left a comment

Choose a reason for hiding this comment

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

The code seems to be working fine and satisfies the requirements of the iP. The quality of the code in terms of readability could be improved on, and try to keep your if/else and try/catch blocks as concise as possible. Jiayou for the rest of your iP!

Comment thread src/main/java/Duke.java Outdated
Comment on lines +42 to +48
var correct = true;
var deleted = false;
var done = order.length() >= 4 && order.substring(0 , 4).equals("done");
var delete = order.length() >= 6 && order.substring(0 , 6).equals("delete");
var todo = order.length() >= 4 && order.substring(0 , 4).equals("todo");
var deadline = order.length() >= 8 && order.substring(0, 8).equals("deadline");
var event = order.length() >= 5 && order.substring(0, 5).equals("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.

Maybe you can consider specifying the data type of your variables instead of just using 'var' for all of them? I'm not sure about the technical benefits of using int, boolean, String etc but it would definitely improve the readability of your code which will be helpful for your tP! Also, it seems like all of them are boolean values so you could consider naming them isCorrect, isDeleted, isInputDone etc to follow the coding standard.

Comment thread src/main/java/Duke.java Outdated
Comment on lines +44 to +48
var done = order.length() >= 4 && order.substring(0 , 4).equals("done");
var delete = order.length() >= 6 && order.substring(0 , 6).equals("delete");
var todo = order.length() >= 4 && order.substring(0 , 4).equals("todo");
var deadline = order.length() >= 8 && order.substring(0, 8).equals("deadline");
var event = order.length() >= 5 && order.substring(0, 5).equals("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.

Not sure if you already know but order.substring(0 , 4).equals("done") could be replaced with order.startsWith("done"), just to improve readability but you will get the same result either way.

Comment thread src/main/java/Duke.java Outdated
Comment on lines +68 to +88
if(orders.get(i).status == 0) {
if(orders.get(i).category == 0){
System.out.println(i + 1 + ". " + " [T][x] "+ orders.get(i).command);
}else if(orders.get(i).category == 1){
System.out.println(i + 1 + ". " + " [D][x] " + orders.get(i).command);
}else if(orders.get(i).category == 2){
System.out.println(i + 1 + ". " + " [E][x] " + orders.get(i).command);
}else {
System.out.println(i + 1 + ". " + " [x] " + orders.get(i).command);
}
}else {
if(orders.get(i).category == 0){
System.out.println(i + 1 + ". " + " [T][v] "+ orders.get(i).command);
}else if(orders.get(i).category == 1){
System.out.println(i + 1 + ". " + " [D][v] " + orders.get(i).command);
}else if(orders.get(i).category == 2){
System.out.println(i + 1 + ". " + " [E][v] " + orders.get(i).command);
}else {
System.out.println(i + 1 + ". " + " [v] " + orders.get(i).command);
}
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

One possible way to make the printing of the tasks out neater could be by overriding the toString method of your Task class so that you can just call System.out.println(i + 1 + ". " + orders.get(i)) instead. It is the same logic as what you are doing here, just that the checking of status and category is done inside the Task class and this will help with abstraction.

Comment thread src/main/java/Duke.java Outdated
Comment on lines +154 to +186
if(correct && !done && !deleted && !order.equals("task list")) {
if(todo){
if(order.length() <= 4){
System.out.println("oops, the todo description cannot be empty");
}else {
Task item = new Task(0, 0, order.substring(4) );
orders.add(item);
}
}else if(deadline){
if(order.length() <= 8){
System.out.println("oops, the deadline description cannot be empty");
}else {
int number = firstSlashTracker(order);
Task item = new Task(1, 0,order.substring(8, number) + "(by: " +
order.substring(number + 3) + ")");
orders.add(item);
}
}else if(event){
if(order.length() <= 5){
System.out.println("oops, the event description cannot be empty");
}else {
int number = firstSlashTracker(order);
Task item = new Task(2, 0, order.substring(5, number) + "(at: " +
order.substring(number + 3) + ")");
orders.add(item);
}

}else {
Task item = new Task(4, 0, order);
orders.add(item);
}

}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I'm not sure why you decided to make another if block to check for todo, event, and deadline since you already checked for the booleans at lines 117, 126 and 137. Would combining the bodies of the 2 if blocks of todo change the output in any way?

Comment thread src/main/java/Task.java Outdated
@@ -0,0 +1,10 @@
public class Task {
public int status;
Copy link
Copy Markdown

@yuxuanxc yuxuanxc Sep 1, 2020

Choose a reason for hiding this comment

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

Does the status(0, 1) refer to whether or not the task is done? If that's the case, changing it to a boolean(eg. isDone) will be helpful in terms of readability. It might reduce confusion as your teammates will not have to guess which variable does what when they read your code for tP/ other group projects in the future.

Comment thread src/main/java/Task.java Outdated
public String command;
Task(int category, int status, String command){
this.category = category;
this.status = status;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The status of the task is always set to 0 when you initialize a new Task object, so the constructor may not have to take in a parameter for status and this.status = 0; could be used instead?

Comment thread src/main/java/Duke.java Outdated
Comment on lines +197 to +252
try {
File myFile = new File(filePath);
Scanner myReader = new Scanner(myFile);
while (myReader.hasNextLine()) {
String data = myReader.nextLine();
System.out.println(data);
}
myReader.close();
} catch (FileNotFoundException e) {
System.out.println("Creating the file to save");
try {
String folderPath = "./data";
Path path = Paths.get(folderPath);

Files.createDirectories(path);

System.out.println("Directory is created!");

//Files.createDirectory(path);

} catch (IOException error) {
System.err.println("Failed to create directory!" + error.getMessage());
}
try {
File fileCreator = new File(filePath);
if (fileCreator.createNewFile()) {
System.out.println("File created: " + fileCreator.getName());
} else {
System.out.println("File already exists.");
}
} catch (IOException ex) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
try {
File myFile = new File(filePath);
Scanner myReader = new Scanner(myFile);
while (myReader.hasNextLine()) {
String data = myReader.nextLine();
System.out.println(data);
}
myReader.close();
} catch (FileNotFoundException e) {
System.out.println("Creating the file to save");
}

try {
FileWriter myWriter = new FileWriter(filePath);
//myWriter.write("Files in Java might be tricky, but it is fun enough!");
myWriter.close();
System.out.println("All changes successfully saved to the storage!");
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I'm sorry but I don't really understand this part. Would it be possible to combine all the try blocks together into one and have multiple catch blocks for different errors instead?

Copy link
Copy Markdown

@chuyouchia chuyouchia left a comment

Choose a reason for hiding this comment

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

In general, the code has some further progress to make. I think you would be on the right track after trying to refactor the code as mentioned.

Potentially, you could implement an iterative approach to this. Instead of trying to solve all the problems at once, try to categorize the issues into UI related, encapsulation or abstraction issues. Try solving them one by one and it might be easier to implement in that respect since many other reviewers are also leaving their comments. Hope that helps you!

Comment thread src/main/java/Duke.java Outdated
break;
}
}
if(exist) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

There are some recurring inconsistencies with usage of white space. It might be good to review to keep it consistent with the rest of the code base.

for example: if (exist) would better adhere to coding styles than if(exist)

Comment thread src/main/java/Duke.java Outdated
// A-trial run
// program to be added
// Find out the position of the first '/'
public int firstSlashTracker (String string){
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Names representing methods must be verbs and written in camelCase. For example, getName(), computeTotalWidth()

So, if the intention is for you to get the value of first slash tracker, the method name could be getFirstSlashTracker instead

Comment thread src/main/java/Duke.java Outdated
Comment on lines +142 to +143
System.out.println(" Yup, this is now added! " + " [E][x] " + order.substring(5, number) + "(at: " +
order.substring(number + 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.

It seems like you are making a line break in order to follow the rule of line length not exceeding 120 and that is great!

However, line breaks should be done before an operator. One possible way could be to change this line to the following format :
System.out.println(" Yup, this is now added! " + " [E][x] "

  • order.substring(5, number) + "(at: "
  • order.substring(number + 3) + ")");

the operator should be a part of the new line when wrapping lines. Hope this example clarifies 👍

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Wow! Thank you for pointing it!!

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