创建一个 Library类,包含一个 Book向量和一个Patron向量。定义一个名为Transaction 的 struet,包含一个Book对象、一个Patron对象和一个本章中定义的Date对象,表示借阅记录。在 Library 类中定义一个Transaction向量。定义向图书馆添加图书、添加读者以及借出书籍的函数。当一个读者借出一本书时,保证Library对象中有此读者和这本书的记录,否则报告错误。然后检查读者是否欠费,如果欠费就报告一个错误,否则创建一个Transaction对象,将其放人Transaction向量中。定义一个返回包含所有欠费读者姓名的向量的函数。该软件是多文件程序,按文件给出代码,在主测试文件中设置菜单,由用户手动录入图书信息,用C++设计实现,只需要写出主测试文件
时间: 2024-02-13 15:06:26 浏览: 27
主测试文件代码如下:
```cpp
#include <iostream>
#include <vector>
#include "Library.h"
using namespace std;
void displayMenu();
int main() {
Library library;
int choice;
do {
displayMenu();
cin >> choice;
switch (choice) {
case 1:
// 添加图书
cout << "请输入图书信息:" << endl;
library.addBook();
break;
case 2:
// 添加读者
cout << "请输入读者信息:" << endl;
library.addPatron();
break;
case 3:
// 借出书籍
library.checkoutBook();
break;
case 4:
// 欠费读者
vector<string> debtors = library.getDebtors();
if (debtors.empty()) {
cout << "目前没有欠费读者" << endl;
} else {
cout << "欠费读者:" << endl;
for (string debtor : debtors) {
cout << debtor << endl;
}
}
break;
case 0:
cout << "谢谢使用!" << endl;
break;
default:
cout << "输入错误,请重新输入" << endl;
break;
}
} while (choice != 0);
return 0;
}
void displayMenu() {
cout << "请选择操作:" << endl;
cout << "1. 添加图书" << endl;
cout << "2. 添加读者" << endl;
cout << "3. 借出书籍" << endl;
cout << "4. 查询欠费读者" << endl;
cout << "0. 退出" << endl;
}
```
Library.h 头文件代码如下:
```cpp
#ifndef LIBRARY_H
#define LIBRARY_H
#include <vector>
#include "Book.h"
#include "Patron.h"
#include "Transaction.h"
class Library {
public:
void addBook();
void addPatron();
void checkoutBook();
std::vector<std::string> getDebtors();
private:
std::vector<Book> books;
std::vector<Patron> patrons;
std::vector<Transaction> transactions;
bool hasBook(const Book& book) const;
bool hasPatron(const Patron& patron) const;
bool hasTransaction(const Transaction& transaction) const;
bool isPatronInDebt(const Patron& patron) const;
};
#endif
```
Library.cpp 源文件代码如下:
```cpp
#include <iostream>
#include <algorithm>
#include "Library.h"
using namespace std;
void Library::addBook() {
Book book;
cin >> book;
books.push_back(book);
cout << "添加成功!" << endl;
}
void Library::addPatron() {
Patron patron;
cin >> patron;
patrons.push_back(patron);
cout << "添加成功!" << endl;
}
void Library::checkoutBook() {
Book book;
Patron patron;
Date date;
cout << "请输入借阅信息:" << endl;
cout << "图书信息:" << endl;
cin >> book;
cout << "读者信息:" << endl;
cin >> patron;
cout << "借阅日期(年 月 日):" << endl;
cin >> date;
if (!hasBook(book)) {
cout << "图书不存在" << endl;
return;
}
if (!hasPatron(patron)) {
cout << "读者不存在" << endl;
return;
}
if (isPatronInDebt(patron)) {
cout << "读者欠费,无法借阅" << endl;
return;
}
Transaction transaction{book, patron, date};
transactions.push_back(transaction);
cout << "借阅成功!" << endl;
}
vector<string> Library::getDebtors() {
vector<string> debtors;
for (Patron patron : patrons) {
if (patron.getFee() > 0) {
debtors.push_back(patron.getName());
}
}
return debtors;
}
bool Library::hasBook(const Book& book) const {
auto it = find(books.begin(), books.end(), book);
return it != books.end();
}
bool Library::hasPatron(const Patron& patron) const {
auto it = find(patrons.begin(), patrons.end(), patron);
return it != patrons.end();
}
bool Library::hasTransaction(const Transaction& transaction) const {
auto it = find(transactions.begin(), transactions.end(), transaction);
return it != transactions.end();
}
bool Library::isPatronInDebt(const Patron& patron) const {
return patron.getFee() > 0;
}
```
Book.h 头文件代码如下:
```cpp
#ifndef BOOK_H
#define BOOK_H
#include <string>
class Book {
public:
Book() = default;
Book(const std::string& isbn, const std::string& title, const std::string& author);
std::string getISBN() const { return isbn; }
std::string getTitle() const { return title; }
std::string getAuthor() const { return author; }
friend bool operator==(const Book& lhs, const Book& rhs);
friend std::istream& operator>>(std::istream& is, Book& book);
private:
std::string isbn;
std::string title;
std::string author;
};
#endif
```
Book.cpp 源文件代码如下:
```cpp
#include <iostream>
#include "Book.h"
using namespace std;
Book::Book(const string& isbn, const string& title, const string& author)
: isbn(isbn), title(title), author(author) {}
bool operator==(const Book& lhs, const Book& rhs) {
return lhs.isbn == rhs.isbn;
}
istream& operator>>(istream& is, Book& book) {
cout << "ISBN: ";
getline(is, book.isbn);
cout << "书名: ";
getline(is, book.title);
cout << "作者: ";
getline(is, book.author);
return is;
}
```
Patron.h 头文件代码如下:
```cpp
#ifndef PATRON_H
#define PATRON_H
#include <string>
class Patron {
public:
Patron() = default;
Patron(const std::string& name, const std::string& id);
std::string getName() const { return name; }
std::string getId() const { return id; }
double getFee() const { return fee; }
void addFee(double amount) { fee += amount; }
friend bool operator==(const Patron& lhs, const Patron& rhs);
friend std::istream& operator>>(std::istream& is, Patron& patron);
private:
std::string name;
std::string id;
double fee = 0.0;
};
#endif
```
Patron.cpp 源文件代码如下:
```cpp
#include <iostream>
#include "Patron.h"
using namespace std;
Patron::Patron(const string& name, const string& id)
: name(name), id(id) {}
bool operator==(const Patron& lhs, const Patron& rhs) {
return lhs.id == rhs.id;
}
istream& operator>>(istream& is, Patron& patron) {
cout << "姓名: ";
getline(is, patron.name);
cout << "ID: ";
getline(is, patron.id);
return is;
}
```
Transaction.h 头文件代码如下:
```cpp
#ifndef TRANSACTION_H
#define TRANSACTION_H
#include "Book.h"
#include "Patron.h"
#include "Date.h"
class Transaction {
public:
Transaction() = default;
Transaction(const Book& book, const Patron& patron, const Date& date);
Book getBook() const { return book; }
Patron getPatron() const { return patron; }
Date getDate() const { return date; }
private:
Book book;
Patron patron;
Date date;
};
#endif
```
Transaction.cpp 源文件代码如下:
```cpp
#include "Transaction.h"
Transaction::Transaction(const Book& book, const Patron& patron, const Date& date)
: book(book), patron(patron), date(date) {}
```