Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
22 changes: 16 additions & 6 deletions src/main/java/sparkle/core/Parser.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
package sparkle.core;

/**
* The {@code Parser} class is responsible for processing user input. It splits the input into a
* command and its associated details.
*/
public class Parser {

public static String[] parse(String userInput) {
String[] commandParts = userInput.split(" ", 2);
String command = commandParts[0].toLowerCase();
String details = commandParts.length > 1 ? commandParts[1].trim() : "";
return new String[]{command, details};
}
/**
* Parses the user input into a command and its details.
*
* @param userInput The raw user input string.
* @return An array where the first element is the command and the second element is the details.
*/
public static String[] parse(String userInput) {
String[] commandParts = userInput.split(" ", 2);
String command = commandParts[0].toLowerCase();
String details = commandParts.length > 1 ? commandParts[1].trim() : "";
return new String[] {command, details};
}
}
2 changes: 1 addition & 1 deletion src/main/java/sparkle/core/Sparkle.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,4 @@ public void run() {
public static void main(String[] args) {
new Sparkle("data/sparkle.txt").run();
}
}
}
83 changes: 48 additions & 35 deletions src/main/java/sparkle/core/Storage.java
Original file line number Diff line number Diff line change
@@ -1,47 +1,60 @@
/** Handles loading and saving of tasks from and to a file. */
package sparkle.core;

import sparkle.exception.SparkleException;
import sparkle.task.Task;

import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
import sparkle.exception.SparkleException;
import sparkle.task.Task;

public class Storage {

private static final String FILE_PATH = "./data/sparkle.txt";
private static final String DIRECTORY_PATH = "./data";

public ArrayList<Task> load() throws SparkleException {
ArrayList<Task> newtasks = new ArrayList<>();
File file = new File(FILE_PATH);

if (!file.exists()) {
return newtasks;
}

try (Scanner scanner = new Scanner(file)) {
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
String[] parts = line.split(" \\| ");
newtasks.add(Task.fromFileFormat(parts));
}
} catch (FileNotFoundException e) {
throw new SparkleException(SparkleException.ErrorType.INVALID_FORMAT, "That file's playing hide and seek… and winning!");
}
return newtasks;
private static final String FILE_PATH = "./data/sparkle.txt";
private static final String DIRECTORY_PATH = "./data";

/**
* Loads tasks from the specified file.
*
* @return A list of tasks loaded from the file.
* @throws SparkleException If the file format is invalid or cannot be read.
*/
public ArrayList<Task> load() throws SparkleException {
ArrayList<Task> newtasks = new ArrayList<>();
File file = new File(FILE_PATH);

if (!file.exists()) {
return newtasks;
}

public void save(ArrayList<Task> tasks) {
try {
new File(DIRECTORY_PATH).mkdirs();
BufferedWriter writer = new BufferedWriter(new FileWriter(FILE_PATH));
for (Task task : tasks) {
writer.write(task.toFileFormat() + "\n");
}
writer.close();
} catch (IOException e) {
System.out.println(" Task list just went poof! Try saving again!");
}
try (Scanner scanner = new Scanner(file)) {
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
String[] parts = line.split(" \\| ");
newtasks.add(Task.fromFileFormat(parts));
}
} catch (FileNotFoundException e) {
throw new SparkleException(
SparkleException.ErrorType.INVALID_FORMAT,
"That file's playing hide and seek… and winning!");
}
return newtasks;
}

/**
* Saves the given list of tasks to the file.
*
* @param tasks The list of tasks to save.
*/
public void save(ArrayList<Task> tasks) {
try {
new File(DIRECTORY_PATH).mkdirs();
BufferedWriter writer = new BufferedWriter(new FileWriter(FILE_PATH));
for (Task task : tasks) {
writer.write(task.toFileFormat() + "\n");
}
writer.close();
} catch (IOException e) {
System.out.println(" Task list just went poof! Try saving again!");
}
}
}
113 changes: 81 additions & 32 deletions src/main/java/sparkle/core/TaskList.java
Original file line number Diff line number Diff line change
@@ -1,51 +1,96 @@
package sparkle.core;

