-
Notifications
You must be signed in to change notification settings - Fork 118
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
[TrungBui32] iP #112
base: master
Are you sure you want to change the base?
[TrungBui32] iP #112
Changes from 4 commits
cd60db6
4814b4e
4f308d2
6f3e69b
8877247
92fffd2
bf62b4d
bc68ba7
b806d12
b4c70c2
8d4b8cf
70f037a
b1c114f
3aaa8d4
46e6bef
1b9df27
85c5f24
1103df7
5ce99e1
134e8fe
2e236e9
b855458
edee1a4
42416ac
58e96c7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import java.util.Scanner; | ||
|
||
public class Poirot { | ||
private static Task[] list_actions = new Task[100]; | ||
private static int last_index = 0; | ||
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 it would be better to use lastIndex instead of last_index. 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. size may be a better name than last_index 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 naming the variables with more than one word in camelCase |
||
|
||
public static void echo(String msg) { | ||
System.out.println("____________________________________________________________\n"); | ||
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 noticed that this was repeated many times. Perhaps it would be good to use a utility function to print the horizontal lines instead. 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. Abstracting the printing of divider to a function will improve readability |
||
System.out.println(msg); | ||
System.out.println("____________________________________________________________\n"); | ||
} | ||
|
||
public static void print(Task[] list) { | ||
if (last_index == 0) { | ||
System.out.println("____________________________________________________________\n"); | ||
System.out.println("No actions available"); | ||
} else { | ||
System.out.println("____________________________________________________________\n"); | ||
for (int i = 0; i < last_index; i++) { | ||
System.out.println((i + 1) + ".[" + list_actions[i].getStatusIcon() + "]" + list_actions[i].getDescription()); | ||
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. Abstract this to a function and give variable names for list_actions[i].getStatusIcon() and list_actions[i].getDescription to improve code readability |
||
} | ||
} | ||
System.out.println("____________________________________________________________\n"); | ||
} | ||
|
||
public static void add(Task action) { | ||
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. "add" is a bit ambiguous. It might be better to use "addTask" instead. |
||
list_actions[last_index] = action; | ||
last_index++; | ||
System.out.println("____________________________________________________________\n"); | ||
System.out.println("added: " + action.getDescription()); | ||
System.out.println("____________________________________________________________\n"); | ||
} | ||
|
||
public static void main(String[] args) { | ||
System.out.println("____________________________________________________________\n"); | ||
System.out.println("Hello! I'm POIROT\n"); | ||
System.out.println("What can I do for you?"); | ||
System.out.println("____________________________________________________________\n"); | ||
boolean working = 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. Since this is a boolean, it might be better to call it "isWorking". |
||
Scanner scan = new Scanner(System.in); | ||
while (working) { | ||
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 doing a saving changes to file at the end of each iteration, as such avoiding to call it every time something is changed, reduce duplicate lines |
||
String input = scan.nextLine(); | ||
String[] list_input = input.split(" "); | ||
switch (list_input[0]) { | ||
case "list": | ||
print(list_actions); | ||
break; | ||
case "bye": | ||
working = false; | ||
break; | ||
case "mark": | ||
int x = Integer.parseInt(list_input[1]) - 1; | ||
list_actions[x].setDone(true); | ||
System.out.println("____________________________________________________________\n"); | ||
System.out.println("Nice! I've marked this task as done:\n"); | ||
System.out.print("[" + list_actions[x].getStatusIcon() + "] "); | ||
System.out.println(list_actions[x].getDescription()); | ||
System.out.println("____________________________________________________________\n"); | ||
break; | ||
case "unmark": | ||
int y = Integer.parseInt(list_input[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. Set a variable for list_input[1] as it is unclear what is that 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. use a better name instead of y |
||
list_actions[y].setDone(false); | ||
System.out.println("____________________________________________________________\n"); | ||
System.out.println("OK, I've marked this task as not done yet:\n"); | ||
System.out.print("[" + list_actions[y].getStatusIcon() + "] "); | ||
System.out.println(list_actions[y].getDescription()); | ||
System.out.println("____________________________________________________________\n"); | ||
break; | ||
default: | ||
String clearInput = input.trim(); | ||
if(clearInput.isEmpty()){ | ||
System.out.println("____________________________________________________________\n"); | ||
System.out.println("Invalid task"); | ||
System.out.println("____________________________________________________________\n"); | ||
} | ||
else { | ||
Task newTask = new Task(clearInput); | ||
add(newTask); | ||
} | ||
break; | ||
} | ||
} | ||
System.out.println("____________________________________________________________\n"); | ||
System.out.println("Bye. Hope to see you again soon!"); | ||
System.out.println("____________________________________________________________\n"); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
public class Task { | ||
protected String description; | ||
protected boolean isDone; | ||
|
||
public Task(String description) { | ||
this.description = description; | ||
this.isDone = false; | ||
} | ||
public String getDescription() { | ||
return description; | ||
} | ||
public String getStatusIcon() { | ||
return (isDone ? "X" : " "); | ||
} | ||
public void setDone(boolean isDone) { | ||
this.isDone = isDone; | ||
} | ||
public void markAsDone(){ | ||
this.isDone = 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.
Use camelCase for variables, E.g. listActions, but a better name will be tasks