ProjectManager

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

import java.util.ArrayList;
import java.util.List;

/**
 * Manages projects by providing functionality to add, remove, and retrieve projects, and check for title uniqueness.
 */
public class ProjectManager {

    private int nextProjectId; // The ID for the next project to be added
    private final List<Project> projects; // The list of projects managed by the project manager

    /**
     * Constructs a project manager with an initial list of projects.
     * Initializes the next project ID to 1.
     */
    public ProjectManager() {
        this.nextProjectId = 1;
        this.projects = new ArrayList<>();
    }

    /**
     * Sets the list of projects managed by the project manager.
     * Also updates the next project ID based on the highest existing project ID.
     *
     * @param incomingProjects The list of projects to set.
     */
    public void setProjects(List<Project> incomingProjects) {
        this.projects.clear();
        this.projects.addAll(incomingProjects);
        nextProjectId = getHighestId() + 1;
    }

    /**
     * Checks if a given project title is unique among existing projects.
     *
     * @param title The title to check for uniqueness.
     * @return True if the title is unique, false otherwise.
     */
    public boolean isTitleUnique(String title) {
        for (Project project : projects) {
            if (project.getTitle().equals(title)) {
                return false;
            }
        }
        return true;
    }

    /**
     * Adds a new project with the given title and description.
     *
     * @param title       The title of the new project.
     * @param description The description of the new project.
     * @return The newly added project.
     * @throws IllegalArgumentException If the title is not unique.
     */
    public Project addProject(String title, String description) {
        if (!isTitleUnique(title)) {
            throw new IllegalArgumentException("Title is not unique");
        }
        Project newProject = new Project(title, nextProjectId, description, java.time.LocalDate.now());
        projects.add(newProject);
        nextProjectId++;
        return newProject;
    }

    /**
     * Removes a project from the project manager.
     *
     * @param project The project to remove.
     */
    public void removeProject(Project project) {
        projects.remove(project);
    }

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

    /**
     * Finds projects whose title contains the given string.
     *
     * @param title The string to search for in project titles.
     * @return A list of projects whose title contains the given string.
     */
    public List<Project> findProjects(String title) {
        List<Project> result = new ArrayList<>();
        for (Project project : projects) {
            if (project.getTitle().contains(title)) {
                result.add(project);
            }
        }
        return result;
    }

    /**
     * Retrieves the highest project ID among existing projects.
     *
     * @return The highest project ID.
     */
    private int getHighestId() {
        int maxId = 0;
        for (Project project : projects) {
            if (project.getId() > maxId) {
                maxId = project.getId();
            }
        }
        return maxId;
    }

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

    /**
     * Retrieves a copy of the list of projects managed by the project manager.
     *
     * @return A copy of the list of projects.
     */
    public List<Project> getProjects() {
        return new ArrayList<>(projects);
    }

    /**
     * Generates a string representation of the project manager.
     *
     * @return A string representation of the project manager.
     */
    @Override
    public String toString() {
        return "ProjectManager{" +
                "nextProjectId=" + nextProjectId +
                ", projects=" + projects +
                '}';
    }
}
Leave a Comment