一、系统功能介绍
系统主要分为三个功能模块:分别是图书管理模块、学生/教师管理模块、以及借阅模块
整体功能模块图如下:
二、实现运行截图
1.图书管理
2.学生管理
3.教师管理
4.教师借阅
5.学生借阅
三、系统源代码
1.系统相关实体类以及全局变量设置
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<iostream>
#include<fstream>
#include<vector>
#define MAX 100
using namespace std;
//图书信息
class Book
{
private:
char id[20];//编号
char ISBN[20];//ISBN
char name[20];//图书名称
char author[20];//作者
char address[20];//出版社
char date[20];//出版日期
float price;//金额
int state;//是否可借(1.是 0.否)
int num;//借阅次数
public:
Book(){}
// 全参构造函数
Book(const char* id, const char* ISBN, const char* name, const char* author, const char* address, const char* date, float price, int state, int num)
{
strcpy(this->id, id);
strcpy(this->ISBN, ISBN);
strcpy(this->name, name);
strcpy(this->author, author);
strcpy(this->address, address);
strcpy(this->date, date);
this->price = price;
this->state = state;
this->num = num;
}
// get方法示例
const char* getId() const { return id; }
const char* getISBN() const { return ISBN; }
const char* getName() const { return name; }
const char* getAuthor() const { return author; }
const char* getAddress() const { return address; }
const char* getDate() const { return date; }
float getPrice() const { return price; }
int getState() const { return state; }
int getNum() const { return num; }
// set方法示例
void setId(const char* id) { strcpy(this->id, id); }
void setISBN(const char* ISBN) { strcpy(this->ISBN, ISBN); }
void setName(const char* name) { strcpy(this->name, name); }
void setAuthor(const char* author) { strcpy(this->author, author); }
void setAddress(const char* address) { strcpy(this->address, address); }
void setDate(const char* date) { strcpy(this->date, date); }
void setPrice(float price) { this->price = price; }
void setState(int state) { this->state = state; }
void setNum(int num) { this->num = num; }
void show()
{
cout << "编号:" << id;
cout << " ISBN:" << ISBN;
cout << " 图书名称:" <<name;
cout << " 作者:" << author;
cout << " 出版社:" << address;
cout << " 出版日期:" << date;
cout << " 价格:" << price;
cout << " 是否可借(1.是 0.否):" << state;
cout << " 借阅次数:" << num << endl;
}
};
//用户父类信息
class User
{
public:
char username[20];//登录名
char password[20];//密码
char no[20];//学号/职工号
char name[20];//姓名
char sex[20];//性别
int age;//年龄
char school[20];//所属学院
};
//学生信息
class Student :public User
{
private:
char classd[20];//专业班级
char date[20];//入学时间
char phone[20];//联系电话
int number;//借阅次数
int state;//状态(1.挂失注销 0.正常)
public:
Student(){}
// 全参构造函数
Student(const char* username, const char* password, const char* no, const char* name,
const char* sex, int age, const char* school, const char* classd,
const char* date, const char* phone, int number, int state)
{
strcpy(this->username, username);
strcpy(this->password, password);
strcpy(this->no, no);
strcpy(this->name, name);
strcpy(this->sex, sex);
this->age = age;
strcpy(this->school, school);
strcpy(this->classd, classd);
strcpy(this->date, date);
strcpy(this->phone, phone);
this->number = number;
this->state = state;
}
// Get方法
const char* getUsername() const { return username; }
const char* getPassword() const { return password; }
const char* getNo() const { return no; }
const char* getName() const { return name; }
const char* getSex() const { return sex; }
int getAge() const { return age; }
const char* getSchool() const { return school; }
const char* getClassd() const { return classd; }
const char* getDate() const { return date; }
const char* getPhone() const { return phone; }
int getNumber() const { return number; }
int getState() const { return state; }
// Set方法
void setUsername(const char* username) { strncpy(this->username, username, sizeof(this->username) - 1); this->username[sizeof(this->username) - 1] = '\0'; }
void setPassword(const char* password) { strncpy(this->password, password, sizeof(this->password) - 1); this->password[sizeof(this->password) - 1] = '\0'; }
void setNo(const char* no) { strncpy(this->no, no, sizeof(this->no) - 1); this->no[sizeof(this->no) - 1] = '\0'; }
void setName(const char* name) { strncpy(this->name, name, sizeof(this->name) - 1); this->name[sizeof(this->name) - 1] = '\0'; }
void setSex(const char* sex) { strncpy(this->sex, sex, sizeof(this->sex) - 1); this->sex[sizeof(this->sex) - 1] = '\0'; }
void setAge(int age) { this->age = age; }
void setSchool(const char* school) {strncpy(this->school, school, sizeof(this->school) - 1); this->school[sizeof(this->school) - 1] = '\0';}
void setClassd(const char* classd) { strncpy(this->classd, classd, sizeof(this->classd) - 1); this->classd[sizeof(this->classd) - 1] = '\0'; }
void setDate(const char* date) { strncpy(this->date, date, sizeof(this->date) - 1); this->date[sizeof(this->date) - 1] = '\0'; }
void setPhone(const char* phone) { strncpy(this->phone, phone, sizeof(this->phone) - 1); this->phone[sizeof(this->phone) - 1] = '\0'; }
void setNumber(int number) { this->number = number; }
void setState(int state) { this->state = state; }
void show()
{
cout << "登录名:" << username;
cout << " 密码:" << password;
cout << " 学号:" << no;
cout << " 姓名:" << name;
cout << " 性别:" << sex;
cout << " 年龄:" << age;
cout << " 所属学院:" << school;
cout << " 专业班级:" << classd;
cout << " 入学时间:" << date;
cout << " 联系电话:" << phone;
cout << " 借阅次数:" << number;
cout << " 状态(1.挂失注销 0.正常):" << state << endl;
}
};
//教师信息
class Teacher :public User
{
private:
char date[20];//入职时间
int number;//借阅次数
int state;//状态(1.挂失注销 0.正常)
public:
Teacher() {}
// 全参构造函数
Teacher(const char* username, const char* password, const char* no, const char* name,
const char* sex, int age, const char* school,
const char* date, int number, int state)
{
strcpy(this->username, username);
strcpy(this->password, password);
strcpy(this->no, no);
strcpy(this->name, name);
strcpy(this->sex, sex);
this->age = age;
strcpy(this->school, school);
strcpy(this->date, date);
this->number = number;
this->state = state;
}
// Get方法
const char* getUsername() const { return username; }
const char* getPassword() const { return password; }
const char* getNo() const { return no; }
const char* getName() const { return name; }
const char* getSex() const { return sex; }
int getAge() const { return age; }
const char* getSchool() const { return school; }
const char* getDate() const { return date; }
int getNumber() const { return number; }
int getState() const { return state; }
// Set方法
void setUsername(const char* username) { strncpy(this->username, username, sizeof(this->username) - 1); this->username[sizeof(this->username) - 1] = '\0'; }
void setPassword(const char* password) { strncpy(this->password, password, sizeof(this->password) - 1); this->password[sizeof(this->password) - 1] = '\0'; }
void setNo(const char* no) { strncpy(this->no, no, sizeof(this->no) - 1); this->no[sizeof(this->no) - 1] = '\0'; }
void setName(const char* name) { strncpy(this->name, name, sizeof(this->name) - 1); this->name[sizeof(this->name) - 1] = '\0'; }
void setSex(const char* sex) { strncpy(this->sex, sex, sizeof(this->sex) - 1); this->sex[sizeof(this->sex) - 1] = '\0'; }
void setAge(int age) { this->age = age; }
void setSchool(const char* school) { strncpy(this->school, school, sizeof(this->school) - 1); this->school[sizeof(this->school) - 1] = '\0'; }
void setDate(const char* date) { strncpy(this->date, date, sizeof(this->date) - 1); this->date[sizeof(this->date) - 1] = '\0'; }
void setNumber(int number) { this->number = number; }
void setState(int state) { this->state = state; }
void show()
{
cout << "登录名:" << username;
cout << " 密码:" << password;
cout << " 工号:" << no;
cout << " 姓名:" << name;
cout << " 性别:" << sex;
cout << " 年龄:" << age;
cout << " 所属学院:" << school;
cout << " 入职时间:" << date;
cout << " 借阅次数:" << number;
cout << " 状态(1.挂失注销 0.正常):" << state << endl;
}
};
class DateTime
{
public:
int year;
int month;
int day;
int hour;
int minute;
int seconds;
};
// 计算加上指定天数后的日期
DateTime addDays(DateTime date, int days)
{
// 首先将指定天数转换为秒数
int secondsToAdd = days * 24 * 60 * 60;
// 将日期转换为秒数
int totalSeconds = date.seconds + date.minute * 60 + date.hour * 60 * 60 + date.day * 24 * 60 * 60;
// 加上指定秒数
totalSeconds += secondsToAdd;
// 将秒数转换回日期
date.seconds = totalSeconds % 60;
totalSeconds /= 60;
date.minute = totalSeconds % 60;
totalSeconds /= 60;
date.hour = totalSeconds % 24;
totalSeconds /= 24;
date.day = totalSeconds;
return date;
}
// 计算当前日期和指定日期相差几天
int daysDifference(DateTime date1, DateTime date2)
{
// 将两个日期都转换为秒数
int totalSeconds1 = date1.seconds + date1.minute * 60 + date1.hour * 60 * 60 + date1.day * 24 * 60 * 60;
int totalSeconds2 = date2.seconds + date2.minute * 60 + date2.hour * 60 * 60 + date2.day * 24 * 60 * 60;
// 计算相差秒数
int differenceSeconds = totalSeconds2 - totalSeconds1;
// 计算相差天数,不足一天按一天计算
int differenceDays = differenceSeconds / (24 * 60 * 60);
return differenceDays;
}
// 比较两个日期的大小
int compareDates(DateTime date1, DateTime date2)
{
// 比较年份
if (date1.year > date2.year)
{
return 1;
}
else if (date1.year < date2.year)
{
return -1;
}
// 年份相同,比较月份
if (date1.month > date2.month)
{
return 1;
}
else if (date1.month < date2.month)
{
return -1;
}
// 年份和月份相同,比较日期
if (date1.day > date2.day)
{
return 1;
}
else if (date1.day < date2.day)
{
return -1;
}
// 日期相同,返回0
return 0;
}
//借阅信息
class Record
{
public:
char id[20];//借阅人编号
char name[20];//姓名
char ISBN[20];//借阅图书ISBN
char book_name[20];//书名
float price;//金额
DateTime beginTime;//借阅时间
DateTime endTime;//归还时间
DateTime nowTime;//实际归还时间
void show()
{
cout << "借阅人编号:" << id;
cout << " 姓名:" << name;
cout << " 借阅图书ISBN:" << ISBN;
cout << " 书名:" << name;
cout << " 金额:" << price;
cout << " 借阅时间:" << beginTime.year << "-"
<<beginTime.month << "-"
<< beginTime.day << " "
<< beginTime.hour << ":"
<< beginTime.minute << ":"
<< beginTime.seconds;
cout << " 借阅时间:" << endTime.year
<< "-" << endTime.month
<< "-" << endTime.day
<< " " << endTime.hour
<< ":" << endTime.minute
<< ":" << endTime.seconds;
cout << " 实际归还时间(-1表示未归还):" << nowTime.year
<< "-" << nowTime.month
<< "-" << nowTime.day
<< " " << nowTime.hour
<< ":" << nowTime.minute
<< ":" << nowTime.seconds << endl;
}
};
int max_number_teacher = 10;
int max_number = 5;//最多借阅本数
int max_day_teacher = 10;
int max_day = 5;//最多借阅天数
float money = 5;//逾期每天金额
2.系统相关方法定义
class Library
{
private:
vector<Book> book;
vector<Student> student;
vector<Teacher> teacher;
vector<Record> record;
public:
//信息保存文件
void save_book();
//读取文件信息
void read_book();
//增加图书信息
void add_book();
//输出全部图书信息
void show_book();
//查找图书信息
void search_book();
//修改图书信息
void update_book();
//删除图书信息
void delete_book();
//信息保存文件
void save_student();
//读取文件信息
void read_student();
//增加学生信息
void add_student();
//输出全部学生信息
void show_student();
//查找学生信息
void search_student();
//修改学生信息
void update_student();
//删除学生信息
void delete_student();
//信息保存文件
void save_teacher();
//读取文件信息
void read_teacher();
//增加教师信息
void add_teacher();
//输出全部教师信息
void show_teacher();
//查找教师信息
void search_teacher();
//修改教师信息
void update_teacher();
//删除教师信息
void delete_teacher();
//管理员登录
int admin_login();
//学生登录借阅
int student_index();
//教师登录借阅
int teacher_index();
//保存借阅信息
void save_Record();
//读取借阅信息
void read_Record();
//借书 1.学生 2.教师
void borrow_book(int index,int state);
//还书
void return_book(int index, int state);
//查看借阅信息
void show_borrow_record();
//计算逾期信息
void show_not_return_record();
//排序前十
void sort();
//挂失
void loss(int index, int state);
};
3.系统方法实现
//信息保存文件
void Library::save_book()
{
ofstream outFile;
outFile.open("book.txt");
for (int i = 0; i < book.size(); i++)
{
outFile << book[i].getId() << " "
<< book[i].getISBN() << " "
<< book[i].getName() << " "
<< book[i].getAuthor() << " "
<< book[i].getAddress() << " "
<< book[i].getDate() << " "
<< book[i].getPrice() << " "
<< book[i].getState() << " "
<< book[i].getNum() << endl;
}
outFile.close();
cout << "文件保存成功!" << endl;
}
//读取文件信息
void Library::read_book()
{
FILE* fp;
fp = fopen("book.txt ", "r");
if (fp == NULL)
{
cout << "打开文件失败!" << endl;
return;
}
char id[20];//编号
char ISBN[20];//ISBN
char name[20];//图书名称
char author[20];//作者
char address[20];//出版社
char date[20];//出版日期
float price;//金额
int state;//是否可借(1.是 0.否)
int num;//借阅次数
while (!feof(fp))
{
fscanf(fp, "%s %s %s %s %s %s %f %d %d\n",
id,
ISBN,
name,
author,
address,
date,
&price,
&state,
&num);
Book temp(id, ISBN, name, author, address, date, price, state, num);
book.push_back(temp);
}
fclose(fp);
cout << "读取文件信息成功!" << endl;
}
//增加图书信息
void Library::add_book()
{
cout << "==========增加图书信息=========" << endl;
char id[20];//编号
char ISBN[20];//ISBN
char name[20];//图书名称
char author[20];//作者
char address[20];//出版社
char date[20];//出版日期
float price;//金额
int state=1;//是否可借(1.是 0.否)
int num=0;//借阅次数
cout << "请输入图书编号:" << endl;
cin >> id;
for (int i = 0; i < book.size(); i++)
{
if (!strcmp(id, book[i].getId()))
{
cout << "存在该信息,添加失败!" << endl;
return;
}
}
cout << "请输入图书ISBN:" << endl;
cin >> ISBN;
for (int i = 0; i < book.size(); i++)
{
if (!strcmp(ISBN, book[i].getISBN()))
{
cout << "存在该信息,添加失败!" << endl;
return;
}
}
cout << "请输入图书名称:" << endl;
cin >> name;
cout << "请输入作者:" << endl;
cin >> author;
cout << "请输入出版社:" << endl;
cin >> address;
cout << "请输入出版日期:" << endl;
cin >> date;
cout << "请输入金额:" << endl;
cin >> price;
Book temp(id, ISBN, name, author, address, date, price, state, num);
book.push_back(temp);
cout << "=======添加图书信息成功======" << endl;
}
//输出全部图书信息
void Library::show_book()
{
cout << "==========显示全部图书信息=========" << endl;
if (book.size() == 0)
{
cout << "没有图书信息!" << endl;
return;
}
for (int i = 0; i < book.size(); i++)
{
book[i].show();
}
cout << "=======显示全部图书信息结束======" << endl;
}
//查找图书信息
void Library::search_book()
{
cout << "1.按照书名查询" << endl;
cout << "2.按照ISBN查询" << endl;
cout << "3.按照出版社查询" << endl;
cout << "请输入你的选择:" << endl;
int ch;
cin >> ch;
if (ch == 1)
{
cout << "请输入你要查找的图书名称:" << endl;
char name[20];
int a = 0;
cin >> name;
for (int i = 0; i < book.size(); i++)
{
if (!strcmp(name, book[i].getName()))
{
a++;
book[i].show();
}
}
if (a == 0)
{
cout << "没有该图书信息!" << endl;
}
}
else if (ch == 2)
{
cout << "请输入你要查找的ISBN:" << endl;
char ISBN[20];
int a = 0;
cin >> ISBN;
for (int i = 0; i < book.size(); i++)
{
if (!strcmp(ISBN, book[i].getISBN()))
{
a++;
book[i].show();
}
}
if (a == 0)
{
cout << "没有该图书信息!" << endl;
}
}
else if (ch == 3)
{
cout << "请输入你要查找的出版社:" << endl;
char address[20];
int a = 0;
scanf("%s", address);
for (int i = 0; i < book.size(); i++)
{
if (!strcmp(address, book[i].getAddress()))
{
a++;
book[i].show();
}
}
if (a == 0)
{
cout << "没有该图书信息!" << endl;
}
}
else
{
cout << "输入有误!" << endl;
return;
}
}
//修改图书信息
void Library::update_book()
{
char id[20];//编号
char ISBN[20];//ISBN
char name[20];//图书名称
char author[20];//作者
char address[20];//出版社
char date[20];//出版日期
float price;//金额
cout << "==========修改图书信息=========" << endl;
cout << "请输入你要修改的图书ISBN:" << endl;
int a = 0;
cin >> ISBN;
for (int i = 0; i < book.size(); i++)
{
if (!strcmp(ISBN, book[i].getId()))
{
cout << "请输入图书名称:" << endl;
cin >> name;
cout << "请输入作者:" << endl;
cin >> author;
cout << "请输入出版社:" << endl;
cin >> address;
cout << "请输入出版日期:" << endl;
cin >> date;
cout << "请输入金额:" << endl;
cin >> price;
book[i].setName(name);
book[i].setAuthor(author);
book[i].setAddress(address);
book[i].setPrice(price);
book[i].setDate(date);
cout << "修改成功!" << endl;
a++;
break;
}
}
if (a == 0)
{
cout << "没有该图书信息,修改失败!" << endl;
}
cout << "=======修改图书信息完毕======" << endl;
}
//删除图书信息
void Library::delete_book()
{
cout << "==========删除图书信息=========" << endl;
cout << "请输入你要删除的图书ISBN:" << endl;
char ISBN[20];
int a = 0;
cin >> ISBN;
for (int i = 0; i < book.size(); i++)
{
if (!strcmp(ISBN, book[i].getISBN()))
{
a++;
book.erase(book.begin() + i);
cout << "删除成功!" << endl;
break;
}
}
if (a == 0)
{
cout << "没有该图书信息,删除失败!" << endl;
return;
}
cout << "=======删除图书信息结束======" << endl;
return;
}
//信息保存文件
void Library::save_student()
{
ofstream outFile;
outFile.open("student.txt");
for (int i = 0; i < student.size(); i++)
{
outFile << student[i].getUsername() << " "
<< student[i].getPassword() << " "
<< student[i].getNo() << " "
<< student[i].getName() << " "
<< student[i].getSex() << " "
<< student[i].getAge() << " "
<< student[i].getSchool() << " "
<< student[i].getClassd() << " "
<< student[i].getDate() << " "
<< student[i].getPhone() << " "
<< student[i].getNumber() << " "
<< student[i].getState() << endl;
}
outFile.close();
cout << "文件保存成功!" << endl;
return;
}
//读取文件信息
void Library::read_student()
{
ifstream in("student.txt", ios::in);
if (!in.is_open())
{
cout << "打开文件失败" << endl;
return;
}
char username[20];//登录名
char password[20];//密码
char no[20];//学号/职工号
char name[20];//姓名
char sex[20];//性别
int age;//年龄
char school[20];//所属学院
char classd[20];//专业班级
char date[20];//入学时间
char phone[20];//联系电话
int number;//借阅次数
int state;//状态(1.挂失注销 0.正常)
while (!in.eof())
{
in >> username
>> password
>> no
>> name
>> sex
>> age
>> school
>> classd
>> date
>> phone
>> number
>> state;
Student temp(username, password, no, name, sex, age, school, classd, date, phone, number, state);
student.push_back(temp);
}
student.pop_back();
in.close();
cout << "读取文件信息成功!" << endl;
}
//增加学生信息
void Library::add_student()
{
char username[20];//登录名
char password[20];//密码
char no[20];//学号/职工号
char name[20];//姓名
char sex[20];//性别
int age;//年龄
char school[20];//所属学院
char classd[20];//专业班级
char date[20];//入学时间
char phone[20];//联系电话
int number=0;//借阅次数
int state=0;//状态(1.挂失注销 0.正常)
cout << "==========增加学生信息=========" << endl;
cout << "请输入学生登录名:" << endl;
cin >> username;
for (int i = 0; i < student.size(); i++)
{
if (!strcmp(username, student[i].getUsername()))
{
cout << "存在该信息,添加失败!" << endl;
return;
}
}
cout << "请输入学生登录密码:" << endl;
cin >> password;
cout << "请输入学生学号:" << endl;
cin >> no;
for (int i = 0; i < student.size(); i++)
{
if (!strcmp(no, student[i].no))
{
cout << "存在该信息,添加失败!" << endl;
return;
}
}
cout << "请输入学生姓名:" << endl;
cin >> name;
cout << "请输入学生性别:" << endl;
cin >> sex;
cout << "请输入学生年龄:" << endl;
cin >> age;
cout << "请输入学生所属学院:" << endl;
cin >> school;
cout << "请输入学生专业班级:" << endl;
cin >> classd;
cout << "请输入学生入学时间:" << endl;
cin >> date;
cout << "请输入学生联系电话:" << endl;
cin >> phone;
Student temp(username, password, no, name, sex, age, school, classd, date, phone, number, state);
student.push_back(temp);
cout << "=======添加学生信息成功======" << endl;
}
//输出全部学生信息
void Library::show_student()
{
cout << "==========显示全部学生信息=========" << endl;
if (student.size() == 0)
{
cout << "没有学生信息!" << endl;
return;
}
for (int i = 0; i < student.size(); i++)
{
student[i].show();
}
cout << "=======显示全部学生信息结束======" << endl;
}
//查找学生信息
void Library::search_student()
{
cout << "1.按照姓名查询" << endl;
cout << "2.按照学号查询" << endl;
cout << "请输入你的选择:" << endl;
int ch;
cin >> ch;
if (ch == 1)
{
cout << "请输入你要查找的学生姓名:" << endl;
char name[20];
int a = 0;
scanf("%s", name);
for (int i = 0; i < student.size(); i++)
{
if (!strcmp(name, student[i].name))
{
a++;
student[i].show();
}
}
if (a == 0)
{
cout << "没有该学生信息!" << endl;
}
}
else if (ch == 2)
{
cout << "请输入你要查找的学号:" << endl;
char no[20];
int a = 0;
cin >> no;
for (int i = 0; i < student.size(); i++)
{
if (!strcmp(no, student[i].no))
{
a++;
student[i].show();
}
}
if (a == 0)
{
cout << "没有该学生信息!" << endl;
}
}
else
{
cout << "输入有误!" << endl;
return;
}
}
//修改学生信息
void Library::update_student()
{
cout << "==========修改学生信息=========" << endl;
cout << "请输入你要修改的学号:" << endl;
char password[20];//密码
char no[20];//学号/职工号
char name[20];//姓名
char sex[20];//性别
int age;//年龄
char school[20];//所属学院
char classd[20];//专业班级
char date[20];//入学时间
char phone[20];//联系电话
int a = 0;
cin >> no;
for (int i = 0; i < student.size(); i++)
{
if (!strcmp(no, student[i].no))
{
cout << "请输入学生登录密码:" << endl;
cin >> password;
cout << "请输入学生姓名:" << endl;
cin >> name;
cout << "请输入学生性别:" << endl;
cin >> sex;
cout << "请输入学生年龄:" << endl;
cin >> age;
cout << "请输入学生所属学院:" << endl;
cin >> school;
cout << "请输入学生专业班级:" << endl;
cin >> classd;
cout << "请输入学生入学时间:" << endl;
cin >> date;
cout << "请输入学生联系电话:" << endl;
cin >> phone;
student[i].setPassword(password);
student[i].setName(name);
student[i].setSex(sex);
student[i].setAge(age);
student[i].setSchool(school);
student[i].setClassd(classd);
student[i].setDate(date);
student[i].setPhone(phone);
cout << "修改成功!" << endl;
a++;
break;
}
}
if (a == 0)
{
cout << "没有该学生信息,修改失败!" << endl;
}
cout << "=======修改学生信息完毕======" << endl;
}
//删除学生信息
void Library::delete_student()
{
cout << "==========删除学生信息=========" << endl;
cout << "请输入你要删除的学好:" << endl;
char no[20];
int a = 0;
cin >> no;
for (int i = 0; i < student.size(); i++)
{
if (!strcmp(no, student[i].no))
{
a++;
student.erase(student.begin() + i);
cout << "删除成功!" << endl;
break;
}
}
if (a == 0)
{
cout << "没有该学生信息,删除失败!" << endl;
return;
}
cout << "=======删除学生信息结束======" << endl;
return;
}
//信息保存文件
void Library::save_teacher()
{
ofstream outFile;
outFile.open("teacher.txt");
for (int i = 0; i < teacher.size(); i++)
{
outFile << teacher[i].getUsername() << " "
<< teacher[i].getPassword() << " "
<< teacher[i].getNo() << " "
<< teacher[i].getName() << " "
<< teacher[i].getSex() << " "
<< teacher[i].getAge() << " "
<< teacher[i].getSchool() << " "
<< teacher[i].getDate() << " "
<< teacher[i].getNumber() << " "
<< teacher[i].getState() << endl;
}
outFile.close();
cout << "文件保存成功!" << endl;
return;
}
//读取文件信息
void Library::read_teacher()
{
ifstream in("teacher.txt", ios::in);
if (!in.is_open())
{
cout << "打开文件失败" << endl;
return;
}
char username[20];//登录名
char password[20];//密码
char no[20];//工号/职工号
char name[20];//姓名
char sex[20];//性别
int age;//年龄
char school[20];//所属学院
char date[20];//入职时间
int number;//借阅次数
int state;//状态(1.挂失注销 0.正常)
while (!in.eof())
{
in >> username
>> password
>> no
>> name
>> sex
>> age
>> school
>> date
>> number
>> state;
Teacher temp(username, password, no, name, sex, age, school, date, number, state);
teacher.push_back(temp);
}
teacher.pop_back();
in.close();
cout << "读取文件信息成功!" << endl;
}
//增加教师信息
void Library::add_teacher()
{
char username[20];//登录名
char password[20];//密码
char no[20];//工号/职工号
char name[20];//姓名
char sex[20];//性别
int age;//年龄
char school[20];//所属学院
char date[20];//入职时间
int number=0;//借阅次数
int state=0;//状态(1.挂失注销 0.正常)
cout << "==========增加教师信息=========" << endl;
cout << "请输入教师登录名:" << endl;
cin >> username;
for (int i = 0; i < teacher.size(); i++)
{
if (!strcmp(username, teacher[i].getUsername()))
{
cout << "存在该信息,添加失败!" << endl;
return;
}
}
cout << "请输入教师登录密码:" << endl;
cin >> password;
cout << "请输入教师工号:" << endl;
cin >> no;
for (int i = 0; i < teacher.size(); i++)
{
if (!strcmp(no, teacher[i].no))
{
cout << "存在该信息,添加失败!" << endl;
return;
}
}
cout << "请输入教师姓名:" << endl;
cin >> name;
cout << "请输入教师性别:" << endl;
cin >> sex;
cout << "请输入教师年龄:" << endl;
cin >> age;
cout << "请输入教师所属学院:" << endl;
cin >> school;
cout << "请输入教师入职时间:" << endl;
cin >> date;
Teacher temp(username, password, no, name, sex, age, school, date, number, state);
teacher.push_back(temp);
cout << "=======添加教师信息成功======" << endl;
}
//输出全部教师信息
void Library::show_teacher()
{
cout << "==========显示全部教师信息=========" << endl;
if (teacher.size() == 0)
{
cout << "没有教师信息!" << endl;
return;
}
for (int i = 0; i < teacher.size(); i++)
{
teacher[i].show();
}
cout << "=======显示全部教师信息结束======" << endl;
}
//查找教师信息
void Library::search_teacher()
{
cout << "1.按照姓名查询" << endl;
cout << "2.按照工号查询" << endl;
cout << "请输入你的选择:" << endl;
int ch;
cin >> ch;
if (ch == 1)
{
cout << "请输入你要查找的教师姓名:" << endl;
char name[20];
int a = 0;
scanf("%s", name);
for (int i = 0; i < teacher.size(); i++)
{
if (!strcmp(name, teacher[i].name))
{
a++;
teacher[i].show();
}
}
if (a == 0)
{
cout << "没有该教师信息!" << endl;
}
}
else if (ch == 2)
{
cout << "请输入你要查找的工号:" << endl;
char no[20];
int a = 0;
cin >> no;
for (int i = 0; i < teacher.size(); i++)
{
if (!strcmp(no, teacher[i].no))
{
a++;
teacher[i].show();
}
}
if (a == 0)
{
cout << "没有该教师信息!" << endl;
}
}
else
{
cout << "输入有误!" << endl;
return;
}
}
//修改教师信息
void Library::update_teacher()
{
cout << "==========修改教师信息=========" << endl;
cout << "请输入你要修改的工号:" << endl;
char password[20];//密码
char no[20];//工号/职工号
char name[20];//姓名
char sex[20];//性别
int age;//年龄
char school[20];//所属学院
char date[20];//入职时间
int a = 0;
cin >> no;
for (int i = 0; i < teacher.size(); i++)
{
if (!strcmp(no, teacher[i].no))
{
cout << "请输入教师登录密码:" << endl;
cin >> password;
cout << "请输入教师姓名:" << endl;
cin >> name;
cout << "请输入教师性别:" << endl;
cin >> sex;
cout << "请输入教师年龄:" << endl;
cin >> age;
cout << "请输入教师所属学院:" << endl;
cin >> school;
cout << "请输入教师入职时间:" << endl;
cin >> date;
teacher[i].setPassword(password);
teacher[i].setName(name);
teacher[i].setSex(sex);
teacher[i].setAge(age);
teacher[i].setSchool(school);
teacher[i].setDate(date);
cout << "修改成功!" << endl;
a++;
break;
}
}
if (a == 0)
{
cout << "没有该教师信息,修改失败!" << endl;
}
cout << "=======修改教师信息完毕======" << endl;
}
//删除教师信息
void Library::delete_teacher()
{
cout << "==========删除教师信息=========" << endl;
cout << "请输入你要删除的工号:" << endl;
char no[20];
int a = 0;
cin >> no;
for (int i = 0; i < teacher.size(); i++)
{
if (!strcmp(no, teacher[i].no))
{
a++;
teacher.erase(teacher.begin() + i);
cout << "删除成功!" << endl;
break;
}
}
if (a == 0)
{
cout << "没有该教师信息,删除失败!" << endl;
return;
}
cout << "=======删除教师信息结束======" << endl;
return;
}
//管理员登录
int Library::admin_login()
{
char username[20];
char password[20];
cout << "请输入登录的管理员用户名:" << endl;
cin >> username;
cout << "请输入登录的管理员密码:" << endl;
cin >> password;
if (!strcmp("admin", username) && !strcmp("123", password))
{
cout << "登录成功!" << endl;
return 1;
}
else
{
cout << "用户名或密码错误,登录失败!" << endl;
return -1;
}
}
//学生登录借阅
int Library::student_index()
{
char username[20];
char pwd[20];
cout << "请输入登录的用户名:" << endl;
cin >> username;
cout << "请输入登录的密码:" << endl;
cin >> pwd;
for (int i = 0; i < student.size(); i++)
{
if (!strcmp(username, student[i].getUsername())
&& !strcmp(pwd, student[i].getPassword()))
{
if (student[i].getState() == 1)
{
cout << "已经注销挂失,登录失败!" << endl;
return -1;
}
cout << "登录成功!" << endl;
return i;
}
}
cout << "登录失败!" << endl;
return -1;
}
//教师登录借阅
int Library::teacher_index()
{
char username[20];
char pwd[20];
cout << "请输入登录的用户名:" << endl;
cin >> username;
cout << "请输入登录的密码:" << endl;
cin >> pwd;
for (int i = 0; i < teacher.size(); i++)
{
if (!strcmp(username, teacher[i].getUsername())
&& !strcmp(pwd, teacher[i].getPassword()))
{
if (teacher[i].getState() == 1)
{
cout << "已经注销挂失,登录失败!" << endl;
return -1;
}
cout << "登录成功!" << endl;
return i;
}
}
cout << "登录失败!" << endl;
return -1;
}
//保存借阅信息
void Library::save_Record()
{
ofstream outFile;
outFile.open("record.txt");
int i = 0, j = 0;
for (i = 0; i < record.size(); i++)
{
outFile << record[i].id << " "
<< record[i].name << " "
<< record[i].ISBN << " "
<< record[i].book_name << " "
<< record[i].price << " "
<< record[i].beginTime.year << " "
<< record[i].beginTime.month << " "
<< record[i].beginTime.day << " "
<< record[i].beginTime.hour << " "
<< record[i].beginTime.minute << " "
<< record[i].beginTime.seconds << " "
<< record[i].endTime.year << " "
<< record[i].endTime.month << " "
<< record[i].endTime.day << " "
<< record[i].endTime.hour << " "
<< record[i].endTime.minute << " "
<< record[i].endTime.seconds << " "
<< record[i].nowTime.year << " "
<< record[i].nowTime.month << " "
<< record[i].nowTime.day << " "
<< record[i].nowTime.hour << " "
<< record[i].nowTime.minute << " "
<< record[i].nowTime.seconds << endl;
}
outFile.close();
cout << "文件保存成功!" << endl;
return;
}
//读取借阅信息
void Library::read_Record()
{
ifstream in("record.txt", ios::in);
if (!in.is_open())
{
cout << "打开文件失败" << endl;
return;
}
while (!in.eof())
{
Record temp;
in >> temp.id
>> temp.name
>> temp.ISBN
>> temp.book_name
>> temp.price
>> temp.beginTime.year
>> temp.beginTime.month
>> temp.beginTime.day
>> temp.beginTime.hour
>> temp.beginTime.minute
>> temp.beginTime.seconds
>> temp.endTime.year
>> temp.endTime.month
>> temp.endTime.day
>> temp.endTime.hour
>> temp.endTime.minute
>> temp.endTime.seconds
>> temp.nowTime.year
>> temp.nowTime.month
>> temp.nowTime.day
>> temp.nowTime.hour
>> temp.nowTime.minute
>> temp.nowTime.seconds;
record.push_back(temp);
}
record.pop_back();
in.close();
cout << "文件导入成功!" << endl;
return;
}
//借书
void Library::borrow_book(int index, int state)
{
//学生
if (state == 1)
{
if (student[index].getNumber() >= max_number)
{
cout << "达到最大借阅数量,借阅失败!" << endl;
return;
}
Record temp_record;
strcpy(temp_record.id, student[index].getNo());
strcpy(temp_record.name, student[index].getName());
cout << "请输入借阅的图书ISBN:" << endl;
cin>>temp_record.ISBN;
int t_temp = 0;
for (int i = 0; i < book.size(); i++)
{
if (!strcmp(temp_record.ISBN, book[i].getISBN()))
{
t_temp++;
book[i].show();
if (book[i].getState() == 0)
{
cout << "该图书不可借阅,借阅失败!" << endl;
return;
}
strcpy(temp_record.book_name, book[i].getName());
temp_record.price = book[i].getPrice();
time_t nowtime;
struct tm* p;
time(&nowtime);
p = localtime(&nowtime);
temp_record.beginTime.year = p->tm_year + 1900;
temp_record.beginTime.month = p->tm_mon + 1;
temp_record.beginTime.day = p->tm_mday;
temp_record.beginTime.hour = p->tm_hour;
temp_record.beginTime.minute = p->tm_min;
temp_record.beginTime.seconds = p->tm_sec;
int day;
cout << "请输入你借阅的天数,且只能为整数:" << endl;
cin >> day;
if (day > max_day || day < 1)
{
cout << "借阅时间在1-" << max_day << "天内!\n";
return;
}
temp_record.endTime = addDays(temp_record.beginTime, day);
temp_record.nowTime.year = -1;
temp_record.nowTime.month = -1;
temp_record.nowTime.day = -1;
temp_record.nowTime.hour = -1;
temp_record.nowTime.minute = -1;
temp_record.nowTime.seconds = -1;
book[i].setState(0);
book[i].setNum(book[i].getNum() + 1);
student[index].setNumber(student[index].getNumber() + 1);
record.push_back(temp_record);
cout << "借阅成功!" << endl;
break;
}
}
if (t_temp == 0)
{
cout << "没有该图书,借阅失败!" << endl;
return;
}
}
//教师
else if(state == 2)
{
if (teacher[index].getNumber() >= max_number)
{
cout << "达到最大借阅数量,借阅失败!" << endl;
return;
}
Record temp_record;
strcpy(temp_record.id, teacher[index].getNo());
strcpy(temp_record.name, teacher[index].getName());
cout << "请输入借阅的图书ISBN:" << endl;
cin >> temp_record.ISBN;
int t_temp = 0;
for (int i = 0; i < book.size(); i++)
{
if (!strcmp(temp_record.ISBN, book[i].getISBN()))
{
t_temp++;
book[i].show();
if (book[i].getState() == 0)
{
cout << "该图书不可借阅,借阅失败!" << endl;
return;
}
strcpy(temp_record.book_name, book[i].getName());
temp_record.price = book[i].getPrice();
time_t nowtime;
struct tm* p;
time(&nowtime);
p = localtime(&nowtime);
temp_record.beginTime.year = p->tm_year + 1900;
temp_record.beginTime.month = p->tm_mon + 1;
temp_record.beginTime.day = p->tm_mday;
temp_record.beginTime.hour = p->tm_hour;
temp_record.beginTime.minute = p->tm_min;
temp_record.beginTime.seconds = p->tm_sec;
int day;
cout << "请输入你借阅的天数,且只能为整数:" << endl;
cin >> day;
if (day > max_day || day < 1)
{
cout << "借阅时间在1-" << max_day << "天内!\n";
return;
}
temp_record.endTime = addDays(temp_record.beginTime, day);
temp_record.nowTime.year = -1;
temp_record.nowTime.month = -1;
temp_record.nowTime.day = -1;
temp_record.nowTime.hour = -1;
temp_record.nowTime.minute = -1;
temp_record.nowTime.seconds = -1;
book[i].setState(0);
book[i].setNum(book[i].getNum() + 1);
teacher[index].setNumber(teacher[index].getNumber() + 1);
record.push_back(temp_record);
cout << "借阅成功!" << endl;
break;
}
}
if (t_temp == 0)
{
cout << "没有该图书,借阅失败!" << endl;
return;
}
}
}
//还书
void Library::return_book(int index, int state)
{
if (state == 1)
{
char ISBN[20];
cout << "请输入归还的图书ISBN:" << endl;
cin >> ISBN;
int t_temp = 0;
for (int i = 0; i < record.size(); i++)
{
if (!strcmp(student[index].getNo(), record[i].id)
&& !strcmp(ISBN, record[i].ISBN)
&& record[i].nowTime.year == -1)
{
t_temp++;
time_t ISBNwtime;
struct tm* p;
time(&ISBNwtime);
p = localtime(&ISBNwtime);
record[i].nowTime.year = p->tm_year + 1900;
record[i].nowTime.month = p->tm_mon + 1;
record[i].nowTime.day = p->tm_mday;
record[i].nowTime.hour = p->tm_hour;
record[i].nowTime.minute = p->tm_min;
record[i].nowTime.seconds = p->tm_sec;
if (compareDates(record[i].nowTime, record[i].endTime) > 0)
{
cout << "逾期归还!" << endl;
}
student[index].setNumber(student[index].getNumber() - 1);
for (int j = 0; j < book.size(); j++)
{
if (!strcmp(ISBN, book[i].getISBN()))
{
book[i].setState(1);
break;
}
}
cout << "归还成功!" << endl;
break;
}
}
if (t_temp == 0)
{
cout << "没有借阅该图书,归还失败!" << endl;
return;
}
}
else if (state == 2)
{
char ISBN[20];
cout << "请输入归还的图书ISBN:" << endl;
cin >> ISBN;
int t_temp = 0;
for (int i = 0; i < record.size(); i++)
{
if (!strcmp(teacher[index].getNo(), record[i].id)
&& !strcmp(ISBN, record[i].ISBN)
&& record[i].nowTime.year == -1)
{
t_temp++;
time_t ISBNwtime;
struct tm* p;
time(&ISBNwtime);
p = localtime(&ISBNwtime);
record[i].nowTime.year = p->tm_year + 1900;
record[i].nowTime.month = p->tm_mon + 1;
record[i].nowTime.day = p->tm_mday;
record[i].nowTime.hour = p->tm_hour;
record[i].nowTime.minute = p->tm_min;
record[i].nowTime.seconds = p->tm_sec;
if (compareDates(record[i].nowTime, record[i].endTime) > 0)
{
cout << "逾期归还!" << endl;
}
teacher[index].setNumber(teacher[index].getNumber() - 1);
for (int j = 0; j < book.size(); j++)
{
if (!strcmp(ISBN, book[i].getISBN()))
{
book[i].setState(1);
break;
}
}
cout << "归还成功!" << endl;
break;
}
}
if (t_temp == 0)
{
cout << "没有借阅该图书,归还失败!" << endl;
return;
}
}
}
//查看借阅信息
void Library::show_borrow_record()
{
if (record.size() <= 0)
{
cout << "没有借阅信息!" << endl;
return;
}
for (int i = 0; i < record.size(); i++)
{
cout << "借书证号:" << record[i].id;
cout << " 学生姓名:" << record[i].name;
cout << " 借阅图书ISBN:" << record[i].ISBN;
cout << " 书名:" << record[i].name;
cout << " 金额:" << record[i].price;
cout << " 借阅时间:" << record[i].beginTime.year << "-"
<< record[i].beginTime.month << "-"
<< record[i].beginTime.day << " "
<< record[i].beginTime.hour << ":"
<< record[i].beginTime.minute << ":"
<< record[i].beginTime.seconds;
cout << " 借阅时间:" << record[i].endTime.year
<< "-" << record[i].endTime.month
<< "-" << record[i].endTime.day
<< " " << record[i].endTime.hour
<< ":" << record[i].endTime.minute
<< ":" << record[i].endTime.seconds;
cout << " 实际归还时间(-1表示未归还):" << record[i].nowTime.year
<< "-" << record[i].nowTime.month
<< "-" << record[i].nowTime.day
<< " " << record[i].nowTime.hour
<< ":" << record[i].nowTime.minute
<< ":" << record[i].nowTime.seconds << endl;
}
}
//计算逾期信息
void Library::show_not_return_record()
{
if (record.size() <= 0)
{
cout << "没有借阅信息!" << endl;
return;
}
time_t nowtime;
struct tm* p;
time(&nowtime);
p = localtime(&nowtime);
DateTime date;
date.year = p->tm_year + 1900;
date.month = p->tm_mon + 1;
date.day = p->tm_mday;
date.hour = p->tm_hour;
date.minute = p->tm_min;
date.seconds = p->tm_sec;
for (int i = 0; i < record.size(); i++)
{
cout << "借书证号:" << record[i].id;
cout << " 学生姓名:" << record[i].name;
cout << " 借阅图书ISBN:" << record[i].ISBN;
cout << " 书名:" << record[i].name;
cout << " 金额:" << record[i].price;
cout << " 借阅时间:" << record[i].beginTime.year << "-" << record[i].beginTime.month << "-" << record[i].beginTime.day << " " << record[i].beginTime.hour << ":" << record[i].beginTime.minute << ":" << record[i].beginTime.seconds;
cout << " 借阅时间:" << record[i].endTime.year << "-" << record[i].endTime.month << "-" << record[i].endTime.day << " " << record[i].endTime.hour << ":" << record[i].endTime.minute << ":" << record[i].endTime.seconds;
cout << " 实际归还时间(-1表示未归还):" << record[i].nowTime.year << "-" << record[i].nowTime.month << "-" << record[i].nowTime.day << " " << record[i].nowTime.hour << ":" << record[i].nowTime.minute << ":" << record[i].nowTime.seconds << endl;
if (record[i].nowTime.year == -1)
{
int day = daysDifference(record[i].endTime, date);
if (day > 0)
{
cout << "逾期" << day << "天,罚款:" << day * money << "元\n";
}
else
{
cout << "未逾期" << endl;
}
}
else
{
int day = daysDifference(record[i].endTime, record[i].nowTime);
if (day > 0)
{
cout << "逾期" << day << "天,罚款:" << day * money << "元\n";
}
else
{
cout << "未逾期" << endl;
}
}
}
}
//排序前十
void Library::sort()
{
if (student.size() == 0)
{
cout << "没有读者信息!" << endl;
return;
}
for (int i = 0; i < student.size(); i++)
{
for (int j = 0; j < student.size() - 1 - i; j++)
{
if (student[j].getNumber() < student[j + 1].getNumber())
{
Student temp = student[j];
student[j] = student[j + 1];
student[j + 1] = temp;
}
}
}
cout << "2.前十借阅读者信息" << endl;
if (student.size() >= 10)
{
for (int i = 0; i < 10; i++)
{
student[i].show();
}
}
else
{
for (int i = 0; i < student.size(); i++)
{
student[i].show();
}
}
}
//挂失
void Library::loss(int index, int state)
{
if (state == 1)
{
student[index].setState(1);
cout << "挂失成功!" << endl;
}
else if (state == 2)
{
teacher[index].setState(1);
cout << "挂失成功!" << endl;
}
return;
}