Skip to content

Commit

Permalink
Final version
Browse files Browse the repository at this point in the history
  • Loading branch information
wongwh2002 committed Oct 11, 2024
1 parent 183a60f commit f83f485
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 48 deletions.
49 changes: 31 additions & 18 deletions src/main/java/classes/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,30 @@

import java.util.Scanner;

/**
* Handles the parsing of user input and execution of commands.
*/
public class Parser {
private final TaskList taskList;
private final Ui ui;
private final Storage storage;

/**
* Constructs a Parser object.
*
* @param taskList the task list to be managed
* @param ui the user interface for displaying messages
* @param storage the storage for loading and saving tasks
*/
public Parser(TaskList taskList, Ui ui, Storage storage) {
this.taskList = taskList;
this.ui = ui;
this.storage = storage;
}

/**
* Handles user input and executes the corresponding commands.
*/
public void handleUserInput() {
Scanner scanner = new Scanner(System.in);
boolean isLooping = true;
Expand All @@ -39,24 +52,24 @@ public void handleUserInput() {

private boolean processCommand(String[] currLine) throws DescriptionEmptyException, IllegalCommandException, MissingDatesException {
switch (CommandType.valueOf(currLine[0].toUpperCase())) {
case BYE:
new ExitCommand().execute(taskList, ui, storage);
return false;
case LIST:
new ListCommand().execute(taskList, ui, storage);
break;
case DELETE:
new DeleteCommand(currLine).execute(taskList, ui, storage);
break;
case ON:
taskList.listTasksOnDate(currLine);
break;
case FIND:
taskList.findTasksByKeyword(currLine);
break;
default:
new AddCommand(currLine).execute(taskList, ui, storage);
break;
case BYE:
new ExitCommand().execute(taskList, ui, storage);
return false;
case LIST:
new ListCommand().execute(taskList, ui, storage);
break;
case DELETE:
new DeleteCommand(currLine).execute(taskList, ui, storage);
break;
case ON:
taskList.listTasksOnDate(currLine);
break;
case FIND:
taskList.findTasksByKeyword(currLine);
break;
default:
new AddCommand(currLine).execute(taskList, ui, storage);
break;
}
return true;
}
Expand Down
41 changes: 28 additions & 13 deletions src/main/java/classes/Storage.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,33 @@
package classes;

import tasks.Task;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

import tasks.Task;

/**
* Handles the loading and saving of tasks to and from a file.
*/
public class Storage {
private final TaskList taskList;
private final Ui ui;

/**
* Constructs a Storage object.
*
* @param taskList the task list to be managed
* @param ui the user interface for displaying messages
*/
public Storage(TaskList taskList, Ui ui) {
this.taskList = taskList;
this.ui = ui;
}

/**
* Loads tasks from the file.
*/
public void loadTasks() {
ui.printWithSeparators(Constants.LOADING_TASKS_MESSAGE);
File file = new File(Constants.FILE_PATH);
Expand Down Expand Up @@ -49,20 +61,23 @@ private void processTaskLine(String line) {
String[] currLine = line.split("\\|");
boolean isDone = currLine[1].trim().equals(Constants.TASK_DONE);
switch (currLine[0].trim()) {
case Constants.TASK_TYPE_TODO:
taskList.addTodoFromFile(currLine[2], isDone);
break;
case Constants.TASK_TYPE_DEADLINE:
taskList.addDeadlineFromFile(currLine[2].trim(), currLine[3].trim(), isDone);
break;
case Constants.TASK_TYPE_EVENT:
taskList.addEventFromFile(currLine[2].trim(), currLine[3].trim(), currLine[4].trim(), isDone);
break;
default:
throw new IllegalArgumentException(Constants.ERROR_UNKNOWN_TASK_TYPE);
case Constants.TASK_TYPE_TODO:
taskList.addTodoFromFile(currLine[2], isDone);
break;
case Constants.TASK_TYPE_DEADLINE:
taskList.addDeadlineFromFile(currLine[2].trim(), currLine[3].trim(), isDone);
break;
case Constants.TASK_TYPE_EVENT:
taskList.addEventFromFile(currLine[2].trim(), currLine[3].trim(), currLine[4].trim(), isDone);
break;
default:
throw new IllegalArgumentException(Constants.ERROR_UNKNOWN_TASK_TYPE);
}
}

/**
* Saves tasks to the file.
*/
public void saveTasks() {
ui.printWithSeparators(Constants.SAVING_TASKS_MESSAGE);
try (FileWriter fw = new FileWriter(Constants.FILE_PATH)) {
Expand Down
87 changes: 70 additions & 17 deletions src/main/java/classes/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,37 +18,59 @@

import static java.lang.Integer.parseInt;

/**
* Manages the list of tasks and provides methods to manipulate them.
*/
public class TaskList {
private final ArrayList<Task> tasks = new ArrayList<>();
private final Ui ui;

/**
* Constructs a TaskList object.
*
* @param ui the user interface for displaying messages
*/
public TaskList(Ui ui) {
this.ui = ui;
}

/**
* Handles multi-word commands and delegates to the appropriate methods.
*
* @param currLine the command and its arguments
* @throws DescriptionEmptyException if the description is empty
* @throws MissingDatesException if the dates are missing
* @throws IllegalCommandException if the command is illegal
*/
public void handleMultiWordCommands(String[] currLine) throws DescriptionEmptyException, MissingDatesException, IllegalCommandException {
if (currLine.length < 2) {
throw new DescriptionEmptyException("Please give correct input! " + currLine[0] + " is not enough!!");
}
CommandType commandType = CommandType.valueOf(currLine[0].toUpperCase());
switch (commandType) {
case TODO:
addTodoFromInput(currLine);
break;
case DEADLINE:
addDeadlineFromInput(currLine);
break;
case EVENT:
addEventFromInput(currLine);
break;
case UNMARK:
unmarkTask(currLine);
break;
case MARK:
markTask(currLine);
break;
default:
throw new IllegalCommandException("I'm sorry, but I don't know what that means :-(");
case TODO:
addTodoFromInput(currLine);
break;
case DEADLINE:
addDeadlineFromInput(currLine);
break;
case EVENT:
addEventFromInput(currLine);
break;
case UNMARK:
unmarkTask(currLine);
break;
case MARK:
markTask(currLine);
break;
case DELETE:
deleteTask(currLine);
break;
case ON:
listTasksOnDate(currLine);
break;
default:
throw new IllegalCommandException("I'm sorry, but I don't know what that means :-(");
}
}

Expand Down Expand Up @@ -111,6 +133,15 @@ private void addAndPrintHandler(Task newTask) {
ui.printWithSeparators("Got it. I've added this task:\n\t" + newTask.toString() + "\n\tNow you have " + getTotalNumTasks() + " tasks in the list.");
}

public void deleteTask(String[] currLine) {
int index = parseInt(currLine[1]);
if (index >= getTotalNumTasks() || getTotalNumTasks() == 0) {
throw new IndexOutOfBoundsException();
}
Task removedTask = tasks.remove(index);
ui.printWithSeparators("Noted. I've removed this task:\n\t" + removedTask.toString() + "\n\tNow you have " + getTotalNumTasks() + " tasks in the list.");
}

public void unmarkTask(String[] currLine) {
int index = parseInt(currLine[1]);
if (index >= getTotalNumTasks()) {
Expand Down Expand Up @@ -139,6 +170,22 @@ public void markTask(String[] currLine) {
ui.printWithSeparators("Nice! I've marked this task as done:\n\t" + tasks.get(index).toString());
}

/**
* Lists all tasks.
*/
public void listTasks() {
StringBuilder sb = new StringBuilder("Here are the tasks in your list:");
for (int i = 0; i < getTotalNumTasks(); i++) {
sb.append(String.format("\n\t%d. %s", i, tasks.get(i)));
}
ui.printWithSeparators(sb.toString());
}

/**
* Lists tasks on a specific date.
*
* @param currLine the command and its arguments
*/
public void listTasksOnDate(String[] currLine) {
if (currLine.length < 2) {
ui.printWithSeparators("Please provide a date in the format yyyy/MM/dd.");
Expand All @@ -164,6 +211,12 @@ private void listTasksOnDateHelper(StringBuilder sb, LocalDate date) {
}
}
}

/**
* Finds tasks by a keyword.
*
* @param currLine the command and its arguments
*/
public void findTasksByKeyword(String[] currLine) {
if (currLine.length < 2) {
ui.printWithSeparators("Please provide a keyword to search for.");
Expand Down

0 comments on commit f83f485

Please sign in to comment.