Skip to content
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

Open
wants to merge 53 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
cefd18d
Add Greet and Exit functions
Cazh1 Aug 31, 2023
a30a0be
Add Echo function
Cazh1 Aug 31, 2023
ff4554d
Add List and Add function
Cazh1 Sep 2, 2023
899542c
Add Mark-as-Done function
Cazh1 Sep 2, 2023
cd10722
Tweak the code to comply with coding standard
Cazh1 Sep 7, 2023
95c15bd
Update code logic to use Object Class
Cazh1 Sep 7, 2023
3d50f0a
Add tracking for types of tasks
Cazh1 Sep 7, 2023
c7b6dac
Update methods to use Object class
Cazh1 Sep 7, 2023
a5d5914
Update methods to use Object class
Cazh1 Sep 7, 2023
50f175c
Merge branch 'master' of https://github.com/Cazh1/ip
Cazh1 Sep 8, 2023
8e1aead
Refactor code
Cazh1 Sep 12, 2023
3dbdfbc
Add Error Handling function
Cazh1 Sep 12, 2023
544d7c9
Add Error Handling function
Cazh1 Sep 12, 2023
060f8b7
Revert "Add Error Handling function"
Cazh1 Sep 12, 2023
712e602
Merge branch 'master' into branch-Level-5
Cazh1 Sep 12, 2023
20a68c2
Merge pull request #1 from Cazh1/branch-Level-5
Cazh1 Sep 12, 2023
16e8cbf
Add Automated Text UI Testing
Cazh1 Sep 14, 2023
5cfdcb5
Update Exception Handler to cover more exceptions
Cazh1 Sep 15, 2023
8ff14d8
Organise files into Packages
Cazh1 Sep 15, 2023
335ff3f
Update Organization of Package
Cazh1 Sep 15, 2023
578c87e
Update Error handling code - clean up
Cazh1 Sep 15, 2023
ca8c8e3
Update Automated Text UI Testing
Cazh1 Sep 16, 2023
23dfdee
Update code with regards to TA comments
Cazh1 Sep 16, 2023
ee3e90e
Add Delete function
Cazh1 Sep 17, 2023
fe43edd
Add Save function
Cazh1 Sep 21, 2023
b7a4dd3
Update Saving capability
Cazh1 Sep 27, 2023
f583868
Merge pull request #2 from Cazh1/branch-Level-6
Cazh1 Sep 27, 2023
6b15613
Merge pull request #3 from Cazh1/branch-Level-7
Cazh1 Sep 27, 2023
4c2227f
Add import for Delete in main
Cazh1 Sep 27, 2023
0a23d97
Update previousList format
Cazh1 Sep 27, 2023
0e39d2e
Add MoreOOP for Storage and Command
Cazh1 Sep 27, 2023
e8b4ef4
Add MoreOOP for TextUi
Cazh1 Oct 2, 2023
32f1853
Add Find function
Cazh1 Oct 2, 2023
96ac6d1
Add Java Documentation
Cazh1 Oct 3, 2023
f41f811
Merge pull request #4 from Cazh1/branch-Level-9
Cazh1 Oct 3, 2023
b3bcec5
Merge pull request #5 from Cazh1/branch-JavaDoc
Cazh1 Oct 3, 2023
d0f4404
Update README.md
Cazh1 Oct 4, 2023
f6ef0c4
Update README.md
Cazh1 Oct 4, 2023
5d0ee0d
Update README.md
Cazh1 Oct 4, 2023
2ff6d44
Update README.md
Cazh1 Oct 4, 2023
9f93c2e
Update README.md
Cazh1 Oct 4, 2023
a077f54
Update README.md
Cazh1 Oct 4, 2023
bd265bb
Update README.md
Cazh1 Oct 4, 2023
e6b2f51
Update README.md
Cazh1 Oct 4, 2023
449b98d
Update README.md
Cazh1 Oct 4, 2023
bfdc87d
Update README.md
Cazh1 Oct 4, 2023
fb8bcff
Update README.md
Cazh1 Oct 4, 2023
f691c78
Update README.md
Cazh1 Oct 4, 2023
8f7dbd9
Update README.md
Cazh1 Oct 4, 2023
531399a
Update README.md
Cazh1 Oct 6, 2023
9b0876a
Update Code Quality
Cazh1 Oct 6, 2023
d17d6b4
Update README.md
Cazh1 Oct 6, 2023
7ee1694
Update Code Quality
Cazh1 Oct 6, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Duke project template
# KenergeticBot project template

This is a project template for a greenfield Java project. It's named after the Java mascot _Duke_. Given below are instructions on how to use it.

Expand All @@ -13,7 +13,7 @@ Prerequisites: JDK 11, update Intellij to the most recent version.
1. If there are any further prompts, accept the defaults.
1. Configure the project to use **JDK 11** (not other versions) as explained in [here](https://www.jetbrains.com/help/idea/sdk.html#set-up-jdk).<br>
In the same dialog, set the **Project language level** field to the `SDK default` option.
3. After that, locate the `src/main/java/Duke.java` file, right-click it, and choose `Run Duke.main()` (if the code editor is showing compile errors, try restarting the IDE). If the setup is correct, you should see something like the below as the output:
3. After that, locate the `src/main/java/KenergeticBot.java` file, right-click it, and choose `Run KenergeticBot.main()` (if the code editor is showing compile errors, try restarting the IDE). If the setup is correct, you should see something like the below as the output:
```
Hello from
____ _
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/Deadline.java
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;
}
}
10 changes: 0 additions & 10 deletions src/main/java/Duke.java

This file was deleted.

15 changes: 15 additions & 0 deletions src/main/java/Event.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
public class Event extends Task{

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?

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;
}
}
183 changes: 183 additions & 0 deletions src/main/java/KenergeticBot.java
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?");

Choose a reason for hiding this comment

The 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);

Choose a reason for hiding this comment

The 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));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice to use printf!

}
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);
}
}
6 changes: 6 additions & 0 deletions src/main/java/KenergeticBotException.java
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.";


Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps you would like to remove these extra spaces?

}
32 changes: 32 additions & 0 deletions src/main/java/Task.java
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;

Choose a reason for hiding this comment

The 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;
}
}
9 changes: 9 additions & 0 deletions src/main/java/Todo.java
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();
}
}
30 changes: 23 additions & 7 deletions text-ui-test/EXPECTED.TXT
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.
____________________________________________________________
5 changes: 5 additions & 0 deletions text-ui-test/input.txt
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
2 changes: 1 addition & 1 deletion text-ui-test/runtest.bat
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ IF ERRORLEVEL 1 (
REM no error here, errorlevel == 0

REM run the program, feed commands from input.txt file and redirect the output to the ACTUAL.TXT
java -classpath ..\bin Duke < input.txt > ACTUAL.TXT
java -classpath ..\bin KenergeticBot < input.txt > ACTUAL.TXT

REM compare the output to the expected output
FC ACTUAL.TXT EXPECTED.TXT