创建一个 Library类,包含一个 Book向量和一个Patron向量。定义一个名为Transaction 的 struet,包含一个Book对象、一个Patron对象和一个本章中定义的Date对象,表示借阅记录。在 Library 类中定义一个Transaction向量。定义向图书馆添加图书、添加读者以及借出书籍的函数。当一个读者借出一本书时,保证Library对象中有此读者和这本书的记录,否则报告错误。然后检查读者是否欠费,如果欠费就报告一个错误,否则创建一个Transaction对象,将其放人Transaction向量中。定义一个返回包含所有欠费读者姓名的向量的函数。
时间: 2024-02-13 18:59:35 浏览: 141
好的,我理解了您的要求。以下是实现您描述的Library类的代码(包括Book、Patron和Date类的实现):
```cpp
#include <iostream>
#include <vector>
#include <string>
class Date {
public:
Date(int year, int month, int day) : year_(year), month_(month), day_(day) {}
int year() const { return year_; }
int month() const { return month_; }
int day() const { return day_; }
private:
int year_;
int month_;
int day_;
};
class Book {
public:
Book(const std::string& title, const std::string& author, int isbn, int num_copies)
: title_(title), author_(author), isbn_(isbn), num_copies_(num_copies) {}
std::string title() const { return title_; }
std::string author() const { return author_; }
int isbn() const { return isbn_; }
int num_copies() const { return num_copies_; }
void set_num_copies(int num_copies) { num_copies_ = num_copies; }
private:
std::string title_;
std::string author_;
int isbn_;
int num_copies_;
};
class Patron {
public:
Patron(const std::string& name, int library_card_num, double fees_owed)
: name_(name), library_card_num_(library_card_num), fees_owed_(fees_owed) {}
std::string name() const { return name_; }
int library_card_num() const { return library_card_num_; }
double fees_owed() const { return fees_owed_; }
void set_fees_owed(double fees_owed) { fees_owed_ = fees_owed; }
private:
std::string name_;
int library_card_num_;
double fees_owed_;
};
struct Transaction {
Book book;
Patron patron;
Date date;
};
class Library {
public:
void add_book(const Book& book) {
books_.push_back(book);
}
void add_patron(const Patron& patron) {
patrons_.push_back(patron);
}
void lend_book(const Book& book, const Patron& patron, const Date& date) {
bool book_found = false;
bool patron_found = false;
for (Book& b : books_) {
if (b.isbn() == book.isbn()) {
if (b.num_copies() > 0) {
b.set_num_copies(b.num_copies() - 1);
book_found = true;
break;
}
}
}
for (Patron& p : patrons_) {
if (p.library_card_num() == patron.library_card_num()) {
if (p.fees_owed() == 0) {
patron_found = true;
break;
}
}
}
if (!book_found) {
std::cout << "Error: book not found in library." << std::endl;
}
if (!patron_found) {
std::cout << "Error: patron not found in library or has unpaid fees." << std::endl;
}
if (book_found && patron_found) {
transactions_.push_back({book, patron, date});
}
}
std::vector<std::string> get_overdue_patrons() const {
std::vector<std::string> overdue_patrons;
for (const Patron& p : patrons_) {
if (p.fees_owed() > 0) {
overdue_patrons.push_back(p.name());
}
}
return overdue_patrons;
}
private:
std::vector<Book> books_;
std::vector<Patron> patrons_;
std::vector<Transaction> transactions_;
};
```
您可以使用上述代码来创建一个Library对象,并调用其成员函数来添加图书、添加读者和借阅图书。例如:
```cpp
int main() {
Library library;
// Add books
Book book1("The Catcher in the Rye", "J.D. Salinger", 12345, 2);
Book book2("To Kill a Mockingbird", "Harper Lee", 67890, 1);
library.add_book(book1);
library.add_book(book2);
// Add patrons
Patron patron1("Alice", 123, 0);
Patron patron2("Bob", 456, 10.5);
library.add_patron(patron1);
library.add_patron(patron2);
// Lend books
Date today(2022, 11, 1);
library.lend_book(book1, patron1, today); // should succeed
library.lend_book(book1, patron2, today); // should fail (book already lent out)
library.lend_book(book2, patron2, today); // should fail (patron has unpaid fees)
// Get overdue patrons
std::vector<std::string> overdue_patrons = library.get_overdue_patrons();
std::cout << "Overdue patrons: ";
for (const std::string& name : overdue_patrons) {
std::cout << name << " ";
}
std::cout << std::endl;
return 0;
}
```
输出应该为:
```
Error: patron not found in library or has unpaid fees.
Error: patron not found in library or has unpaid fees.
Overdue patrons: Bob
```
阅读全文