创建一个 Library类,包含一个 Book向量和一个Patron向量。定义一个名为Transaction 的 struet,包含一个Book对象、一个Patron对象和一个本章中定义的Date对象,表示借阅记录。在 Library 类中定义一个Transaction向量。定义向图书馆添加图书、添加读者以及借出书籍的函数。当一个读者借出一本书时,保证Library对象中有此读者和这本书的记录,否则报告错误。然后检查读者是否欠费,如果欠费就报告一个错误,否则创建一个Transaction对象,将其放人Transaction向量中。定义一个返回包含所有欠费读者姓名的向量的函数。该软件是多文件程序,按文件给出代码,在主测试文件中设置菜单,由用户手动录入图书信息,用C++设计实现,只需要写出主测试文件
时间: 2024-02-13 12:06:20 浏览: 65
这里提供一个主测试文件的示例代码,其他文件的具体实现需要根据题目要求自行编写。
```
#include <iostream>
#include <vector>
#include "Library.h"
#include "Book.h"
#include "Patron.h"
#include "Transaction.h"
using namespace std;
int main() {
Library library;
int choice;
do {
cout << "1. Add book" << endl;
cout << "2. Add patron" << endl;
cout << "3. Check out book" << endl;
cout << "4. List patrons with fees" << endl;
cout << "5. Quit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch(choice) {
case 1: {
string title, author;
cout << "Enter book title: ";
cin >> title;
cout << "Enter book author: ";
cin >> author;
Book book(title, author);
library.addBook(book);
cout << "Book added successfully!" << endl;
break;
}
case 2: {
string name;
int id;
cout << "Enter patron name: ";
cin >> name;
cout << "Enter patron ID: ";
cin >> id;
Patron patron(name, id);
library.addPatron(patron);
cout << "Patron added successfully!" << endl;
break;
}
case 3: {
int bookId, patronId;
cout << "Enter book ID: ";
cin >> bookId;
cout << "Enter patron ID: ";
cin >> patronId;
Book book = library.getBook(bookId);
Patron patron = library.getPatron(patronId);
if (book.getTitle() == "" || patron.getName() == "") {
cout << "Error: Book or patron not found!" << endl;
} else if (patron.getFees() > 0) {
cout << "Error: Patron has unpaid fees!" << endl;
} else {
Date date;
Transaction transaction(book, patron, date);
library.addTransaction(transaction);
cout << "Book checked out successfully!" << endl;
}
break;
}
case 4: {
vector<string> patronsWithFees = library.getPatronsWithFees();
if (patronsWithFees.size() == 0) {
cout << "No patrons with unpaid fees." << endl;
} else {
cout << "Patrons with unpaid fees: ";
for (string patronName : patronsWithFees) {
cout << patronName << ", ";
}
cout << endl;
}
break;
}
case 5: {
cout << "Goodbye!" << endl;
break;
}
default: {
cout << "Invalid choice. Please try again." << endl;
break;
}
}
} while (choice != 5);
return 0;
}
```
阅读全文