Skip to content
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
1795e04
Added Skeleton code that outputs the required output as per source we…
owenyeo Aug 23, 2023
4130123
Added echoing functionality
owenyeo Aug 24, 2023
6390de9
Added the functionality of listing
owenyeo Aug 24, 2023
b9fcb6a
Added functionality to unmark and mark tasks as done
owenyeo Aug 24, 2023
61b2190
Added functionality to add ToDo, Event, and Deadline
owenyeo Aug 24, 2023
7dfe76e
Added ChatBotException, InvalidCommandException, and InvalidDescrptio…
owenyeo Aug 24, 2023
85de7a8
Added delete functionality
owenyeo Aug 24, 2023
4fe5821
Added InvalidIndexException
owenyeo Aug 31, 2023
3201c19
Improved on documentation
owenyeo Aug 31, 2023
2c3b245
Added functionality to store DateTime objects
owenyeo Aug 31, 2023
88f8909
Refactored code into different files for more OOP
owenyeo Sep 1, 2023
2ed0b5e
Reorganised code into packages.
owenyeo Sep 1, 2023
c311b41
Add Gradle support for Chat Bot
owenyeo Sep 2, 2023
7c12866
Add JUnit tests into IP
owenyeo Sep 4, 2023
e11eebb
Follow coding convention
owenyeo Sep 4, 2023
d367c95
Add Find command
owenyeo Sep 4, 2023
15915cc
Add Checkstyle Support
owenyeo Sep 5, 2023
105a851
Add GUI support for ChatBot
owenyeo Sep 7, 2023
82a31ff
Add FXML Support to GUI
owenyeo Sep 9, 2023
70e492f
Add Assertion statements
owenyeo Sep 12, 2023
d958d4e
Improve Code Quality
owenyeo Sep 13, 2023
ef6da21
Add functionality to detect duplicates
owenyeo Sep 13, 2023
a59f792
Add User Guide and Screenshot
owenyeo Sep 19, 2023
b1a3625
Add Screenshot and User Guide
owenyeo Sep 19, 2023
2ad87a1
Improve README
owenyeo Sep 20, 2023
eb3e12f
Fix hard-coding file path
owenyeo Sep 20, 2023
53531c3
Fix bug with FXML
owenyeo Sep 22, 2023
b6674b8
Moved README to doc folder
owenyeo Sep 23, 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
18 changes: 18 additions & 0 deletions 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# On branch master
# Your branch is up to date with 'origin/master'.
#
# Changes to be committed:
# modified: src/main/java/ChatBot.java
# new file: src/main/java/ChatBotException.java
# modified: src/main/java/Deadline.java
# modified: src/main/java/Event.java
# new file: src/main/java/InvalidCommandException.java
# new file: src/main/java/InvalidDescriptionException.java
# modified: src/main/java/ToDo.java
#
Added ChatBotException, InvalidCommandException, and InvalidDescriptionException.

333 changes: 333 additions & 0 deletions src/main/java/ChatBot.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,333 @@
import java.util.Scanner;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

