创建一个 Library类,包含一个 Book向量和一个Patron向量。定义一个名为Transaction 的 struet,包含一个Book对象、一个Patron对象和一个本章中定义的Date对象,表示借阅记录。在 Library 类中定义一个Transaction向量。定义向图书馆添加图书、添加读者以及借出书籍的函数。当一个读者借出一本书时,保证Library对象中有此读者和这本书的记录,否则报告错误。然后检查读者是否欠费,如果欠费就报告一个错误,否则创建一个Transaction对象,将其放人Transaction向量中。定义一个返回包含所有欠费读者姓名的向量的函数。多文件程序,按文件给出代码,测试文件用户手动录入信息的操作,用户可以手动添加信息
时间: 2024-02-13 14:03:04 浏览: 13
我们先定义一个 Book 类和一个 Patron 类,然后再定义 Library 类和 Transaction 类。
Book 类:
```C++
class Book {
public:
Book(std::string isbn, std::string title, std::string author, int year);
std::string getISBN() const;
std::string getTitle() const;
std::string getAuthor() const;
int getYear() const;
private:
std::string isbn;
std::string title;
std::string author;
int year;
};
```
Patron 类:
```C++
class Patron {
public:
Patron(std::string name, std::string card_number);
std::string getName() const;
std::string getCardNumber() const;
double getFees() const;
void addFee(double fee);
private:
std::string name;
std::string card_number;
double fees;
};
```
Date 类:
```C++
class Date {
public:
Date(int year, int month, int day);
int getYear() const;
int getMonth() const;
int getDay() const;
private:
int year;
int month;
int day;
};
```
Transaction 类:
```C++
class Transaction {
public:
Transaction(Book book, Patron patron, Date date);
Book getBook() const;
Patron getPatron() const;
Date getDate() const;
private:
Book book;
Patron patron;
Date date;
};
```
Library 类:
```C++
class Library {
public:
void addBook(Book book);
void addPatron(Patron patron);
void checkout(Book book, Patron patron, Date date);
std::vector<std::string> getDebtors() const;
private:
std::vector<Book> books;
std::vector<Patron> patrons;
std::vector<Transaction> transactions;
};
```
接下来是实现这些类的方法:
Book 类:
```C++
Book::Book(std::string isbn, std::string title, std::string author, int year)
: isbn(isbn), title(title), author(author), year(year) {}
std::string Book::getISBN() const {
return isbn;
}
std::string Book::getTitle() const {
return title;
}
std::string Book::getAuthor() const {
return author;
}
int Book::getYear() const {
return year;
}
```
Patron 类:
```C++
Patron::Patron(std::string name, std::string card_number)
: name(name), card_number(card_number), fees(0) {}
std::string Patron::getName() const {
return name;
}
std::string Patron::getCardNumber() const {
return card_number;
}
double Patron::getFees() const {
return fees;
}
void Patron::addFee(double fee) {
fees += fee;
}
```
Date 类:
```C++
Date::Date(int year, int month, int day)
: year(year), month(month), day(day) {}
int Date::getYear() const {
return year;
}
int Date::getMonth() const {
return month;
}
int Date::getDay() const {
return day;
}
```
Transaction 类:
```C++
Transaction::Transaction(Book book, Patron patron, Date date)
: book(book), patron(patron), date(date) {}
Book Transaction::getBook() const {
return book;
}
Patron Transaction::getPatron() const {
return patron;
}
Date Transaction::getDate() const {
return date;
}
```
Library 类:
```C++
void Library::addBook(Book book) {
books.push_back(book);
}
void Library::addPatron(Patron patron) {
patrons.push_back(patron);
}
void Library::checkout(Book book, Patron patron, Date date) {
// check if book and patron are in the library
bool book_found = false;
for (auto b : books) {
if (b.getISBN() == book.getISBN()) {
book_found = true;
break;
}
}
if (!book_found) {
std::cout << "Error: book not found in library." << std::endl;
return;
}
bool patron_found = false;
for (auto p : patrons) {
if (p.getCardNumber() == patron.getCardNumber()) {
patron_found = true;
break;
}
}
if (!patron_found) {
std::cout << "Error: patron not found in library." << std::endl;
return;
}
// check if patron owes fees
if (patron.getFees() > 0) {
std::cout << "Error: patron owes fees." << std::endl;
return;
}
// add transaction
Transaction transaction(book, patron, date);
transactions.push_back(transaction);
}
std::vector<std::string> Library::getDebtors() const {
std::vector<std::string> debtors;
for (auto patron : patrons) {
if (patron.getFees() > 0) {
debtors.push_back(patron.getName());
}
}
return debtors;
}
```
在测试文件中,我们可以手动录入图书馆的信息,包括图书、读者和借阅记录,然后查询所有欠费读者的姓名。
测试文件:
```C++
#include <iostream>
#include "Book.h"
#include "Patron.h"
#include "Date.h"
#include "Transaction.h"
#include "Library.h"
int main() {
Library library;
std::cout << "Enter books (ISBN, title, author, year):" << std::endl;
std::string line;
while (std::getline(std::cin, line) && !line.empty()) {
std::stringstream ss(line);
std::string isbn, title, author;
int year;
ss >> isbn >> title >> author >> year;
Book book(isbn, title, author, year);
library.addBook(book);
}
std::cout << "Enter patrons (name, card number):" << std::endl;
while (std::getline(std::cin, line) && !line.empty()) {
std::stringstream ss(line);
std::string name, card_number;
ss >> name >> card_number;
Patron patron(name, card_number);
library.addPatron(patron);
}
std::cout << "Enter transactions (ISBN, card number, year, month, day):" << std::endl;
while (std::getline(std::cin, line) && !line.empty()) {
std::stringstream ss(line);
std::string isbn, card_number;
int year, month, day;
ss >> isbn >> card_number >> year >> month >> day;
Book book(isbn, "", "", 0);
Patron patron("", card_number);
Date date(year, month, day);
library.checkout(book, patron, date);
}
std::vector<std::string> debtors = library.getDebtors();
if (debtors.empty()) {
std::cout << "No debtors." << std::endl;
} else {
std::cout << "Debtors:" << std::endl;
for (auto debtor : debtors) {
std::cout << debtor << std::endl;
}
}
return 0;
}
```