Intermediate level · Module 6: Intermediate Java Project · Lesson 2 of 3
Implementing Features and Persistence
Implement the service rules and the CSV repository, adding tests alongside each feature.
In this lesson you will
- Implement business rules in a service class with custom exceptions
- Implement CSV saving and loading behind the repository interface
Key concepts
test-alongside developmentdate handling with LocalDateCSV repositorydata integrity
The lesson
Build one feature at a time with its tests: add a member, add a book, lend, return, list overdue. LocalDate handles dates: LocalDate.now().plusDays(14) gives a due date, and isBefore compares dates for overdue checks.
The CSV repository writes members, books and loans to separate files and rebuilds the objects on load. Loans refer to members and books by ID, so load members and books first and then connect the loans to them.
Test persistence with a temporary folder: save data, load it into a new repository and assert that the loaded objects equal the originals. JUnit's @TempDir provides a fresh folder for each test.
Step by step
- Implement lend and return with their exceptions and tests.
- Implement the overdue report using LocalDate.
- Implement the CSV repository save and load.
- Write a round-trip test with @TempDir.
Practice activity
Implement all features of the library system with passing tests for each.
Expected result: Every feature works from the menu, data survives a restart and mvn test passes.
Check your understanding
-
How do you calculate a due date 14 days from today?
Show answer
LocalDate.now().plusDays(14).
-
What does a round-trip persistence test check?
Show answer
That data saved and then loaded is equal to the original data.
Taught live by an instructor — online or in person. Free course lessons are for self-study and do not lead to a certificate — see registration options.