请用C++ 编程语言实现上述过程
时间: 2024-04-21 16:25:22 浏览: 17
好的,下面是使用 C++ 实现的代码:
```c++
#include <iostream>
#include <string>
#include <vector>
using namespace std;
// 定义 Book 类
class Book {
public:
string barcode;
string name;
int borrowTimes;
bool isOnShelf;
// 构造函数
Book(string barcode, string name) {
this->barcode = barcode;
this->name = name;
borrowTimes = 0;
isOnShelf = true;
}
// 显示属性值
void display() {
cout << "图书条形码:" << barcode << endl;
cout << "图书名称:" << name << endl;
cout << "借出次数:" << borrowTimes << endl;
cout << "是否在架:" << (isOnShelf ? "是" : "否") << endl;
}
};
// 定义 Student 类
class Student {
public:
string id;
string name;
string gender;
int maxBorrowNum;
int borrowedNum;
// 构造函数
Student(string id, string name, string gender) {
this->id = id;
this->name = name;
this->gender = gender;
maxBorrowNum = 3;
borrowedNum = 0;
}
// 显示属性值
void display() {
cout << "学号:" << id << endl;
cout << "姓名:" << name << endl;
cout << "性别:" << gender << endl;
cout << "借阅册数限制:" << maxBorrowNum << endl;
cout << "在借册数:" << borrowedNum << endl;
}
};
// 定义 BorrowRecord 类
class BorrowRecord {
public:
string studentId;
string bookBarcode;
string borrowTime;
string returnTime;
// 构造函数
BorrowRecord(string studentId, string bookBarcode, string borrowTime) {
this->studentId = studentId;
this->bookBarcode = bookBarcode;
this->borrowTime = borrowTime;
returnTime = "";
}
// 设置还书时间
void setReturnTime(string returnTime) {
this->returnTime = returnTime;
}
// 显示借书记录
void display() {
cout << "读者编号:" << studentId << endl;
cout << "图书条形码:" << bookBarcode << endl;
cout << "借出时间:" << borrowTime << endl;
if (returnTime != "") {
cout << "归还时间:" << returnTime << endl;
}
}
};
// 定义 Library 类
class Library {
private:
vector<Book> books;
vector<Student> students;
vector<BorrowRecord> borrowRecords;
public:
// 添加图书
void addBook(Book book) {
books.push_back(book);
}
// 添加学生
void addStudent(Student student) {
students.push_back(student);
}
// 查找图书
int findBook(string barcode) {
for (int i = 0; i < books.size(); i++) {
if (books[i].barcode == barcode) {
return i;
}
}
return -1;
}
// 查找学生
int findStudent(string id) {
for (int i = 0; i < students.size(); i++) {
if (students[i].id == id) {
return i;
}
}
return -1;
}
// 借书
bool borrowBook(string studentId, string bookBarcode, string borrowTime) {
int bookIndex = findBook(bookBarcode);
int studentIndex = findStudent(studentId);
if (bookIndex == -1 || studentIndex == -1) {
cout << "借书失败:图书或读者不存在!" << endl;
return false;
}
if (!books[bookIndex].isOnShelf) {
cout << "借书失败:该图书已被借出!" << endl;
return false;
}
if (students[studentIndex].borrowedNum >= students[studentIndex].maxBorrowNum) {
cout << "借书失败:该读者已达到借阅上限!" << endl;
return false;
}
books[bookIndex].isOnShelf = false;
books[bookIndex].borrowTimes++;
students[studentIndex].borrowedNum++;
borrowRecords.push_back(BorrowRecord(studentId, bookBarcode, borrowTime));
cout << "借书成功!" << endl;
return true;
}
// 还书
bool returnBook(string studentId, string bookBarcode, string returnTime) {
int bookIndex = findBook(bookBarcode);
int studentIndex = findStudent(studentId);
if (bookIndex == -1 || studentIndex == -1) {
cout << "还书失败:图书或读者不存在!" << endl;
return false;
}
if (books[bookIndex].isOnShelf) {
cout << "还书失败:该图书已在架!" << endl;
return false;
}
books[bookIndex].isOnShelf = true;
students[studentIndex].borrowedNum--;
for (int i = 0; i < borrowRecords.size(); i++) {
if (borrowRecords[i].studentId == studentId && borrowRecords[i].bookBarcode == bookBarcode && borrowRecords[i].returnTime == "") {
borrowRecords[i].setReturnTime(returnTime);
break;
}
}
cout << "还书成功!" << endl;
return true;
}
// 显示所有图书
void displayAllBooks() {
cout << "所有图书:" << endl;
for (int i = 0; i < books.size(); i++) {
books[i].display();
cout << endl;
}
}
// 显示所有学生
void displayAllStudents() {
cout << "所有学生:" << endl;
for (int i = 0; i < students.size(); i++) {
students[i].display();
cout << endl;
}
}
// 显示所有借书记录
void displayAllBorrowRecords() {
cout << "所有借书记录:" << endl;
for (int i = 0; i < borrowRecords.size(); i++) {
borrowRecords[i].display();
cout << endl;
}
}
};
// 测试代码
int main() {
// 初始化图书和学生
Book book1("B001", "C++ Primer");
Book book2("B002", "Effective C++");
Book book3("B003", "The C++ Programming Language");
Student student1("S001", "张三", "男");
Student student2("S002", "李四", "女");
// 初始化图书馆
Library library;
library.addBook(book1);
library.addBook(book2);
library.addBook(book3);
library.addStudent(student1);
library.addStudent(student2);
// 测试借书和还书
library.borrowBook("S001", "B001", "2021-10-01");
library.borrowBook("S001", "B002", "2021-10-02");
library.borrowBook("S001", "B003", "2021-10-03");
library.borrowBook("S001", "B001", "2021-10-04"); // 借书失败,该图书已被借出
library.borrowBook("S001", "B004", "2021-10-05"); // 借书失败,图书不存在
library.borrowBook("S002", "B002", "2021-10-06"); // 借书失败,该读者已达到借阅上限
library.returnBook("S001", "B001", "2021-10-10");
library.returnBook("S001", "B002", "2021-10-11");
library.returnBook("S001", "B003", "2021-10-12");
library.returnBook("S001", "B001", "2021-10-13"); // 还书失败,该图书已在架
library.returnBook("S001", "B004", "2021-10-14"); // 还书失败,图书不存在
// 显示所有图书、学生和借书记录
library.displayAllBooks();
library.displayAllStudents();
library.displayAllBorrowRecords();
return 0;
}
```
这段代码实现了一个简单的图书借阅管理系统,其中包括了图书、学生、借书记录和图书馆四个类。您可以根据自己的需求进行修改和扩展。