-
Notifications
You must be signed in to change notification settings - Fork 193
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
[Xue Yushan] iP #197
base: master
Are you sure you want to change the base?
[Xue Yushan] iP #197
Conversation
src/main/java/list.java
Outdated
public class List { | ||
private static String[] currentList = new String[100]; | ||
public static int listSize = 0; | ||
private static int[] isDone = new int[100]; |
There was a problem hiding this comment.
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() { |
There was a problem hiding this comment.
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
There was a problem hiding this 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
src/main/java/Duke.java
Outdated
+ "____________________________________________________________\n"); | ||
break; | ||
} | ||
else if (str.equals("list")) { |
There was a problem hiding this comment.
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
src/main/java/Duke.java
Outdated
@@ -1,3 +1,6 @@ | |||
import java.util.Scanner; | |||
import java.lang.String; | |||
|
|||
public class Duke { | |||
public static void main(String[] args) { |
There was a problem hiding this comment.
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]; |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
src/main/java/Duke.java
Outdated
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("____________________________________________________________"); | ||
} |
There was a problem hiding this comment.
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."); |
There was a problem hiding this comment.
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.
System.out.println("The list is empty."); | |
System.out.println("The list is empty."); | |
return; |
} | ||
} | ||
|
||
public static void mark(int number) { |
There was a problem hiding this comment.
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
src/main/java/Duke.java
Outdated
else if (str.equals("list")) { | ||
System.out.println("____________________________________________________________\n" | ||
+ "Here are the tasks in your list: "); | ||
List.printItems(); | ||
System.out.println("____________________________________________________________"); | ||
} |
There was a problem hiding this comment.
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 {
src/main/java/Duke.java
Outdated
Scanner input = new Scanner(System.in); | ||
while (true){ | ||
while (true) { |
There was a problem hiding this comment.
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
There was a problem hiding this 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.
src/main/java/Duke.java
Outdated
greeting(); | ||
ArrayList<Task> tasks = getDatafromFile("tasks"); | ||
|
||
if (tasks.size() == 0) { |
There was a problem hiding this comment.
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()) {
}
src/main/java/Duke.java
Outdated
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); | ||
} |
There was a problem hiding this comment.
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
src/main/java/Duke.java
Outdated
System.out.println(line | ||
+ d.getMessage() + "\n" + line); | ||
} | ||
} else if (inputString.contains("done") == true) { |
There was a problem hiding this comment.
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")) {
}
src/main/java/Duke.java
Outdated
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; | ||
} |
There was a problem hiding this comment.
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]; |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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"; |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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
No description provided.