Go: A small library

A bigger command-line program with rules, errors and a report.

What to build

A program that manages a library: books, members, borrowing and returning.

Requirements

  • Read n, then n commands, one per line:
  • book TITLE adds a book (a single word). member NAME adds a member (a single word). Both print ok; adding one that already exists prints error: duplicate TITLE (or NAME).
  • borrow NAME TITLE lends the book. Possible errors: error: unknown member NAME, error: unknown book TITLE, error: TITLE is borrowed by WHO and error: NAME has too many books (a member may hold at most 2). Otherwise print ok.
  • return NAME TITLE takes the book back and prints ok; if NAME does not hold TITLE print error: NAME does not have TITLE.
  • report prints each book in alphabetical order as TITLE: available or TITLE: borrowed by NAME, then each member in alphabetical order as NAME: COUNT books.
  • The checks for borrow are made in the order listed.

Input

Line 1: n. Then n commands.

Output

One result line per command (several for report).

Constraints

1 ≤ n ≤ 60; titles and names are single words.

Example

Input:

9
book Go
book Rust
member Ann
member Bob
borrow Ann Go
borrow Bob Go
borrow Bob Rust
return Ann Go
report

Output:

ok
ok
ok
ok
ok
error: Go is borrowed by Ann
ok
ok
Go: available
Rust: borrowed by Bob
Ann: 0 books
Bob: 1 books

Why it works

The rules are kept in one Library type with a method for each command, and each method returns an error for every broken rule. The loop in main only reads commands and prints either ok or the message of the error.

Examples

Example 1 · Input
9
book Go
book Rust
member Ann
member Bob
borrow Ann Go
borrow Bob Go
borrow Bob Rust
return Ann Go
report
Expected output
ok
ok
ok
ok
ok
error: Go is borrowed by Ann
ok
ok
Go: available
Rust: borrowed by Bob
Ann: 0 books
Bob: 1 books
Example 2 · Input
5
member Zed
borrow Zed Ghost
borrow Amy Ghost
book Ghost
borrow Zed Ghost
Expected output
ok
error: unknown book Ghost
error: unknown member Amy
ok
ok

Hints

Hint 1

Keep a map of title to the name of the borrower (empty text means available), a map of member to the number of books, and write each rule as a method that returns an error.

Hint 2

Sort the titles and the names with sort.Strings before printing the report.

Solution

Show one correct solution (try it yourself first)
package main

import (
	"fmt"
	"sort"
)

type Library struct {
	books   map[string]string
	members map[string]int
}

func NewLibrary() *Library {
	return &Library{books: map[string]string{}, members: map[string]int{}}
}

func (l *Library) AddBook(title string) error {
	if _, ok := l.books[title]; ok {
		return fmt.Errorf("duplicate %s", title)
	}
	l.books[title] = ""
	return nil
}

func (l *Library) AddMember(name string) error {
	if _, ok := l.members[name]; ok {
		return fmt.Errorf("duplicate %s", name)
	}
	l.members[name] = 0
	return nil
}

func (l *Library) Borrow(name, title string) error {
	count, ok := l.members[name]
	if !ok {
		return fmt.Errorf("unknown member %s", name)
	}
	who, ok := l.books[title]
	if !ok {
		return fmt.Errorf("unknown book %s", title)
	}
	if who != "" {
		return fmt.Errorf("%s is borrowed by %s", title, who)
	}
	if count >= 2 {
		return fmt.Errorf("%s has too many books", name)
	}
	l.books[title] = name
	l.members[name] = count + 1
	return nil
}

func (l *Library) Return(name, title string) error {
	if who, ok := l.books[title]; !ok || who != name || name == "" {
		return fmt.Errorf("%s does not have %s", name, title)
	}
	l.books[title] = ""
	l.members[name]--
	return nil
}

func (l *Library) Report() {
	titles := []string{}
	for t := range l.books {
		titles = append(titles, t)
	}
	sort.Strings(titles)
	for _, t := range titles {
		if l.books[t] == "" {
			fmt.Printf("%s: available\n", t)
		} else {
			fmt.Printf("%s: borrowed by %s\n", t, l.books[t])
		}
	}
	names := []string{}
	for m := range l.members {
		names = append(names, m)
	}
	sort.Strings(names)
	for _, m := range names {
		fmt.Printf("%s: %d books\n", m, l.members[m])
	}
}

func main() {
	var n int
	fmt.Scan(&n)
	lib := NewLibrary()
	for i := 0; i < n; i++ {
		var cmd string
		fmt.Scan(&cmd)
		var err error
		switch cmd {
		case "book":
			var t string
			fmt.Scan(&t)
			err = lib.AddBook(t)
		case "member":
			var m string
			fmt.Scan(&m)
			err = lib.AddMember(m)
		case "borrow":
			var m, t string
			fmt.Scan(&m, &t)
			err = lib.Borrow(m, t)
		case "return":
			var m, t string
			fmt.Scan(&m, &t)
			err = lib.Return(m, t)
		case "report":
			lib.Report()
			continue
		default:
			continue
		}
		if err != nil {
			fmt.Println("error:", err)
		} else {
			fmt.Println("ok")
		}
	}
}

▶ Open this solution in the compiler

Lesson: Putting it together: a command-line program →