Introduction
A catalogue is the simplest useful database: a list of records you can add to, search, sort and update. This project practises encapsulation (a Book controls its own borrowed state) and shows how Comparators make many sort orders trivial.
Problem statement
add takes an ISBN in the form 978-0134685991, a title, an author and a year, separated by |, and rejects duplicates, malformed ISBNs and implausible years. Books can be borrowed and returned (refusing to borrow a borrowed book or return one that isn't out). Users can search titles and authors, list books by year range, list the catalogue sorted by title, author or year, and see statistics: counts, the oldest book, the most borrowed book and books per author.Objectives
- Encapsulate state changes in a class (borrow / giveBack)
- Validate data in a constructor
- Sort with Comparators and search with streams
- Return Optional for 'may not exist' lookups
Features
- ISBN, title, author and year validation
- Borrow/return with state checks and borrow counts
- Case-insensitive search and year-range queries
- Three sort orders and catalogue statistics
Concepts demonstrated
- Encapsulation
- Constructor validation
- Comparators
- Optional
- Stream filtering
System architecture
Book has private final identity fields and a private borrowed flag that only borrow and giveBack change; its constructor throws IllegalArgumentException for invalid data. The catalogue is a static List<Book>. find(isbn) returns an Optional<Book>. Commands are dispatched with a switch; list chooses a Comparator with a switch expression. Statistics use stream min, max, distinct and count.
Folder structure
Complete source code
src/Main.java
What each file does
- src/Main.java
- The
Bookclass, the command loop, add/find/search helpers and statistics.
How to run it
Compile
Run
Test against the expected output
Expected output
Generated by running the project with tests/sample-input.txt. Your output should match tests/expected-output.txt exactly.
add 978-0134685991 | Effective Java | Joshua Bloch | 2018 add 978-0596009205 | Head First Java | Kathy Sierra | 2005 add 978-0321349606 | Java Concurrency in Practice | Brian Goetz | 2006 add 978-0132350884 | Clean Code | Robert Martin | 2008 add 978-0201633610 | Design Patterns | Erich Gamma | 1994 add 978-0134685991 | Duplicate | Someone | 2020 add 12345 | Bad ISBN | Nobody | 2020 add 978-1111111111 | Future Book | Nobody | 2099 borrow 978-0134685991 borrow 978-0134685991 return 978-0596009205 borrow 978-0321349606 return 978-0134685991 borrow 978-0134685991 find java years 2000 2010 list year list author list price stats quit
Testing
- Run with
tests/sample-input.txtand compare withtests/expected-output.txt. - A duplicate ISBN, a short ISBN and the year 2099 are rejected.
- Borrowing Effective Java twice is refused the second time; after returning, it can be borrowed again (2 times in total).
list yearshows newest first;list priceis reported as an invalid sort order.
Common errors
Search misses 'Java' in 'Head First Java'
Compare lower-cased strings with contains, not equals.
Sorting by title puts lower-case titles last
Use String.CASE_INSENSITIVE_ORDER.
Possible improvements
- Save the catalogue to a CSV file and load it at start-up
- Track who borrowed each book and the due date
- Support several copies of the same title
Additional challenges
- 1Add a
remove ISBNcommand that refuses borrowed books. - 2Search by ISBN prefix.
- 3Print the catalogue grouped by decade.
Built and tested it yourself? Mark it to update your dashboard.