for Robot Artificial Inteligence

18. Library 연습문제

|

Exercise

main.cpp

#include <iostream>
#include <conio.h>
#include <cstdlib>
#include "library.h"
#include "book.h"
#include "user.h"

using namespace std;


int main()
{
    const Book book1("Arkadiusz Wlodarczyk", "C++ course", 2009);
    const Book book2("Wioletta Kozik", "JAVA course", 2016);
    const Book book3("Adrian Szuszkiewicz", "C# course", 2002);
    const Book book4 = book3;
    const Book book5 = book3;
    const Book book6 = book3;

    Library library(5);
    Librarian librarian("Foo", "Bar", 5);
    Borrower borrower("Edek", "Czytacz", 5);

    library.addBook(book1);
    library.addBook(book2);
    library.addBook(book3);
    library.addBook(book4);
    library.addBook(book6);

    int position;
    char choice;

    while (true)
    {
        library.showBooks();
        borrower.showBooks();
        cout << "Would you like to Borrow - B/b or Return - R/r: ";
		cin >> choice;

        switch(choice)
        {
        case 'b':
        case 'B':
            {
                cout << "Which book would you like to rent, POSITION IN LIBRARY: ";
                cin >> position;
                librarian.lendBook(library, borrower, position);
                break;
            }
        case 'r':
        case 'R':
            {
                cout << "Which book would you like to return, POSITION IN USER: ";
                cin >> position;
                borrower.returnBook(library, position);
                break;
            }
		default:
			break;
        }
        system("cls");
    }
    return 0;
}

library.h

#ifndef LIBRARY_H_INCLUDED
#define LIBRARY_H_INCLUDED

#include "book.h"
#include "user.h"

class Library
{
    private:
        int currentAmountOfBooks;
        int maxAmountOfBooks;
        Book* books;

    public:
        void addBook(const Book&);
        Book& getBook(int);
        void showBooks();

        friend void Librarian::lendBook(Library&, Borrower&, int);

        Library(int);
        ~Library();
};


#endif // LIBRARY_H_INCLUDED

book.h

#ifndef BOOK_H_INCLUDED
#define BOOK_H_INCLUDED

#include <iostream>

class Book
{
    private:
        std::string title;
        std::string author;
        int publicationYear;
        int bookId;

    public:
        static int numberOfBooks;

        std::string getTitle() const;
        std::string getAuthor() const;
        int getPublicationYear() const;
        int getBookId() const;

        Book(const Book&);
        Book(std::string, std::string, int);
        Book();
        ~Book();
};


#endif // BOOK_H_INCLUDED

user.h

#ifndef USER_H_INCLUDED
#define USER_H_INCLUDED
#include <string>
#include "book.h"

class Library;

class User
{
protected:
    std::string name;
    std::string lastName;

public:
    std::string getName();
    std::string getLastName();
    User(std::string, std::string);
};

class Borrower: public User
{
protected:
    friend class Librarian;
    int userAmountOfBooks;
    int maxAmountOfBooks;
    Book* booksInPossesion;

public:
    Borrower(std::string, std::string, int);

    ~Borrower();
    Borrower(const Borrower&) = delete;
    Borrower& operator= (const Borrower&) = delete;

    void returnBook(Library&, int);
    Book& getBook(int id);
    void showBooks();
};

class Librarian: public Borrower
{

public:
    Librarian(std::string, std::string, int);
    void lendBook(Library&, Borrower&, int);
};


#endif // USER_H_INCLUDED

book.cpp

#include "book.h"

int Book::numberOfBooks = 0;

std::string Book::getTitle() const
{
    return this->title;
}

std::string Book::getAuthor() const
{
    return this->author;
}

int Book::getPublicationYear() const
{
    return this->publicationYear;
}

int Book::getBookId() const
{
    return this->bookId;
}

Book::Book(const Book& bookToCopy)
{
    this->title = bookToCopy.getTitle();
    this->author = bookToCopy.getAuthor();
    this->publicationYear = bookToCopy.getPublicationYear();
    this->bookId = Book::numberOfBooks++;
}

Book::Book(std::string author, std::string title, int publicationYear)
{
    this->author = author;
    this->title = title;
    this->publicationYear = publicationYear;
    this->bookId = numberOfBooks++;
}

Book::Book()
{

}

Book::~Book()
{

}

