创建一个 Library类,包含一个 Book向量和一个Patron向量。定义一个名为Transaction 的 struet,包含一个Book对象、一个Patron对象和一个本章中定义的Date对象,表示借阅记录。在 Library 类中定义一个Transaction向量。定义向图书馆添加图书、添加读者以及借出书籍的函数。当一个读者借出一本书时,保证Library对象中有此读者和这本书的记录,否则报告错误。然后检查读者是否欠费,如果欠费就报告一个错误,否则创建一个Transaction对象,将其放人Transaction向量中。定义一个返回包含所有欠费读者姓名的向量的函数。该软件是多文件程序,按文件给出代码,在主测试文件中设置菜单,由用户手动录入图书信息,用C++设计实现,只需要写出主测试文件
时间: 2024-02-13 19:06:39 浏览: 107
以下是主测试文件的代码,实现了添加图书、添加读者、借出书籍以及查看欠费读者的功能:
```c++
#include <iostream>
#include <vector>
#include "Library.h"
using namespace std;
// 函数声明
void printMenu();
void addBookToLibrary(Library& lib);
void addPatronToLibrary(Library& lib);
void checkoutBook(Library& lib);
void viewFines(Library& lib);
int main() {
Library lib; // 创建图书馆对象
int choice;
do {
printMenu();
cin >> choice;
switch (choice) {
case 1:
addBookToLibrary(lib);
break;
case 2:
addPatronToLibrary(lib);
break;
case 3:
checkoutBook(lib);
break;
case 4:
viewFines(lib);
break;
case 5:
cout << "Goodbye!" << endl;
break;
default:
cout << "Invalid choice. Please try again." << endl;
break;
}
cout << endl;
} while (choice != 5);
return 0;
}
// 打印菜单
void printMenu() {
cout << "Welcome to the Library!" << endl;
cout << "1. Add a book to the library" << endl;
cout << "2. Add a patron to the library" << endl;
cout << "3. Checkout a book" << endl;
cout << "4. View fines" << endl;
cout << "5. Quit" << endl;
cout << "Enter your choice: ";
}
// 向图书馆添加图书
void addBookToLibrary(Library& lib) {
string title, author;
int year;
cout << "Enter the book title: ";
cin.ignore();
getline(cin, title);
cout << "Enter the book author: ";
getline(cin, author);
cout << "Enter the year the book was published: ";
cin >> year;
Book book(title, author, year);
lib.addBook(book);
cout << "Book added successfully!" << endl;
}
// 向图书馆添加读者
void addPatronToLibrary(Library& lib) {
string name;
int id;
cout << "Enter the patron name: ";
cin.ignore();
getline(cin, name);
cout << "Enter the patron ID: ";
cin >> id;
Patron patron(name, id);
lib.addPatron(patron);
cout << "Patron added successfully!" << endl;
}
// 借出书籍
void checkoutBook(Library& lib) {
int bookId, patronId;
cout << "Enter the book ID: ";
cin >> bookId;
cout << "Enter the patron ID: ";
cin >> patronId;
Book book = lib.getBook(bookId);
Patron patron = lib.getPatron(patronId);
if (book.getId() == -1) {
cout << "Book not found. Please try again." << endl;
return;
}
if (patron.getId() == -1) {
cout << "Patron not found. Please try again." << endl;
return;
}
if (lib.isPatronOwesFines(patron)) {
cout << "Patron owes fines and cannot checkout books." << endl;
return;
}
if (!lib.isBookAvailable(book)) {
cout << "Book is not available for checkout." << endl;
return;
}
Transaction transaction(book, patron, Date());
lib.addTransaction(transaction);
cout << "Book checked out successfully!" << endl;
}
// 查看欠费读者
void viewFines(Library& lib) {
vector<string> patrons = lib.getPatronsOwingFines();
if (patrons.size() == 0) {
cout << "No patrons owe fines." << endl;
return;
}
cout << "Patrons owing fines:" << endl;
for (int i = 0; i < patrons.size(); i++) {
cout << "- " << patrons[i] << endl;
}
}
```
其中,Library类的头文件定义如下:
```c++
#ifndef LIBRARY_H
#define LIBRARY_H
#include <vector>
#include "Book.h"
#include "Patron.h"
#include "Transaction.h"
class Library {
public:
void addBook(const Book& book);
void addPatron(const Patron& patron);
void addTransaction(const Transaction& transaction);
bool isBookAvailable(const Book& book) const;
bool isPatronOwesFines(const Patron& patron) const;
Book getBook(int id) const;
Patron getPatron(int id) const;
std::vector<std::string> getPatronsOwingFines() const;
private:
std::vector<Book> books;
std::vector<Patron> patrons;
std::vector<Transaction> transactions;
static const double DAILY_FINE = 0.1;
};
#endif
```
其中,Book、Patron和Transaction类的定义分别如下:
```c++
#ifndef BOOK_H
#define BOOK_H
#include <string>
class Book {
public:
Book(std::string title, std::string author, int year);
int getId() const;
std::string getTitle() const;
std::string getAuthor() const;
int getYear() const;
bool isCheckedOut() const;
void checkOut();
void checkIn();
private:
static int nextId;
int id;
std::string title;
std::string author;
int year;
bool checkedOut;
};
#endif
```
```c++
#ifndef PATRON_H
#define PATRON_H
#include <string>
class Patron {
public:
Patron(std::string name, int id);
int getId() const;
std::string getName() const;
double getFines() const;
void addFine(double fine);
void payFines();
private:
int id;
std::string name;
double fines;
};
#endif
```
```c++
#ifndef TRANSACTION_H
#define TRANSACTION_H
#include "Book.h"
#include "Patron.h"
#include "Date.h"
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;
};
#endif
```
其他类的实现可根据需要自行编写。
阅读全文