import sparkle.exception.SparkleException;
import java.util.ArrayList;
import sparkle.exception.SparkleException;
import sparkle.task.Task;

/**
* The {@code TaskList} class manages a collection of tasks. It provides methods to add, remove, and
* modify tasks.
*/
public class TaskList {

private ArrayList<Task> tasks;
private ArrayList<Task> tasks;

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

public TaskList(ArrayList<Task> tasks) {
this.tasks = tasks;
}
/**
* Constructs a {@code TaskList} with the given list of tasks.
*
* @param tasks The initial list of tasks.
*/
public TaskList(ArrayList<Task> tasks) {
this.tasks = tasks;
}

public void addTask(Task task) {
tasks.add(task);
}
/**
* Adds a task to the list.
*
* @param task The task to be added.
*/
public void addTask(Task task) {
tasks.add(task);
}

public Task deleteTask(int taskNumber) throws SparkleException {
if (taskNumber < 0 || taskNumber >= tasks.size()) {
throw new SparkleException(SparkleException.ErrorType.INVALID_TASK_NUMBER, "");
}
Task deleteTask = tasks.get(taskNumber);
tasks.remove(taskNumber);
return deleteTask;
/**
* Deletes a task from the list.
*
* @param taskNumber The index of the task to be deleted.
* @return The deleted task.
* @throws SparkleException If the task number is invalid.
*/
public Task deleteTask(int taskNumber) throws SparkleException {
if (taskNumber < 0 || taskNumber >= tasks.size()) {
throw new SparkleException(SparkleException.ErrorType.INVALID_TASK_NUMBER, "");
}
Task deleteTask = tasks.get(taskNumber);
tasks.remove(taskNumber);
return deleteTask;
}

public void markTaskAsDone(int taskNumber) throws SparkleException {
if (taskNumber < 0 || taskNumber >= tasks.size()) {
throw new SparkleException(SparkleException.ErrorType.INVALID_TASK_NUMBER, "");
}
tasks.get(taskNumber).markAsDone();
/**
* Marks a task as done.
*
* @param taskNumber The index of the task to be marked as done.
* @throws SparkleException If the task number is invalid.
*/
public void markTaskAsDone(int taskNumber) throws SparkleException {
if (taskNumber < 0 || taskNumber >= tasks.size()) {
throw new SparkleException(SparkleException.ErrorType.INVALID_TASK_NUMBER, "");
}
tasks.get(taskNumber).markAsDone();
}

public void markTaskAsUndone(int taskNumber) throws SparkleException {
if (taskNumber < 0 || taskNumber >= tasks.size()) {
throw new SparkleException(SparkleException.ErrorType.INVALID_TASK_NUMBER, "");
}
tasks.get(taskNumber).markAsUndone();
/**
* Marks a task as undone.
*
* @param taskNumber The index of the task to be marked as undone.
* @throws SparkleException If the task number is invalid.
*/
public void markTaskAsUndone(int taskNumber) throws SparkleException {
if (taskNumber < 0 || taskNumber >= tasks.size()) {
throw new SparkleException(SparkleException.ErrorType.INVALID_TASK_NUMBER, "");
}
tasks.get(taskNumber).markAsUndone();
}

public ArrayList<Task> getTasks() {
return tasks;
}
/**
* Returns the list of tasks.
*
* @return The list of tasks.
*/
public ArrayList<Task> getTasks() {
return tasks;
}

/**
* Returns the number of tasks in the list.
*
* @return The number of tasks.
*/

public ArrayList<Task> findTasks(String keyword) {
ArrayList<Task> matchingTasks = new ArrayList<>();
Expand All @@ -56,7 +101,11 @@ public ArrayList<Task> findTasks(String keyword) {
}
return matchingTasks;
}

 /**
* Returns the number of tasks in the list.
*
* @return The number of tasks.
*/
public int size() {
return tasks.size();
}
Expand Down
Loading