Facebook Pixel

Exercise: Class Diagrams

This exercise puts the class diagrams lesson into code.

Scenario

The diagram below describes a small library system. Library aggregates both Book and Member. A Member can borrow a Book through a borrow association that runs through Library. Each Book tracks its own availability; Library coordinates the borrow and return operations.

Commands

Each command appears as a row in the input matrix; every command produces exactly one output line.

CommandBehaviorOutput
["addbook", title]Add a book (initially available)"Added <title>"
["member", name]Register a member"Registered <name>"
["borrow", name, title]Attempt to borrow"<name> not found" if member missing; "<title> not found" if book missing; "<title> unavailable" if already borrowed; "<name> borrowed <title>" on success
["return", title]Attempt to return"<title> not found" if book missing; "<title> not borrowed" if currently available; "<title> returned" on success
Example
Input
7
addbook Python101
member Alice
borrow Alice Python101
borrow Bob Python101
return Python101
return Python101
borrow Alice Python101
Output
Added Python101
Registered Alice
Alice borrowed Python101
Bob not found
Python101 returned
Python101 not borrowed
Alice borrowed Python101
Explanation
The book is added and Alice is registered. Alice successfully borrows Python101. Bob is not a registered member, so the borrow fails. Returning Python101 succeeds. Attempting to return it again fails because it is already available. Alice borrows it once more successfully.

Your task

Fill in the four methods on Library so that the commands produce the output above.

Invest in Yourself
Your new job is waiting. 83% of people that complete the program get a job offer. Unlock unlimited access to all content and features.
Go Pro