-
Notifications
You must be signed in to change notification settings - Fork 191
[Wang Zhihuang] iP #181
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?
[Wang Zhihuang] iP #181
Changes from 7 commits
60a88c0
8747f62
ab4602c
b43adfa
68d69b3
c3cd436
6b63884
9089498
5aab948
5d11a58
aa34d61
753e0a8
eea7087
b6f0c99
326a6f2
04badc3
30ba786
22b68f0
3a8a6f4
793418f
44695ee
5cf8dd0
6b52c9e
3d92406
054a4d6
770747e
99938ba
0b0382a
258e217
25f9be6
433daa2
c89f1a9
86057c2
60db3bb
b89640a
8fbb849
1e3f1a9
e99c7f8
ab2da15
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 |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| public class Deadline extends Task { | ||
|
|
||
| protected String by; | ||
|
|
||
| public Deadline(String description, String by) { | ||
| super(description); | ||
| this.by = by; | ||
| } | ||
|
|
||
| public String getIcon() { | ||
| return "[D]"; | ||
| } | ||
|
|
||
| public String getBy() { | ||
| return "(by:" + by + ")"; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return getIcon() + super.toString() + "(by:" + by + ")"; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,10 @@ | ||
| import java.util.Scanner; | ||
|
|
||
| public class Duke { | ||
| public static void main(String[] args) { | ||
| String logo = " ____ _ \n" | ||
| + "| _ \\ _ _| | _____ \n" | ||
| + "| | | | | | | |/ / _ \\\n" | ||
| + "| |_| | |_| | < __/\n" | ||
| + "|____/ \\__,_|_|\\_\\___|\n"; | ||
| System.out.println("Hello from\n" + logo); | ||
| TaskManager taskManager = new TaskManager(); | ||
| Scanner scanner = new Scanner(System.in); | ||
| Ui ui = new Ui(taskManager, scanner); | ||
| ui.start(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| public class Event extends Task{ | ||
| protected String at; | ||
|
|
||
| public Event(String description, String at) { | ||
| super(description); | ||
| this.at = at; | ||
| } | ||
|
|
||
| public String getIcon() { | ||
| return "[E]"; | ||
| } | ||
| public String getAt() { | ||
| return "(at:" + at + ")"; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return getIcon() + super.toString() + "(at:" + at + ")"; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| public class Task { | ||
| protected String description; | ||
| protected boolean isDone; | ||
|
|
||
| public Task(String description) { | ||
| this.description = description; | ||
| this.isDone = false; | ||
| } | ||
|
|
||
| public void taskDone() { | ||
| this.isDone = true; | ||
| } | ||
|
|
||
| public String getStatusIcon() { | ||
| return (isDone ? "[X]" : "[ ]"); | ||
| } | ||
| public String getIcon() { | ||
| return ""; | ||
| } | ||
| public String getBy() { | ||
| return ""; | ||
| } | ||
| public String getAt() { | ||
| return ""; | ||
| } | ||
|
|
||
|
||
|
|
||
| public String toString() { | ||
| return getStatusIcon() + " " + this.description ; | ||
| } | ||
|
|
||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| import java.util.ArrayList; | ||
|
|
||
| public class TaskManager { | ||
| private ArrayList<Task> taskList; | ||
|
|
||
| public TaskManager() { | ||
| this.taskList = new ArrayList<>(); | ||
| } | ||
|
|
||
| /** | ||
| * Adds task to taskList ArrayList | ||
| * | ||
| * @param task Task to be checked | ||
| */ | ||
| public void add(String task) { | ||
| String taskType = getCommand(task); | ||
| Task newTask; | ||
| String description = getDescription(task); | ||
| switch(taskType) { | ||
|
||
| case "todo": | ||
| newTask = new ToDo(description); | ||
| taskList.add(newTask); | ||
| break; | ||
| case "deadline": | ||
| String by = getDate(task); | ||
| newTask = new Deadline(description, by); | ||
| taskList.add(newTask); | ||
| break; | ||
| case "event": | ||
| String at = getDate(task); | ||
| newTask = new Event(description, at); | ||
| taskList.add(newTask); | ||
| break; | ||
| default: | ||
| System.out.println(" Invalid command, please try again"); | ||
| return; | ||
| } | ||
|
||
|
|
||
| System.out.println(" Got it. I've added this task:\n " + newTask); | ||
| printSize(); | ||
| } | ||
|
|
||
| /** | ||
| * Checks if specified task is done | ||
| * | ||
| * @param taskNumber task number of task to be checked | ||
| */ | ||
| public void checkDone(int taskNumber) { | ||
| taskList.get(taskNumber - 1).taskDone(); | ||
| } | ||
|
|
||
| public String getDescription(String task) { | ||
| String description; | ||
| int separator; | ||
| if (getCommand(task).equals("todo")) { | ||
| description = task.substring(5); | ||
| } else if (getCommand(task).equals("deadline")) { | ||
| separator = task.indexOf("/by"); | ||
| description = task.substring(9, separator); | ||
| } else if (getCommand(task).equals("event")){ | ||
| separator = task.indexOf("/at"); | ||
| description = task.substring(6, separator); | ||
| } else { | ||
| description = null; | ||
| } | ||
| return description; | ||
| } | ||
|
|
||
| public String getCommand(String task) { | ||
| String[] command = task.split(" "); | ||
| String taskType = command[0]; | ||
|
|
||
| return taskType; | ||
| } | ||
| public void printSize() { | ||
| if (getSize() == 1) { | ||
| System.out.println(" Now you have " + 1 + " task in the list."); | ||
| } else { | ||
| System.out.println(" Now you have " + getSize() + " tasks in the list."); | ||
| } | ||
|
||
| } | ||
|
|
||
| /** | ||
| * Returns name of task given task number | ||
| * | ||
| * @param index index of task in taskList ArrayList | ||
| * @return Name of Task | ||
| */ | ||
| public String getName(int index) { | ||
| return taskList.get(index - 1).toString(); | ||
| } | ||
|
|
||
|
|
||
|
|
||
| public int getSize() { | ||
| return taskList.size(); | ||
| } | ||
|
|
||
|
|
||
| public void list() { | ||
| for (int i = 0; i < taskList.size(); i++) { | ||
| Task t = taskList.get(i); | ||
| if (t.getIcon().equals("[D]")) { | ||
| System.out.println(" " + (i + 1) + "." + t); | ||
| } else if (t.getIcon().equals("[E]")) { | ||
| System.out.println(" " + (i + 1) + "." + t); | ||
| } else { | ||
| System.out.println(" " + (i + 1) + "." + t); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
| public String getDate(String description) { | ||
| String date; | ||
| if (description.contains("/by")) { | ||
| int indexOfSeparator = description.indexOf("/by"); | ||
| date = description.substring(indexOfSeparator + 3); | ||
| } else if (description.contains("/at")) { | ||
| int indexOfSeparator = description.indexOf("/at"); | ||
| date = description.substring(indexOfSeparator + 3); | ||
| } else { | ||
| date = null; | ||
| } | ||
| return date; | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| public class ToDo extends Task{ | ||
| public ToDo(String description) { | ||
| super(description); | ||
| } | ||
|
|
||
| public String getIcon() { | ||
| return "[T]"; | ||
| } | ||
| @Override | ||
| public String toString() { | ||
| return getIcon() + super.toString(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| import java.util.Scanner; | ||
|
|
||
| public class Ui { | ||
| private TaskManager taskManager; | ||
| private Scanner scanner; | ||
|
|
||
| public Ui(TaskManager taskManager, Scanner scanner) { | ||
| this.taskManager = taskManager; | ||
| this.scanner = scanner; | ||
| } | ||
|
|
||
| public void start() { | ||
| String lineBreak = " ____________________________________________________________\n"; | ||
| String logo = "\n" + | ||
| " \n" + | ||
| " ,--. ,------. ,--. ,--. ,--. \n" + | ||
| ",-' '-.,---.| .-. \\ ,---.| | `--',---,-' '-. \n" + | ||
| "'-. .-| .-. | | \\ | .-. | | ,--( .-'-. .-' \n" + | ||
| " | | ' '-' | '--' ' '-' | '--| .-' `)| | \n" + | ||
| " `--' `---'`-------' `---'`-----`--`----' `--' \n" + | ||
| " \n"; | ||
|
||
| String greetings = lineBreak | ||
| + logo | ||
| + " Welcome to the toDoList Chatbot\n" | ||
| + " What would you like to do today?\n" | ||
| + lineBreak; | ||
| String farewell = " Bye. Hope to see you again soon!"; | ||
| System.out.println(greetings); | ||
|
|
||
| while (true) { | ||
| String input = scanner.nextLine(); | ||
| String[] command = input.split(" "); | ||
| String firstWord = command[0]; | ||
| //Task t = new Task(input); | ||
|
|
||
| System.out.print(lineBreak); | ||
| switch (firstWord) { | ||
| case "bye": | ||
| System.out.println(farewell); | ||
| return; | ||
| case "list": | ||
| System.out.println(" Here are the tasks in your list:"); | ||
| taskManager.list(); | ||
| break; | ||
| case "done": | ||
| System.out.println(" Nice! I've marked this task as done: "); | ||
| int taskNumber = Integer.parseInt(command[1]); | ||
| taskManager.checkDone(taskNumber); | ||
| System.out.println(" " + taskManager.getName(taskNumber)); | ||
| break; | ||
|
|
||
| default: | ||
| taskManager.add(input); | ||
| } | ||
| System.out.print(lineBreak); | ||
| } | ||
| } | ||
| } | ||
|
||
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.
I like how you created a taskManager class to handle the commands and an Ui class to handle all the output, making the code very easy to understand.