Skip to content

Commit

Permalink
Update JavaDoc
Browse files Browse the repository at this point in the history
  • Loading branch information
QianChangru committed Sep 10, 2023
1 parent dcc4829 commit b0af1ec
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 16 deletions.
1 change: 0 additions & 1 deletion data/tasks.txt
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
0 | test0 | test0 | test
6 changes: 6 additions & 0 deletions src/main/java/duke/Duke.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

import java.io.IOException;

/**
* Encapsulates the chat bot.
*/
public class Duke {

private Storage storage;
Expand All @@ -24,6 +27,9 @@ public Duke(String filePath) {
}
}

/**
* Runs the program.
*/
public void run() {
Ui.welcomeMessage();
Scanner userInput = new Scanner(System.in);
Expand Down
55 changes: 41 additions & 14 deletions src/main/java/duke/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,22 @@
import java.util.ArrayList;
import java.util.Scanner;

/**
* Manages storage of the task list.
*/
public class Storage {
private final String filePath;
private String filePath;

public Storage(String filePath) {
this.filePath = filePath;
}

/**
* Loads the task list with file.
*
* @return The task list.
* @throws IOException If unable to gain input from file.
*/
public ArrayList<Task> load() throws IOException {
File dir = new File("./data");
if (!dir.exists()) {
Expand All @@ -28,12 +37,18 @@ public ArrayList<Task> load() throws IOException {
Scanner s = new Scanner(f);
ArrayList<Task> tasks = new ArrayList<>();
while (s.hasNext()) {
tasks.add(addTask(s.nextLine()));
tasks.add(inputToTask(s.nextLine()));
}
return tasks;
}

private Task addTask(String input) {
/**
* Converts the String input to Task.
* @param input The String input.
* @return The corresponding Task.
*/
private Task inputToTask(String input) {
String taskType = input.split(" \\| ")[0];
boolean isComplete = input.split(" \\| ")[1].equals("1");
String description = input.split(" \\| ")[2];
Expand All @@ -43,35 +58,47 @@ private Task addTask(String input) {
LocalDate d = LocalDate.parse(input.split(" \\| ")[3], DateTimeFormatter.ofPattern("MMM dd yyyy", Locale.ENGLISH));
return new Deadline(description, isComplete, d);
} else {
LocalDate start = LocalDate.parse(input.split(" \\| ")[3], DateTimeFormatter.ofPattern("MMM dd yyyy", Locale.ENGLISH));
LocalDate end = LocalDate.parse(input.split(" \\| ")[4], DateTimeFormatter.ofPattern("MMM dd yyyy", Locale.ENGLISH));
LocalDate start = LocalDate.parse(input.split(" \\| ")[3],
DateTimeFormatter.ofPattern("MMM dd yyyy", Locale.ENGLISH));
LocalDate end = LocalDate.parse(input.split(" \\| ")[4],
DateTimeFormatter.ofPattern("MMM dd yyyy", Locale.ENGLISH));
return new Event(description, isComplete, start, end);
}
}


public void addOneLineToFile(ArrayList<Task> list) throws DukeException {
/**
* Adds the last task in the task list to file.
*
* @param list The task list.
* @throws DukeException If unable to write to file.
*/
public void addTheLastTaskToFile(ArrayList<Task> list) throws DukeException {
try {
FileWriter fw = new FileWriter(filePath, true);
if (list.size() == 1) {
fw.write(list.get(0).toTxt());
} else {
fw.write("\n" + list.get(list.size() - 1).toTxt());
if (list.size() != 1) {
fw.write("\n");
}
fw.write(list.get(list.size() - 1).toTxt());
fw.close();
} catch (IOException e) {
throw new DukeException("Unable to write to file.");
}
}

/**
* Rewrites the whole file with the task list.
*
* @param list The task list.
* @throws DukeException If unable to write to file.
*/
public void rewriteFile(ArrayList<Task> list) throws DukeException {
try {
FileWriter fw = new FileWriter(filePath);
for (int i = 0; i < list.size(); i++) {
if (i == list.size() - 1) {
fw.write(list.get(i).toTxt());
} else {
fw.write(list.get(i).toTxt() + "\n");
fw.write(list.get(i).toTxt());
if (i != list.size() - 1) {
fw.write("\n");
}
}
fw.close();
Expand Down
23 changes: 22 additions & 1 deletion src/main/java/duke/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import java.util.ArrayList;

/**
* Encapsulates task list in the chat bot.
*/
public class TaskList {
protected ArrayList<Task> tasks;
private Storage storage;
Expand All @@ -16,22 +19,40 @@ public TaskList(ArrayList<Task> tasks, Storage storage) {
this.storage = storage;
}

/**
* Lists all tasks in the task list.
*/
public void listTasks() {
Ui.listTasks(this.tasks);
}

/**
* Adds one task to the task list.
*
* @param t The task to be added.
*/
public void addTask(Task t) {
this.tasks.add(t);
Ui.addTask(t, this.tasks);
this.storage.addOneLineToFile(this.tasks);
this.storage.addTheLastTaskToFile(this.tasks);
}

/**
* Deletes the task at the specified position in the task list.
*
* @param num The position starts from 0.
*/
public void deleteTask(int num) {
Task re = tasks.remove(num);
this.storage.rewriteFile(tasks);
Ui.deleteTask(re, tasks);
}

/**
* Marks the task at the specified position in the task list.
*
* @param num The position starts from 0.
*/
public void markTask(int num) {
Task t = tasks.get(num);
t.markAsDone();
Expand Down
7 changes: 7 additions & 0 deletions src/test/java/duke/TaskListTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@

import static org.junit.jupiter.api.Assertions.assertEquals;

/**
* Tests TaskList class.
*/
public class TaskListTest {

/**
* Tests addTask function.
*/
@Test
public void addTask() {
Task t = new Task("test");
Expand Down
7 changes: 7 additions & 0 deletions src/test/java/duke/TaskTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@

import static org.junit.jupiter.api.Assertions.assertEquals;

/**
* Tests task class.
*/
public class TaskTest {

/**
* Tests markAsDone function.
*/
@Test
public void markAsDone() {
Task t = new Task("test", false);
Expand Down

0 comments on commit b0af1ec

Please sign in to comment.