Library question

Hi there


I am working on assignment where I have build library with books
Library have limit of how many books it can store.
Books have unique ID.
Books can be borrowed and returned to library by people.

I have library class, book class, person class.

I am confused about moving data (ie books).

I am creating an array of book type inside library with the max number of books that library can hold.
Books are created with default constructor from Book Class.

How should I implement borrowing and returning of the book?
When person borrows book, should I somehow clear it space from an array?
book[1] = {0} ???

How to return book?

I don't want exact code but more just talk trough the problem with someone who understands C++ better then me :) I should have enough knowledge to solve this, my problem is creating consistent solution.
As at the moment it feels that I am making 1 step ahead 2 back.


thanks :)

If your question is still open, I’d like to put my pennyworth in.
There are several approaches possible to that problem, according to which class should be more in charge, in your opinion, of managing the “loan procedure”.

Let’s assume you’ve got a basic situation like this:
- a class Book with a few properties like id, isbn and collocation;
- a class User (or Person) with a name and a phone number;
- a class Library with a vector of Books and a vector of Users.

The three simplest options are:
a) class User is accountable for managing the borrowing.
In this scenario, class User must have got something like a struct Borrowing which contains at least a pointer to the borrowed book and an integer which represents the borrowing date (the first field could optionally be an integer which represents the book id).
If the user can borrow more than one book, class User should keep count of them by a vector of instances of Borrowing.

Meanwhile, class Book could hold an additional field, bool available, to simplify Library work (i.e. check before loaning a book to a User).

In this scenario, User asks Library for a Book, choosing between the ones in the catalogue, and returns it in due time, or suffers a penalty like being banned from borrowing for a period of time.

b) class Library is appointed “loan manager”.
Again, several options are possible, the smarter of which, in my opinion, involve inheritance. However, it’s possible to avoid it.
In place of a vector of Book(s), you may have a vector of instances of a struct Loan where you define
- an instance of Book;
- a bool to check if it’s already borrowed or not (redundant, but handy);
- an integer for the date of borrowing.

I think you’ll also need to turn your vector of User(s) into a struct like this:
- User;
- vector of pointers to instances of Loan.

c) Book should manage it’s own borrow.
It doesn’t differ a lot from the other scenarios, but it has its downside: Book should be aware of User, which is not sensible. So I’m leaving this option out for the moment.

If you haven’t fall asleep yet, I’d like to add two notes:
- the best way to manage dates would not be of course by an integer, but by a better developed class;
- inheritance should be always kept into some account. Let me explain in just two points:
A library could contain more than just books, for example magazines and dvd. So I think an abstract base class Item with id and collocation plus a pure virtual function loan() would make your code ready for future improvements.
It’s possible to make Library derive from both Book and User instead of managing instances of them, but I think it would make the program logic unnecessarily complex.

Happy coding!
Last edited on
It all depends on what other operations you need to support. If possible, post the full assignment and we can give more informed opinions.

The simplest thing is to have a library that has an array of books. Each book has a field that identifies the borrower. This could be a person ID, a pointer to the person structure, etc. If the field is "blank" (nullptr, id==0, whatever) then the book has not been borrowed.
Topic archived. No new replies allowed.