Task

mail@pastecode.io avatar
unknown
java
2 months ago
5.4 kB
1
Indexable
Never
package Model;

import java.time.LocalDate;
import java.io.Serializable;

/**
 * Represents a task with a description, ID, assigned user, state, last update date, and priority.
 * Implements Comparable to allow for sorting based on priority and description.
 * Implements Serializable to support object serialization.
 */
public class Task implements Comparable<Task>, Serializable {

    private static final long serialVersionUID = 1L; // Used to identify the version of the class that created the serialized object

    private final String description; // The description of the task
    private final int id; // The unique identifier of the task
    private String takenBy; // The user assigned to the task
    private TaskState state; // The current state of the task
    private LocalDate lastUpdate; // The date when the task was last updated
    private TaskPrio prio; // The priority of the task

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

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

    /**
     * Retrieves the user assigned to the task.
     *
     * @return The user assigned to the task.
     */
    public String getTakenBy() {
        return takenBy;
    }

    /**
     * Retrieves the current state of the task.
     *
     * @return The current state of the task.
     */
    public TaskState getState() {
        return state;
    }

    /**
     * Retrieves the last update date of the task.
     *
     * @return The last update date of the task.
     */
    public LocalDate getLastUpdate() {
        return lastUpdate;
    }

    /**
     * Retrieves the priority of the task.
     *
     * @return The priority of the task.
     */
    public TaskPrio getPrio() {
        return prio;
    }

    /**
     * Constructs a task with the given description, ID, assigned user, state, last update date, and priority.
     *
     * @param description The description of the task.
     * @param id          The ID of the task.
     * @param takenBy     The user assigned to the task.
     * @param state       The current state of the task.
     * @param lastUpdate  The last update date of the task.
     * @param prio        The priority of the task.
     */
    public Task(String description, int id, String takenBy, TaskState state, LocalDate lastUpdate, TaskPrio prio) {
        this.description = description;
        this.id = id;
        this.takenBy = takenBy;
        this.state = state;
        this.lastUpdate = lastUpdate;
        this.prio = prio;
    }

    /**
     * Sets the user assigned to the task.
     * Throws IllegalStateException if the task is already assigned to a user.
     *
     * @param takenBy The user to be assigned to the task.
     * @throws IllegalStateException If the task is already assigned to a user.
     */
    public void setTakenBy(String takenBy) {
        if (this.takenBy != null) {
            throw new IllegalStateException("Task is already taken by " + this.takenBy);
        }
        this.takenBy = takenBy;
        this.lastUpdate = LocalDate.now();
    }

    /**
     * Sets the state of the task.
     *
     * @param state The new state of the task.
     */
    public void setState(TaskState state) {
        this.state = state;
        this.lastUpdate = LocalDate.now();
    }

    /**
     * Sets the priority of the task.
     *
     * @param prio The new priority of the task.
     */
    public void setPriority(TaskPrio prio) {
        this.prio = prio;
        this.lastUpdate = LocalDate.now();
    }

    /**
     * Compares this task to another task based on priority and description.
     *
     * @param o The task to compare to.
     * @return A negative integer, zero, or a positive integer as this task is less than, equal to, or greater than the specified task.
     */
    @Override
    public int compareTo(Task o) {
        int prioCompare = this.prio.compareTo(o.prio);
        if (prioCompare != 0) {
            return prioCompare;
        } else {
            return this.description.compareTo(o.description);
        }
    }

    /**
     * Checks if this task 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 {
            Task task = (Task) obj;
            result = prio == task.prio && description.equals(task.description); // Compare the description and priority of the tasks to determine if they are equal
        }
        return result;
    }

    /**
     * Generates a string representation of the task.
     *
     * @return A string representation of the task.
     */
    @Override
    public String toString() {
        return "Task{" +
                "description='" + description + '\'' +
                ", id=" + id +
                ", TakenBy='" + takenBy + '\'' +
                ", state=" + state +
                ", lastUpdate=" + lastUpdate +
                ", prio=" + prio +
                '}';
    }
}
Leave a Comment