-
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
Changes from 5 commits
85d1982
cac3d8e
1fd8497
6466eb7
6141721
babe161
d198914
1a6a0ac
9f131cc
7cac7da
6be3ec0
5aa495e
ecee40d
1b59f78
8cfb82a
e060cf4
0cf473f
b8d0935
d69ddd3
97a99f5
3516ed4
0d92829
6c1b940
27ea764
3a00f4e
329a47e
de1b451
22393ec
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) { | ||
String logo = " ____ _ \n" | ||
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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")) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
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 commentThe 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 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("____________________________________________________________"); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
} else { | ||
List.addToList(str); | ||
} | ||
} | ||
} | ||
} | ||
} |
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]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe 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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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]); | ||
} | ||
} | ||
} | ||
} |
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]; | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. name of this array "int[] isDone" sounds like a boolean name. |
||||||||
|
||||||||
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."); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The if and else makes the happy path not prominent.
Suggested change
|
||||||||
} 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]); | ||||||||
} | ||||||||
} | ||||||||
} | ||||||||
} |
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