In modern Java development (v16+), Records have become the standard for simple data transfer objects (DTOs). This project refactors the previous library catalog to use a Record instead of a traditional class. By doing so, we eliminate boilerplate code such as explicit constructors and getters. The project demonstrates how records seamlessly integrate with the Java Collections Framework, providing a cleaner and more readable way to manage a "shelf" of immutable book entries.
- Boilerplate Reduction: Replaced a 20-line class with a 1-line record definition.
- Immutability by Design: Ensured that book titles and authors cannot be modified after creation, increasing data safety.
- Modern Syntax: Utilized record-style accessors (e.g.,
book.title()instead ofbook.getTitle()). - Clean Code Evolution: Applied the latest Java features to make the codebase more maintainable and expressive.
- Java 16+ (Records, Collections Framework)
- Book: A record serving as an immutable data unit for library entries.
- LibraryCatalogApp: The main execution class utilizing the refactored record model.
Title: The Master and Margarita, Author: Mikhail Bulgakov
Title: 1984, Author: George Orwell
Project Structure:
JavaBasics_Task_453_V0.2/
├── src/
│ └── com/yurii/pavlenko/
│ ├── app/
│ │ └── LibraryCatalogApp.java
│ └── library/
│ └── models/
│ └── Book.java
├── LICENSE
├── TASK.md
├── THEORY.md
└── README.md
Code
package com.yurii.pavlenko.app;
import com.yurii.pavlenko.library.models.Book;
import java.util.ArrayList;
import java.util.List;
public class LibraryCatalogApp {
public static void main(String[] args) {
List<Book> catalog = new ArrayList<>();
catalog.add(new Book("The Master and Margarita", "Mikhail Bulgakov"));
catalog.add(new Book("1984", "George Orwell"));
for (Book book : catalog) {
System.out.println("Title: " + book.title() + ", Author: " + book.author());
}
}
}package com.yurii.pavlenko.library.models;
public record Book(String title, String author) {
}This project is licensed under the MIT License.
Copyright (c) 2026 Yurii Pavlenko
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files...
License: MIT