【问题标题】:Architecture of the Library-project图书馆项目的架构
【发布时间】:2016-12-08 19:02:41
【问题描述】:

这是我的第一个问题!我试图在 Stroustrup 的《使用 C++ 的原理和实践》一书中解决问题。第9章有三个问题:

1) 创建类书,这将是图书馆程序软件的一部分。以下成员必须是:ISBN 号、作者姓氏、作者姓名和注册日期。创建函数 ....

这个我做了,头文件:

#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) 为图书馆创建类 Patron。这个类必须有以下成员:客户的名字,他的卡号和费用的大小。还创建一些函数来设置和从类中获取数据。 我也这样做过:

#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

问题出在第三个

3) 创建类库。在其中包括书籍和赞助人的向量。还包括一个结构 Transaction 并创建它的 Vector。创建函数,可以添加有关书籍和客户的数据。如果一个人拿了一本书,图书馆必须知道这个人是否是客户,这本书是否属于它的基金会。

这里有个问题... 这是我的尝试:

#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

我不明白 Patron、Book 和 Library 之间的连接应该是什么样子,以及如何解决这里的最终问题.. 如果有人可以向我解释并编写基本代码,将不胜感激。

【问题讨论】:

  • QString 和 QVector 是什么?确定std::string 和std::vector 就足够了吗?
  • @Bathsheba 也许不是,他可能想要一个 GUI,然后他会使用 Qt
  • 是的,QString 和 QVector 等价于 std::Vector 和 std::String。我正在使用 Qt
  • 然而,恕我直言,实际的逻辑代码(为了可重用性)不应该依赖于 Qt。我通常尽可能将 GUI 和逻辑分开,并在逻辑中使用 std::* 将 QString 仅用于 GUI
  • 感谢您的回复,但我认为 GUI 与否无关紧要,我使用的是 Qt,std 和反之亦然是绝对可以接受的,实际上没有区别,使用哪个标准和无论哪种方式,架构问题仍然存在..

标签: c++ qt architecture


【解决方案1】:

如果您想要关于图书的信息,您可以将布尔值替换为变量book,例如“book_purchase”。默认情况下,您可以使用名为 no_book 的书设置变量,并执行一个 set 函数来归属新书。

在你的类客户端

private book_purchase 

在你的 book 构造函数中

this.book_purchase = new book();
this.book_purchase.set_Name("no_book");

当您将一本书 ($here myBook$) 归于客户时,请使用类似的东西

this.set_book_purchase(mybook.get_Name());

当图书馆需要知道客户是否拥有一本书时:

client.get_book_purchase();

【讨论】:

    【解决方案2】:

    首先你需要创建一个struct Transaction,而不是另一个class。交易几乎不会与库链接,因此您的代码必须如下所示:

    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
    
    
    };
    

    关于您的书籍和您的顾客的所有信息都是私有的,因此如果您的图书馆必须管理您必须将 set 函数添加到您的其他班级。 Set 函数允许其他类可以修改相关类的数据。例如,在 name_author 的图书类中:

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

    您必须为库将要管理的每个变量执行此操作。

    在您的库类中,您现在可以添加类似的内容

    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);
        }
    
    }
    

    以后你可以像这样管理现有的书

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

    如果您不想更改参数,请设置 NULL,您可以从向量中提取 book1。对赞助人的管理也是如此。

    要知道一个人是否是客户,您可以使用与 getPaid_a_Fee() 相同的逻辑。

    如果 library 需要知道客户是否拥有(或不)一本书,请在您的客户类中添加一个名为 book_purchase 的布尔值。当他拿起一本书时,使用 set_... 来更新布尔值。

    希望能帮到你!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-09
      • 2011-03-26
      • 2012-06-14
      相关资源
      最近更新 更多