-
Notifications
You must be signed in to change notification settings - Fork 200
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
[Tze Loong] iP #213
base: master
Are you sure you want to change the base?
[Tze Loong] iP #213
Changes from 6 commits
7558b46
dbc5dac
f808559
50aad95
312117e
98bc638
2306a2a
cb74c50
fe13d52
f5259c3
dc3b488
7b1e715
88c2683
1712749
de06295
3b1d6d4
409496b
8c09680
d44a21d
1e53d33
2b06b30
9a1c515
57076f4
b847776
c8ce553
6df5295
212cf99
7e9c93f
b624d2a
6009396
da04bb5
c7ddfbe
1ad8a59
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,10 +1,77 @@ | ||
import java.util.Scanner; | ||
|
||
public class Duke { | ||
public static void main(String[] args) { | ||
|
||
public static void greet_user() { | ||
System.out.println("Hello! I'm Duke \n"); | ||
String logo = " ____ _ \n" | ||
+ "| _ \\ _ _| | _____ \n" | ||
+ "| | | | | | | |/ / _ \\\n" | ||
+ "| |_| | |_| | < __/\n" | ||
+ "|____/ \\__,_|_|\\_\\___|\n"; | ||
System.out.println("Hello from\n" + logo); | ||
System.out.println(logo); | ||
System.out.println("How can i help u? \n"); | ||
} | ||
|
||
public static void echo() { | ||
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. Good job for using a verb to name this method |
||
Scanner scan = new Scanner(System.in); | ||
|
||
String input = scan.next(); | ||
if ("bye".equals(input)) { | ||
System.out.println("Goodbye. Hope to see u again :) \n"); | ||
} else { | ||
System.out.println(input); | ||
echo(); | ||
} | ||
|
||
scan.close(); | ||
|
||
} | ||
|
||
public static void main(String[] args) { | ||
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. Try keeping your main method short and have a parser to deal with interpreting user commands instead |
||
greet_user(); | ||
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. Good job on extracting this part as a separate function |
||
int counter = 0; | ||
|
||
Tasks[] list_of_tasks = new Tasks[101]; // Following the assumption that there will be no more than 100 tasks | ||
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. Perhaps you try using camelcase when naming variables. Eg. listOfTasks 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. It may be better to avoid using a magic number (100) and instead define it at the top of the file as MAX_NUMBER_OF_TASKS |
||
for (int k = 0; k < 101; k++) { | ||
list_of_tasks[k] = new Tasks(""); | ||
} | ||
|
||
Scanner scan = new Scanner(System.in); | ||
String input = scan.nextLine(); | ||
|
||
while (!"bye".equals(input)) { | ||
if ("list".equals(input)) { | ||
System.out.println("Here are the tasks in your list: "); | ||
for (int i = 0; i < counter; i++) { | ||
System.out.println( | ||
i + 1 + "." + " " + list_of_tasks[i].getStatusIcon() + " " + list_of_tasks[i].description); | ||
} | ||
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. It may be clearer to extract these parts as separate functions to avoid nesting loops. |
||
} else if (input.length() >= 4 && input.substring(0, 4).equals("mark")) { | ||
Integer index = Integer.valueOf(input.substring(5, input.length())); | ||
list_of_tasks[index - 1].markAsDone(); | ||
System.out.println("Nice! This task is completed"); | ||
System.out | ||
.println(list_of_tasks[index - 1].getStatusIcon() + " " + list_of_tasks[index - 1].description); | ||
} else if (input.length() >= 6 && input.substring(0, 6).equals("unmark")) { | ||
Integer index = Integer.valueOf(input.substring(7, input.length())); | ||
list_of_tasks[index - 1].markAsUnDone(); | ||
System.out.println("Ok, This task is still not complete"); | ||
System.out | ||
.println(list_of_tasks[index - 1].getStatusIcon() + " " + list_of_tasks[index - 1].description); | ||
} else { | ||
System.out.println("added: " + input); | ||
list_of_tasks[counter].description = input; | ||
list_of_tasks[counter].isDone = false; | ||
counter++; | ||
} | ||
scan = new Scanner(System.in); | ||
input = scan.nextLine(); | ||
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. Since you abstracted the [greet_user()] function, it may be better if you maintain Single Level of Abstraction Principle (SLAP), and also extract this portion as a separate function |
||
|
||
} | ||
|
||
System.out.println("Goodbye. Hope to see u again :) \n"); | ||
scan.close(); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
public class Tasks { | ||
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. Perhaps you could name this class as "Task" (singular) instead as it refers to one task |
||
|
||
protected String description; | ||
protected boolean isDone; | ||
|
||
public Tasks(String description) { | ||
this.description = description; | ||
this.isDone = false; | ||
} | ||
|
||
public String getStatusIcon() { | ||
return (isDone ? "[X]" : "[ ]"); // mark done task with X | ||
} | ||
|
||
public void markAsDone() { | ||
this.isDone = true; | ||
} | ||
|
||
public void markAsUnDone() { | ||
this.isDone = false; | ||
} | ||
|
||
} |
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.
Consistent 4 spaces indent throughout the code, good job