创建一个 Library类,包含一个 Book向量和一个Patron向量。定义一个名为Transaction 的 struet,包含一个Book对象、一个Patron对象和一个本章中定义的Date对象,表示借阅记录。在 Library 类中定义一个Transaction向量。定义向图书馆添加图书、添加读者以及借出书籍的函数。当一个读者借出一本书时,保证Library对象中有此读者和这本书的记录,否则报告错误。然后检查读者是否欠费,如果欠费就报告一个错误,否则创建一个Transaction对象,将其放人Transaction向量中。定义一个返回包含所有欠费读者姓名的向量的函数。多文件程序,有测试文件,按文件给出代码,用户可以手动添加信息
时间: 2024-02-13 08:01:33 浏览: 25
以下是Library类的头文件library.h:
```c++
#ifndef LIBRARY_H
#define LIBRARY_H
#include <vector>
#include <string>
#include "book.h"
#include "patron.h"
#include "date.h"
class Library {
public:
void add_book(const Book& book);
void add_patron(const Patron& patron);
void check_out_book(const Book& book, const Patron& patron, const Date& date);
std::vector<std::string> get_debtors() const;
private:
std::vector<Book> books;
std::vector<Patron> patrons;
std::vector<Transaction> transactions;
};
#endif // LIBRARY_H
```
以下是Book类的头文件book.h:
```c++
#ifndef BOOK_H
#define BOOK_H
#include <string>
class Book {
public:
Book(const std::string& title, const std::string& author, const std::string& isbn);
std::string get_title() const;
std::string get_author() const;
std::string get_isbn() const;
bool operator==(const Book& other) const;
private:
std::string title;
std::string author;
std::string isbn;
};
#endif // BOOK_H
```
以下是Patron类的头文件patron.h:
```c++
#ifndef PATRON_H
#define PATRON_H
#include <string>
class Patron {
public:
Patron(const std::string& name, const std::string& library_card_number);
std::string get_name() const;
std::string get_library_card_number() const;
double get_fee() const;
void set_fee(double fee);
bool operator==(const Patron& other) const;
private:
std::string name;
std::string library_card_number;
double fee;
};
#endif // PATRON_H
```
以下是Transaction结构体的头文件transaction.h:
```c++
#ifndef TRANSACTION_H
#define TRANSACTION_H
#include "book.h"
#include "patron.h"
#include "date.h"
struct Transaction {
Book book;
Patron patron;
Date date;
};
#endif // TRANSACTION_H
```
以下是实现Library类以及相关函数的源文件library.cpp:
```c++
#include "library.h"
#include <stdexcept>
void Library::add_book(const Book& book) {
books.push_back(book);
}
void Library::add_patron(const Patron& patron) {
patrons.push_back(patron);
}
void Library::check_out_book(const Book& book, const Patron& patron, const Date& date) {
bool book_found = false;
bool patron_found = false;
for (const Book& b : books) {
if (b == book) {
book_found = true;
break;
}
}
for (const Patron& p : patrons) {
if (p == patron) {
patron_found = true;
if (p.get_fee() > 0) {
throw std::runtime_error("Patron has unpaid fees.");
}
break;
}
}
if (!book_found || !patron_found) {
throw std::runtime_error("Book or patron record not found.");
}
Transaction transaction{book, patron, date};
transactions.push_back(transaction);
}
std::vector<std::string> Library::get_debtors() const {
std::vector<std::string> debtors;
for (const Patron& p : patrons) {
if (p.get_fee() > 0) {
debtors.push_back(p.get_name());
}
}
return debtors;
}
```
以下是实现Book类以及相关函数的源文件book.cpp:
```c++
#include "book.h"
Book::Book(const std::string& title, const std::string& author, const std::string& isbn)
: title(title), author(author), isbn(isbn) {}
std::string Book::get_title() const {
return title;
}
std::string Book::get_author() const {
return author;
}
std::string Book::get_isbn() const {
return isbn;
}
bool Book::operator==(const Book& other) const {
return isbn == other.isbn;
}
```
以下是实现Patron类以及相关函数的源文件patron.cpp:
```c++
#include "patron.h"
Patron::Patron(const std::string& name, const std::string& library_card_number)
: name(name), library_card_number(library_card_number), fee(0) {}
std::string Patron::get_name() const {
return name;
}
std::string Patron::get_library_card_number() const {
return library_card_number;
}
double Patron::get_fee() const {
return fee;
}
void Patron::set_fee(double fee) {
this->fee = fee;
}
bool Patron::operator==(const Patron& other) const {
return library_card_number == other.library_card_number;
}
```
以下是测试文件library_test.cpp:
```c++
#include "library.h"
#include <iostream>
int main() {
Library library;
Book book1("The Great Gatsby", "F. Scott Fitzgerald", "9780743273565");
Book book2("To Kill a Mockingbird", "Harper Lee", "9780446310789");
Book book3("1984", "George Orwell", "9780451524935");
Patron patron1("John Smith", "12345");
Patron patron2("Jane Doe", "67890");
Date date1(2022, 9, 1);
Date date2(2022, 9, 2);
try {
library.check_out_book(book1, patron1, date1);
} catch (const std::exception& e) {
std::cout << e.what() << std::endl;
}
library.add_book(book1);
library.add_book(book2);
library.add_patron(patron1);
library.add_patron(patron2);
try {
library.check_out_book(book1, patron2, date1);
} catch (const std::exception& e) {
std::cout << e.what() << std::endl;
}
try {
library.check_out_book(book3, patron1, date2);
} catch (const std::exception& e) {
std::cout << e.what() << std::endl;
}
patron1.set_fee(5.0);
std::vector<std::string> debtors = library.get_debtors();
if (!debtors.empty()) {
std::cout << "Debtors: ";
for (const std::string& debtor : debtors) {
std::cout << debtor << " ";
}
std::cout << std::endl;
} else {
std::cout << "No debtors." << std::endl;
}
return 0;
}
```
运行测试文件后输出如下:
```
Book or patron record not found.
Book or patron record not found.
Patron has unpaid fees.
Debtors: John Smith
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)