A bigger program with several rules, classes and error messages, built from everything in this course.
What to build
A program that manages book loans.
Requirements
- Commands, one per line, read until the input ends:
book ID TITLE(the title may have spaces),member NAME,borrow NAME ID,return NAME IDandreport. borrowprintsNAME borrowed TITLEandreturnprintsNAME returned TITLE.bookandmemberprint nothing.reportprints a line for every member in the order they joined:NAME: id1, id2with the books they hold in order of ID, orNAME: none.- Errors print
error: MESSAGEand change nothing:unknown member NAME,unknown book ID,book ID is on loan,NAME has 2 loans(a member may hold at most 2 books) andNAME does not have ID(returning a book that is not held by that member).
Input
Any number of command lines.
Output
One line for each borrow, return, error, and one for each member in a report.
Constraints
At most 100 lines; IDs and names are single words.
Example
Input:
book b1 Dune
book b2 Emma
member ann
borrow ann b1
borrow ann b2
report
Output:
ann borrowed Dune
ann borrowed Emma
ann: b1, b2
Why it works
The class enforces every rule and throws, and the loop only reads commands and prints, so a bad command can never break the data.
Examples
Example 1 · Input
book b1 Dune book b2 Emma member ann borrow ann b1 borrow ann b2 report
Expected output
ann borrowed Dune ann borrowed Emma ann: b1, b2
Example 2 · Input
book x Solo Book member a member b borrow a x borrow b x return b x return a x report
Expected output
a borrowed Solo Book error: book x is on loan error: b does not have x a returned Solo Book a: none b: none
Hints
Hint 1
A Library class holds books (id => title and borrower) and members (name => list of book ids), and throws exceptions for every broken rule.
Hint 2
Print the error text from the exception in the main loop, prefixed with ~error: ~.
Solution
Show one correct solution (try it yourself first)
<?php
class Library
{
private array $books = [];
private array $members = [];
public function addBook(string $id, string $title): void
{
$this->books[$id] = ["title" => $title, "holder" => null];
}
public function addMember(string $name): void
{
$this->members[$name] ??= [];
}
public function borrow(string $name, string $id): string
{
if (!isset($this->members[$name])) {
throw new RuntimeException("unknown member $name");
}
if (!isset($this->books[$id])) {
throw new RuntimeException("unknown book $id");
}
if ($this->books[$id]["holder"] !== null) {
throw new RuntimeException("book $id is on loan");
}
if (count($this->members[$name]) >= 2) {
throw new RuntimeException("$name has 2 loans");
}
$this->books[$id]["holder"] = $name;
$this->members[$name][] = $id;
return $this->books[$id]["title"];
}
public function giveBack(string $name, string $id): string
{
if (!isset($this->members[$name])) {
throw new RuntimeException("unknown member $name");
}
if (!isset($this->books[$id])) {
throw new RuntimeException("unknown book $id");
}
if ($this->books[$id]["holder"] !== $name) {
throw new RuntimeException("$name does not have $id");
}
$this->books[$id]["holder"] = null;
$this->members[$name] = array_values(array_diff($this->members[$name], [$id]));
return $this->books[$id]["title"];
}
public function report(): array
{
$lines = [];
foreach ($this->members as $name => $ids) {
sort($ids);
$lines[] = $name . ": " . ($ids ? implode(", ", $ids) : "none");
}
return $lines;
}
}
$library = new Library();
while (($line = fgets(STDIN)) !== false) {
$parts = explode(" ", trim($line), 3);
try {
switch ($parts[0]) {
case "book":
$library->addBook($parts[1], $parts[2] ?? "");
break;
case "member":
$library->addMember($parts[1]);
break;
case "borrow":
$title = $library->borrow($parts[1], $parts[2]);
echo $parts[1], " borrowed ", $title, "\n";
break;
case "return":
$title = $library->giveBack($parts[1], $parts[2]);
echo $parts[1], " returned ", $title, "\n";
break;
case "report":
foreach ($library->report() as $text) {
echo $text, "\n";
}
break;
}
} catch (RuntimeException $e) {
echo "error: ", $e->getMessage(), "\n";
}
}