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

sarjinius iP #474

Open
wants to merge 59 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
55f9f9f
docs/README.md: Tweak document template
Jan 7, 2024
f837ddb
Add Gradle support
May 24, 2020
a6f7324
Bump gradle and lib version
Eclipse-Dominator Aug 5, 2023
4fda194
Greets the user
sarjinius Jan 26, 2024
30db664
Exits with goodbye message
sarjinius Jan 26, 2024
5b2458d
Adds horizontal lines
sarjinius Jan 26, 2024
a4fbab2
Adds echo method
sarjinius Jan 26, 2024
dff389f
Echoes commands until bye is input.
sarjinius Jan 26, 2024
7f84653
Adds indentations
sarjinius Jan 26, 2024
bdb8f27
Renames command to task, creates a task list, modifies echo method to…
sarjinius Jan 26, 2024
1f1e2b1
Adds method that prints task list
sarjinius Jan 26, 2024
c3c9722
Creates a new Task class
sarjinius Jan 27, 2024
7458497
Adds mark, unmark, and toString methods to Task class
sarjinius Jan 27, 2024
1bfce71
Adds isCommandBye, isCommandList, and getTask methods.
sarjinius Jan 27, 2024
1d5af97
Adds isCommandMark, isCommandUnmark, and getIndex methods.
sarjinius Jan 27, 2024
3057eee
Adds markTaskAsDone and markTaskAsNotDone methods
sarjinius Jan 27, 2024
dbd50a2
Adds classes Todo, Deadline and Event that extend the Task class.
sarjinius Jan 29, 2024
b565b98
Adds isCommandTodo, isCommandDeadline, isCommandEvent, and numTasks m…
sarjinius Jan 29, 2024
0178d19
Adds methods that gets the fields to be used in the Todo, Deadline, a…
sarjinius Jan 29, 2024
08d5140
Replaces addTask method with addTodo, addDeadline, addEvent methods, …
sarjinius Jan 29, 2024
c70784b
Updates input and expected scripts for automated testing
sarjinius Jan 30, 2024
8e8f791
Handles empty description errors and unknown command errors
sarjinius Jan 31, 2024
2d54ec1
Adds support for deleting tasks from the list.
sarjinius Jan 31, 2024
da1e33c
Uses Enums for Command Types and moves different classes to new files
sarjinius Feb 11, 2024
f774215
Moves methods from Virtue class to other classes
sarjinius Feb 11, 2024
8ef4a63
Initializes task list from hard drive when chatbot starts up.
sarjinius Feb 12, 2024
5ff1d0b
Writes task list into file after list is modified.
sarjinius Feb 12, 2024
b012664
Merge branch 'branch-Level-7'
sarjinius Feb 12, 2024
4e82e16
Adds ability for chatbot to understand dates and times.
sarjinius Feb 13, 2024
c03eaf8
Merge branch 'branch-Level-8'
sarjinius Feb 13, 2024
e89cfd2
Code refactored to add more OOP
sarjinius Feb 13, 2024
85c0262
Merge branch 'add-gradle-support'
sarjinius Feb 13, 2024
a36cee0
Can run the chatbot using Gradle
sarjinius Feb 14, 2024
9169b79
TodoTest.java cannot import Todo class
sarjinius Feb 14, 2024
beebe61
Added testing ability for Todo and VirtueTaskList classes.
sarjinius Feb 14, 2024
36260c6
Changes .jar name created to virtue.jar
sarjinius Feb 14, 2024
9839681
Added JavaDoc to most classes and methods
sarjinius Feb 15, 2024
341de20
Code modified to follow the coding standard
sarjinius Feb 15, 2024
7d86535
JavaDoc modified to follow coding standard
sarjinius Feb 15, 2024
17640c9
Adds a find method that can filter the task list by a keyword.
sarjinius Feb 15, 2024
23bc68a
Merge branch 'branch-Level-9'
sarjinius Feb 15, 2024
2936133
Merge branch 'branch-A-JavaDoc'
sarjinius Feb 15, 2024
462fa52
Merge branch 'branch-A-CodingStandard'
sarjinius Feb 15, 2024
2740425
Finds coding style violations and resolves them
sarjinius Feb 16, 2024
0b48734
Merge branch 'branch-A-CheckStyle'
sarjinius Feb 16, 2024
95de22d
Incorporates a GUI into the chatbot.
sarjinius Feb 20, 2024
819a874
Merge branch 'branch-Level-10'
sarjinius Feb 20, 2024
bf1cf6f
Adds assert checks to the code
sarjinius Feb 24, 2024
86214c2
Refactors the code to improve code quality.
sarjinius Feb 24, 2024
347f5f9
Merge pull request #2 from sarjinius/branch-A-Assertions
sarjinius Feb 25, 2024
8036d65
Merge branch 'master' into branch-A-CodeQuality
sarjinius Feb 25, 2024
3f42a80
Merge pull request #3 from sarjinius/branch-A-CodeQuality
sarjinius Feb 25, 2024
19a8b03
Merge branch 'master' of https://github.com/sarjinius/ip
sarjinius Feb 25, 2024
7ab0a1c
Provide a way to perform commands on multiple tasks
sarjinius Feb 26, 2024
f311cf5
Merge branch 'branch-C-MassOps'
sarjinius Feb 26, 2024
6688e6e
Give credit for reused work
sarjinius Feb 26, 2024
2d3f605
Add a representative screenshot to the docs folder
sarjinius Feb 26, 2024
5d9d7fd
Add a brief user guide
sarjinius Feb 26, 2024
6b00519
Provide appropriate messages to invalid inputs
sarjinius Mar 6, 2024
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
391 changes: 385 additions & 6 deletions src/main/java/Duke.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,389 @@
import java.util.ArrayList;
import java.util.Objects;
import java.util.Scanner;

