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

[Xue Yushan] iP #197

Open
wants to merge 28 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 48 additions & 1 deletion src/main/java/Duke.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import java.util.Scanner;
import java.lang.String;

public class Duke {
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 main method exceeds 30 lines, consider refactoring the console input and output into separate methods

String logo = " ____ _ \n"
Expand All @@ -6,5 +9,49 @@ public static void main(String[] args) {
+ "| |_| | |_| | < __/\n"
+ "|____/ \\__,_|_|\\_\\___|\n";
System.out.println("Hello from\n" + logo);

System.out.println("____________________________________________________________\n"
+ "Hello! I'm Duke\n"
+ "What can I do for you?\n"
+ "____________________________________________________________\n");
Scanner input = new Scanner(System.in);
while (true) {
Copy link

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

String str = input.nextLine();
if (str.equals("bye")) {
System.out.println("____________________________________________________________\n"
+ "Bye. Hope to see you again soon!\n"
+ "____________________________________________________________\n");
break;
}
else if (str.equals("list")) {

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

System.out.println("____________________________________________________________\n"
+ "Here are the tasks in your list: ");
List.printItems();
System.out.println("____________________________________________________________");
}
Copy link

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 {

else if (str.contains("Done ") == true) {
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("____________________________________________________________");
}

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.

} else {
List.addToList(str);
}
}
}
}
}
42 changes: 42 additions & 0 deletions src/main/java/List.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
public class List {
private static String[] currentList = new String[100];
public static int listSize = 0;
private static int[] isDone = new int[100];

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

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.


public static void addToList(String input) {
currentList[listSize] = input;
listSize++;
System.out.println("____________________________________________________________\n"
+ "Added: " + input + "\n"
+ "____________________________________________________________\n");
}

public static void printItems() {
if (listSize == 0) {
System.out.println("The list is empty.");
} else {
for (int i = 1; i <= listSize; i++) {
System.out.print(i + ". " );
System.out.print("[");
if (isDone[i-1] == 1) {

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

System.out.print("X");
} else {
System.out.print(" ");
}
System.out.print("] " + currentList[i-1] + "\n");
}
}
}

public static void mark(int number) {

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

isDone[number-1] = 1;
}

public static void markedItems() {

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

for (int i = 0; i < isDone.length; i++) {
if (isDone[i] == 1) {
System.out.println("[X] " + currentList[i]);
}
}
}
}
42 changes: 42 additions & 0 deletions src/main/java/list.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
public class List {
private static String[] currentList = new String[100];
public static int listSize = 0;
private static int[] isDone = new int[100];
Copy link

@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


public static void addToList(String input) {
currentList[listSize] = input;
listSize++;
System.out.println("____________________________________________________________\n"
+ "Added: " + input + "\n"
+ "____________________________________________________________\n");
}

public static void printItems() {
if (listSize == 0) {
System.out.println("The list is empty.");

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;

} else {
for (int i = 1; i <= listSize; i++) {
System.out.print(i + ". " );
System.out.print("[");
if (isDone[i-1] == 1) {
System.out.print("X");
} else {
System.out.print(" ");
}
System.out.print("] " + currentList[i-1] + "\n");
}
}
}

public static void mark(int number) {
isDone[number-1] = 1;
}

public static void markedItems() {
for (int i = 0; i < isDone.length; i++) {
if (isDone[i] == 1) {
System.out.println("[X] " + currentList[i]);
}
}
}
}