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 1 commit
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
60 changes: 27 additions & 33 deletions src/main/java/Duke.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
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"
+ "| _ \\ _ _| | _____ \n"
Expand All @@ -12,52 +10,48 @@ public static void main(String[] args) {
+ "|____/ \\__,_|_|\\_\\___|\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");
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){
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");
if (str.equals("bye")) {
System.out.println("____________________________________________________________\n"
+ "Bye. Hope to see you again soon!\n"
+ "____________________________________________________________\n");
break;
}
else if (str.equals("list")){
System.out.println("____________________________________________________________\n" +
"Here are the tasks in your list: ");
list.printItems();
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){
else if (str.contains("Done ") == true) {
String nums = "";
for (int i=0;i<str.length();i++){
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (Character.isDigit(c) == true){
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();
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("____________________________________________________________");
}
}
else {
list.addToList(str);
} 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]);
}
}
}
}