Skip to content

Commit

Permalink
Merge branch 'master' into branch-Level-9
Browse files Browse the repository at this point in the history
  • Loading branch information
QianChangru committed Sep 10, 2023
2 parents 8ac5391 + c529dc9 commit b88d605
Show file tree
Hide file tree
Showing 13 changed files with 215 additions and 23 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
13 changes: 11 additions & 2 deletions src/main/java/duke/Deadline.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
import java.time.format.DateTimeFormatter;
import java.util.Locale;


/**
* Encapsulates deadline which are Tasks with start and end.
*/
public class Deadline extends Task {
private LocalDate by;
public Deadline(String description, LocalDate by) {
Expand All @@ -19,12 +23,17 @@ public Deadline(String description, boolean isDone, LocalDate by) {
@Override
public String toString() {
return "[D]" + super.toString() + " (by: "
+ this.by.format(DateTimeFormatter.ofPattern("MMM dd yyyy", Locale.ENGLISH)) + ")";
+ this.by.format(DateTimeFormatter.ofPattern("MMM dd yyyy", Locale.ENGLISH))
+ ")";
}

/**
* Returns a String representation of the task for storage.
*/
@Override
public String toTxt() {
return "D | " + super.toTxt()
+ " | " + this.by.format(DateTimeFormatter.ofPattern("MMM dd yyyy", Locale.ENGLISH));
+ " | "
+ this.by.format(DateTimeFormatter.ofPattern("MMM dd yyyy", Locale.ENGLISH));
}
}
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
3 changes: 3 additions & 0 deletions src/main/java/duke/DukeException.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package duke;

/**
* Encapsulates exception for the chat bot.
*/
public class DukeException extends RuntimeException{
public DukeException(String message) {
super(message);
Expand Down
15 changes: 12 additions & 3 deletions src/main/java/duke/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import java.time.format.DateTimeFormatter;
import java.util.Locale;

/**
* Encapsulates events which are Tasks with start and end.
*/
public class Event extends Task{
private LocalDate startTime;
private LocalDate endTime;
Expand All @@ -21,13 +24,19 @@ public Event(String description, boolean isDone, LocalDate startTime, LocalDate

@Override
public String toString() {
return "[E]" + super.toString() + " (from: " + startTime.format(DateTimeFormatter.ofPattern("MMM dd yyyy", Locale.ENGLISH))
+ " to: " + endTime.format(DateTimeFormatter.ofPattern("MMM dd yyyy", Locale.ENGLISH)) + ")";
return "[E]" + super.toString() + " (from: "
+ startTime.format(DateTimeFormatter.ofPattern("MMM dd yyyy", Locale.ENGLISH))
+ " to: "
+ endTime.format(DateTimeFormatter.ofPattern("MMM dd yyyy", Locale.ENGLISH)) + ")";
}

/**
* Returns a String representation of the task for storage.
*/
@Override
public String toTxt() {
return "E | " + super.toTxt() + " | " + this.startTime.format(DateTimeFormatter.ofPattern("MMM dd yyyy", Locale.ENGLISH))
return "E | " + super.toTxt() + " | "
+ this.startTime.format(DateTimeFormatter.ofPattern("MMM dd yyyy", Locale.ENGLISH))
+ " | " + this.endTime.format(DateTimeFormatter.ofPattern("MMM dd yyyy", Locale.ENGLISH));
}
}
11 changes: 11 additions & 0 deletions src/main/java/duke/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import java.time.LocalDate;

/**
* Parses the user input.
*/
public class Parser {
private String command;
private Storage storage;
Expand All @@ -14,6 +17,9 @@ public Parser(String command, Storage storage, TaskList taskList) {
this.taskList = taskList;
}

/**
* Parses the user input and initiate following operations.
*/
public void parse() {
try{
if (command.startsWith("list")) {
Expand Down Expand Up @@ -43,6 +49,8 @@ public void parse() {
} else if (command.startsWith("bye")) {
this.isEnd = true;
Ui.farewellMessage();
} else if (command.startsWith("find")) {
String keyword = command.split(" ", 2)[1];
} else {
throw new DukeException(" OOPS!!! I'm sorry, but I don't know what that means :-(");
}
Expand All @@ -51,6 +59,9 @@ public void parse() {
}
}

/**
* Returns if the parser is ended.
*/
public boolean isEnd() {
return this.isEnd;
}
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
20 changes: 19 additions & 1 deletion src/main/java/duke/Task.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
package duke;

/**
* Encapsulates tasks for the chatbot.
*/
public class Task {
protected String description;
protected boolean isDone;

/**
* Constructs a new Task object.
*
* @param description Description of the task.
*/
public Task(String description) {
this.description = description;
this.isDone = false;
Expand All @@ -14,10 +22,17 @@ public Task(String description, boolean isDone) {
this.isDone = isDone;
}

/**
* Gets the status Icon of the Task.
*/
public String getStatusIcon() {
return (isDone ? "X" : " "); // mark done task with X
// X for done task
return (isDone ? "X" : " ");
}

/**
* Marks the task as done.
*/
public void markAsDone() {
this.isDone = true;
}
Expand All @@ -27,6 +42,9 @@ public String toString() {
return "[" + this.getStatusIcon() + "] " + this.description;
}

/**
* Returns a String representation of the task for storage.
*/
public String toTxt() {
return (this.isDone ? "1" : "0") + " | " + this.description;
}
Expand Down
38 changes: 37 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,26 +19,59 @@ 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();
Ui.markTask(t);
this.storage.rewriteFile(tasks);
}

/**
* Searches Tasks using keyword.
*
* @param keyword The keyword.
*/
public void searchTask(String keyword) {
ArrayList<Task> result = new ArrayList<>();
for (int i = 0; i < this.tasks.size(); i++) {
if (tasks.get(i).toString().contains(keyword)) {
result.add(tasks.get(i));
}
}
Ui.listMatchingTasks(result);
}
}
6 changes: 6 additions & 0 deletions src/main/java/duke/ToDo.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package duke;

/**
* Encapsulates todos which are Tasks.
*/
public class ToDo extends Task {
public ToDo(String description) {
super(description);
Expand All @@ -14,6 +17,9 @@ public String toString() {
return "[T]" + super.toString();
}

/**
* Returns a String representation of the task for storage.
*/
@Override
public String toTxt() {
return "T | " + super.toTxt();
Expand Down
Loading

0 comments on commit b88d605

Please sign in to comment.