/**
* A chat bot that can be renamed, and responds to inputs from users
*
* @author Owen Yeo
* Version Level-7
*/
public class ChatBot {

//Name of the user's ChatBot.
private String name;

//Scanner used to see user input
private static final Scanner sc = new Scanner(System.in);

//Check if the chat has ended.
private boolean hasEnded = false;

//Array of Tasks to store in the list.
private ArrayList<Task> list = new ArrayList<>();

//String representing a border.
private static final String BORDER = "____________________________________________________________\n";

//String representing the file directory.
private static final String DATA_SAVE_PATH = "src/main/java/data/chatBot.txt";

//Enum Command to make code cleaner and allow for the use of
//switch case statements.
private static enum Command {
Copy link

Choose a reason for hiding this comment

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

Maybe consider using Commands instead of Command?

BYE("bye"),
DISPLAY_LIST("list"),
MARK("mark"),
UNMARK("unmark"),
ADD_TODO("todo"),
ADD_DEADLINE("deadline"),
ADD_EVENT("event"),
DELETE("delete");

private final String input;

private Command(String input) {
this.input = input;
}

/**
* Parses the input and returns the appropriate command if the input is
* valid.
*
* @param input User's input
* @return Command that tells what the chatbot should do.
* @return null if the input in invalid
*/
public static Command parseInput(String input) {
for(Command command: Command.values()) {
if (command.input.equals(input)) {
return command;
}
}

return null;
}
}

//Constructor that allows for the naming of your own bot.
public ChatBot(String name) {
this.name = name;
}

/**
* Command to introduce the bot. Outputs an introduction with the bot's name.
*
*/
public void intro() {
System.out.println(BORDER);
System.out.println("Hello! I am " + this.name + ".\n");
System.out.println("What can I do for you today?\n");
System.out.println(BORDER);
saveTasks();
}

/**
* To exit chat and end the session.
*
* @return void
*/
public void exitChat() {
System.out.println(BORDER);
System.out.println("Bye. Have a bad day you doofus.\n");
System.out.println(BORDER);
hasEnded = true;
}

/**
* To exit chat and end the session.
*
* @return boolean The hasEnded encapsulated in the chatbot.
*/
public boolean isEnded() {
return this.hasEnded;
}

/**
* Adds the user input into a list, depending on the command.
* If description is wrong, throws an exception.
*
* @param taskString
* @param command
* @throws InvalidDescriptionException
*/
public void addToList(String taskString, Command command)
throws InvalidDescriptionException {
switch(command) {
case ADD_TODO:
Copy link

Choose a reason for hiding this comment

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

indentation of case should be same as switch i think?

if (taskString.equals(" ")) {
throw new InvalidDescriptionException("What? Where's your label? Stop this.");
}
list.add(new ToDo(taskString));
saveTasks();
break;

case ADD_DEADLINE:
try {
String[] deadlineParts = taskString.split("/by");
list.add(new Deadline(deadlineParts[0].trim(), deadlineParts[1].trim()));
saveTasks();
} catch (IndexOutOfBoundsException e) {
throw new InvalidDescriptionException("Are you stupid? Can you follow instructions?");
}
break;

case ADD_EVENT:
try {
String[] eventParts = taskString.split("/from");
String eventLabel = eventParts[0];
String[] eventParts2 = eventParts[1].split("/to");
list.add(new Event(eventLabel.trim(), eventParts2[0].trim(), eventParts2[1].trim()));
saveTasks();
} catch (IndexOutOfBoundsException e) {
throw new InvalidDescriptionException("Are you stupid? Can you follow instructions?");
Copy link

Choose a reason for hiding this comment

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

haha this actually made me laugh. q funny

}
break;

}
System.out.println(BORDER);
System.out.println("What? You ain't finishing it. Added: \n");
System.out.println(list.get(list.size() - 1) + "\n");
System.out.println("Now you have an overwhelming " + list.size() + " things to do.\n");
System.out.println(BORDER);
}

/**
* Prints the list that has been built so far.
*
*/
public void displayList() {

System.out.println(BORDER);
for (int i = 0; i < list.size(); i++) {
System.out.println((i+1) + ". " + list.get(i));
}

System.out.println(BORDER);
}

/**
* To mark tasks as done.
*
* @param int listNum the item on the list to mark.
*/
public void mark(int listNum) {
int index = listNum - 1;

Task task = list.get(index);
task.done();
System.out.println(BORDER);
System.out.println("Took you long enough. I've marked this task as done:");
System.out.println(task.toString());
System.out.println(BORDER);

saveTasks();
}

/**
* To unmark a list item as undone.
*
* @param listNum Index of the item on the list to unmark.
*/
public void unmark(int listNum) {
int index = listNum - 1;

Task task = list.get(index);
task.undone();

System.out.println(BORDER);
System.out.println("You incompetent child. I've unmarked the task. Please get it together.");
System.out.println(task.toString());
System.out.println(BORDER);

saveTasks();

}

/**
* Deletes the item off the list and prints it out with a message.
*
* @param listNum Index of the item of the list to delete.
*/
public void delete(int listNum) {
int index = listNum - 1;
Copy link

Choose a reason for hiding this comment

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

Perhaps be more descriptive? index of (indexDone)?


Task task = list.get(index);

System.out.println(BORDER);
System.out.println("I knew you couldn't finish it. Or maybe you did. I don't care. Deleted:\n");
System.out.println(task.toString());
System.out.println("Now you have an overwhelming " + (list.size() - 1) + " things to do.\n");
System.out.println(BORDER);

list.remove(index);
saveTasks();
}

/**
* Saves the tasks into a text file.
*/
private void saveTasks() {
try {
BufferedWriter bw = new BufferedWriter(new FileWriter
(DATA_SAVE_PATH, false));

for (int i = 0; i < list.size(); i++) {
bw.write(list.get(i).toSaveString());
bw.newLine();
}
bw.close();

} catch (IOException e) {
System.out.println("Error writing file:" + e.getMessage());
}
}

/**
* Reads the input of the user, and executes the commands accordingly.
* If a command is unknown, throws an exception.
*
* @param input
* @throws InvalidDescriptionException
* @throws InvalidCommandException
* @throws InvalidIndexException
*/
public void readInput(String input) throws
InvalidDescriptionException, InvalidCommandException, InvalidIndexException {
//Split the input so that we can read integers.
String[] inputStrings = input.split(" ", 2);
Command command = Command.parseInput(inputStrings[0]);
if (command == null) {
throw new InvalidCommandException("What are you saying? Try again.");
}

switch(command) {
case BYE:
this.exitChat();
break;

case DISPLAY_LIST:
this.displayList();
break;

case MARK:
try {
mark(Integer.parseInt(inputStrings[1]));
} catch (NumberFormatException e) {
throw new InvalidIndexException("Are you stupid? That's not a number.");
} catch (IndexOutOfBoundsException e) {
throw new InvalidIndexException("That's not even a number on the list, idiot.");
}
break;
case UNMARK:
try {
unmark(Integer.parseInt(inputStrings[1]));
} catch (NumberFormatException e) {
throw new InvalidIndexException("Are you stupid? That's not a number.");
} catch (IndexOutOfBoundsException e) {
throw new InvalidIndexException("That's not even a number on the list, idiot.");
}
break;

case ADD_TODO:
addToList(inputStrings[1], command);
break;

case ADD_DEADLINE:
addToList(inputStrings[1], command);
break;

case ADD_EVENT:
addToList(inputStrings[1], command);
break;

case DELETE:
try {
delete(Integer.parseInt(inputStrings[1]));
} catch (NumberFormatException e) {
throw new InvalidIndexException("Are you stupid? That's not a number.");
} catch (IndexOutOfBoundsException e) {
throw new InvalidIndexException("That's not even a number on the list, idiot.");
}
break;

default:
throw new InvalidCommandException("Don't be stupid, speak english.");
}
}

public static void main(String[] args) {

//Test chatbot named "Bobby Wasabi".
ChatBot chatbot = new ChatBot("Bobby Wasabi");
chatbot.intro();

//While chat has not ended, keep reading input.
while(!chatbot.isEnded()) {
String input = sc.nextLine();
chatbot.readInput(input);
}
}
}
12 changes: 12 additions & 0 deletions src/main/java/ChatBotException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* Class representing exceptions thrown by the chatbot.
*
* @author Owen Yeo
*/
public class ChatBotException extends RuntimeException {

public ChatBotException(String e) {
super(e);
}

}
Loading