-
Notifications
You must be signed in to change notification settings - Fork 78
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
[Chua Zhong Heng] iP #63
base: master
Are you sure you want to change the base?
Changes from 17 commits
cefd18d
a30a0be
ff4554d
899542c
cd10722
95c15bd
3d50f0a
c7b6dac
a5d5914
50f175c
8e1aead
3dbdfbc
544d7c9
060f8b7
712e602
20a68c2
16e8cbf
5cfdcb5
8ff14d8
335ff3f
578c87e
ca8c8e3
23dfdee
ee3e90e
fe43edd
b7a4dd3
f583868
6b15613
4c2227f
0a23d97
0e39d2e
e8b4ef4
32f1853
96ac6d1
f41f811
b3bcec5
d0f4404
f6ef0c4
5d0ee0d
2ff6d44
9f93c2e
a077f54
bd265bb
e6b2f51
449b98d
bfdc87d
fb8bcff
f691c78
8f7dbd9
531399a
9b0876a
d17d6b4
7ee1694
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,15 @@ | ||
public class Deadline extends Task{ | ||
protected String deadline; | ||
public Deadline(String description, String taskType, String deadline) { | ||
super(description, taskType); | ||
this.deadline = deadline; | ||
} | ||
|
||
public String getDeadline() { | ||
return deadline; | ||
} | ||
|
||
public String toString() { | ||
return taskType + super.toString() + deadline; | ||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
public class Event extends Task{ | ||
protected String dateTime; | ||
public Event(String description, String taskType, String dateTime) { | ||
super(description, taskType); | ||
this.dateTime = dateTime; | ||
} | ||
|
||
public String getDateTime() { | ||
return dateTime; | ||
} | ||
|
||
public String toString() { | ||
return taskType + super.toString() + dateTime; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
import java.util.Scanner; | ||
import java.util.ArrayList; | ||
|
||
public class KenergeticBot { | ||
|
||
public static void printGreetingMessage() { | ||
printLine(); | ||
System.out.println(" Hello! I'm KenergeticBot\n" + " What can I do for you?"); | ||
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 would like to replace all the magic strings and numbers in your code with variables for greater readability? |
||
printLine(); | ||
} | ||
|
||
public static void printExitMessage() { | ||
printLine(); | ||
System.out.println(" Bye. Hope to see you again soon!"); | ||
printLine(); | ||
} | ||
|
||
public static void printLine() { | ||
System.out.println(" ____________________________________________________________"); | ||
} | ||
|
||
//Returns True if the text contains "mark", False if not | ||
public static boolean checkTextForMark(String item) { | ||
if (item.length() > 4 && item.startsWith("mark")) { | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
//Returns True if the text contains "unmark", False if not | ||
public static boolean checkTextForUnmark(String item) { | ||
if (item.length() > 6 && item.startsWith("unmark")) { | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
//Returns True if the text contains "todo", False if not | ||
public static boolean checkTextForTodo(String item) { | ||
if (item.length() > 3 && item.startsWith("todo")) { | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
//Returns True if the text contains "deadline", False if not | ||
public static boolean checkTextForDeadline(String item) { | ||
if (item.length() > 7 && item.startsWith("deadline")) { | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
//Returns True if the text contains "event", False if not | ||
public static boolean checkTextForEvent(String item) { | ||
if (item.length() > 4 && item.startsWith("event")) { | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
//Main logic for the bot's dialogue | ||
public static void botDialogue(ArrayList<Task> taskList) { | ||
Scanner input = new Scanner(System.in); | ||
String item = input.nextLine(); | ||
if (item.equals("bye")) { | ||
printExitMessage(); | ||
return; | ||
} else if (item.equals("list")) { | ||
list(taskList); | ||
} else if (checkTextForMark(item)) { | ||
String[] splitItem = item.split(" "); | ||
int listIndex = Integer.parseInt(splitItem[1]); | ||
mark(taskList, listIndex); | ||
} else if (checkTextForUnmark(item)) { | ||
String[] splitItem = item.split(" "); | ||
int listIndex = Integer.parseInt(splitItem[1]); | ||
unmark(taskList, listIndex); | ||
} else { | ||
try { | ||
add(taskList, item); | ||
} catch (KenergeticBotException e) { //exception thrown when user inputs command outside of the usual commands | ||
System.out.println(KenergeticBotException.INVALID_COMMAND); // unable to print sad face ˙◠˙ | ||
} | ||
} | ||
botDialogue(taskList); | ||
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. Is this recursion??? |
||
} | ||
|
||
//Creates a "Todo" object and adds to the taskList | ||
public static void addTodo(ArrayList<Task> taskList, String item) throws KenergeticBotException { | ||
String formattedString = item.replace("todo", "").trim(); | ||
if (formattedString.isEmpty()) { | ||
throw new KenergeticBotException(); | ||
} | ||
String taskType = "[T]"; | ||
Task newTask = new Todo(formattedString, taskType); | ||
taskList.add(newTask); | ||
System.out.println(" Got it. I've added this task:"); | ||
System.out.printf(" %s\n", newTask); | ||
System.out.printf(" Now you have %d tasks in the list.\n", taskList.size()); | ||
} | ||
|
||
//Creates a "Deadline" object and adds to the taskList | ||
public static void addDeadline(ArrayList<Task> taskList, String item) { | ||
String formattedString[] = item.replace("deadline", "").trim().split("/"); | ||
String taskType = "[D]"; | ||
String deadlineDate = "(" + formattedString[1].replace("by", "by:") + ")"; | ||
Task newTask = new Deadline(formattedString[0], taskType, deadlineDate); | ||
taskList.add(newTask); | ||
System.out.println(" Got it. I've added this task:"); | ||
System.out.printf(" %s\n", newTask); | ||
System.out.printf(" Now you have %d tasks in the list.\n", taskList.size()); | ||
} | ||
|
||
//Creates a "Event" object and adds to the taskList | ||
public static void addEvent(ArrayList<Task> taskList, String item) { | ||
String formattedString[] = item.replace("event", "").split("/"); | ||
String taskType = "[E]"; | ||
String eventFrom = formattedString[1].replace("from", "from:"); | ||
String eventTo = formattedString[2].replace("to", "to:"); | ||
String dateTime = "(" + eventFrom + eventTo + ")"; | ||
Task newTask = new Event(formattedString[0], taskType, dateTime); | ||
taskList.add(newTask); | ||
System.out.println(" Got it. I've added this task:"); | ||
System.out.printf(" %s\n", newTask); | ||
System.out.printf(" Now you have %d tasks in the list.\n", taskList.size()); | ||
} | ||
|
||
//Controls the logic for adding items to the list | ||
public static void add(ArrayList<Task> taskList, String item) throws KenergeticBotException { | ||
printLine(); | ||
if (checkTextForTodo(item)) { | ||
try { | ||
addTodo(taskList, item); | ||
} catch (KenergeticBotException e) { //throws exception when the todo command is not followed with a description | ||
System.out.println(KenergeticBotException.MISSING_DESCRIPTION); | ||
} | ||
} else if (checkTextForDeadline(item)) { | ||
addDeadline(taskList, item); | ||
} else if (checkTextForEvent(item)) { | ||
addEvent(taskList, item); | ||
} else { | ||
throw new KenergeticBotException(); | ||
} | ||
printLine(); | ||
} | ||
|
||
public static void list(ArrayList<Task> taskList) { | ||
printLine(); | ||
System.out.println(" Here are the tasks in your list:"); | ||
for (int i = 0; i < taskList.size(); i++) { | ||
System.out.printf(" %d.%s\n", i+1, taskList.get(i)); | ||
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. Nice to use |
||
} | ||
printLine(); | ||
} | ||
|
||
public static void mark(ArrayList<Task> taskList, int listIndex) { | ||
printLine(); | ||
System.out.println(" Nice! I've marked this task as done:"); | ||
taskList.get(listIndex - 1).mark();; | ||
System.out.printf(" %s\n", taskList.get(listIndex - 1)); | ||
printLine(); | ||
} | ||
|
||
public static void unmark(ArrayList<Task> taskList, int listIndex) { | ||
printLine(); | ||
System.out.println(" OK, I've marked this task as not done yet:"); | ||
taskList.get(listIndex - 1).unmark(); | ||
System.out.printf(" %s\n", taskList.get(listIndex - 1)); | ||
printLine(); | ||
} | ||
|
||
public static void main(String[] args) { | ||
printGreetingMessage(); | ||
ArrayList<Task> taskList = new ArrayList<Task>(); | ||
botDialogue(taskList); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
public class KenergeticBotException extends Exception { | ||
public static String INVALID_COMMAND = " ☹ OOPS!!! I'm sorry, but I don't know what that means :-("; | ||
public static String MISSING_DESCRIPTION = " \u02D9\u25E0\u02D9 OOPS!!! The description of a todo cannot be 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. Perhaps you would like to remove these extra spaces? |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
public class Task { | ||
protected String description; | ||
protected boolean isDone; | ||
protected String taskType; | ||
protected int taskIndex; | ||
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 would like to remove this unused variable? |
||
|
||
public Task(String description, String taskType) { | ||
this.description = description; | ||
this.isDone = false; | ||
this.taskType = taskType; | ||
} | ||
|
||
public String getStatusIcon() { | ||
return (isDone ? "[X]" : "[ ]"); // mark done task with X | ||
} | ||
|
||
public String getTaskType() { | ||
return taskType; | ||
} | ||
|
||
public void mark() { | ||
this.isDone = true; | ||
} | ||
|
||
public void unmark() { | ||
this.isDone = false; | ||
} | ||
|
||
public String toString() { | ||
return getStatusIcon() + " " + description; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
public class Todo extends Task{ | ||
public Todo(String description, String taskType) { | ||
super(description, taskType); | ||
} | ||
|
||
public String toString() { | ||
return taskType + super.toString(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,23 @@ | ||
Hello from | ||
____ _ | ||
| _ \ _ _| | _____ | ||
| | | | | | | |/ / _ \ | ||
| |_| | |_| | < __/ | ||
|____/ \__,_|_|\_\___| | ||
|
||
____________________________________________________________ | ||
Hello! I'm KenergeticBot | ||
What can I do for you? | ||
____________________________________________________________ | ||
____________________________________________________________ | ||
Got it. I've added this task: | ||
[T][ ] borrow book | ||
Now you have 1 tasks in the list. | ||
____________________________________________________________ | ||
____________________________________________________________ | ||
Here are the tasks in your list: | ||
1.[T][ ] borrow book | ||
____________________________________________________________ | ||
____________________________________________________________ | ||
Got it. I've added this task: | ||
[D][ ] return book (by: Sunday) | ||
Now you have 2 tasks in the list. | ||
____________________________________________________________ | ||
____________________________________________________________ | ||
Got it. I've added this task: | ||
[E][ ] project meeting (from: Mon 2pm to: 4pm) | ||
Now you have 3 tasks in the list. | ||
____________________________________________________________ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
todo borrow book | ||
list | ||
deadline return book /by Sunday | ||
event project meeting /from Mon 2pm /to 4pm | ||
bye |
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.
Perhaps you want to add an additional space in front of the curly paranthesis?