library.cpp

#include "library.h"


void Library::addBook(const Book& book)
{
    if (currentAmountOfBooks < maxAmountOfBooks)
    {
        this->books[currentAmountOfBooks] = book;
        this->currentAmountOfBooks++;
    }
    else
    {

    }
}

Book& Library::getBook(int position)
{
    if (position < currentAmountOfBooks)
    {
        return books[position];
    }
    else
    {
        return books[0];
    }
}

void Library::showBooks()
{
    std::cout << "LIBRARY BOOKS: " << std::endl;
    for (int i = 0; i < currentAmountOfBooks; i++)
    {
        std::cout << "BOOK ID: " << getBook(i).getBookId() << "  ";
        std::cout << "POSITION IN LIBRARY: " << i << "  ";
        std::cout << "Author: " << getBook(i).getAuthor() << "  ";
        std::cout << "Title: " << getBook(i).getTitle() << "  ";
        std::cout << "Publication Year: " << getBook(i).getPublicationYear() << std::endl;
    }
    std::cout << std::endl;
}

Library::Library(int maxAmountOfBooks)
{
    this->currentAmountOfBooks = 0;
    this->maxAmountOfBooks = maxAmountOfBooks;
    this->books = new Book[maxAmountOfBooks];
}

Library::~Library()
{
    delete[] books;
}

user.cpp

#include "user.h"

#include "library.h"

std::string User::getName()
{
    return this->name;
}

std::string User::getLastName()
{
    return this->lastName;
}

User::User(std::string name, std::string lastName)
{
    this->name = name;
    this->lastName = lastName;
}

Borrower::Borrower(std::string name, std::string lastName, int maxAmountOfBooks)
        : User (name, lastName)
{
    this->userAmountOfBooks = 0;
    this->maxAmountOfBooks = maxAmountOfBooks;
    booksInPossesion = new Book[maxAmountOfBooks];
}

Borrower::~Borrower()
{
    delete[] booksInPossesion;
}

void Borrower::returnBook(Library& libraryToReturnTo, int position)
{
    if (position < this->userAmountOfBooks && this->userAmountOfBooks > 0)
    {
        libraryToReturnTo.addBook(booksInPossesion[position]);

        for(int i = 0; i < maxAmountOfBooks-1; i++)
        {
            if (i >= position)
                booksInPossesion[i] = booksInPossesion[i+1];
        }
        this->userAmountOfBooks--;
    }
    else
    {
    }
}

Book& Borrower::getBook(int position)
{
    if (position < this->userAmountOfBooks)
    {
        return booksInPossesion[position];
    }
    else
    {
        return booksInPossesion[0];
    }
}

void Borrower::showBooks()
{
    std::cout << getName() << " " << getLastName() << " BOOKS: " << std::endl;
    for (int i = 0; i < userAmountOfBooks; i++)
    {
        std::cout << "BOOK ID: " << getBook(i).getBookId() << "  ";
        std::cout << "POSITION IN USER: " << i << "  ";
        std::cout << "Author: " << getBook(i).getAuthor() << "  ";
        std::cout << "Title: " << getBook(i).getTitle() << "  ";
        std::cout << "Publication Year: " << getBook(i).getPublicationYear() << std::endl;
    }
    std::cout << std::endl;
}

Librarian::Librarian(std::string name, std::string lastName, int maxAmountOfBooks)
        : Borrower(name, lastName, maxAmountOfBooks)
    {}

void Librarian::lendBook(Library& libraryToLendFrom, Borrower& personToLendTo, int position)
{
    if (libraryToLendFrom.currentAmountOfBooks > 0)
    {
        if (personToLendTo.userAmountOfBooks < personToLendTo.maxAmountOfBooks && position < libraryToLendFrom.currentAmountOfBooks)
        {
            personToLendTo.booksInPossesion[personToLendTo.userAmountOfBooks] = libraryToLendFrom.books[position];

            for(int i = 0; i < libraryToLendFrom.maxAmountOfBooks-1; i++)
            {
                if (i >= position)
                    libraryToLendFrom.books[i] = libraryToLendFrom.books[i+1];
            }

            libraryToLendFrom.currentAmountOfBooks--;
            personToLendTo.userAmountOfBooks++;
        }
        else
        {
        }
    }
    else
    {
    }
}

Comments