-
Notifications
You must be signed in to change notification settings - Fork 114
[ThienDuc3112] iP #100
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
base: master
Are you sure you want to change the base?
[ThienDuc3112] iP #100
Changes from 7 commits
eff6d00
175416a
a464a71
e4d98c5
db6b1af
c4699cf
75ab4ec
b404f55
69c8498
4287dc0
cc1c022
5660dd7
f2e5d2a
de42978
3032c85
418a21a
3d6dda7
c2c4b65
7c1bec4
a4d16bc
8589761
1566ce5
515c9ad
5c330cb
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,4 +1,4 @@ | ||
| # Duke User Guide | ||
| # DBot User Guide | ||
|
|
||
| // Update the title above to match the actual product name | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| import java.util.*; | ||
|
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 import just the libraries you need? |
||
|
|
||
| public class DBot { | ||
| private static boolean isOn; | ||
| private static List<Task> datas; | ||
|
|
||
| public static void main(String[] args) { | ||
| isOn = 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. Consider using more descriptive names, that is easily identified to serve a definite purpose |
||
| datas = new ArrayList<>(); | ||
|
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 use a more descriptive name for your array list, as it is unclear what is being stored here. |
||
|
|
||
| String greeting = "____________________________________________________________\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. use "final" and SCREAMING_SNAKE_CASE for the greeting variable as it is a constant |
||
| "Hello! I'm DBot\nWhat can I do for you?\n" | ||
| + "____________________________________________________________\n"; | ||
| System.out.println(greeting); | ||
|
|
||
| Scanner in = new Scanner(System.in); | ||
|
|
||
| while (isOn) { | ||
| System.out.print("Command: "); | ||
| String line = in.nextLine().strip(); | ||
| System.out.println("____________________________________________________________"); | ||
|
|
||
| if (line.equals("bye")) { | ||
|
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. Followed if else layout convention. Good job |
||
| System.out.println("Bye. Hope to see you again soon!"); | ||
| isOn = false; | ||
| } else if (line.equals("list")) { | ||
| list(); | ||
| } else if (line.startsWith("mark ")) { | ||
| mark(line); | ||
| } else if (line.startsWith("unmark ")) { | ||
| unmark(line); | ||
| } else if (line.startsWith("todo ")) { | ||
| todo(line); | ||
| } else if (line.startsWith("event ")) { | ||
| event(line); | ||
| } else if (line.startsWith("deadline ")) { | ||
| deadline(line); | ||
| } else { | ||
| add(line); | ||
| } | ||
|
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 refactor this into another function? This chunk is long and hard to read. |
||
|
|
||
| System.out.println("____________________________________________________________"); | ||
| } | ||
| } | ||
|
|
||
| private static void add(String line) { | ||
| datas.add(new Task(line)); | ||
| System.out.print("added: "); | ||
| System.out.println(line); | ||
| } | ||
|
|
||
| private static void 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. Consider naming it as |
||
| for (int i = 0; i < datas.size(); i++) { | ||
| System.out.printf("%d.", i + 1); | ||
| System.out.println(datas.get(i).toString()); | ||
| } | ||
| } | ||
|
|
||
| private static void mark(String line) { | ||
| try { | ||
| int option = Integer.parseInt(line.substring(line.indexOf(" ") + 1)); | ||
| System.out.println("Nice! I've marked this task as done:"); | ||
| datas.get(option - 1).mark(); | ||
| System.out.println(datas.get(option - 1).toString()); | ||
| } catch (Exception e) { | ||
| System.out.println("Invalid input"); | ||
| } | ||
| } | ||
|
|
||
| private static void unmark(String line) { | ||
| try { | ||
| int option = Integer.parseInt(line.substring(line.indexOf(" ") + 1)); | ||
| System.out.println("OK, I've marked this task as not done yet:"); | ||
| datas.get(option - 1).unmark(); | ||
| System.out.println(datas.get(option - 1).toString()); | ||
| } catch (Exception e) { | ||
| System.out.println("Invalid input"); | ||
| } | ||
| } | ||
|
|
||
| private static void todo(String line) { | ||
| String todo = line.substring(line.indexOf(" ") + 1).trim(); | ||
| Todo task =new Todo(todo); | ||
|
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. Fix the formatting after the = sign. |
||
| datas.add(task); | ||
| System.out.println("Got it. I've added this task:"); | ||
| System.out.println(task); | ||
| System.out.println("Now you have " + datas.size() + " tasks in the list."); | ||
| } | ||
|
|
||
| private static void deadline(String line) { | ||
| String deadlinePrompt = line.substring(line.indexOf(" ") + 1).trim(); | ||
| Hashtable<String, String> arguments = Utilities.getCommandArgument(deadlinePrompt); | ||
| String deadline = deadlinePrompt.substring(0, deadlinePrompt.indexOf("/")).trim(); | ||
| Deadline task = new Deadline(deadline, arguments.get("by")); | ||
| datas.add(task); | ||
| System.out.println("Got it. I've added this task:"); | ||
| System.out.println(task); | ||
| System.out.println("Now you have " + datas.size() + " tasks in the list."); | ||
| } | ||
|
|
||
| private static void event(String line) { | ||
|
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 methods better for easy maintainability and scalability |
||
| String eventPrompt = line.substring(line.indexOf(" ") + 1).trim(); | ||
| Hashtable<String, String> argument = Utilities.getCommandArgument(eventPrompt); | ||
| String event = eventPrompt.substring(0, eventPrompt.indexOf("/")).trim(); | ||
| Event task = new Event(event, argument.get("from"), argument.get("to")); | ||
| datas.add(task); | ||
| System.out.println("Got it. I've added this task:"); | ||
| System.out.println(task); | ||
| System.out.println("Now you have " + datas.size() + " tasks in the list."); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| public class Deadline extends Task { | ||
| String by; | ||
| public Deadline(String task, String by) { | ||
| super(task); | ||
| this.by = by; | ||
| } | ||
| @Override | ||
| public String toString(){ | ||
| return "[D]" + super.toString() + " (by: " + by + ")"; | ||
| } | ||
| } |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| public class Event extends Task { | ||
| String from; | ||
| String to; | ||
|
Comment on lines
+2
to
+3
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 something like |
||
| public Event(String task, String from, String to) { | ||
| super(task); | ||
| this.from = from; | ||
| this.to = to; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "[E]" + super.toString() + " (from: " + from + " to: " + to + ")"; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| public class Task { | ||
| protected boolean isDone; | ||
| protected String task; | ||
|
|
||
| public Task(String task) { | ||
| this(task, false); | ||
| } | ||
| public Task(String task, boolean isDone) { | ||
| this.task = task.strip(); | ||
| this.isDone = isDone; | ||
| } | ||
|
|
||
| public String getTask() { | ||
| return task; | ||
| } | ||
|
|
||
| public boolean isDone() { | ||
| return isDone; | ||
| } | ||
|
|
||
| public void mark(){ | ||
|
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 a space before the "{" to keep layout consistent |
||
| isDone = true; | ||
| } | ||
|
|
||
| public void unmark(){ | ||
| isDone = false; | ||
| } | ||
|
|
||
| public String toString(){ | ||
| return "[" + | ||
| (isDone ? "X" : " ") + | ||
| "] " + | ||
| task; | ||
|
Comment on lines
+67
to
+70
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 write this all in 1 line of code to make your code more readable.
Comment on lines
+67
to
+70
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 same line implementation to increase code readability |
||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| public class Todo extends Task { | ||
| public Todo(String task) { | ||
| super(task); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "[T]" + super.toString(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import java.util.Hashtable; | ||
|
|
||
| public class Utilities { | ||
| public static Hashtable<String, String> getCommandArgument(String line) { | ||
| Hashtable<String, String> args = new Hashtable<>(); | ||
|
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 to not use vague names for variables |
||
| String[] commandParams = (line.substring(line.indexOf(" ") + 1).split("(?=/)|(?<=/)")); | ||
| boolean isArgument = false; | ||
| for (String s : commandParams) { | ||
| s = s.trim(); | ||
| if (s.equals("/")) { | ||
| isArgument = true; | ||
| } else if (isArgument) { | ||
| args.put(s.substring(0, s.indexOf(' ')), s.substring(s.indexOf(' ')).trim()); | ||
| isArgument = false; | ||
| } | ||
| } | ||
| return args; | ||
| } | ||
| } | ||
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.
Avoid using wildcard imports
Import only the functions that you need