Skip to content

Commit

Permalink
Merge branch 'A-JavaDoc'
Browse files Browse the repository at this point in the history
  • Loading branch information
AdiMangalam committed Oct 11, 2024
2 parents 60e059c + a91164f commit 9aa9f02
Show file tree
Hide file tree
Showing 9 changed files with 303 additions and 48 deletions.
8 changes: 5 additions & 3 deletions data/flash.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
D | 1 | return book | June 6th
T | 0 | join sports club
D | 0 | return book | June 6th
T | 1 | join sports club
T | 0 | complete project
T | 0 | test
T | 0 | test2
D | 0 | test2 | 1
T | 0 | test
D | 0 | test idk | 4
E | 0 | test 23 | today | tomorrow
T | 0 | test test
D | 0 | djnsakj | dsnad
E | 0 | fdsnfds | dasdsa | fadda
78 changes: 49 additions & 29 deletions src/main/java/Flash.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
import exception.FlashException;

import java.util.Scanner;

/**
* Main class for the Flash chatbot.
* Handles interaction with the user and manages tasks through commands.
*/
public class Flash {

private static final String FILE_PATH = "./data/flash.txt";
private static Storage storage;
private static TaskList taskList;

/**
* The entry point of the application.
* Initializes storage and task list, loads tasks from the file,
* and continuously listens for user commands.
*
* @param args the command-line arguments
*/
public static void main(String[] args) {
storage = new Storage(FILE_PATH);
taskList = new TaskList();
Expand All @@ -23,39 +33,49 @@ public static void main(String[] args) {
Scanner in = UI.readCommand();
UI.displayWelcomeMessage();

while(true) {
while (true) {
try {
String input = in.nextLine();
String command = Parser.parseCommand(input);

if (command.equalsIgnoreCase("bye")) {
UI.displayByeMessage();
break;
} else if (command.equals("list")) {
UI.displayTasks(taskList.tasks);
} else if (command.equals("mark")) {
taskList.markTask(input);
storage.save(taskList.tasks);
} else if (command.equals("unmark")) {
taskList.unMarkTask(input);
storage.save(taskList.tasks);
} else if (command.equals("todo")) {
taskList.addTodo(input);
storage.save(taskList.tasks);
} else if (command.equals("deadline")) {
taskList.addDeadline(input);
storage.save(taskList.tasks);
} else if (command.equals("event")) {
taskList.addEvent(input);
} else if (command.equals("delete")) {
taskList.deleteTask(input);
storage.save(taskList.tasks);
} else if (command.equals("find")) {
taskList.listMatchedTasks(input);
} else {
throw new FlashException("Uh-oh! I don't know what that means.");
switch (command) {
case "bye":
UI.displayByeMessage();
return;
case "list":
UI.displayTasks(taskList.tasks);
break;
case "mark":
taskList.markTask(input);
storage.save(taskList.tasks);
break;
case "unmark":
taskList.unMarkTask(input);
storage.save(taskList.tasks);
break;
case "todo":
taskList.addTodo(input);
storage.save(taskList.tasks);
break;
case "deadline":
taskList.addDeadline(input);
storage.save(taskList.tasks);
break;
case "event":
taskList.addEvent(input);
storage.save(taskList.tasks);
break;
case "delete":
taskList.deleteTask(input);
storage.save(taskList.tasks);
break;
case "find":
taskList.listMatchedTasks(input);
break;
default:
throw new FlashException("Uh-oh! I don't know what that means.");
}
} catch (FlashException e){
} catch (FlashException e) {
System.out.println("____________________________________________________________");
System.out.println(e.getMessage());
System.out.println("____________________________________________________________");
Expand Down
43 changes: 40 additions & 3 deletions src/main/java/Parser.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
/**
* Provides utility methods for parsing user input commands.
*/
public class Parser {

/**
* Parses the command from user input.
*
* @param input the user input string
* @return the command as a lowercase string, or an empty string if input is empty
*/
public static String parseCommand(String input) {
// Trim leading/trailing spaces
input = input.trim();
if (input.isEmpty()) {
return "";
}

// Find the index of the first space
int spaceIndex = input.indexOf(' ');
if (spaceIndex == -1) {
return input.toLowerCase();
Expand All @@ -15,21 +22,45 @@ public static String parseCommand(String input) {
}
}

/**
* Extracts the task number from the input.
*
* @param input the user input string
* @return the task number as an integer (0-indexed)
*/
static int parseTaskNumber(String input) {
return Integer.parseInt(input.split(" ")[1]) - 1;
}

/**
* Parses the description of a ToDo task from the input.
*
* @param input the user input string
* @return the description of the ToDo task
*/
static String parseTodo(String input) {
return input.substring(5).trim();
}

/**
* Parses a Deadline task's description and deadline.
*
* @param input the user input string
* @return an array containing the task description and deadline
*/
public static String[] parseDeadline(String input) {
String[] parts = input.replaceFirst("deadline ", "").split(" /by ");
String description = parts[0].trim();
String by = parts[1].trim();
return new String[]{description, by};
}

/**
* Parses an Event task's description, start, and end times.
*
* @param input the user input string
* @return an array containing the task description, start time, and end time
*/
public static String[] parseEvent(String input) {
String[] parts = input.replaceFirst("event ", "").split(" /from | /to ");
String description = parts[0].trim();
Expand All @@ -38,6 +69,12 @@ public static String[] parseEvent(String input) {
return new String[]{description, from, to};
}

/**
* Extracts the keyword from the input.
*
* @param input the user input string
* @return the search keyword
*/
public static String parseKeyword(String input) {
String[] parts = input.split(" ", 2);
return parts[1];
Expand Down
55 changes: 51 additions & 4 deletions src/main/java/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,25 @@
import java.util.ArrayList;
import java.util.List;

/**
* Manages the list of tasks and provides methods to add, delete, and modify tasks.
*/
public class TaskList {
public List<Task> tasks;

/**
* Constructs an empty TaskList.
*/
public TaskList() {
this.tasks = new ArrayList<>();
}

/**
* Adds a deadline task to the list.
*
* @param input the user's input for the deadline
* @throws FlashException if the input is invalid or missing details
*/
public void addDeadline(String input) throws FlashException {
try {
String[] parsedInput = Parser.parseDeadline(input);
Expand All @@ -27,6 +39,12 @@ public void addDeadline(String input) throws FlashException {
}
}

/**
* Adds an event task to the list.
*
* @param input the user's input for the event
* @throws FlashException if the input is invalid or missing details
*/
public void addEvent(String input) throws FlashException {
try {
String[] parsedInput = Parser.parseEvent(input);
Expand All @@ -41,19 +59,31 @@ public void addEvent(String input) throws FlashException {
}
}

/**
* Deletes a task from the list by its number.
*
* @param input the user's input for the task number
* @throws FlashException if the task number is invalid
*/
public void deleteTask(String input) throws FlashException {
try {
int taskNumber = Parser.parseTaskNumber(input);
Task task = tasks.get(taskNumber);
tasks.remove(taskNumber);
UI.displayTaskDeleted(tasks, task);
} catch (ArrayIndexOutOfBoundsException | NumberFormatException e) {
throw new FlashException("Uh-oh! task.Task number is needed for deletion. Enter a valid task number.");
throw new FlashException("Uh-oh! Task number is needed for deletion. Enter a valid task number.");
} catch (IndexOutOfBoundsException e) {
throw new FlashException("Invalid task number. Please enter a valid task number.");
}
}

/**
* Adds a todo task to the list.
*
* @param input the user's input for the todo
* @throws FlashException if the todo description is empty
*/
public void addTodo(String input) throws FlashException {
if (input.length() <= 5) {
throw new FlashException("Uh-oh! Description for Todo Needed!! Cannot be left empty.");
Expand All @@ -69,6 +99,12 @@ public void addTodo(String input) throws FlashException {
UI.displayTaskAdded(tasks, task);
}

/**
* Marks a task as not done.
*
* @param input the user's input for the task number
* @throws FlashException if the task number is invalid
*/
public void unMarkTask(String input) throws FlashException {
try {
int taskNumber = Parser.parseTaskNumber(input);
Expand All @@ -80,6 +116,12 @@ public void unMarkTask(String input) throws FlashException {
}
}

/**
* Marks a task as done.
*
* @param input the user's input for the task number
* @throws FlashException if the task number is invalid
*/
public void markTask(String input) throws FlashException {
try {
int taskNumber = Parser.parseTaskNumber(input);
Expand All @@ -91,13 +133,18 @@ public void markTask(String input) throws FlashException {
}
}

/**
* Lists all tasks that match a keyword.
*
* @param input the user's input for the search keyword
* @throws FlashException if the input is invalid
*/
public void listMatchedTasks(String input) throws FlashException {
try{
try {
String keyword = Parser.parseKeyword(input);
int index = 1;
List<Task> matchedTasks = new ArrayList<>();
for (Task task : this.tasks) {
if(task.getDescription().contains(keyword)) {
if (task.getDescription().contains(keyword)) {
matchedTasks.add(task);
}
}
Expand Down
Loading

0 comments on commit 9aa9f02

Please sign in to comment.