创建一个 Library类,包含一个 Book向量和一个Patron向量。定义一个名为Transaction 的 struet,包含一个Book对象、一个Patron对象和一个本章中定义的Date对象,表示借阅记录。在 Library 类中定义一个Transaction向量。定义向图书馆添加图书、添加读者以及借出书籍的函数。当一个读者借出一本书时,保证Library对象中有此读者和这本书的记录,否则报告错误。然后检查读者是否欠费,如果欠费就报告一个错误,否则创建一个Transaction对象,将其放人Transaction向量中。定义一个返回包含所有欠费读者姓名的向量的函数。多文件程序,按文件给出代码,有录入信息的操作,用户可以手动添加信息
时间: 2024-02-13 15:03:01 浏览: 22
以下为多文件程序代码:
```
// Library.h文件
#ifndef LIBRARY_H
#define LIBRARY_H
#include <vector>
#include "Book.h"
#include "Patron.h"
#include "Transaction.h"
class Library {
public:
void addBook(Book&);
void addPatron(Patron&);
void checkoutBook(Book&, Patron&, const Date&);
std::vector<std::string> getDebtors();
private:
std::vector<Book> books;
std::vector<Patron> patrons;
std::vector<Transaction> transactions;
};
#endif
// Book.h文件
#ifndef BOOK_H
#define BOOK_H
#include <string>
#include "Date.h"
class Book {
public:
Book(std::string t, std::string a, std::string i, bool c);
std::string getTitle();
std::string getAuthor();
std::string getISBN();
bool isCheckedOut();
void checkOut();
void checkIn();
private:
std::string title;
std::string author;
std::string isbn;
bool checkedOut;
Date checkedOutDate;
};
#endif
// Book.cpp文件
#include "Book.h"
Book::Book(std::string t, std::string a, std::string i, bool c) {
title = t;
author = a;
isbn = i;
checkedOut = c;
}
std::string Book::getTitle() {
return title;
}
std::string Book::getAuthor() {
return author;
}
std::string Book::getISBN() {
return isbn;
}
bool Book::isCheckedOut() {
return checkedOut;
}
void Book::checkOut() {
checkedOut = true;
checkedOutDate = Date::getDate();
}
void Book::checkIn() {
checkedOut = false;
}
// Patron.h文件
#ifndef PATRON_H
#define PATRON_H
#include <string>
#include "Date.h"
class Patron {
public:
Patron(std::string n, std::string id);
std::string getName();
std::string getId();
double getFees();
void addFee(double);
void payFees(double);
private:
std::string name;
std::string patronId;
double fees;
};
#endif
// Patron.cpp文件
#include "Patron.h"
Patron::Patron(std::string n, std::string id) {
name = n;
patronId = id;
fees = 0.0;
}
std::string Patron::getName() {
return name;
}
std::string Patron::getId() {
return patronId;
}
double Patron::getFees() {
return fees;
}
void Patron::addFee(double f) {
fees += f;
}
void Patron::payFees(double f) {
fees -= f;
}
// Date.h文件
#ifndef DATE_H
#define DATE_H
#include <string>
class Date {
public:
static Date getDate();
std::string toString();
private:
int month;
int day;
int year;
};
#endif
// Date.cpp文件
#include "Date.h"
#include <ctime>
#include <string>
#include <iomanip>
Date Date::getDate() {
std::time_t now = std::time(nullptr);
std::tm* timeinfo = std::localtime(&now);
Date d;
d.month = timeinfo->tm_mon + 1;
d.day = timeinfo->tm_mday;
d.year = timeinfo->tm_year + 1900;
return d;
}
std::string Date::toString() {
std::stringstream ss;
ss << std::setfill('0') << std::setw(2) << month << "/"
<< std::setfill('0') << std::setw(2) << day << "/"
<< std::setfill('0') << std::setw(4) << year;
return ss.str();
}
// Transaction.h文件
#ifndef TRANSACTION_H
#define TRANSACTION_H
#include "Book.h"
#include "Patron.h"
#include "Date.h"
struct Transaction {
Book book;
Patron patron;
Date date;
};
#endif
// Library.cpp文件
#include "Library.h"
void Library::addBook(Book& b) {
books.push_back(b);
}
void Library::addPatron(Patron& p) {
patrons.push_back(p);
}
void Library::checkoutBook(Book& b, Patron& p, const Date& d) {
bool bookFound = false;
bool patronFound = false;
for (Book& book : books) {
if (book.getISBN() == b.getISBN()) {
bookFound = true;
if (book.isCheckedOut()) {
std::cout << "Error: Book is already checked out.\n";
return;
}
break;
}
}
if (!bookFound) {
std::cout << "Error: Book not found.\n";
return;
}
for (Patron& patron : patrons) {
if (patron.getId() == p.getId()) {
patronFound = true;
if (patron.getFees() > 0) {
std::cout << "Error: Patron has unpaid fees.\n";
return;
}
break;
}
}
if (!patronFound) {
std::cout << "Error: Patron not found.\n";
return;
}
b.checkOut();
Transaction t = { b, p, d };
transactions.push_back(t);
}
std::vector<std::string> Library::getDebtors() {
std::vector<std::string> debtors;
for (Patron& patron : patrons) {
if (patron.getFees() > 0) {
debtors.push_back(patron.getName());
}
}
return debtors;
}
// main.cpp文件
#include <iostream>
#include "Library.h"
int main() {
Library library;
Book book1("The Great Gatsby", "F. Scott Fitzgerald", "9780141182636", false);
Book book2("To Kill a Mockingbird", "Harper Lee", "9780446310789", false);
Book book3("1984", "George Orwell", "9780451524935", false);
library.addBook(book1);
library.addBook(book2);
library.addBook(book3);
Patron patron1("John Smith", "12345");
Patron patron2("Jane Doe", "67890");
library.addPatron(patron1);
library.addPatron(patron2);
Date d = Date::getDate();
library.checkoutBook(book1, patron1, d);
library.checkoutBook(book2, patron2, d);
library.checkoutBook(book3, patron1, d);
std::vector<std::string> debtors = library.getDebtors();
if (debtors.size() > 0) {
std::cout << "The following patrons have unpaid fees:\n";
for (std::string& debtor : debtors) {
std::cout << debtor << std::endl;
}
} else {
std::cout << "No patrons have unpaid fees.\n";
}
return 0;
}
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)