Skip to content

[Xue Yushan] iP#197

Open
Mist0919 wants to merge 28 commits intonus-cs2113-AY2122S1:masterfrom
Mist0919:master
Open

[Xue Yushan] iP#197
Mist0919 wants to merge 28 commits intonus-cs2113-AY2122S1:masterfrom
Mist0919:master

Conversation

@Mist0919
Copy link
Copy Markdown

@Mist0919 Mist0919 commented Sep 2, 2021

No description provided.

public class List {
private static String[] currentList = new String[100];
public static int listSize = 0;
private static int[] isDone = new int[100];
Copy link
Copy Markdown

@itsleeqian itsleeqian Sep 2, 2021

Choose a reason for hiding this comment

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

name of this array "int[] isDone" sounds like a boolean name.
maybe change the name to better represent what you are trying to store

isDone[number-1] = 1;
}

public static void markedItems() {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

name of this function "markedItems()" sounds like a list. maybe should make it sound like a verb instead

Copy link
Copy Markdown

@theodorekwok theodorekwok 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 following most of the code guidelines. Do consider refactoring the functionalities into methods to reduce the number of conditionals

+ "____________________________________________________________\n");
break;
}
else if (str.equals("list")) {
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 else if should be on line 25, to follow the K&R egyption style bracketing, likewise for line 32.
It was done correctly on line 52

import java.lang.String;

public class Duke {
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.

The main method exceeds 30 lines, consider refactoring the console input and output into separate methods

public class List {
private static String[] currentList = new String[100];
public static int listSize = 0;
private static int[] isDone = new int[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.

The value 100 is a magic number, consider extracting it as a constant variable

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Agreed! Also make sure the constants are named using proper naming conventions.

Comment on lines +33 to +51
String nums = "";
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (Character.isDigit(c) == true) {
nums += c;
}
}
int index = Integer.parseInt(nums);
if (index > List.listSize) {
System.out.println("____________________________________________________________\n"
+ "The index is out of range.\n"
+ "____________________________________________________________\n");
} else {
List.mark(index);
System.out.println("____________________________________________________________\n"
+ "Nice! I've marked this task as done: ");
List.markedItems();
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.

The abstraction levels here are different violating the single level of abstraction principle (SLAP) from the for loop in line 34 to 39 and the method calling in line 46 and 49 respectively.
Consider abstracting the for loop in line 34 to 39 as a separate method that is responsible for the console input.


public static void printItems() {
if (listSize == 0) {
System.out.println("The list is empty.");
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 if and else makes the happy path not prominent.

Suggested change
System.out.println("The list is empty.");
System.out.println("The list is empty.");
return;

}
}

public static void mark(int number) {
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 renaming this method to make it more clear on what it does. For eg setTaskAsCompleted

+ "Here are the tasks in your list: ");
List.printItems();
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.

The bracket for the if-else statement should be on the same line as else, so the line should be :
} else {

+ "____________________________________________________________\n");
Scanner input = new Scanner(System.in);
while (true){
while (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.

I like that you added the spacing after for the while loop and the { bracket

Copy link
Copy Markdown

@iamabhishek98 iamabhishek98 left a comment

Choose a reason for hiding this comment

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

I have added my suggestions. Please note that although I have not repeated myself, what I have commented in one place applies to the rest of the codebase as well.

greeting();
ArrayList<Task> tasks = getDatafromFile("tasks");

if (tasks.size() == 0) {
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 also do something like:

if (!tasks.size()) {
}

Comment on lines +38 to +95
try {
printList(tasks);
} catch (DukeException d) {
System.out.println(line
+ d.getMessage() + "\n" + line);
}
} else if (inputString.contains("done") == true) {
try {
setDone(inputString, tasks);
} catch (IndexOutOfBoundsException i) {
System.out.println(line
+ "☹ OOPS!!! The index is out of range.\n"
+ line);
} catch (NumberFormatException n) {
System.out.println(line
+ "☹ OOPS!!! The index of done indstruction should be a number.\n"
+ line);
} catch (NullPointerException p) {
System.out.println(line
+ "☹ OOPS!!! The index of done indstruction cannot be empty.\n"
+ line);
}
} else if (inputString.contains("delete") == true) {
try {
deleteTask(inputString, tasks);
} catch (IndexOutOfBoundsException i) {
System.out.println(line
+ "☹ OOPS!!! The index is out of range.\n"
+ line);
} catch (NumberFormatException n) {
System.out.println(line
+ "☹ OOPS!!! The index of delete indstruction should be a number.\n"
+ line);
}
} else if (inputString.contains("clear") == true) {
clearList(tasks);
} else {
try {
addTask(inputString, tasks);
}
catch(IndexOutOfBoundsException i) {
String type = "";
if (inputString.contains("todo")) {
type = "todo";
} else if (inputString.contains("deadline")) {
type = "deadline";
} else if (inputString.contains("event")) {
type = "event";
}
if (type == "") {
System.out.println(line
+ "☹ OOPS!!! I'm sorry, but I don't know what that means :-(\n"
+ line);
} else {
System.out.println(line
+ "☹ OOPS!!! The description of a " + type + " cannot be empty.\n"
+ line);
}
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 using so many nested try catch blocks. Please abstract out the logic into different functions

System.out.println(line
+ d.getMessage() + "\n" + line);
}
} else if (inputString.contains("done") == 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.

There is no need to explicitly check if it is equal to true because the contains method returns a boolean value. You can change this to:

else if (inputString.contains("done")) {
}

Comment on lines +206 to +256
ArrayList<Task> tasks = new ArrayList<Task>();
String path = "C:\\Users\\XUEY0013\\Documents\\ip\\src\\main\\" + fileName+ ".txt";
File file = new File(path);
if (!file.exists()) {
return new ArrayList<Task> ();
}
BufferedReader reader = null;
try {
FileInputStream fileInputStream = new FileInputStream(path);
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
reader = new BufferedReader(inputStreamReader);
String tempString = "";
Task task = new Task("null");

while ((tempString = reader.readLine()) != null) {
if (tempString.charAt(TYPE_POS) == 'T') {
task = (new Todo(tempString.substring(TASK_POS)));
} else if (tempString.charAt(TYPE_POS) == 'E') {
String event = tempString.substring(TASK_POS,
tempString.indexOf("(") - 1);
String at = tempString.substring(tempString.indexOf(":") + 2,
tempString.indexOf(")"));
task = (new Event(event, at));
} else if (tempString.charAt(TYPE_POS) == 'D') {
String deadline = tempString.substring(TASK_POS,
tempString.indexOf("(") - 1);
String by = tempString.substring(tempString.indexOf(":") + 2,
tempString.indexOf(")"));
task = (new Deadline(deadline, by));
}

if (tempString.charAt(7) == 'X') {
task.markedAsDone();
}

tasks.add(task);
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return tasks;
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Having such long functions can be hard to read. Please consider abstracting out repeated code in line with the DRY (Don't Repeat Yourself) principle. Also add comments to improve the readability of your code

public class List {
private static String[] currentList = new String[100];
public static int listSize = 0;
private static int[] isDone = new int[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.

Agreed! Also make sure the constants are named using proper naming conventions.

for (int i = 1; i <= listSize; i++) {
System.out.print(i + ". " );
System.out.print("[");
if (isDone[i-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.

Please maintain spacing between the operators and the operands

}

public final void markedAsDone() {
this.isDone = "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.

As mentioned earlier, avoid using magic strings - please extract them out as constants.

type[listSize] = "T";
} else if (input.contains("deadline")) {
String by = input.substring(input.indexOf("/") + 4);
l[listSize] = input.substring(9, input.indexOf("/")) + " (by: " + 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.

Avoid having complex nested logic like this. Either separate it out into different lines or abstract it out into different functions

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.

5 participants