创建一个 Library类,包含一个 Book向量和一个Patron向量。定义一个名为Transaction 的 struet,包含一个Book对象、一个Patron对象和一个本章中定义的Date对象,表示借阅记录。在 Library 类中定义一个Transaction向量。定义向图书馆添加图书、添加读者以及借出书籍的函数。当一个读者借出一本书时,保证Library对象中有此读者和这本书的记录,否则报告错误。然后检查读者是否欠费,如果欠费就报告一个错误,否则创建一个Transaction对象,将其放人Transaction向量中。定义一个返回包含所有欠费读者姓名的向量的函数。多文件程序,按文件给出代码,有录入信息的操作,用户可以手动添加信息。需要有自主测试文件
时间: 2024-02-13 21:07:34 浏览: 10
好的,下面是代码实现。
首先是 `Book` 类的定义:
```cpp
#include <string>
class Book {
public:
Book(std::string isbn, std::string title, std::string author, std::string genre);
std::string get_isbn() const;
std::string get_title() const;
std::string get_author() const;
std::string get_genre() const;
private:
std::string m_isbn;
std::string m_title;
std::string m_author;
std::string m_genre;
};
Book::Book(std::string isbn, std::string title, std::string author, std::string genre)
: m_isbn(isbn), m_title(title), m_author(author), m_genre(genre)
{}
std::string Book::get_isbn() const {
return m_isbn;
}
std::string Book::get_title() const {
return m_title;
}
std::string Book::get_author() const {
return m_author;
}
std::string Book::get_genre() const {
return m_genre;
}
```
然后是 `Patron` 类的定义:
```cpp
#include <string>
class Patron {
public:
Patron(std::string name, std::string card_num);
std::string get_name() const;
std::string get_card_num() const;
double get_fees() const;
void set_fees(double fees);
private:
std::string m_name;
std::string m_card_num;
double m_fees;
};
Patron::Patron(std::string name, std::string card_num)
: m_name(name), m_card_num(card_num), m_fees(0.0)
{}
std::string Patron::get_name() const {
return m_name;
}
std::string Patron::get_card_num() const {
return m_card_num;
}
double Patron::get_fees() const {
return m_fees;
}
void Patron::set_fees(double fees) {
m_fees = fees;
}
```
接下来是 `Date` 类的定义:
```cpp
class Date {
public:
Date(int year, int month, int day);
int get_year() const;
int get_month() const;
int get_day() const;
private:
int m_year;
int m_month;
int m_day;
};
Date::Date(int year, int month, int day)
: m_year(year), m_month(month), m_day(day)
{}
int Date::get_year() const {
return m_year;
}
int Date::get_month() const {
return m_month;
}
int Date::get_day() const {
return m_day;
}
```
然后是 `Transaction` 结构体的定义:
```cpp
struct Transaction {
Book book;
Patron patron;
Date date;
};
```
接下来是 `Library` 类的定义:
```cpp
#include <vector>
#include <string>
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> m_books;
std::vector<Patron> m_patrons;
std::vector<Transaction> m_transactions;
};
void Library::add_book(const Book& book) {
m_books.push_back(book);
}
void Library::add_patron(const Patron& patron) {
m_patrons.push_back(patron);
}
void Library::check_out_book(const Book& book, const Patron& patron, const Date& date) {
// Check if book and patron are in the library
bool book_found = false;
bool patron_found = false;
for (const Book& b : m_books) {
if (b.get_isbn() == book.get_isbn()) {
book_found = true;
break;
}
}
for (const Patron& p : m_patrons) {
if (p.get_card_num() == patron.get_card_num()) {
patron_found = true;
break;
}
}
if (!book_found || !patron_found) {
throw std::runtime_error("Error: book or patron not in library");
}
// Check if patron owes fees
if (patron.get_fees() > 0.0) {
throw std::runtime_error("Error: patron owes fees");
}
// Create transaction and add to vector
Transaction t = {book, patron, date};
m_transactions.push_back(t);
}
std::vector<std::string> Library::get_debtors() const {
std::vector<std::string> debtors;
for (const Patron& p : m_patrons) {
if (p.get_fees() > 0.0) {
debtors.push_back(p.get_name());
}
}
return debtors;
}
```
最后是主函数 `main` 的实现:
```cpp
#include <iostream>
#include <stdexcept>
#include <vector>
int main() {
Library library;
// Add books
Book b1("1234", "The Great Gatsby", "F. Scott Fitzgerald", "Fiction");
Book b2("5678", "To Kill a Mockingbird", "Harper Lee", "Fiction");
Book b3("9012", "The Catcher in the Rye", "J.D. Salinger", "Fiction");
library.add_book(b1);
library.add_book(b2);
library.add_book(b3);
// Add patrons
Patron p1("John Smith", "0001");
Patron p2("Jane Doe", "0002");
library.add_patron(p1);
library.add_patron(p2);
// Check out books
Date d1(2022, 5, 1);
Date d2(2022, 5, 2);
Date d3(2022, 5, 3);
try {
library.check_out_book(b1, p1, d1);
} catch (std::runtime_error& e) {
std::cerr << e.what() << std::endl;
}
try {
library.check_out_book(b2, p2, d2);
} catch (std::runtime_error& e) {
std::cerr << e.what() << std::endl;
}
try {
library.check_out_book(b3, p1, d3);
} catch (std::runtime_error& e) {
std::cerr << e.what() << std::endl;
}
// Get debtors
std::vector<std::string> debtors = library.get_debtors();
std::cout << "Debtors:" << std::endl;
for (const std::string& debtor : debtors) {
std::cout << debtor << std::endl;
}
return 0;
}
```
以上代码实现了一个简单的图书馆系统,可以添加图书和读者,借阅图书,并检查读者是否欠费,最后返回欠费读者的姓名列表。
相关推荐
![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)
![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)
![.zip](https://img-home.csdnimg.cn/images/20210720083646.png)
![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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)