Ivan Ivan - 4 months ago 19
C++ Question

Architecture of the Library-project

This is my first question here! I have tried to solve problem in Stroustrup's book "Principles and Practice Using C++". In chapter 9 there are three problems:

1) Create class Book, which will be the part of program software of Library. Following members have to be: for ISBN number, surname of author, name of author and Date of registration. Create function ....

this I have done, header file:

#ifndef BOOK_H
#define BOOK_H
#include <QString>
#include <iostream>
using std::ostream;

class Book
{
public:
Book();
/*
prevent datacheck
generate constructor*/

bool isGiven() const;
int get_ISBN() const;
void set_ISBN(const int &isbn_); // ? right format ?


void give_in_hands(const bool &given_);
void set_surname_author(const QString &value);
void set_name_author(const QString &value);
QString get_surname_author() const;
QString get_name_author()const;

void set_date(const struct Date a);

int get_date(int y);

bool operator==( const Book& b);
// ostream& operator<<(ostream& os);

enum Genre{
novel,ski,horror,comedy
};
Genre get_genre( );


private:

int ISBN;
QString surname_author;
QString name_author;

/* // declare a structure for date ?
int year;
int day;
enum months{
jan=1,feb,mar,apr,may,jun,jul,avg,sen,okt,nov,dec
};*/
bool given;

int year;
int day;
int month;

};

struct Date{

int year;
int day;
/* enum months{
jan=1,feb,mar,apr,may,jun,jul,avg,sen,okt,nov,dec
};*/
int month;
Date(int year_,int day_, int month_){
year=year_;
day=day_;
month = month_;
}


};

#endif // BOOK_H


2) Create class Patron for the Library. This class has to have following members: name of client, number of his card and size of a fee. Create also some function to set and get data from the class.
This I have done as well:

#ifndef PATRON_H
#define PATRON_H

#include <QString>

class Patron
{
QString name;
int cardNumber;
int fee;
bool Paid_a_fee;

public:
Patron();

//did a person pay a fee or not
void set_Pay_or_not(const bool& a);
bool did_pay_or_not() const;

int getCardNumber() const;
void setCardNumber(int value);
int getFee() const;
void setFee(int value);
};

#endif // PATRON_H


The problem is in the third

3) Create class Library. Include in it Vectors of Book and Patron. Include also a structure Transaction and create Vector of its. Create function, which could add data about books and clients. If a person has taken a book, Library has to know, if person is client or not and does the book belong to its foundation.

Here I have got a problem...
this is my try to do:

#ifndef LIBRARY_H
#define LIBRARY_H

#include <patron.h>
#include <book.h>
#include <QVector>

class Transaction{
Book book_one;
Patron person_one;


};

class Library
{
public:
Library();
QVector <Book> books;
QVector <Patron> patrons;

};


#endif // LIBRARY_H


I do not understand how the connection between Patron, Book and Library should look like and how to solve the final problem here..
If someone could explain it to me and write basic code as well, this would be greatly appreciate.

Answer

First of all you need to create a struct Transaction and not an other class. The transactions will be hardly linked with the library so your code had to look like :

class Library
{
public:
  Library();

    struct Transaction
    {
    Book book_one;
    Patron person_one;
    };

    QVector <Book> books;
    QVector <Patron> patrons;

    QVector <Transaction> transaction; // here your transaction vector


};

All the information about your books and your patrons are private so if it's your library who had to manage that you had to add set functions to your other class. Set functions allow that other class can modify data of the concerned class. For example, in your book class for the name_author :

void Set_name_author(QString name)
{
  this.name_author=name;
}

You had to do that for each variable thaht library will be manage.

In your library class you can now add something like that

Void manage_book(Book myBook; QString name, QString IBSR, QString surname)
{
    if(name!=NULL)
    {
      myBook.set_Name(name);
    }

    if(IBSR!=NULL)
    {
      myBook.set_IBSR(IBSR);
    }

    if(surname!=NULL)
    {
      myBook.set_Surname(Surname);
    }

}

Later you can manage existing book like that

myLibrary.manage_book(book1,NULL,NULL,"bob");

Put NULL if you don’t want to change the parameter and you can extract book1 from your vector. It’s the same for the patron’smanagement.

To know if a person is a client you can use the same logic with a getPaid_a_Fee().

If library need to know if a client own (or not) a book add a boolean called book_purchase to your client class. When he takes a book use a set_... to update the boolean.

Hope i help you !