Skip to content

[Kim Gyurim] iP#188

Open
yok2086 wants to merge 29 commits intonus-cs2113-AY2425S2:masterfrom
yok2086:master
Open

[Kim Gyurim] iP#188
yok2086 wants to merge 29 commits intonus-cs2113-AY2425S2:masterfrom
yok2086:master

Conversation

@yok2086
Copy link
Copy Markdown

@yok2086 yok2086 commented Feb 6, 2025

No description provided.

Copy link
Copy Markdown

@ElonKoh ElonKoh left a comment

Choose a reason for hiding this comment

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

Overall, I found your code easy to read with the correct syntaxes and your variables are named appropriately. I made some minor notes to improve readability, best practice suggestions, and consistency issues. Good work!

Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
int i = 0;
char taskType = input.charAt(input.indexOf("[") + 1);
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 this variable (taskType) be better named to indicate it being a part of the user input?

+ "added: " + input + "\n" + LINE_SEPERATOR);
task[i] = new Task(input);
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.

I like how readable this code logic for your while loop is, abstracting out all the print methods :)

System.out.println(LINE_SEPERATOR + "\n" + "[" + task[index].getStatusIcon() + "] " + task[index].getDescription() + "\n" + LINE_SEPERATOR);
}

public static int getIndex(String marking, String find) {
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 like that you abstracted this bit of code and named your variables well with a verb and camelCase to make the main method really readable

}

public String getStatusIcon() {
return (isDone ? "X" : " "); // 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.

I like how you write comments to explain code 🤌🏻
but it could be better to explain why instead of what the code is doing

import java.util.Scanner;

public class David {
public static final String LINE_SEPERATOR = "____________________________________________________________";
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 like how you named this static final variable in ALL_CAPS


@Override
public String toString() {
return "[" + getStatusIcon() + "]"; // You can customize this string as needed
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

what is the message you intend to convey with this comment?

Copy link
Copy Markdown

@H-ZhanHao H-ZhanHao left a comment

Choose a reason for hiding this comment

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

Overall nice and understandable code! Just some nits to fix to make it even better.

+ "| |_| | |_| | < __/\n"
+ "|____/ \\__,_|_|\\_\\___|\n";
System.out.println("Hello from\n" + logo);
Task[] task = new Task[100];
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Magic number. Perhaps a constant would be more suitable?

System.out.println(printTaskType(task, i));
i++;
} else if (input.startsWith("event")) {
task[i] = new Event((input.substring(getIndex(input, " ") + 1, getIndex(input, "/") - 1)), input.substring(getIndex(input, "from") + 5, getIndex(input, "to") - 2), input.substring(getIndex(input, "to") + 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.

Seems like the string filtering takes up a lot of space. Maybe abstracting it into a separate method would make this more readable.

import java.util.Scanner;

public class David {
public static final String LINE_SEPERATOR = "____________________________________________________________";
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Great idea to make the line a constant.

}
} else if (input.startsWith("mark")) {
markTask(task, Integer.parseInt(input.split(" ")[1]) - 1);
printMark(task, Integer.parseInt(input.split(" ")[1]) - 1);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

markTask and printMark both take the same arguments. Could it be better to combine the two?

Copy link
Copy Markdown

@sevenseasofbri sevenseasofbri left a comment

Choose a reason for hiding this comment

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

Good job so far! You can improve readability by reducing deep indentation and refactoring long methods into smaller ones.


Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
int i = task.size();
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 naming non-loop iterator variables names such as i since this is not inuitive. Rather name this variable something like numTasks

Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
int i = task.size();
char input_type = input.charAt(input.indexOf("[") + 1);
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 the use of magic literals by storing them in variables or consts

public class David {
public static final String LINE_SEPERATOR = "____________________________________________________________";

public static void main(String[] args) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This main function is quite long, >30 LOC. Consider breaking it into parts and put them in different functions.

Comment on lines +30 to +35
if (input.equals("list")) {
System.out.println(LINE_SEPERATOR + "\nHere are the tasks in your list:\n");
for (int j = 0; j < task.size(); j++) {
System.out.println((j + 1) + "." + task.get(j).toString() + "\n" + LINE_SEPERATOR);
}
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This is 4 levels of indentation, so this can be deemed arrowhead code. Try to refactor the inner levels into methods.

Comment on lines +36 to +43
else if (input.startsWith("mark")) {
markTask(task, Integer.parseInt(input.split(" ")[1]) - 1);
printMark(task, Integer.parseInt(input.split(" ")[1]) - 1);
StorageManager.saveTasks(task);
} else if (input.startsWith("unmark")) {
unmarkTask(task, Integer.parseInt(input.split(" ")[1]) - 1);
printUnmark(task, Integer.parseInt(input.split(" ")[1]) - 1);
StorageManager.saveTasks(task);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Good job keeping a consistent level of abstraction here.

Comment on lines +59 to +71

}
else if (input.startsWith("delete")) {
deleteTask(task, Integer.parseInt(input.split(" ")[1]) - 1);
StorageManager.saveTasks(task);
i--;
}

else {
throw new IllegalArgumentException(LINE_SEPERATOR + System.lineSeparator() + " Sorry... I don't know what you mean by that? Could you try again?" + System.lineSeparator() + LINE_SEPERATOR);
}
}
catch (IllegalArgumentException e) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

if-else conditionals should be of the form (layout-wise):

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

Comment on lines +81 to +82
}
private static String printTaskType(ArrayList<Task> task, int 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.

try to have atleast 1 line space between methods.

Comment on lines +22 to +27
static {
File folder = new File("data");
if (!folder.isDirectory() && !folder.mkdirs()) {
System.err.println("Failed to create directory: " + folder.getAbsolutePath());
}
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Since this is a class-level member, this means every instance of StorageManager will access the same data folder. Theoretically speaking, it is ok for this application. But it might make more sense for each instance of this StorageManager to access their own folders. Just a thought, consider looking into the textbook sections on OOP and class-level members.

}

public static ArrayList<Task> loadTasks() {
File file = new File(FILE_PATH); // create a File for the given file path
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

No need to add a comment if it is obvious through the code.

Comment on lines +52 to +58
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(" \\| ");
if (parts.length < 3) {
System.out.println("Skipping invalid task entry: " + line);
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.

this is ~3 levels of indentation, consider refactoring inner indents to another method

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