Java library console project


Project Summary

I embarked on a project aimed at enhancing my proficiency in Object Oriented Programming (OOP), Java, Maven, and IntelliJ. Additionally, I sought to deepen and expand my understanding of Git and GitHub. This project presented itself as an optimal opportunity to start my endeavors. In this article, I intend to explain my thought processes and demonstrate the rationale behind my choices and actions.

Overview

Technology Used

Model Classes

The initial step involves the creation of multiple model classes, which include entities such as Book.java and Author.java. These classes are essential for instantiating the objects that will populate the project. Model classes delineate the structure and behavior of resources within the Java program. For instance, the Author.java class code below serves as a foundational template that all other model classes within the project adhere to.

# Author.java

public class Author {
    private String name;
    private String nationality;
    private LocalDate dateOfBirth;
    private LocalDate dateOfDeath;

    // Default constructor
    public Author() {}

    // Parameterized constructor
    public Author(String name, String nationality, LocalDate dateOfBirth, LocalDate dateOfDeath) {
        this.name = name;
        this.nationality = nationality;
        this.dateOfBirth = dateOfBirth;
        this.dateOfDeath = dateOfDeath;
    }

    // Getters and setters...
}

All other Model classes adhere to this identical structure. Two constructors are created, one with defaults and one that is parameterized. Setters and Getters are created for each of the fields that are contained within the class. Having all the model classes created based on this model ensures a systematic format conducive to efficient debugging. Additionally, it facilitates an easy framework for review purposes while developing.

Services

Now that the models are established for current use, it is necessary to create Service classes that serve as an intermediary layer between the Model classes and the business logic of the application. These service classes provide a range of logic methods that not only enhance the functionality of the Model classes but also ensure that the data returned adheres to the required business rules and validations. Focusing specifically on the Author Model, the service class AuthorService.java includes several important methods, such as authorSummary, addGenre, and removeGenre, each with its unique role.

Author Service Class

The authorSummary method, for instance, introduces the capability to represent the Author object as a string, effectively summarizing key aspects of the author’s identity including their name, nationality, and date of birth/death. This allows for the seamless display of individual information, which can be particularly useful in user interfaces where space is limited. Given the transient nature of human life, the dateOfDeath field can be null, and this is acceptable for presenting the Author’s information in a flexible manner. However, it is preferable to have a more human-readable format that caters to user experience. This method checks whether the dateOfDeath is null, returning an appropriate response based on whether it contains a value or not. If a value is present, the method formats the date in a user-friendly manner; if not, it provides an output that they are alive. Here is my code that I used to display the Author’s summary:

# AuthorService.java

public static String authorSummary(Author author) {
        String deathInfo = authorLiving(author)
                ? "Currently Alive"
                : author.getDateOfDeath().toString();

        return "\n------------Author Summary------------\n" +
                "Name: " + author.getName() + ",\n" +
                "Nationality: " + author.getNationality() + ",\n" +
                "Date of Birth: " + author.getDateOfBirth() + ",\n" +
                "Date of Death: " + deathInfo + ",\n" +
                "Primary Genre(s): " + author.getGenres();
    }

A method exists to compute the age of the author. It calculates from birth to death or from birth to the present year. This is done by utilizing the java.time.LocalDate and java.time.Period classes from the java.time library. This approach enables date calculations with only two imports and requires minimal additional code for date entry. The code includes three checks that are executed during its operation:

  • In the event that the dateOfDeath field is populated, compute the number of years elapsed between the dateOfBirth and dateOfDeath fields.
  • If the dateOfDeath field possesses a null value: It is assessed whether the duration between the date of birth and the present time exceeds 120 years; if this criterion is met, an error will be conveyed, indicating that either the data is incomplete or erroneous.
  • In the event that the dateOfDeath field possesses a null value and no errors are detected, it will return a String of text.

This attempts to capture the possible routes that the code takes. By clearly outlining these pathways, we can ensure a comprehensive understanding of the various scenarios that arise during execution. Any correct input should fall under one of these options: whether it navigates through a straightforward process, encounters a conditional branch that directs it to different outcomes, or leads to exceptions that require special handling. This detailed framework enables us to systematically analyze how input interacts with the code, facilitating more effective debugging and optimization strategies. Here is the full method as it is implemented:

# AuthorService.java

public static String authorAge(Author author) {
        if (author.getDateOfDeath() != null) {
            int ageOfDeath = Period.between(author.getDateOfBirth(), author.getDateOfDeath()).getYears();
            return author.getName()  + " died at the age of " + ageOfDeath;
        } else {
            int currentAge = Period.between(author.getDateOfBirth(), LocalDate.now()).getYears();

            if (currentAge > 120) { return "Error: Age is over 120, there is probably a mistake in the Authors Data. " +
                                            "Please check and try again."; }

            return author.getName() + " is currently " + currentAge + " years old.";
        }
    }

UML Diagram

Currently, the UML diagram is displayed above, illustrating the existing configuration and the relationships among the classes involved. I plan to introduce a BookService and a LibraryService in the near future, which will enhance functionality and simplify the management of books and libraries. The BookService will be responsible for retrieving books, updating their information, and processing transactions, while the LibraryService will manage the overall operations of the library, including cataloging, member management, and resource allocation. I will ensure that this article is updated with these changes once they are finalized.


Leave a comment