Project

 avatar
unknown
java
a year ago
7.2 kB
6
Indexable
package Model;

import Model.Matcher.ITaskMatcher;

import java.io.Serializable;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Represents a project containing tasks with a title, ID, description, creation date, next task ID, and list of tasks.
 * Implements Comparable to allow for sorting based on title.
 * Implements Serializable to support object serialization.
 */
public class Project implements Comparable<Project>, Serializable {

    private static final long serialVersionUID = 1L; // Used to identify the version of the class that created the serialized object
    private final String title; // The title of the project
    private final int id; // The unique identifier of the project
    private final String description; // The description of the project
    private final LocalDate created; // The creation date of the project
    private int nextTaskId; // The ID for the next task to be added to the project
    private final List<Task> tasks; // The list of tasks in the project

    /**
     * Constructs a project with the given title, ID, description, and creation date.
     * Initializes next task ID to 1 and initializes the list of tasks.
     *
     * @param title       The title of the project.
     * @param id          The ID of the project.
     * @param description The description of the project.
     * @param created     The creation date of the project.
     */
    public Project(String title, int id, String description, LocalDate created) {
        this.title = title;
        this.id = id;
        this.description = description;
        this.created = created;
        this.nextTaskId = 1;
        this.tasks = new ArrayList<>();
    }

    /**
     * Retrieves a task from the project by its ID.
     *
     * @param id The ID of the task to retrieve.
     * @return The task with the specified ID, or null if not found.
     */
    public Task getTaskById(int id) {
        for (Task task : tasks) {
            if (task.getId() == id) {
                return task;
            }
        }
        return null;
    }

    /**
     * Finds tasks in the project that match the given matcher.
     *
     * @param matcher The matcher to use for finding tasks.
     * @return A list of tasks that match the given criteria.
     */
    public List<Task> findTasks(ITaskMatcher matcher) {
        List<Task> matchedTasks = new ArrayList<>();
        for (Task task : tasks) {
            if (matcher.match(task)) {
                matchedTasks.add(task);
            }
        }
        Collections.sort(matchedTasks);
        return matchedTasks;
    }

    /**
     * Adds a new task to the project with the given description and priority.
     *
     * @param description The description of the new task.
     * @param prio        The priority of the new task.
     * @return The newly added task.
     */
    public Task addTask(String description, TaskPrio prio) {
        Task newTask = new Task(description, nextTaskId, null, TaskState.TO_DO, LocalDate.now(), prio);
        tasks.add(newTask);
        nextTaskId++;
        return newTask;
    }

    /**
     * Removes a task from the project.
     *
     * @param task The task to remove.
     * @return True if the task was removed successfully, false otherwise.
     */
    public boolean removeTask(Task task) {
        return tasks.remove(task);
    }

    /**
     * Retrieves the state of the project based on the states of its tasks.
     *
     * @return The state of the project.
     */
    public ProjectState getState() {
        if (tasks.isEmpty()) {
            return ProjectState.EMPTY;
        }

        boolean allTasksDone = true;
        for (Task task : tasks) {
            if (task.getState() != TaskState.DONE) {
                allTasksDone = false;
                break;
            }
        }
        if (allTasksDone) {
            return ProjectState.COMPLETED;
        } else {
            return ProjectState.ONGOING;
        }
    }

    /**
     * Retrieves the date when the project was last updated.
     *
     * @return The date when the project was last updated.
     */
    public LocalDate getLastUpdated() {
        if (tasks.isEmpty()) {
            return created;
        }
        LocalDate lastUpdated = tasks.get(0).getLastUpdate();
        for (Task task : tasks) {
            if (task.getLastUpdate().isAfter(lastUpdated)) {
                lastUpdated = task.getLastUpdate();
            }
        }
        return lastUpdated;
    }

    /**
     * Compares this project to another project based on title.
     *
     * @param o The project to compare to.
     * @return A negative integer, zero, or a positive integer as this project is less than, equal to, or greater than the specified project.
     */
    @Override
    public int compareTo(Project o) {
        return this.title.compareTo(o.title);
    }

    /**
     * Checks if this project is equal to another object.
     *
     * @param obj The object to compare to.
     * @return True if the objects are equal, false otherwise.
     */
    @Override
    public boolean equals(Object obj) {
        boolean result;
        if (this == obj) {
            result = true;
        } else if (obj == null || getClass() != obj.getClass()) {
            result = false;
        } else {
            Project project = (Project) obj; // Downcasting the object to a Project object to compare the title
            result = title.equals(project.title);
        }
        return result;
    }

    /**
     * Retrieves the title of the project.
     *
     * @return The title of the project.
     */
    public String getTitle() {
        return title;
    }

    /**
     * Retrieves the ID of the project.
     *
     * @return The ID of the project.
     */
    public int getId() {
        return id;
    }

    /**
     * Retrieves the description of the project.
     *
     * @return The description of the project.
     */
    public String getDescription() {
        return description;
    }

    /**
     * Retrieves the creation date of the project.
     *
     * @return The creation date of the project.
     */
    public LocalDate getCreated() {
        return created;
    }

    /**
     * Retrieves the ID for the next task to be added to the project.
     *
     * @return The ID for the next task.
     */
    public int getNextTaskId() {
        return nextTaskId;
    }

    /**
     * Retrieves a copy of the list of tasks in the project.
     *
     * @return A copy of the list of tasks.
     */
    public List<Task> getTasks() {
        return new ArrayList<>(tasks);
    }

    /**
     * Generates a string representation of the project.
     *
     * @return A string representation of the project.
     */
    @Override
    public String toString() {
        return "Project{" +
                "title='" + title + '\'' +
                ", id=" + id +
                ", description='" + description + '\'' +
                ", created=" + created +
                ", nextTaskId=" + nextTaskId +
                ", tasks=" + tasks +
                '}';
    }
}
Editor is loading...
Leave a Comment