创建一个 Library类,包含一个 Book向量和一个Patron向量。定义一个名为Transaction 的 struet,包含一个Book对象、一个Patron对象和一个本章中定义的Date对象,表示借阅记录。在 Library 类中定义一个Transaction向量。定义向图书馆添加图书、添加读者以及借出书籍的函数。当一个读者借出一本书时,保证Library对象中有此读者和这本书的记录,否则报告错误。然后检查读者是否欠费,如果欠费就报告一个错误,否则创建一个Transaction对象,将其放人Transaction向量中。定义一个返回包含所有欠费读者姓名的向量的函数。该软件是多文件程序,按文件给出代码,在主测试文件中设置菜单,由用户手动输入图书信息,用C++设计实现,重新写一份
时间: 2024-02-13 16:05:31 浏览: 17
这里是头文件 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:
struct Transaction {
Book book;
Patron patron;
Date date;
};
Library();
void add_book(Book book);
void add_patron(Patron patron);
void check_out_book(Book book, Patron patron, Date date);
std::vector<std::string> get_debtors() const;
private:
std::vector<Book> books;
std::vector<Patron> patrons;
std::vector<Transaction> transactions;
};
#endif
```
这里是实现文件 library.cpp:
```c++
#include "library.h"
Library::Library() {}
void Library::add_book(Book book) {
books.push_back(book);
}
void Library::add_patron(Patron patron) {
patrons.push_back(patron);
}
void Library::check_out_book(Book book, Patron patron, Date date) {
bool book_found = false;
bool patron_found = false;
for (int i = 0; i < books.size(); i++) {
if (books[i].get_isbn() == book.get_isbn()) {
book_found = true;
break;
}
}
for (int i = 0; i < patrons.size(); i++) {
if (patrons[i].get_card_number() == patron.get_card_number()) {
patron_found = true;
if (patrons[i].get_fees_owed() > 0) {
throw std::runtime_error("Patron owes fees");
}
break;
}
}
if (!book_found || !patron_found) {
throw std::runtime_error("Book or patron not found");
}
transactions.push_back(Transaction{book, patron, date});
}
std::vector<std::string> Library::get_debtors() const {
std::vector<std::string> debtors;
for (int i = 0; i < patrons.size(); i++) {
if (patrons[i].get_fees_owed() > 0) {
debtors.push_back(patrons[i].get_name());
}
}
return debtors;
}
```
这里是头文件 book.h:
```c++
#ifndef BOOK_H
#define BOOK_H
#include <string>
class Book {
public:
Book(std::string isbn, std::string title, std::string author);
std::string get_isbn() const;
std::string get_title() const;
std::string get_author() const;
private:
std::string isbn;
std::string title;
std::string author;
};
#endif
```
这里是实现文件 book.cpp:
```c++
#include "book.h"
Book::Book(std::string isbn, std::string title, std::string author) : isbn(isbn), title(title), author(author) {}
std::string Book::get_isbn() const {
return isbn;
}
std::string Book::get_title() const {
return title;
}
std::string Book::get_author() const {
return author;
}
```
这里是头文件 patron.h:
```c++
#ifndef PATRON_H
#define PATRON_H
#include <string>
class Patron {
public:
Patron(std::string name, std::string card_number);
std::string get_name() const;
std::string get_card_number() const;
double get_fees_owed() const;
void set_fees_owed(double fees);
private:
std::string name;
std::string card_number;
double fees_owed;
};
#endif
```
这里是实现文件 patron.cpp:
```c++
#include "patron.h"
Patron::Patron(std::string name, std::string card_number) : name(name), card_number(card_number), fees_owed(0) {}
std::string Patron::get_name() const {
return name;
}
std::string Patron::get_card_number() const {
return card_number;
}
double Patron::get_fees_owed() const {
return fees_owed;
}
void Patron::set_fees_owed(double fees) {
fees_owed = fees;
}
```
这里是头文件 date.h:
```c++
#ifndef DATE_H
#define DATE_H
#include <string>
class Date {
public:
Date(int year, int month, int day);
std::string to_string() const;
private:
int year;
int month;
int day;
};
#endif
```
这里是实现文件 date.cpp:
```c++
#include "date.h"
Date::Date(int year, int month, int day) : year(year), month(month), day(day) {}
std::string Date::to_string() const {
return std::to_string(year) + "-" + std::to_string(month) + "-" + std::to_string(day);
}
```
这里是主测试文件 main.cpp:
```c++
#include <iostream>
#include "library.h"
int main() {
Library library;
std::string input;
std::cout << "Enter book information (ISBN title author): ";
std::getline(std::cin, input);
std::string isbn = input.substr(0, input.find(" "));
input.erase(0, input.find(" ") + 1);
std::string title = input.substr(0, input.find(" "));
input.erase(0, input.find(" ") + 1);
std::string author = input;
Book book(isbn, title, author);
library.add_book(book);
std::cout << "Enter patron information (name card number): ";
std::getline(std::cin, input);
std::string name = input.substr(0, input.find(" "));
input.erase(0, input.find(" ") + 1);
std::string card_number = input;
Patron patron(name, card_number);
library.add_patron(patron);
std::cout << "Enter checkout information (ISBN card number year month day): ";
std::getline(std::cin, input);
isbn = input.substr(0, input.find(" "));
input.erase(0, input.find(" ") + 1);
card_number = input.substr(0, input.find(" "));
input.erase(0, input.find(" ") + 1);
int year = std::stoi(input.substr(0, input.find(" ")));
input.erase(0, input.find(" ") + 1);
int month = std::stoi(input.substr(0, input.find(" ")));
input.erase(0, input.find(" ") + 1);
int day = std::stoi(input);
Book checkout_book("", "", "");
Patron checkout_patron("", "");
for (int i = 0; i < library.get_books().size(); i++) {
if (library.get_books()[i].get_isbn() == isbn) {
checkout_book = library.get_books()[i];
break;
}
}
for (int i = 0; i < library.get_patrons().size(); i++) {
if (library.get_patrons()[i].get_card_number() == card_number) {
checkout_patron = library.get_patrons()[i];
break;
}
}
try {
library.check_out_book(checkout_book, checkout_patron, Date(year, month, day));
std::cout << "Checkout successful" << std::endl;
} catch (std::runtime_error& e) {
std::cout << "Error: " << e.what() << std::endl;
}
std::vector<std::string> debtors = library.get_debtors();
if (debtors.size() > 0) {
std::cout << "The following patrons owe fees:" << std::endl;
for (int i = 0; i < debtors.size(); i++) {
std::cout << debtors[i] << std::endl;
}
}
return 0;
}
```