Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string>
- #include <iostream>
- #include <iomanip>
- #include "Library.h"
- void displayMenu();
- bool menuSelection( int option, Library* lib );
- void addBookInformation( Library* lib );
- void removeBook( Library* lib );
- void displayBook( Library* lib );
- int main() {
- Library lib;
- std::cout << "Welcome to the Library!n";
- displayMenu();
- int option = 0;
- do {
- std::cout << "nChoose an option from the menu ";
- std::cin >> option;
- std::cout << 'n';
- } while( menuSelection( option, &lib ) );
- std::cout << "nPress any key and enter to quit.n";
- std::cin.get();
- return 0;
- }
- void displayMenu() {
- std::cout << "================================================n";
- std::cout << "1: Add a book to the libraryn";
- std::cout << "2: Remove book from the libraryn";
- std::cout << "3: Display the number of books in the libraryn";
- std::cout << "4: Display a books informationn";
- std::cout << "5: Display the list of all booksn";
- std::cout << "6: Display menu optionn";
- std::cout << "7: Exit the library and quitn";
- std::cout << "================================================n";
- }
- bool menuSelection( int option, Library* lib ) {
- switch( option ) {
- case 1: {
- addBookInformation( lib );
- std::cout.flush();
- system( "clear" );
- displayMenu();
- std::cout << "nYou have entered a book into the libray.n";
- break;
- }
- case 2: {
- removeBook( lib );
- std::cout.flush();
- system( "clear" );
- displayMenu();
- std::cout << "nYou have removed a book from the library.n";
- break;
- }
- case 3: {
- unsigned int numBooks = lib->totalBooks();
- if( numBooks != 1 ) {
- std::cout << "nThere are " << numBooks << " books in our library.n";
- } else {
- std::cout << "nThere is 1 book in our library.n";
- }
- break;
- }
- case 4: {
- displayBook( lib );
- break;
- }
- case 5: {
- unsigned int numBooks = lib->totalBooks();
- if( numBooks > 0 ) {
- std::cout << *lib;
- } else {
- std::cout << "nThere are no books to display.n";
- }
- break;
- }
- case 6: {
- std::cin.ignore();
- std::cout.flush();
- system( "clear" );
- displayMenu();
- break;
- }
- case 7: {
- return false;
- }
- default: {
- std::cout << "nInvalid selection please try again.n";
- break;
- }
- }
- return true;
- }
- void addBookInformation( Library* lib ) {
- static unsigned int bookId = 0;
- unsigned int year = 0;
- std::string title, author;
- std::cin.ignore();
- std::cout << "Please enter the books title: ";
- std::getline( std::cin, title );
- std::cout << "Please enter the books author: ";
- std::getline( std::cin, author );
- std::cout << "Please enter the books year: ";
- std::cin >> year;
- bookId++; // increment our book id so each one is unique TODO: this can be replaced to have same id for multiple books if the books are exact matches.
- Book book( title, author, year );
- lib->addBook( std::to_string( bookId ), book );
- }
- void removeBook( Library* lib ) {
- unsigned int numBooks = lib->totalBooks();
- if( numBooks == 0 ) {
- std::cout << "nThere are 0 books in library; nothing to remove.n";
- return;
- }
- std::cout << "nRemove book by ID(I) or by Book(B)n";
- char choice;
- std::cin >> choice;
- if( choice == 'i' || choice == 'I' ) {
- std::cout << "Enter the books ID ";
- unsigned int id;
- std::cin >> id;
- lib->removeBook( std::to_string( id ) );
- } else if( choice == 'b' || choice == 'B' ) {
- std::cin.ignore();
- std::cout << "What is the title of the book? ";
- std::string title;
- std::getline( std::cin, title );
- std::cout << "Who is the author of the book? ";
- std::string author;
- std::getline( std::cin, author );
- std::cout << "What year was the book published ";
- unsigned int year;
- std::cin >> year;
- Book book( title, author, year );
- lib->removeBook( book );
- } else {
- std::cout << "nYou entered an invalid selectionn";
- }
- }
- void displayBook( Library* lib ) {
- unsigned int numBooks = lib->totalBooks();
- if( numBooks == 0 ) {
- std::cout << "nThere are 0 books in the library; nothing to display.n";
- return;
- }
- std::cout << "nFind book by ID(I) or by Book(B)n";
- char choice;
- std::cin >> choice;
- if( choice == 'i' || choice == 'I' ) {
- std::cout << "Enter the books ID ";
- unsigned int id;
- std::cin >> id;
- Book* book = lib->findBook( std::to_string( id ) );
- if( book ) {
- std::cout << *book;
- } else {
- std::cout << "nBook was not found.n";
- }
- } else if( choice == 'b' || choice == 'B' ) {
- std::cin.ignore();
- std::cout << "What is the title of the book? ";
- std::string title;
- std::getline( std::cin, title );
- std::cout << "Who is the author of the book? ";
- std::string author;
- std::getline( std::cin, author );
- std::cout << "What year was the book published? ";
- unsigned int year;
- std::cin >> year;
- Book bookToFind( title, author, year );
- Book* actualBook = lib->findBook( bookToFind );
- if( actualBook ) {
- std::cout << *actualBook;
- } else {
- std::cout << "nBook was not found.n";
- }
- } else {
- std::cout << "nYou entered an invalid selectionn";
- }
- }
- #ifndef BOOK_H
- #define BOOK_H
- #include <string>
- #include <iostream>
- #include <iomanip>
- class Book {
- private:
- std::string title_;
- std::string author_;
- unsigned int year_;
- public:
- Book(); // default
- Book( const std::string& title, const std::string& author, unsigned int year );
- std::string titleIs() const;
- std::string authorIs() const;
- unsigned int yearPublished() const;
- void updateTitle( const std::string& title );
- void updateAuthor( const std::string& author );
- void updateYear( unsigned int year );
- bool operator==( const Book& other ) const;
- };
- std::ostream& operator<<( std::ostream& out, const Book& book );
- #endif // BOOK_H
- #include "Book.h"
- Book::Book() {
- } // default
- Book::Book( const std::string& title, const std::string& author, unsigned int year ) :
- title_( title ),
- author_( author ),
- year_( year ) {
- }
- std::string Book::titleIs() const {
- return title_;
- }
- std::string Book::authorIs() const {
- return author_;
- }
- unsigned int Book::yearPublished() const {
- return year_;
- }
- void Book::updateTitle( const std::string& title ) {
- title_ = title;
- }
- void Book::updateAuthor( const std::string& author ) {
- author_ = author;
- }
- void Book::updateYear( unsigned int year ) {
- year_ = year;
- }
- bool Book::operator==( const Book& other ) const {
- return ( title_ == other.title_ &&
- author_ == other.author_ &&
- year_ == other.year_ );
- }
- std::ostream& operator<<( std::ostream& out, const Book& book ) {
- out << std::setw( 15 ) << "Title: " << book.titleIs() << 'n'
- << std::setw( 15 ) << "Author: " << book.authorIs() << 'n'
- << std::setw( 15 ) << "Year: " << book.yearPublished() << 'n';
- return out;
- }
- #ifndef LIBRARY_H
- #define LIBRARY_H
- #include "Book.h"
- #include <map>
- class Library {
- private:
- std::multimap<std::string, Book> books_;
- // This is used if the library has several copies of the same book
- // that have the same ID in the multimap above.
- std::map<std::string, unsigned int> inventory_;
- public:
- Library(); // deafault
- void addBook( const std::string& id, Book& book );
- void removeBook( const std::string& id );
- void removeBook( Book& book );
- Book* findBook( const std::string& id );
- Book* findBook( Book& book );
- std::size_t totalBooks() const;
- std::size_t totalUniqueBooks() const;
- // Three different ways to return the library back to user
- std::multimap<std::string, Book> mapOfBooks() const;
- // Currently Supports List and Vector
- template< template < class ... > class Container, class ... Args >
- void listOfBooks( Container<Book, Args...>& c ) const;
- private:
- // Helper function to calculate the number of unique books.
- std::size_t calculateUniqueNumberOfBooks() const;
- };
- template<template<class...> class Container, class...Args>
- void Library::listOfBooks( Container<Book, Args...>& c ) const {
- auto it = books_.begin();
- while ( it != books_.end() ) {
- c.emplace_back( it->second );
- }
- }
- std::ostream& operator<<( std::ostream& out, const Library& library );
- void displayLibrary( const Library& library );
- #endif // LIBRARY_H
- #include "Library.h"
- #include <vector>
- Library::Library() {
- } // deafault
- void Library::addBook( const std::string& id, Book& book ) {
- books_.insert( std::pair<std::string,Book>( id, book ) );
- }
- void Library::removeBook( const std::string& id ) {
- auto it = books_.begin();
- while( it != books_.end() ) {
- if( id == it->first ) {
- // found match so remove it
- it = books_.erase( it );
- } else {
- it++;
- }
- }
- }
- void Library::removeBook( Book& book ) {
- auto it = books_.begin();
- while( it != books_.end() ) {
- if( book == it->second ) {
- // found match so remove it
- it = books_.erase( it );
- } else {
- it++;
- }
- }
- }
- Book* Library::findBook( const std::string& id ) {
- auto it = books_.begin();
- while( it != books_.end() ) {
- if( id == it->first ) {
- return &it->second;
- } else{
- it++;
- }
- }
- return nullptr;
- }
- Book* Library::findBook( Book& book ) {
- auto it = books_.begin();
- while( it != books_.end() ) {
- if( book == it->second ) {
- return &it->second;
- } else {
- it++;
- }
- }
- return nullptr;
- }
- std::multimap<std::string, Book> Library::mapOfBooks() const {
- return books_;
- }
- std::size_t Library::totalBooks() const {
- return books_.size();
- }
- std::size_t Library::totalUniqueBooks() const {
- //TODO: For now just return total number of books
- return books_.size();
- }
- std::size_t Library::calculateUniqueNumberOfBooks() const {
- //TODO: For now just return total number of books
- return books_.size();
- }
- std::ostream& operator<<( std::ostream& out, const Library& library ) {
- for( auto b : library.mapOfBooks() ) {
- out << "ID " << b.first << 'n'
- << b.second;
- }
- return out;
- }
- void displayLibrary( const Library& library ) {
- std::cout << library;
- }
- Library::Library() {
- } // deafault [sic]
- Book::Book() {
- } // default
- Book::Book( const std::string& title, const std::string& author, unsigned int year ) :
- title_( title ),
- author_( author ),
- year_( year ) {
- }
- Book( std::string title, std::string author, unsigned int year ) :
- title_{ std::move(title) },
- author_{ std::move(author) },
- year_{ year }
- { }
- // Three different ways to return the library back to user
- std::multimap<std::string, Book> mapOfBooks() const;
- // Currently Supports List and Vector
- template< template < class ... > class Container, class ... Args >
- void listOfBooks( Container<Book, Args...>& c ) const;
- template<template<class...> class Container, class...Args>
- void Library::listOfBooks( Container<Book, Args...>& c ) const {
- auto it = books_.begin();
- while ( it != books_.end() ) {
- c.emplace_back( it->second );
- }
- }
- void Library::removeBook( const std::string& id ) {
- auto it = books_.begin();
- while( it != books_.end() ) {
- if( id == it->first ) {
- // found match so remove it
- it = books_.erase( it );
- } else {
- it++;
- }
- }
- }
Add Comment
Please, Sign In to add comment