public class Duke {

Choose a reason for hiding this comment

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

Different Classes can be placed in a separate file for better readability?
e.g Creating Task.java to store the Class Task

public class Task {
private String description;
private boolean isDone;

public Task(String description) {
this.description = description;
this.isDone = false;
}

private String getStatusIcon() {
return (isDone ? "X" : " "); // mark done task with X
}

public void markAsDone() {
this.isDone = true;
}

public void markAsNotDone() {
this.isDone = false;
}

@Override
public String toString() {
return "[" + this.getStatusIcon() + "] " + this.description;
}
}

public class Todo extends Task {
public Todo(String description) {
super(description);
}

@Override
public String toString() {
return "[T]" + super.toString();
}
}

public class Deadline extends Task {
private String by;

public Deadline(String description, String by) {
super(description);
this.by = by;
}

@Override
public String toString() {
return "[D]" + super.toString() + " (by: " + this.by + ")";
}
}

public class Event extends Task {
private String from;
private String to;

public Event(String description, String from, String to) {
super(description);
this.from = from;
this.to = to;
}

@Override
public String toString() {
return "[E]" + super.toString() + " (from: " + this.from + " to: " + this.to + ")";
}
}

// Exceptions unique to Duke.
public class DukeException extends Exception {
public DukeException(String message) {
super(message);
}
}

public class EmptyDescriptionException extends DukeException {

public EmptyDescriptionException(String message) {
super(message);
}
}

public class UnknownCommandException extends DukeException {
public UnknownCommandException(String message) {
super(message);
}
}

// The scanner the chatbot uses to scan users' inputs.
Scanner sc = new Scanner(System.in);

// A horizontal line.
private final String horizontalLine = "____________________________________________________________";

// Prints with an indention.
private void printWithIndent(String str) {
System.out.println(" " + str);
}

// Prints a horizontal line.
private void printHorizontalLine() {
printWithIndent(horizontalLine);
}

// Gets the first word of a string.
private String getFirstWord(String str) {
return str.split(" ", 2)[0];
}

// Gets the words of a string other than the first one.
private String getOtherThanFirstWord(String str) {
return str.split(" ", 2)[1];
}

// Greets the user.
private void greet() {
printHorizontalLine();
printWithIndent("Hello! I'm Virtue \n What can I do for you?");
printHorizontalLine();
}

// Exits with a goodbye message.
private void bye() {
printHorizontalLine();
printWithIndent("Bye. Hope to see you again soon!");
printHorizontalLine();
}

// Checks if the last input is bye.
private boolean isCommandBye(String command) {
return Objects.equals(command, "bye");
}

// Checks if the last input is list.
private boolean isCommandList(String command) {
return Objects.equals(command, "list");
}

// Checks if the last input is mark.
private boolean isCommandMark(String command) {
String firstWord = getFirstWord(command);
return Objects.equals(firstWord, "mark");
}

// Checks if the last input is unmark.
private boolean isCommandUnmark(String command) {
String firstWord = getFirstWord(command);
return Objects.equals(firstWord, "unmark");
}

// Checks if the last input is todo.
private boolean isCommandTodo(String command) {
String firstWord = getFirstWord(command);
return Objects.equals(firstWord, "todo");

Choose a reason for hiding this comment

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

reductant use of Object.equals method can be checked using String equality

}

// Checks if the last input is deadline.
private boolean isCommandDeadline(String command) {
String firstWord = getFirstWord(command);
return Objects.equals(firstWord, "deadline");
}

// Checks if the last input is event.
private boolean isCommandEvent(String command) {
String firstWord = getFirstWord(command);
return Objects.equals(firstWord, "event");
}

// Checks if the last input is delete.
private boolean isCommandDelete(String command) {
String firstWord = getFirstWord(command);
return Objects.equals(firstWord, "delete");
}

// Gets the index input by the user for a mark/unmark/delete command.
private int getMarkUnmarkDeleteIndex(String command) {
String otherThanFirstWord = getOtherThanFirstWord(command);
return Integer.parseInt(otherThanFirstWord);
}

// Gets the description for a todo command.
private String getTodoDescription(String command) throws EmptyDescriptionException {
try {
return getOtherThanFirstWord(command);
} catch (ArrayIndexOutOfBoundsException e) {
throw new EmptyDescriptionException("OOPS!!! The description of a todo cannot be empty.");
}
}

// Gets the description for a deadline command.
private String getDeadlineDescription(String command) throws EmptyDescriptionException {
try {
String otherThanFirstWord = getOtherThanFirstWord(command);
return otherThanFirstWord.split(" /by ", 2)[0];
} catch (ArrayIndexOutOfBoundsException e) {
throw new EmptyDescriptionException("OOPS!!! The description of a deadline cannot be empty.");
}
}

// Gets the deadline for a deadline command.
private String getDeadlineBy(String command) {
String otherThanFirstWord = getOtherThanFirstWord(command);
return otherThanFirstWord.split(" /by ", 2)[1];
}

// Gets the description for an event command.
private String getEventDescription(String command) throws EmptyDescriptionException {
try {
String otherThanFirstWord = getOtherThanFirstWord(command);
return otherThanFirstWord.split(" /from ", 2)[0];
} catch (ArrayIndexOutOfBoundsException e) {
throw new EmptyDescriptionException("OOPS!!! The description of an event cannot be empty.");
}
}

// Gets the start for a deadline command.
private String getEventFrom(String command) {
String otherThanFirstWord = getOtherThanFirstWord(command);
String fromAndTo = otherThanFirstWord.split(" /from ", 2)[1];
return fromAndTo.split(" /to ", 2)[0];
}

// Gets the end for a deadline command.
private String getEventTo(String command) {
String otherThanFirstWord = getOtherThanFirstWord(command);
String fromAndTo = otherThanFirstWord.split(" /from ", 2)[1];
return fromAndTo.split(" /to ", 2)[1];
}

// The task list.
ArrayList<Task> taskList = new ArrayList<>();

// Gets the number of tasks in the task list.
private int numTasks() {
return taskList.size();
}

// Gets the i-th task from the task list, where i is the index.
private Task getTask(int index) {
return taskList.get(index - 1);
}

// Adds a todo task to the task list.
private void addTodo(String description) {
Todo todo = new Todo(description);
taskList.add(todo);
int numTasks = numTasks();
String sOrNone = numTasks == 1 ? "" : "s";
printHorizontalLine();
printWithIndent(" Got it. I've added this task:");
printWithIndent(" " + todo);
printWithIndent(" Now you have " + numTasks + " task" + sOrNone + " in the list.");
printHorizontalLine();
}

// Adds a deadline task to the task list.
private void addDeadline(String description, String by) {
Deadline deadline = new Deadline(description, by);
taskList.add(deadline);
int numTasks = numTasks();
String sOrNone = numTasks == 1 ? "" : "s";
printHorizontalLine();
printWithIndent(" Got it. I've added this task:");
printWithIndent(" " + deadline);
printWithIndent(" Now you have " + numTasks + " task" + sOrNone + " in the list.");
printHorizontalLine();
}

// Adds an event task to the task list.
private void addEvent(String description, String from, String to) {
Event event = new Event(description, from, to);
taskList.add(event);
int numTasks = numTasks();
String sOrNone = numTasks == 1 ? "" : "s";
printHorizontalLine();
printWithIndent(" Got it. I've added this task:");
printWithIndent(" " + event);
printWithIndent(" Now you have " + numTasks + " task" + sOrNone + " in the list.");
printHorizontalLine();
}

// Marks the i-th task in the task list as done, where i is the index.
private void markTaskAsDone(int index) {
Task task = getTask(index);
task.markAsDone();
printHorizontalLine();
printWithIndent(" Nice! I've marked this task as done:");
printWithIndent(" " + task);
printHorizontalLine();
}

// Marks the i-th task in the task list as not done, where i is the index.
private void markTaskAsNotDone(int index) {
Task task = getTask(index);
task.markAsNotDone();
printHorizontalLine();
printWithIndent(" OK, I've marked this task as not done yet:");
printWithIndent(" " + task);
printHorizontalLine();
}

// Prints the task list.
private void printTaskList() {
int numOfTasks = taskList.size();
printHorizontalLine();
printWithIndent(" Here are the tasks in your list:");

for (int index = 1; index <= numOfTasks; index++) {
printWithIndent(" " + index + "." + taskList.get(index - 1));
}

printHorizontalLine();
}

// Deletes the i-th task in the task list, where i is the index.
private void deleteTask(int index) {
Task deletedTask = taskList.remove(index - 1);
int numTasks = numTasks();
printHorizontalLine();
printWithIndent(" Noted. I've removed this task:");
printWithIndent(" " + deletedTask);
printWithIndent(" Now you have " + numTasks + " tasks in the list.");
printHorizontalLine();
}

// Handles the command input by the user.
private void handleCommand(String command) throws UnknownCommandException {
try {
if (isCommandMark(command)) {
markTaskAsDone(getMarkUnmarkDeleteIndex(command));
} else if (isCommandUnmark(command)) {
markTaskAsNotDone(getMarkUnmarkDeleteIndex(command));
} else if (isCommandList(command)) {
printTaskList();
} else if (isCommandTodo(command)) {
addTodo(getTodoDescription(command));
} else if (isCommandDeadline(command)) {
addDeadline(getDeadlineDescription(command), getDeadlineBy(command));
} else if (isCommandEvent(command)) {
addEvent(getEventDescription(command), getEventFrom(command), getEventTo(command));
} else if (isCommandDelete(command)) {
deleteTask(getMarkUnmarkDeleteIndex(command));
} else {
throw new UnknownCommandException("OOPS!!! I'm sorry, but I don't know what that means :-(");
}
} catch (EmptyDescriptionException e) {
printHorizontalLine();
System.out.println(" " + e.getMessage());
printHorizontalLine();
}
}

// Takes inputs from user until bye has been input.
private void takeInputsUntilBye() {

Choose a reason for hiding this comment

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

method name although is intuitive , but it can be made a bit more general e.g userInput

// Waits for command from user
String command = sc.nextLine();

// While user hasn't input bye, add task to task list
while (!isCommandBye(command)) {
// If list is input, print list, else add task to list
try {
handleCommand(command);
} catch (UnknownCommandException e) {
printHorizontalLine();
System.out.println(" " + e.getMessage());
printHorizontalLine();
} finally {
command = sc.nextLine();
}
}
}

// Runs the chatbot.
private void run() {
greet();
takeInputsUntilBye();
bye();
}

public static void main(String[] args) {
String logo = " ____ _ \n"
+ "| _ \\ _ _| | _____ \n"
+ "| | | | | | | |/ / _ \\\n"
+ "| |_| | |_| | < __/\n"
+ "|____/ \\__,_|_|\\_\\___|\n";
System.out.println("Hello from\n" + logo);
Duke virtue = new Duke();
virtue.run();
}
}
Loading