Skip to content

Commit

Permalink
Create different task type subclasses to align towards the OOP standard
Browse files Browse the repository at this point in the history
  • Loading branch information
muller317 committed Oct 3, 2024
1 parent 171337c commit e5a9556
Show file tree
Hide file tree
Showing 9 changed files with 322 additions and 109 deletions.
5 changes: 4 additions & 1 deletion src/main/java/muller/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import muller.command.MarkCommand;
import muller.command.MullerException;
import muller.command.OnCommand;
import muller.command.RemindCommand;
import muller.command.UnmarkCommand;

/**
Expand All @@ -30,12 +31,14 @@ public Command parse(String fullCommand) throws MullerException {
String[] inputs = fullCommand.split(" ", 2);

// Programmer-level assumption: inputs should always have at least one element (the command itself)
assert inputs.length > 0 : "Command should have at least one token";
assert inputs.length > 0 : "Command should not be empty";
String commandWord = inputs[0].toLowerCase();

switch (commandWord) {
case "bye":
return new ExitCommand();
case "remind":
return new RemindCommand(inputs);
case "list":
return new ListCommand();
case "mark":
Expand Down
47 changes: 33 additions & 14 deletions src/main/java/muller/storage/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,18 @@
import java.util.ArrayList;

import muller.command.MullerException;
import muller.task.DeadlineTask;
import muller.task.EventTask;
import muller.task.Task;
import muller.task.TaskList;
import muller.task.TodoTask;

/**
* Handles the loading and saving of tasks to and from a file.
*/
public class Storage {
private String filePath;

/**
* Constructs a Storage object with the specified file path.
*
Expand All @@ -26,6 +30,7 @@ public class Storage {
public Storage(String filePath) {
this.filePath = filePath;
}

/**
* Loads tasks from the specified file.
*
Expand Down Expand Up @@ -86,25 +91,39 @@ public void saveTasks(TaskList tasks) throws MullerException {
* Parses a line of text from the file into a Task object.
*
* @param parts The components of the task read from the file.
* @return The Task object parsed from the file.
* @return The specific Task object (TodoTask, DeadlineTask, EventTask) parsed from the file.
* @throws MullerException If there is an issue parsing the task.
*/
private Task parseTask(String[] parts) throws MullerException {
String type = parts[0];
boolean isDone = parts[1].equals("1");
String name = parts[2];
Task task = new Task(name);
task.setType(type);
task.markAsDone(isDone);
String type = parts[0].trim(); // Get task type: [T], [D], [E]
boolean isDone = parts[1].equals("1"); // Done status
String name = parts[2].trim(); // Task name

if (type.equals("D") && parts.length >= 4) {
LocalDate date = LocalDate.parse(parts[3], Task.INPUT_DATE_FORMATTER);
task.setDate(date);
} else if (type.equals("E") && parts.length >= 5) {
LocalDate startDate = LocalDate.parse(parts[3], Task.INPUT_DATE_FORMATTER);
LocalDate endDate = LocalDate.parse(parts[4], Task.INPUT_DATE_FORMATTER);
task.setDateRange(startDate, endDate);
Task task;
switch (type) {
case "T":
task = new TodoTask(name);
break;
case "D":
if (parts.length < 4) {
throw new MullerException("Deadline task is missing date information.");
}
LocalDate deadline = LocalDate.parse(parts[3].trim(), Task.OUTPUT_DATE_FORMATTER);
task = new DeadlineTask(name, deadline);
break;
case "E":
if (parts.length < 5) {
throw new MullerException("Event task is missing start and/or end date information.");
}
LocalDate startDate = LocalDate.parse(parts[3].trim(), Task.OUTPUT_DATE_FORMATTER);
LocalDate endDate = LocalDate.parse(parts[4].trim(), Task.OUTPUT_DATE_FORMATTER);
task = new EventTask(name, startDate, endDate);
break;
default:
throw new MullerException("Unknown task type found in file.");
}

return task;
}
}

70 changes: 69 additions & 1 deletion src/main/java/muller/task/DeadlineTask.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,72 @@
package muller.task;

public class DeadlineTask {
import java.time.LocalDate;

/**
* Represents a task that has a deadline.
*/
public class DeadlineTask extends Task {
private LocalDate deadline;
/**
* Constructs a DeadlineTask object with the specified name and deadline date.
*
* @param name The name of the deadline task.
* @param deadline The deadline date of the task.
*/
public DeadlineTask(String name, LocalDate deadline) {
super(name);
this.deadline = deadline;
this.type = "[D]";
}

/**
* Returns the deadline date of the task.
*
* @return The deadline date.
*/
public LocalDate getDeadline() {
return deadline;
}

/**
* Checks if the task occurs on a specified date.
*
* @param date The date to check.
* @return True if the task's deadline is on the specified date, false otherwise.
*/
@Override
public boolean isOnDate(LocalDate date) {
return this.deadline.equals(date);
}

/**
* Checks if the task is due within the next 3 days.
*
* @return True if the deadline is within the next 3 days, false otherwise.
*/
@Override
public boolean isDueSoon() {
return deadline.isBefore(LocalDate.now().plusDays(3)) && deadline.isAfter(LocalDate.now());
}

/**
* Converts the DeadlineTask to a string format suitable for saving to a file.
*
* @return The string representation of the DeadlineTask for saving.
*/
@Override
public String convertToFileString() {
return "D | " + (isDone() ? "1" : "0") + " | " + getName() + " | " + deadline.format(OUTPUT_DATE_FORMATTER);
}

/**
* Returns a string representation of the task, including its deadline.
*
* @return A string representing the task and its deadline.
*/
@Override
public String toString() {
return this.type + getDoneIcon() + " " + getName() + " (by: " + deadline.format(OUTPUT_DATE_FORMATTER) + ")";
}
}

85 changes: 84 additions & 1 deletion src/main/java/muller/task/EventTask.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,87 @@
package muller.task;

public class EventTask {
import java.time.LocalDate;

/**
* Represents a task that occurs over a range of dates.
*/
public class EventTask extends Task {
private LocalDate startDate;
private LocalDate endDate;

/**
* Constructs an EventTask object with the specified name, start date, and end date.
*
* @param name The name of the event task.
* @param startDate The start date of the event.
* @param endDate The end date of the event.
*/
public EventTask(String name, LocalDate startDate, LocalDate endDate) {
super(name);
this.startDate = startDate;
this.endDate = endDate;
this.type = "[E]";
}

/**
* Checks if the task occurs on a specified date.
*
* @param date The date to check.
* @return True if the date is within the event's start and end dates, false otherwise.
*/
@Override
public boolean isOnDate(LocalDate date) {
return !date.isBefore(startDate) && !date.isAfter(endDate);
}

/**
* Checks if the event starts within the next 3 days.
*
* @return True if the start date is within the next 3 days, false otherwise.
*/
@Override
public boolean isDueSoon() {
return startDate.isBefore(LocalDate.now().plusDays(3)) && startDate.isAfter(LocalDate.now());
}

/**
* Returns the start date of the task.
*
* @return The event start date.
*/
public LocalDate getStartDate() {
return startDate;
}

/**
* Returns the start date of the task.
*
* @return The event start date.
*/
public LocalDate getEndDate() {
return endDate;
}

/**
* Converts the EventTask to a string format suitable for saving to a file.
*
* @return The string representation of the EventTask for saving.
*/
@Override
public String convertToFileString() {
return "E | " + (isDone() ? "1" : "0") + " | " + getName() + " | " + startDate.format(OUTPUT_DATE_FORMATTER)
+ " | " + endDate.format(OUTPUT_DATE_FORMATTER);
}

/**
* Returns a string representation of the task, including its start and end dates.
*
* @return A string representing the task and its date range.
*/
@Override
public String toString() {
return this.type + getDoneIcon() + " " + getName() + " (from: " + startDate.format(OUTPUT_DATE_FORMATTER)
+ " to: " + endDate.format(OUTPUT_DATE_FORMATTER) + ")";
}
}

Loading

0 comments on commit e5a9556

Please sign in to comment.