CodeBlanks Logo Icon CodeBlanks

Putting it together

Combine the Library class with a console menu.

Console menu

The final lesson combines all pieces. In main a loop lists the books, the user picks one, and checkout lends it. The loop walks the collection with lib.count() and lib.getBook(i).

We take the Book and Library classes from the previous lessons and rewrite main. The menu lists books as 0: Tutunamayanlar, then checks out book 0. This combines every piece of the project into one working app. The library application is now complete.

Listing loop

The menu loop walks each book with for (size_t i = 0; i < lib.count(); ++i). It reads the title via lib.getBook(i).getTitle() and prints it with std::cout << i << ": " << title. Each line shows one book.

After listing, lib.checkout(0) checks out book 0. On success we print "borrowed". This simple menu calls every method of the project: addBook for adding, count and getBook for listing, checkout for lending. In a real app you would read user input with std::cin.

Practice List and check out via a menu

Take all classes from the previous lesson. In main add two books. Use a for loop to iterate up to lib.count() and print each book as i: title. Then check out book 0 with lib.checkout(0) and print "borrowed" on success.

Starter code

main.cpp
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

class Book {
private:
    std::string title;
    std::string author;
public:
    Book(std::string t, std::string a) : title(t), author(a) {}
    std::string getTitle() const { return title; }
    std::string getAuthor() const { return author; }
};

class Library {
private:
    std::vector<Book> books;
    std::vector<bool> borrowed;
public:
    void addBook(const Book& b) { books.push_back(b); borrowed.push_back(false); }
    size_t count() const { return books.size(); }
    Book getBook(int idx) const { return books[idx]; }
    bool checkout(int idx) {
        if (idx < 0 || idx >= (int)books.size()) return false;
        if (borrowed[idx]) return false;
        borrowed[idx] = true;
        return true;
    }
    bool searchByTitle(const std::string& t) const {
        auto it = std::find_if(books.begin(), books.end(),
            [&t](const Book& b) { return b.getTitle() == t; });
        return it != books.end();
    }
};

int main() {
    Library lib;
    lib.addBook(Book("Tutunamayanlar", "Oğuz Atay"));
    lib.addBook(Book("İnce Memed", "Yaşar Kemal"));
    // list books, check out book 0 and print the result
    return 0;
}

Sample solution

main.cpp
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

class Book {
private:
    std::string title;
    std::string author;
public:
    Book(std::string t, std::string a) : title(t), author(a) {}
    std::string getTitle() const { return title; }
    std::string getAuthor() const { return author; }
};

class Library {
private:
    std::vector<Book> books;
    std::vector<bool> borrowed;
public:
    void addBook(const Book& b) { books.push_back(b); borrowed.push_back(false); }
    size_t count() const { return books.size(); }
    Book getBook(int idx) const { return books[idx]; }
    bool checkout(int idx) {
        if (idx < 0 || idx >= (int)books.size()) return false;
        if (borrowed[idx]) return false;
        borrowed[idx] = true;
        return true;
    }
    bool searchByTitle(const std::string& t) const {
        auto it = std::find_if(books.begin(), books.end(),
            [&t](const Book& b) { return b.getTitle() == t; });
        return it != books.end();
    }
};

int main() {
    Library lib;
    lib.addBook(Book("Tutunamayanlar", "Oğuz Atay"));
    lib.addBook(Book("İnce Memed", "Yaşar Kemal"));
    for (size_t i = 0; i < lib.count(); ++i) {
        std::cout << i << ": " << lib.getBook(i).getTitle() << "\n";
    }
    if (lib.checkout(0)) {
        std::cout << "borrowed";
    }
    return 0;
}

Expected output

0: Tutunamayanlar
1: İnce Memed
borrowed

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What is used to list books in the menu?

main.cpp
for (size_t i = 0; i < ???; ++i)
  • std::vector
  • class Book
  • lib.count() Correct answer
  • std::sort

Explanation: lib.count() gives the book count as the loop bound.

2 Match each method to its purpose

Term Meaning
lib.count() Book count
lib.getBook(i) ith book
lib.checkout(0) Check out
lib.searchByTitle(t) Search by title

3 Complete the loop body

Question
for (size_t i = 0; i < lib.count(); ++i) {
    std::cout << i << ": " << lib.____(i).getTitle() << "\n";
}
Answer
for (size_t i = 0; i < lib.count(); ++i) {
    std::cout << i << ": " << lib.getBook(i).getTitle() << "\n";
}

Explanation: lib.getBook(i) reaches the ith book.

All lessons in Intermediate