分析系统图书管理系统是对学生、教师信息和图书信息进行管理,学生基本信息包括学号,姓名,院系,最大借阅图书数量属性,教师基本信息包括工号,姓名,院系,最大借阅图书数量属性,图书基本信息中包括图书号,书名,作者,出版社,图书数量属性,要求输入、输出、修改、查询以上信息功能, 设计并实现功能。考虑借阅人、图书是否可以采用继承派生关系,使用多态等机制实现不同类型学生借阅不同类型的图书或资源。以下是一些基本功能,根据情况可以进行添加修改
- 添加借阅人信息;
- 修改借阅人信息;
- 处理借阅人借阅信息;
- 处理借阅人还书信息;
- 添加图书或资源信息;
- 修改图书或资源信息;
- 查看图书信息;
- 提供用户管理功能
#pragma once #include主函数 main.cpp#include #include #include #include #include #include using namespace std;
#include "PeopleManager.h"
int main()
{
PeopleManager PEO;
int choice = 0;//接收用户的选择
while (true)
{
PEO.Show_Menu();//展示交互菜单
cout << "情输入您的选择:" << endl;
cin >> choice;//接收用户的选择
switch (choice)
{
case 0:
PEO.ExitSystem();//退出系统
break;
case 1:
PEO.student_Operation();//学生操作
break;
case 2:
PEO.teacher_Operation();//教书操作
break;
case 3:
PEO.manager_Operation();//管理员操作
break;
default:
cout << "输入的数据有误,请重新输入!" << endl;
system("pause");
system("cls");
break;
}
}
}
用户类 定义 Account.h
#pragma once
#include "All.h"
class Account
{
private:
string password;//密码
int id;//判断身份,1:学生 2:老师 3:管理员
public:
Account();
Account(string, int);
void chingePassword(string);
string getPassword();
int getId();
void display();
void setId(int id);
};
账户类 实现 Account.cpp
#include "Account.h"
Account::Account()
{
}
Account::Account(string m_password, int m_id)
{
this->password = m_password;
this->id = m_id;
}
void Account::chingePassword(string m_password)
{
this->password = m_password;
}
string Account::getPassword()
{
return this->password;
return string();
}
int Account::getId()
{
return this->id; return 0;}
void Account::display()
{
if (this->id == 1) {
cout << "该学生的密码是:" << this->password << endl;
}
else if (this->id == 2) {
cout << "该教师的密码是:" << this->password << endl;
}
else {
cout << "该管理员的密码是:" << this->password << endl;
}
}
void Account::setId(int id)
{
this->id = id;
}
个人用户类 定义 People.h
#pragma once
#include "All.h"
#include "Account.h"
#include "CColective.h"
#include "CLIbraryManager.h"
class People
{
private:
int p_num;//学号或者工号
string p_name;//姓名
string p_faculty;//院系
int p_maxBookNum;//最大借书数量
Account account;
vector v_book;
public:
People();
People(int m_id, string m_password, int m_num, string m_name, string m_faculty);
void setNum(int m_num);//设置或修改学号
void setName(string m_name);//设置或修改姓名
void setFaculty(string m_faculty);//设置或修改院系
void borrowBook();//借书
void return_Book();//还书
void display();//显示所有信息(包括学生信息,书本信息)
void dispaly_book();//仅显示书本信息
int getNum();
string getName();
string getFaculty();
int getMaxBookNum();
Account& getAccount();
vector& get_v_book();
};
个人用户类 实现 People.cpp
#include "People.h"
People::People()
{
this->p_num = -1;
this->p_name = "";
this->p_faculty = "";
}
People::People(int m_id, string m_password, int m_num, string m_name, string m_faculty)
{
this->account.setId(m_id);
this->account.chingePassword(m_password);
this->p_num = m_num;
int i;
this->p_name = m_name;
this->p_faculty = m_faculty;
if (account.getId() == 1) {
this->p_maxBookNum = 3;
}
else if (account.getId() == 2) {
this->p_maxBookNum = 5;
}
}
void People::setNum(int m_num)
{
this->p_num = m_num;
}
void People::setName(string m_name)
{
this->p_name = m_name;
}
void People::setFaculty(string m_faculty)
{
this->p_faculty = m_faculty;
}
void People::borrowBook()
{
CLIbraryManager LM;
int c;
while (true)
{
cout << "tt-----------------" << endl;
cout << "tt|1、查看所有书籍|" << endl;
cout << "tt-----------------" << endl;
cout << "tt|2、通过编号借书|" << endl;
cout << "tt-----------------" << endl;
cout << "tt|3、通过书名借书|" << endl;
cout << "tt-----------------" << endl;
cout << "tt|4、查找书籍 |" << endl;
cout << "tt-----------------" << endl;
cout << "tt|0、 返回 |" << endl;
cout << "tt-----------------" << endl;
cin >> c;
switch (c)
{
if (this->v_book.size() < this->getMaxBookNum()){
case 2:
{
if (this->v_book.size() == this->getMaxBookNum()) {
cout << "已达到最大借书数量!"
<< "t借书失败!" << endl;
system("pause");
system("cls");
break;
}
int m_Id;
cout << "请输入编号:";
cin >> m_Id;
if (LM.find_By_Num(m_Id) != nullptr)
{
this->v_book.push_back(LM.find_By_Num(m_Id));
cout << "借书成功!" << endl;
}
else {
cout << "未查询到该书信息!"
<< "t借书失败!" << endl;
}
system("pause");
system("cls");
break;
}
case 3:
{
if (this->v_book.size() == this->getMaxBookNum()) {
cout << "已达到最大借书数量!"
<< "t借书失败!" << endl;
system("pause");
system("cls");
break;
}
string m_name;
cout << "请输入书名:";
cin >> m_name;
if (LM.find_By_Name(m_name) != nullptr)
{
this->v_book.push_back(LM.find_By_Name(m_name));
cout << "借书成功!" << endl;
}
else {
cout << "未查询到该书信息!"
<< "t借书失败!" << endl;
}
system("pause");
system("cls");
break;
}
}
else {
cout << "已达到最大借书数量!"
<< "t借书失败!" << endl;
break;
}
case 1:
{
system("cls");
LM.show_BookArray();
break;
}
case 4:
{
system("cls");
LM.find_Book();
break;
}
case 0:
{
system("cls");
return;
break;
}
default:
{
cout << "输入的数据有误!" << endl;
break;
}
}
}
}
void People::return_Book()
{
int c;
while (true)
{
cout << "tt-----------------" << endl;
cout << "tt|1、查看已借书籍|" << endl;
cout << "tt-----------------" << endl;
cout << "tt|2、通过编号还书|" << endl;
cout << "tt-----------------" << endl;
cout << "tt|3、通过书名还书|" << endl;
cout << "tt-----------------" << endl;
cout << "tt|0、 返回 |" << endl;
cout << "tt-----------------" << endl;
cin >> c;
switch (c)
{
case 1:
{
cout << this->v_book.size() << endl;
this->dispaly_book();
system("pause");
system("cls");
break;
}
case 2:
{
int index = -1;
int m_Id;
cout << "请输入编号:";
cin >> m_Id;
for (vector::iterator iter = v_book.begin(); iter != v_book.end(); iter++) {
if ((*iter)->m_Id == m_Id) {
index = 1;
(*iter)->showInfo();
cout << endl;
int i = 1;
cout << "是否确认归还此书籍?" << endl;
cout << "1.是"
<< "n2.否" << endl;
cin >> i;
switch (i)
{
case 1:
{
v_book.erase(iter);
cout << "还书成功!" << endl;
break;
}
case 2:
{
cout << "返回!" << endl;
break;
}
}
break;
}
}
if (index == -1) {
cout << "未查询到该书信息!" << endl;
}
system("pause");
system("cls");
break;
}
case 3:
{
int index = -1;
string m_name;
cout << "请输入书名:";
cin >> m_name;
for (vector::iterator iter = v_book.begin(); iter != v_book.end(); iter++) {
if ((*iter)->m_Name == m_name) {
index = 1;
(*iter)->showInfo();
cout << endl;
int i = 1;
cout << "是否确认归还此书籍?" << endl;
cout << "1.是"
<< "n2.否" << endl;
cin >> i;
switch (i)
{
case 1:
{
v_book.erase(iter);
cout << "还书成功!" << endl;
break;
}
case 2:
{
cout << "返回!" << endl;
break;
}
}
break;
}
}
if (index == -1) {
cout << "未查询到该书信息!" << endl;
}
system("pause");
system("cls");
break;
}
case 0:
{
system("cls");
return;
break;
}
default:
{
cout << "输入的数据有误!" << endl;
break;
}
}
//如果还书过后已经没有书了,则返回上级菜单
if (v_book.empty()) {
return;
}
}
}
void People::display()
{
if (this->account.getId() == 1) {
cout << "学生学号:" << this->p_num
<< "t学生姓名:" << this->p_name
<< "t学生院系:" << this->p_faculty
<< "t最大借书数量为:" << this->p_maxBookNum
<< "n------------------------------------------------借书信息------------------------------------------------"
<< "n已借:" << this->v_book.size() << "本书"
<< "t还可借:" << this->p_maxBookNum - v_book.size() << "本书" << endl;
if (!v_book.empty()) {
cout << " 已借书籍信息" << endl;
//vector::iterator inter = v_book.begin();
int i = 0;
for (auto book : v_book) {
i++;
cout << "第 " << i << " 本书为:" << endl;
book->showInfo();
cout << endl << endl;
}
}
}
else if (this->account.getId() == 2) {
cout << "教师工号:" << this->p_num
<< "t教师姓名:" << this->p_name
<< "t教师院系:" << this->p_faculty
<< "t最大借书数量为:" << this->p_maxBookNum
<< "n------------------------------------------------借书信息------------------------------------------------"
<< "n已借:" << this->v_book.size() << "本书"
<< "t还可借:" << this->p_maxBookNum - v_book.size() << "本书" << endl;
if (!v_book.empty()) {
cout << " 已借书籍信息" << endl;
//vector::iterator inter = v_book.begin();
int i = 0;
for (auto book : v_book) {
i++;
cout << "第 " << i << " 本书为:" << endl;
book->showInfo();
cout << endl << endl;
}
}
}
else if (this->account.getId() == 3) {
cout << "该管理员编号:" << this->p_num
<< "t管理员姓名:" << this->p_name
<< "t管理员级别:" << this->p_faculty
<< endl;
}
system("pause");
system("cls");
}
void People::dispaly_book()
{
cout << "t------------------------------------------------借书信息------------------------------------------------"
<< "t已借:" << this->v_book.size() << "本书"
<< "t还可借:" << this->p_maxBookNum - v_book.size() << "本书" << endl;
if (!v_book.empty()) {
cout << " 已借书籍信息" << endl;
//vector::iterator inter = v_book.begin();
int i = 0;
for (auto book : v_book) {
i++;
cout << "第 " << i << " 本书为:" << endl;
book->showInfo();
}
}
}
int People::getNum()
{
return this->p_num;
return 0;
}
string People::getName()
{
return this->p_name;
return string();
}
string People::getFaculty()
{
return this->p_faculty;
return string();
}
int People::getMaxBookNum()
{
return this->p_maxBookNum;
return 0;
}
Account& People::getAccount()
{
return this->account;
}
vector& People::get_v_book()
{
return this->v_book;
}
账户管理类 定义 PeopleManager.h
#pragma once
#include "People.h"
#define PEOPLE "PeopleFile.txt"
#include "CLIbraryManager.h"
class PeopleManager
{
private:
const int m_Num = 2003;//初始账号
const string m_Possword = "666666";//初始密码
const string m_faculty = "默认管理员";//初始密码
const int m_id = 3;
const string m_name = "小明";
People* P = new People(m_id, m_Possword, m_Num, m_name, m_faculty);
public:
void color(unsigned short color_index);//可局部颜色改变
PeopleManager();//构造函数
int get_Rows_Number(string FILE_NAME);//获取文件有效行的数量
//显示菜单( 0.退出系统 1.学生操作 2.教师操作 3.管理员操作)
void Show_Menu();
//退出程序函数
void ExitSystem();
//添加人
vector v_People;
//int p_PeopleNum = v_People.size();//记录人的数量
void add_People();
//设置标志位判断最初文件是否存在
bool p_isPeople;
//保存人的信息
void save_Pepple();
//初始化文件中已存在的数据
void init_Person();
//显示所有人物(包括学生,教师,管理员)信息,管理员权限
void show_People();
//查找人物实现, 管理员,教师权限
void find_People();//管理员查找函数
People& search_People(People* people);//通过id和num唯一确定,并返回引用
//清空人物信息,管理员权限
void clear_People();
//通过查找删除学生信息,管理员权限
void delete_People();
//判断密码是否正确
People* judge_Possword(int Id, string possword);
//学生操作
void student_Operation();
//教师操作
void teacher_Operation();
People& find_People1();//教师查找学生的函数
void see_Student();//教师查看学生信息的函数(全部查看,单个查看)
void show_Student();//教师的查看所有学生信息
//管理员操作
void manager_Operation();
void people_Info_Manage(People& people);//人物信息管理
void see_Teacher(); //管理员查看教师信息的函数(全部查看,单个查看)
People& find_People2();//管理员查找教师
void show_Teacher();//管理员的查看所有教师信息
void reset_Password();//重置密码
CLIbraryManager LM;
void book_Info_Manage(CLIbraryManager &LM);//图书信息管理
~PeopleManager();//析构函数函数
};
账户管理类 实现 PeopleManager.cpp
#include "PeopleManager.h"
void PeopleManager::color(unsigned short color_index)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color_index);
}
PeopleManager::PeopleManager()
{
v_People.push_back(this->P);//添加默认管理员信息
ifstream ifs;
ifs.open(PEOPLE, ios::in);//打开文件
//文件不存在
if (!ifs.is_open())//ifs.is_open()返回值为bool类型,如果能打开返回true
{
this->p_isPeople = false;//文件不存在,将m_isFile赋值为false
ifs.close();
return;
}
//文件存在,但数据为空
char ch;
ifs >> ch;//传入空字符
if (ifs.eof())//如果文件是空,则返回true
{
this->p_isPeople = false;//文件存在,但无数据,将p_isPeople赋值为false
ifs.close();
return;
}
//文件存在且不为空
else if (!ifs.eof())
{
this->p_isPeople = true;//文件存在且有数据,将p_isPeople赋值为true
//将读取的数据存入数组中
this->init_Person();
}
}
int PeopleManager::get_Rows_Number(string FILE_NAME)
{
ifstream ifs(FILE_NAME.c_str());
int line_count = 0; ///记录文件中行的数量
string tmp_content; ///临时保存行数据
while (!ifs.eof())
{
std::getline(ifs, tmp_content, 'n');
///过滤空行
line_count += !tmp_content.empty();
}
return line_count;
}
void PeopleManager::Show_Menu()
{
color(3);
cout << "————————————————————————" << endl;
cout << "|—————— 欢迎使用图书管理系统!—————|" << endl;
cout << "————————————————————————" << endl;
cout << "|———————— 0.退出系统 ——————|" << endl;
cout << "————————————————————————" << endl;
cout << "|———————— 1.学生登录 ——————|" << endl;
cout << "————————————————————————" << endl;
cout << "|———————— 2.教师登录 ——————|" << endl;
cout << "————————————————————————" << endl;
cout << "|———————— 3.管理员登录 ——————|" << endl;
cout << "————————————————————————" << endl;
cout << endl;
}
void PeopleManager::ExitSystem()
{
cout << "欢迎下次使用!" << endl;
system("pause");
exit(0);
}
void PeopleManager::add_People()
{
color(6);
cout << "请输入添加的人数:" << endl;
int add_number = 0;
cin >> add_number;//接收用户输入的大小
if (add_number > 0)
{
color(6);
for (int i = 0; i < add_number; i++)
{
color(6);
int p_num;//学号或者工号/管理员编号
string p_name;//姓名
string p_faculty;//院系
string password;//密码
cout << "请选择添加的身份:" << endl;
cout << "1.学生"
<< "n2.教师"
<< "n3.管理员" << endl;
int index;
cin >> index;
switch (index)
{
case 1:
{
int m_index = -1;//条件判断标志位
do
{
color(6);
cout << "请输入第" << i + 1 << "个人信息:" << endl;
cout << "学生学号:" << endl;
cin >> p_num;
cout << endl;
cout << "学生姓名:" << endl;
cin >> p_name;
cout << "学生院系:" << endl;
cin >> p_faculty;
cout << "请设置学生登录密码:" << endl;
cin >> password;
m_index = -1;
for (auto iter : v_People) {
if (iter->getAccount().getId() == 1 && iter->getNum() == p_num) {
color(4);
m_index = 1;
cout << "该学生学号已存在,请重新输入信息!" << endl;
break;
}
}
} while (m_index == 1);
while (password.length() > 13) {
color(4);
cout << "密码长度小于 13位 请重新设置密码:";
cin >> password;
}
People* people = new People(index, password, p_num, p_name, p_faculty);
people->getAccount().chingePassword(password);
v_People.push_back(people);
break;
}
case 2:
{
int m_index = -1;//条件判断标志位
do
{
color(6);
cout << "请输入第" << i + 1 << "个人信息:" << endl;
cout << "教师工号:" << endl;
cin >> p_num;
cout << endl;
cout << "教师姓名:" << endl;
cin >> p_name;
cout << "教师院系:" << endl;
cin >> p_faculty;
cout << "请设置教师登录密码:" << endl;
cin >> password;
m_index = -1;
for (auto iter : v_People) {
if (iter->getAccount().getId() == 2 && iter->getNum() == p_num) {
color(4);
m_index = 1;
cout << "该教师工号已存在,请重新输入信息!" << endl;
break;
}
}
} while (m_index == 1);
while (password.length() > 13) {
color(4);
cout << "密码长度小于 13位 请重新设置密码:";
cin >> password;
}
People* people = new People(index, password, p_num, p_name, p_faculty);
people->getAccount().chingePassword(password);
v_People.push_back(people);
break;
}
case 3:
{
int m_index = -1;//条件判断标志位
do
{
color(6);
cout << "请输入第" << i + 1 << "个人信息:" << endl;
cout << "管理员编号:" << endl;
cin >> p_num;
cout << endl;
cout << "管理员姓名:" << endl;
cin >> p_name;
cout << "管理员级别:" << endl;
cout << "(超级管理员、普通管理员)" << endl;
cin >> p_faculty;
cout << "请设置管理员登录密码:" << endl;
cin >> password;
m_index = -1;
for (auto iter : v_People) {
if (iter->getAccount().getId() == 3 && iter->getNum() == p_num) {
color(4);
m_index = 1;
cout << "该管理员编号已存在,请重新输入信息!" << endl;
break;
}
}
} while (m_index == 1);
while (password.length() > 13) {
color(4);
cout << "密码长度小于 13位 请重新设置密码:";
cin >> password;
}
People* people = new People(index, password, p_num, p_name, p_faculty);
people->getAccount().chingePassword(password);
v_People.push_back(people);
break;
}
default:
break;
}
}
this->save_Pepple();
cout << "成功添加 " << add_number << " 个人信息!" << endl;
}
else {
color(4);
cout << "输入的数据有误!" << endl;
}
system("pause");
system("cls");
}
void PeopleManager::save_Pepple()
{
ofstream ofs;//定义
ofs.open(PEOPLE, ios::out);//用输出的方式文件——写文件
//将个人的数据写入到文件中
//vector::iterator inter = v_People.begin(); inter != v_People.end(); inter++
for (auto inter : v_People)
{
string index;
if (!inter->get_v_book().empty()) {
index = "true";
ofs << index << " ";
}
else {
index = "false";
ofs << index << " ";
}
ofs << inter->getAccount().getId() << " " //判断身份的Id
<< inter->getNum() << " " //用于登录的学生账户
<< inter->getAccount().getPassword() << " " //登录密码
<< inter->getName() << " " //姓名
<< inter->getFaculty() << " " << endl;//院系
//<< inter->getMaxBookNum() << " ";//最大借书数量
if (!inter->get_v_book().empty()) {
for (auto i : inter->get_v_book())
{
ofs << "book" << " "
<< i->m_Id << " "
<< i->m_Name << " "
<< i->m_Press << " "
<< i->m_Price << " "
<< i->m_Page << " "
<< i->GetOtherProperties() << endl;
}
}
else {
ofs << endl;
}
}
//关闭文件
ofs.close();
}
//---------------------------------------------------------------> 初始化 有可能有问题!!!<-------------已优化
void PeopleManager::init_Person()
{
int rows = get_Rows_Number(PEOPLE);
int num = 1;//判断当前行
ifstream ifs;
ifs.open(PEOPLE, ios::in);
string index;
int p_Id; //人的id
int p_Num; //人的账户
string p_password;//密码
string p_name;//人的名字
string p_faculty;//院系
//int p_MaxBookNum;//最大借书数量
int d_Id;//书本编号
string d_Name; //书本名字
string d_Press; //出版社
string d_Price; //图书价格
string d_Page_Words; //图书页数或字数
string d_OtherProperties;//书的其他属性(Isbn,Issn,Format)
//vector::iterator iter = people->get_v_book().begin();
People* people = NULL;
while (ifs >> index)
{
if (index == "true")
{
if (people != NULL)
{
v_People.push_back(people);
people = NULL;
if (ifs >> p_Id && ifs >> p_Num && ifs >> p_password && ifs >> p_name && ifs >> p_faculty)
{
people = new People(p_Id, p_password, p_Num, p_name, p_faculty);
}
}
else {
people = NULL;
if (ifs >> p_Id && ifs >> p_Num && ifs >> p_password && ifs >> p_name && ifs >> p_faculty)
{
people = new People(p_Id, p_password, p_Num, p_name, p_faculty);
}
}
}
else if (index == "book")
{
if (ifs >> d_Id && ifs >> d_Name && ifs >> d_Press && ifs >> d_Price && ifs >> d_Page_Words && ifs >> d_OtherProperties)
{
CColective* book = NULL;
//截取读取的其他属性的前4个字符串。如果为"ISBN",则说明这一行数据是一般图书
if (d_OtherProperties.substr(0, 4) == "ISBN")
{
book = new CBooks(d_Id, d_Name, d_Press, d_Price, d_Page_Words);
book->SetOtherProperties(d_OtherProperties.substr(5, d_OtherProperties.size() - 1));
}
//截取读取的其他属性的前4个字符串。如果为"ISSN",则说明这一行数据是杂志
else if (d_OtherProperties.substr(0, 4) == "ISSN")
{
book = new CMagazine(d_Id, d_Name, d_Press, d_Price, d_Page_Words);
book->SetOtherProperties(d_OtherProperties.substr(5, d_OtherProperties.size() - 1));
}
//否则就是电子书类
else
{
book = new CEBook(d_Id, d_Name, d_Press, d_Price, d_Page_Words);
book->SetOtherProperties(d_OtherProperties);
}
//将读取的书本信息存入书本数组中
people->get_v_book().push_back(book);
}
}
else if(index == "false"){
while (ifs >> p_Id && ifs >> p_Num && ifs >> p_password && ifs >> p_name && ifs >> p_faculty)
{
if (p_Id == this->m_id && p_Num == this->m_Num && p_password == this->m_Possword && p_name == this->m_name && p_faculty == this->m_faculty) {
break;
}
else {
People* people = new People(p_Id, p_password, p_Num, p_name, p_faculty);
v_People.push_back(people);
break;
}
}
}
if (num == rows && people!= NULL) {
v_People.push_back(people);
}
num++;
}
ifs.close();//关闭文件
}
void PeopleManager::show_People()
{
for (auto people : v_People) {
people->display();
cout << endl;
}
system("pause");
system("cls");
}
void PeopleManager::show_Student()
{
int index = 0;
for (vector::iterator iter = v_People.begin(); iter != v_People.end(); iter++) {
if ((*iter)->getAccount().getId() == 1) {
index++;
(*iter)->display();
cout << endl << endl;
}
}
if (index == 0) {
color(4);
cout << "暂无学生信息!" << endl;
system("pause");
system("cls");
}
}
void PeopleManager::find_People()
{
if (v_People.size() == 0) {
color(4);
cout << "无人物信息!" << endl;
}
else
{
color(6);
cout << "可通过以下查找:" << endl;
cout << "1.学号/工号/编号" << endl
<< "2.姓名" << endl;
int select;//用户查找选择
cin >> select;
int people_Id = 0;
string people_Name = "";
int index = -1;
switch (select)
{
case 1:
{
color(6);
cout << "请输入学号/工号/编号:" << endl;
cin >> people_Id;
for (auto people : v_People) {
if (people_Id == people->getNum()) {
people->display();
index = 1;
}
}
break;
}
case 2:
{
color(6);
cout << "请输入姓名:" << endl;
cin >> people_Name;
for (auto people : v_People) {
if (people_Name == people->getName()) {
people->display();
index = 1;
}
}
break;
}
default:
color(4);
cout << "输入的数据有误!" << endl;
break;
}
if (index == -1) {
color(4);
cout << "未查询到相关信息!" << endl;
}
system("cls");
}
}
People& PeopleManager::find_People1()
{
color(6);
cout << "可通过以下查找:" << endl;
cout << "1.学生学号" << endl
<< "2.学生姓名" << endl;
int select;//用户查找选择
cin >> select;
int people_Id = 0;
string people_Name = "";
int index = -1;
switch (select)
{
case 1:
{
color(6);
cout << "请输入学生学号:" << endl;
cin >> people_Id;
for (auto people : v_People) {
if (people_Id == people->getNum() && people->getAccount().getId() == 1) {
return *people;
index = 1;
break;
}
}
break;
}
case 2:
{
color(6);
cout << "请输入学生姓名:" << endl;
cin >> people_Name;
for (auto people : v_People) {
if (people_Name == people->getName() && people->getAccount().getId() == 1) {
return *people;
index = 1;
break;
}
}
break;
}
default:
color(4);
cout << "输入的数据有误!" << endl;
break;
}
if (index == -1) {
color(4);
cout << "未查询到相关信息!" << endl;
}
}
void PeopleManager::see_Student()
{
while (true)
{
color(3);
cout << "tt---------------------" << endl;
cout << "tt|1、查看所有学生信息|" << endl;
cout << "tt---------------------" << endl;
cout << "tt|2、查找单个学生信息|" << endl;
cout << "tt---------------------" << endl;
cout << "tt|0、返回 |" << endl;
cout << "tt---------------------" << endl;
int i;
cin >> i;
switch (i)
{
case 0:
{
system("cls");
return;
break;
}
case 1:
{
system("cls");
show_Student();
break;
}
case 2:
{
find_People1().display();
break;
}
default:
color(4);
cout << "输入的数据有误!" << endl;
system("pause");
system("cls");
break;
}
}
}
People& PeopleManager::search_People(People *people)
{
for (auto iter : v_People) {
if (iter->getAccount().getId() == people->getAccount().getId() && iter->getNum() == people->getNum()) {
return *iter;
}
}
// TODO: 在此处插入 return 语句
}
void PeopleManager::clear_People()
{
color(4);
cout << "是否确认清空所有人物信息?" << endl;
cout << "1.是"
<< "n2.否" << endl;
int select = -1;
cin >> select;
switch (select)
{
case 1:
{
color(4);
v_People.clear();
this->p_isPeople = false;
cout << "清空所有人物信息成功!" << endl;
this->save_Pepple();
break;
}
case 2:
{
color(4);
cout << "退出清空所有人物信息!" << endl;
return;
}
default:
color(4);
cout << "输入的数据有误!" << endl;
break;
}
system("pause");
system("cls");
}
void PeopleManager::delete_People()
{
if (!this->p_isPeople) {
color(4);
cout << "文件不存在或记录为空!" << endl;
}
else {
color(6);
cout << "可通过以下删除:" << endl;
cout << "1.学号/工号/编号" << endl
<< "2.姓名" << endl;
int select;//用户查找选择
cin >> select;
int people_Id = 0;
string people_Name = "";
int index = -1;
switch (select)
{
case 1:
{
color(6);
cout << "请输入学号/工号/编号:" << endl;
cin >> people_Id;
//(*iter) 解引用
for (vector::iterator iter = v_People.begin(); iter != v_People.end(); iter++) {
if ((*iter)->getNum() == people_Id)
{
color(4);
index = 1;
(*iter)->display();
int i = -1;
cout << "是否确认删除该人物信息?" << endl;
cout << "1.是"
<< "n2.否" << endl;
cin >> i;
switch (i)
{
case 1:
{
color(4);
v_People.erase(iter);
cout << "删除成功!" << endl;
this->save_Pepple();
break;
}
case 2:
{
color(4);
cout << "退出删除!" << endl;
return;
break;
}
default:
break;
}
system("pause");
system("cls");
break;
}
}
break;
}
case 2:
{
color(6);
cout << "请输入姓名:" << endl;
cin >> people_Name;
//(*iter) 解引用
for (vector::iterator iter = v_People.begin(); iter != v_People.end(); iter++) {
if ((*iter)->getName() == people_Name) {
color(4);
index = 1;
(*iter)->display();
int i = -1;
cout << "是否确认删除该人物信息?" << endl;
cout << "1.是"
<< "n2.否" << endl;
cin >> i;
switch (i)
{
case 1:
{
color(4);
v_People.erase(iter);
cout << "删除成功!" << endl;
this->save_Pepple();
break;
}
case 2:
{
color(4);
cout << "退出删除!" << endl;
return;
break;
}
default:
break;
}
}
}
break;
}
default:
{
color(4);
cout << "输入的数据有误!" << endl;
break;
}
if (index == -1) {
color(4);
cout << "未查询到相关信息!" << endl;
}
}
}
system("pause");
system("cls");
}
People* PeopleManager::judge_Possword(int m_Id, string m_possword)
{
for (vector::iterator iter = v_People.begin(); iter != v_People.end(); iter++) {
if ((*iter)->getNum() == m_Id && (*iter)->getAccount().getPassword() == m_possword) {
return *iter;
break;
}
}
return NULL;
}
void PeopleManager::student_Operation()
{
color(6);
int m_num;
string m_possword;
cout << "请输入账号(学号) :";
cin >> m_num;
cout << endl;
cout << "请输入密码 :";
cin >> m_possword;
People* people = this->judge_Possword(m_num, m_possword);
if (people != NULL && people->getAccount().getId() == 1) {
cout << "登录成功!欢迎您!" << endl;
system("pause");
system("cls");
int c;
while (true)
{
color(3);
cout << "tt-----------------" << endl;
cout << "tt|1、查看自身信息|" << endl;
cout << "tt-----------------" << endl;
cout << "tt|2、修改个人密码|" << endl;
cout << "tt-----------------" << endl;
cout << "tt|3、 借阅书籍 |" << endl;
cout << "tt-----------------" << endl;
cout << "tt|4、 归还书籍 |" << endl;
cout << "tt-----------------" << endl;
cout << "tt|0、退出学生操作|" << endl;
cout << "tt-----------------" << endl;
cin >> c;
switch (c)
{
case 1:
{
people->display();
break;
}
case 2:
{
color(6);
cout << "输入新密码:" << endl;
cin >> m_possword;
people->getAccount().chingePassword(m_possword);
search_People(people).getAccount().chingePassword(m_possword);
cout << "修改成功" << endl;
system("pause");
system("cls");
break;
}
case 3:
{
system("cls");
people->borrowBook();
break;
}
case 4:
{
system("cls");
color(6);
if (people->get_v_book().size()==0) {
cout << "您还未借书" << endl;
system("pause");
system("cls");
break;
}
else {
people->return_Book();
break;
}
}
case 0:
system("cls");
return;
break;
}
this->save_Pepple();
}
}
else {
color(4);
cout << "账户或密码错误!请重新输入" << endl;
system("pause");
system("cls");
}
}
void PeopleManager::teacher_Operation()
{
color(6);
int m_num;
string m_possword;
cout << "请输入账号(工号) :";
cin >> m_num;
cout << endl;
cout << "请输入密码 :";
cin >> m_possword;
People* people = this->judge_Possword(m_num, m_possword);
if (people != NULL && people->getAccount().getId() == 2) {
cout << "登录成功!欢迎您!" << endl;
system("pause");
system("cls");
int c;
while (true)
{
color(3);
cout << "tt-----------------" << endl;
cout << "tt|1、查看自身信息|" << endl;
cout << "tt-----------------" << endl;
cout << "tt|2、查看学生信息|" << endl;
cout << "tt-----------------" << endl;
cout << "tt|3、修改个人密码|" << endl;
cout << "tt-----------------" << endl;
cout << "tt|4、 借阅书籍 |" << endl;
cout << "tt-----------------" << endl;
cout << "tt|5、 归还书籍 |" << endl;
cout << "tt-----------------" << endl;
cout << "tt|0、退出教师操作|" << endl;
cout << "tt-----------------" << endl;
cin >> c;
switch (c)
{
case 1:
{
people->display();
break;
}
case 2:
{
system("cls");
see_Student();
break;
}
case 3:
{
color(6);
cout << "输入新密码:" << endl;
cin >> m_possword;
people->getAccount().chingePassword(m_possword);
search_People(people).getAccount().chingePassword(m_possword);
cout << "修改成功" << endl;
system("pause");
system("cls");
break;
}
case 4:
{
system("cls");
people->borrowBook();
break;
}
case 5:
{
system("cls");
color(6);
if (people->get_v_book().size() == 0) {
cout << "您还未借书" << endl;
system("pause");
system("cls");
break;
}
else {
people->return_Book();
break;
}
}
case 0:
system("cls");
return;
break;
}
this->save_Pepple();
}
}
else {
color(4);
cout << "账户或密码错误!请重新输入" << endl;
system("pause");
system("cls");
}
}
void PeopleManager::manager_Operation()
{
color(6);
int m_num;
string m_possword;
cout << "请输入账号(编号) :";
cin >> m_num;
cout << endl;
cout << "请输入密码 :";
cin >> m_possword;
People* people = this->judge_Possword(m_num, m_possword);
if (people != NULL && people->getAccount().getId() == 3) {
cout << "登录成功!欢迎您!" << endl;
system("pause");
system("cls");
int c;
while (true)
{
color(3);
cout << "tt-------------------" << endl;
cout << "tt|1、人物信息管理 |" << endl;
cout << "tt-------------------" << endl;
cout << "tt|2、图书信息管理 |" << endl;
cout << "tt-------------------" << endl;
cout << "tt|0、退出管理员操作|" << endl;
cout << "tt-------------------" << endl;
cin >> c;
switch (c)
{
case 1:
{
system("cls");
people_Info_Manage(*people);
system("cls");
break;
}
case 2:
{
system("cls");
book_Info_Manage(LM);
break;
}
case 0:
system("cls");
return;
break;
}
this->save_Pepple();
}
}
else {
color(4);
cout << "账户或密码错误!请重新输入" << endl;
system("pause");
system("cls");
}
}
void PeopleManager::people_Info_Manage(People& people)
{
while (true)
{
color(3);
cout << "tt-----------------" << endl;
cout << "tt|1、查看自身信息|" << endl;
cout << "tt-----------------" << endl;
cout << "tt|2、查看学生信息|" << endl;
cout << "tt-----------------" << endl;
cout << "tt|3、查看教师信息|" << endl;
cout << "tt-----------------" << endl;
cout << "tt|4、添加人物信息|" << endl;
cout << "tt-----------------" << endl;
cout << "tt|5、模糊搜索信息|" << endl;
cout << "tt-----------------" << endl;
cout << "tt|6、重置人物密码|" << endl;
cout << "tt-----------------" << endl;
cout << "tt|7、修改个人密码|" << endl;
cout << "tt-----------------" << endl;
cout << "tt|8、删除人物信息|" << endl;
cout << "tt-----------------" << endl;
cout << "tt|9、清空人物信息|" << endl;
cout << "tt-----------------" << endl;
cout << "tt|0、 返回 |" << endl;
cout << "tt-----------------" << endl;
int c;
cin >> c;
switch (c)
{
case 1:
{
people.display();
system("cls");
break;
}
case 2:
{
system("cls");
see_Student();
break;
}
case 3:
{
system("cls");
see_Teacher();
break;
}
case 4:
{
system("cls");
add_People();
break;
}
case 5:
{
system("cls");
find_People();
break;
}
case 6:
{
system("cls");
reset_Password();
break;
}
case 7:
{
color(6);
system("cls");
if (people.getNum() == 2003 && people.getFaculty() == "默认管理员") {
color(4);
cout << "默认管理员不支持修改自身密码!" << endl;
system("pause");
system("cls");
break;
}
string m_possword;
cout << "输入新密码:" << endl;
cin >> m_possword;
people.getAccount().chingePassword(m_possword);
search_People(&people).getAccount().chingePassword(m_possword);
cout << "修改成功" << endl;
system("pause");
system("cls");
break;
}
case 8:
{
system("cls");
delete_People();
system("pause");
system("cls");
break;
}
case 9:
{
system("cls");
clear_People();
break;
}
case 0:
//system("cls");
return;
break;
}
}
}
void PeopleManager::see_Teacher()
{
while (true)
{
color(3);
cout << "tt---------------------" << endl;
cout << "tt|1、查看所有教师信息|" << endl;
cout << "tt---------------------" << endl;
cout << "tt|2、查找单个教师信息|" << endl;
cout << "tt---------------------" << endl;
cout << "tt|0、返回 |" << endl;
cout << "tt---------------------" << endl;
int i;
cin >> i;
switch (i)
{
case 0:
{
system("cls");
return;
break;
}
case 1:
{
system("cls");
show_Teacher();
break;
}
case 2:
{
system("cls");
find_People2().display();
break;
}
default:
color(4);
cout << "输入的数据有误!" << endl;
break;
}
}
}
People& PeopleManager::find_People2()
{
color(6);
cout << "可通过以下查找:" << endl;
cout << "1.教师工号" << endl
<< "2.教师姓名" << endl;
int select;//用户查找选择
cin >> select;
int people_Id = 0;
string people_Name = "";
int index = -1;
switch (select)
{
case 1:
{
color(6);
cout << "请输入教师工号:" << endl;
cin >> people_Id;
for (auto people : v_People) {
if (people_Id == people->getNum() && people->getAccount().getId() == 2) {
return *people;
index = 1;
break;
}
}
break;
}
case 2:
{
color(6);
cout << "请输入教师姓名:" << endl;
cin >> people_Name;
for (auto people : v_People) {
if (people_Name == people->getName() && people->getAccount().getId() == 2) {
return *people;
index = 1;
break;
}
}
break;
}
default:
color(4);
cout << "输入的数据有误!" << endl;
break;
}
if (index == -1) {
color(4);
cout << "未查询到相关信息!" << endl;
}
}
void PeopleManager::show_Teacher()
{
int index = 0;
for (vector::iterator iter = v_People.begin(); iter != v_People.end(); iter++) {
if ((*iter)->getAccount().getId() == 2) {
index++;
(*iter)->display();
cout << endl;
}
}
if (!index) {
color(4);
cout << "暂无学教师信息!" << endl;
system("pause");
system("cls");
}
}
void PeopleManager::reset_Password()
{
while (true)
{
color(3);
cout << "tt---------------------" << endl;
cout << "tt|1、 重置学生密码 |" << endl;
cout << "tt---------------------" << endl;
cout << "tt|2、 重置教师密码 |" << endl;
cout << "tt---------------------" << endl;
cout << "tt|0、 返回 |" << endl;
cout << "tt---------------------" << endl;
int i;
cin >> i;
switch (i)
{
case 0:
{
system("cls");
return;
break;
}
case 1:
{
color(6);
find_People1().getAccount().chingePassword("111111");
cout << "重置学生密码成功!" << endl;
cout << "默认密码:111111" << endl;
this->save_Pepple();
system("pause");
system("cls");
break;
}
case 2:
{
color(6);
find_People2().getAccount().chingePassword("222222");
cout << "重置学生密码成功!" << endl;
cout << "默认密码:222222" << endl;
system("pause");
system("cls");
this->save_Pepple();
break;
}
default:
color(4);
cout << "输入的数据有误!" << endl;
system("pause");
system("cls");
break;
}
}
}
void PeopleManager::book_Info_Manage(CLIbraryManager& LM)
{
while (true)
{
color(3);
cout << "tt-----------------" << endl;
cout << "tt|1、 增加书本 |" << endl;
cout << "tt-----------------" << endl;
cout << "tt|2、 显示书本 |" << endl;
cout << "tt-----------------" << endl;
cout << "tt|3、 删除书本 |" << endl;
cout << "tt-----------------" << endl;
cout << "tt|4、 查找书本 |" << endl;
cout << "tt-----------------" << endl;
cout << "tt|5、 书本排序 |" << endl;
cout << "tt-----------------" << endl;
cout << "tt|6、 清空书本 |" << endl;
cout << "tt-----------------" << endl;
cout << "tt|0、 返回 |" << endl;
cout << "tt-----------------" << endl;
int c;
cin >> c;
switch (c)
{
case 0:
{
system("cls");
return;
break;
}
case 1:
{
LM.Add_Book();
break;
}
case 2:
{
LM.show_BookArray();
break;
}
case 3:
{
LM.delete_Book();
break;
}
case 4:
{
LM.find_Book();
break;
}
case 5:
{
LM.sort_BookArray();
break;
}
case 6:
{
LM.clear_Txt();
break;
}
default:
{
system("cls");
break;
}
}
}
}
PeopleManager::~PeopleManager()
{
v_People.erase(v_People.begin());
//this->save_Pepple();
if (!v_People.empty()) {//释放内存
vector p;
v_People.swap(p);
}
}
//书本基类 CColective.h
#pragma once //书本基类 #include//一般图书类 定义 CBooks.h#include using namespace std; class CColective { public: int m_Id;//书本编号 string m_Name; //书本名字 string m_Press; //出版社 string m_Price; //图书价格 string m_Page; //图书页数 virtual void showInfo() = 0; //显示书本信息,多态 virtual string getClass() = 0; //获取书本类型 virtual void SetOtherProperties(string Other_Properties) = 0;//设置其他属性 virtual string GetOtherProperties() = 0;//获得其他属性 virtual bool Judge(string OtherProperties) = 0; };
#pragma once //一般图书类 #include "CColective.h" #include//一般图书类 实现 CBooks.cpp#include using namespace std; class CBooks : public CColective { private: string m_Isbn = "ISBN-"; //书本Isbn码 int njudge;//判断Ibsn码合法性 public: CBooks();//无参构造函数 CBooks(int Id, string Name, string Press, string Price, string Page); //有参构造函数 ~CBooks(); bool Judge(string OtherProperties); //显示书本信息 virtual void showInfo(); //获取书本类型 virtual string getClass(); //设置其他属性 virtual void SetOtherProperties(string Isbn); virtual string GetOtherProperties();//获得其他属性 };
#include "CBooks.h"
CBooks::CBooks()
{
this->m_Id = 0;
this->m_Name = "";
this->m_Press = "";
this->m_Price = "";
this->m_Page = "";
}
CBooks::CBooks(int Id, string Name, string Press, string Price, string Page)
{
this->m_Id = Id;
this->m_Name = Name;
this->m_Press = Press;
this->m_Price = Price;
this->m_Page = Page;
}
CBooks::~CBooks()
{
}
bool CBooks::Judge(string OtherProperties)
{
int tmp = 0;
int ss[20] = { 0 };
for (int i = 0, j = 0; i < OtherProperties.length(); i++) {
if (OtherProperties.substr(i, 1) >= "0" && OtherProperties.substr(i, 1) <= "9") {
ss[j] = atoi(OtherProperties.substr(i, 1).c_str());
j++;
}
}
for (int i = 0; i < 12; i++) {
if (i % 2 == 0)
{
tmp += ss[i] * 1;
}
else {
tmp += ss[i] * 3;
}
}
if (tmp % 10 == 0 && tmp % 10 == ss[12] || 10 - (tmp % 10) == ss[12])
{
return true;
}
else
{
return false;
}
}
void CBooks::showInfo()
{
cout << "书本编号:" << this->m_Id
<< "t书本类型:" << this->getClass()
<< "t书本名称:" << this->m_Name
<< "t书本出版社:" << this->m_Press
<< "t书本价格:" << this->m_Price
<< "t书本页数:" << this->m_Page
<< "t其他属性——书本Ibsn号:" << this->m_Isbn;
}
string CBooks::getClass()
{
return string("一般图书");
}
void CBooks::SetOtherProperties(string Isbn)
{
this->m_Isbn.append(Isbn);//append(),追加函数,将两个string字符串拼接在一起
}
string CBooks::GetOtherProperties()
{
return this->m_Isbn;
return string();
}
//电子书类 定义 CEBook.h
#pragma once //电子书类 #include "CColective.h" #include//电子书类 实现 CEBook.cpp#include using namespace std; class CEBook : public CColective { private: string m_Format; //电子书的格式(txt、pdf、epub、azw3、kindle) public: CEBook(); CEBook(int Id, string Name, string Press, string Price, string Words);//Words电子书的字数 ~CEBook(); //显示书本信息 virtual void showInfo(); //获取书本类型 virtual string getClass(); //设置其他属性 virtual void SetOtherProperties(string Format); virtual string GetOtherProperties();//获得其他属性 bool Judge(string OtherProperties); };
#include "CEBook.h"
CEBook::CEBook()
{
this->m_Id = 0;
this->m_Name = "";
this->m_Press = "";
this->m_Price = "";
this->m_Page = "";
}
CEBook::CEBook(int Id, string Name, string Press, string Price, string Words)
{
this->m_Format = "";
this->m_Id = Id;
this->m_Name = Name;
this->m_Press = Press;
this->m_Price = Price;
this->m_Page = Words;
}
CEBook::~CEBook()
{
}
void CEBook::showInfo()
{
cout << "书本编号:" << this->m_Id
<< "t书本类型:" << this->getClass()
<< "t书本名称:" << this->m_Name
<< "t书本出版社:" << this->m_Press
<< "t书本价格:" << this->m_Price
<< "t书本字数:" << this->m_Page
<< "t其他属性——书本格式:格式为 . " << this->m_Format;
}
string CEBook::getClass()
{
return string("电子书");
}
void CEBook::SetOtherProperties(string Format)
{
this->m_Format.append(Format);
}
string CEBook::GetOtherProperties()
{
return this->m_Format;
return string();
}
bool CEBook::Judge(string OtherProperties)
{
return false;
}
//杂志书籍类 定义 CMagazine.h
#pragma once //杂志书籍类 #include "CColective.h" #include//杂志书籍类 实现 CMagazine.cpp#include using namespace std; class CMagazine : public CColective { private: string m_Issn = "ISSN-";//杂志期刊号 格式:ISSN XXXX-XXXX public: CMagazine(); CMagazine(int Id, string Name, string Press, string Price, string Page); ~CMagazine(); //显示书本信息 virtual void showInfo(); //获取书本类型 virtual string getClass(); //设置其他属性 virtual void SetOtherProperties(string Issn); virtual string GetOtherProperties();//获得其他属性 bool Judge(string OtherProperties); };
#include "CMagazine.h"
CMagazine::CMagazine()
{
this->m_Id = 0;
this->m_Name = "";
this->m_Press = "";
this->m_Price = "";
this->m_Page = "";
}
CMagazine::CMagazine(int Id, string Name, string Press, string Price, string Page)
{
this->m_Id = Id;
this->m_Name = Name;
this->m_Press = Press;
this->m_Price = Price;
this->m_Page = Page;
}
CMagazine::~CMagazine()
{
}
void CMagazine::showInfo()
{
cout << "书本编号:" << this->m_Id
<< "t书本类型:" << this->getClass()
<< "t书本名称:" << this->m_Name
<< "t书本出版社:" << this->m_Press
<< "t书本价格:" << this->m_Price
<< "t书本页数:" << this->m_Page
<< "t其他属性——书本Issn号:" << this->m_Issn;
}
string CMagazine::getClass()
{
return string("杂志书籍");
}
void CMagazine::SetOtherProperties(string Issn)
{
this->m_Issn.append(Issn);//append(),追加函数,将两个string字符串拼接在一起
}
string CMagazine::GetOtherProperties()
{
return this->m_Issn;
return string();
}
bool CMagazine::Judge(string OtherProperties)
{
return false;
}
图书管理类 定义 CLIbraryManager.h
#pragma once //防止头文件重复包含 //管理类 //管理内容: //与用户的沟通菜单界面 //对书本的增、删、查等操作 //与文件的读写交互 #include "CColective.h" #include "CBooks.h" //一般图书 #include "CEBook.h" //电子书 #include "CMagazine.h" //杂志 #include图书管理类 实现 CLIbraryManager.h//system 不明确 #include //文件流头文件 #define FILENAME "BookFile.txt" using namespace std; class CLIbraryManager { public: CLIbraryManager();//构造函数 //菜单显示 void Show_Menu(); //0.退出系统 void ExitSystem(); //1.添加实现 int m_BookNum;//记录书本的数量 CColective** m_BookArray;//书本数组指针 void Add_Book(); //保存文件 void Save(); //设置标志判断最开始文件是否存在 bool m_isFile; //统计文件中已有书本数量 int Get_BookNum(); //初始化文件中已有书本数组、数量 void init_Book(); //2.显示书本 void show_BookArray(); //4.查找实现 void find_Book();//函数 //6.清空txt实现 void clear_Txt(); //3.删除书本信息实现 void delete_Book();//函数 int isBook(int Id); int isBook(string name); //5.按照书本序号排序 void sort_BookArray(); //通过编号返回书本 CColective* find_By_Num(int Id); //通过书名返回书本 CColective* find_By_Name(string name); ~CLIbraryManager();//析构函数 };
#include "CLIbraryManager.h" #include运行结果 说明using namespace std; CLIbraryManager::CLIbraryManager() { ifstream ifs; ifs.open(FILENAME, ios::in);//打开文件 //文件不存在 if (!ifs.is_open())//ifs.is_open()返回值为bool类型,如果能打开返回true { this->m_isFile = false;//文件不存在,将m_isFile赋值为false this->m_BookNum = 0;//初始化书的本数 this->m_BookArray = NULL;//初始化书数组指针 ifs.close(); return; } //文件存在,但数据为空 char ch; ifs >> ch;//传入空字符 if (ifs.eof())//如果文件是空,则返回true { this->m_isFile = false;//文件存在,但无数据,将m_isFile赋值为false this->m_BookNum = 0;//初始化书的本数 this->m_BookArray = NULL;//初始化书数组指针 ifs.close(); return; } //文件存在且不为空 else if (!ifs.eof()) { this->m_isFile = true;//文件存在且有数据,将m_isFile赋值为true int num = this->Get_BookNum(); this->m_BookNum = num; this->m_BookArray = new CColective* [this->m_BookNum]; //将读取的数据存入数组中 this->init_Book(); } } //显示菜单函数 void CLIbraryManager::Show_Menu() { cout << "————————————————————————" << endl; cout << "—————— 欢迎使用图书管理系统!——————" << endl; cout << "———————— 0.退出管理程序 ————————" << endl; cout << "———————— 1.增加书本信息 ————————" << endl; cout << "———————— 2.显示书本信息 ————————" << endl; cout << "———————— 3.删除书本信息 ————————" << endl; cout << "———————— 4.查找书本信息 ————————" << endl; cout << "———————— 5.按照编号排序 ————————" << endl; cout << "———————— 6.清空所有文档 ————————" << endl; cout << "————————————————————————" << endl; cout << endl; } //退出程序函数 void CLIbraryManager::ExitSystem() { cout << "欢迎下次使用!" << endl; system("pause"); exit(0); } //增加书函数 void CLIbraryManager::Add_Book() { cout << "请输入添加书本的数量:" << endl; int add_number = 0; cin >> add_number;//接收用户输入的大小 if (add_number > 0) //输入大于零才执行增加操作 { //执行添加 int newSize = this->m_BookNum + add_number;//计算需要开辟的空间大小,新的数量 = 原来记录的 + 新输入的 CColective** newspace = new CColective * [newSize];//分配空间 //如果原来的数组不为空,则添加到新开辟的数组中 if (this->m_BookArray != NULL) { for (int i = 0; i < this->m_BookNum; i++) { newspace[i] = this->m_BookArray[i]; } } //添加新数据 for (int i = 0; i < add_number; i++) { int d_Id = 0;//书本编号 string d_Name; //书本名字 string d_Press; //出版社 string d_Price; //图书价格 string d_Page_Words; //图书页数或字数 string d_OtherProperties;//书的其他属性(Isbn,Issn,Format) cout << "请选择要添加的类型:" << endl; cout << "1.一般图书" << endl; cout << "2.杂志" << endl; cout << "3.电子书" << endl; int select;//用户的选择 cin >> select; CColective* book = NULL;//创建book形参 cout << "请输入第" << i + 1 << "本书编号:" << endl; cin >> d_Id; cout << endl; cout << "请输入第" << i + 1 << "本书书名:" << endl; cin >> d_Name; cout << endl; cout << "请输入第" << i + 1 << "本书出版社:" << endl; cin >> d_Press; cout << endl; cout << "请输入第" << i + 1 << "本书价格:" << endl; cin >> d_Price; cout << endl; switch (select) { case 1: { //{开始 模块一:屏蔽一般图书ISBN号检测 //cout << "请输入第" << i + 1 << "本书页数:" << endl;//一般图书的页数 //cin >> d_Page_Words; //cout << endl; //book = new CBooks(d_Id, d_Name, d_Press, d_Price, d_Page_Words); //cout << "请输入第" << i + 1 << "本书Ibsn号:" << endl; //cin >> d_OtherProperties; //结束} //{开始 模块二:一般图书ISBN号检测 do { cout << "请输入第" << i + 1 << "本书页数:" << endl;//一般图书的页数 cin >> d_Page_Words; cout << endl; book = new CBooks(d_Id, d_Name, d_Press, d_Price, d_Page_Words); cout << "请输入第" << i + 1 << "本书Ibsn号:" << endl; cin >> d_OtherProperties; if (!book->Judge(d_OtherProperties)) { cout << "ISBN码不合法!" << endl << "请重新输入:" << endl << endl; cout << "请输入第" << i + 1 << "本书编号:" << endl; cin >> d_Id; cout << endl; cout << "请输入第" << i + 1 << "本书书名:" << endl; cin >> d_Name; cout << endl; cout << "请输入第" << i + 1 << "本书出版社:" << endl; cin >> d_Press; cout << endl; cout << "请输入第" << i + 1 << "本书价格:" << endl; cin >> d_Price; } } while (!book->Judge(d_OtherProperties)); //结束} cout << endl; book->SetOtherProperties(d_OtherProperties); break; } case 2: { cout << "请输入第" << i + 1 << "本书页数:" << endl; cin >> d_Page_Words; cout << endl; book = new CMagazine(d_Id, d_Name, d_Press, d_Price, d_Page_Words); cout << "请输入第" << i + 1 << "本书Issn号:" << endl; cin >> d_OtherProperties; cout << endl; book->SetOtherProperties(d_OtherProperties); break; } case 3: { cout << "请输入第" << i + 1 << "本书字数:" << endl;//电子书的字数 cin >> d_Page_Words; cout << endl; book = new CEBook(d_Id, d_Name, d_Press, d_Price, d_Page_Words); cout << "请输入第" << i + 1 << "本书格式:" << endl;//电子书的格式 cout << "电子书的格式有:txt、pdf、epub、azw3、kindle" << endl; cin >> d_OtherProperties; cout << endl; book->SetOtherProperties(d_OtherProperties); break; } default: break; } //将创建好的书本增加到数组中 newspace[this->m_BookNum + i] = book; } delete[]this->m_BookArray;//释放原来的空间 this->m_BookArray = newspace;//将新的空间指向m_BookArrary this->m_BookNum = newSize;//更新新的书本数量 //保存数据到文件中 //this->delete_txt(); this->Save(); this->m_isFile = true;//添加成功,则说明文件创建成功且数据不为空 cout << "成功添加" << add_number << "本新书" << endl; } else { cout << "输入的数据有误!" << endl; } system("pause"); system("cls"); } //保存书本数据 void CLIbraryManager::Save() { ofstream ofs;//定义 ofs.open(FILENAME, ios::out);//用输出的方式文件——写文件 //将每本书的数据写入到文件中 for (int i = 0; i < this->m_BookNum; i++) { ofs << this->m_BookArray[i]->m_Id << " " << this->m_BookArray[i]->m_Name << " " << this->m_BookArray[i]->m_Press << " " << this->m_BookArray[i]->m_Price << " " << this->m_BookArray[i]->m_Page << " " << this->m_BookArray[i]->GetOtherProperties() << endl; } //关闭文件 ofs.close(); } //获取文件中已有书本的数量 int CLIbraryManager::Get_BookNum() { int d_Id = 0;//书本编号 string d_Name; //书本名字 string d_Press; //出版社 string d_Price; //图书价格 string d_Page_Words; //图书页数或字数 string d_OtherProperties;//书的其他属性(Isbn,Issn,Format) ifstream ifs; ifs.open(FILENAME, ios::in); int num = 0; while (ifs >> d_Id && ifs >> d_Name && ifs >> d_Press && ifs >> d_Price && ifs >> d_Page_Words && ifs >> d_OtherProperties) { num++; } return num; } //初始化文件中已有书本数组、数量 void CLIbraryManager::init_Book() { ifstream ifs; ifs.open(FILENAME, ios::in); int d_Id;//书本编号 string d_Name; //书本名字 string d_Press; //出版社 string d_Price; //图书价格 string d_Page_Words; //图书页数或字数 string d_OtherProperties;//书的其他属性(Isbn,Issn,Format) int index = 0;//记录数组位置 while (ifs >> d_Id && ifs >> d_Name && ifs >> d_Press && ifs >> d_Price && ifs >> d_Page_Words && ifs >> d_OtherProperties) { CColective* book = NULL; //截取读取的其他属性的前4个字符串。如果为"ISBN",则说明这一行数据是一般图书 if (d_OtherProperties.substr(0, 4) == "ISBN") { book = new CBooks(d_Id, d_Name, d_Press, d_Price, d_Page_Words); book->SetOtherProperties(d_OtherProperties.substr(5, d_OtherProperties.size() - 1)); } //截取读取的其他属性的前4个字符串。如果为"ISSN",则说明这一行数据是杂志 else if (d_OtherProperties.substr(0, 4) == "ISSN") { book = new CMagazine(d_Id, d_Name, d_Press, d_Price, d_Page_Words); book->SetOtherProperties(d_OtherProperties.substr(5, d_OtherProperties.size() - 1)); } //否则就是电子书类 else { book = new CEBook(d_Id, d_Name, d_Press, d_Price, d_Page_Words); book->SetOtherProperties(d_OtherProperties); } //将读取的书本信息存入书本数组中 this->m_BookArray[index] = book; index++; } ifs.close();//关闭文件 } //显示函数 void CLIbraryManager::show_BookArray() { if (this->m_BookNum == 0) { cout << "无书本信息!" << endl; } else { for (int i = 0; i < this->Get_BookNum(); i++) { this->m_BookArray[i]->showInfo(); cout << endl << endl;//利用函数的多态,调用派生类显示函数 } } system("pause"); system("cls"); } //查找函数 void CLIbraryManager::find_Book() { if (this->m_BookNum == 0) { cout << "无书本信息!" << endl; } else { cout << "可通过以下查找:" << endl; cout << "1.书本编号" << endl << "2.书本名称" << endl; int select;//用户查找选择 cin >> select; int Id = 0; string book_name = ""; switch (select) { case 1: { cout << "请输入书本编号:" << endl; cin >> Id; for (int i = 0; i < this->m_BookNum; i++) { if (i == this->m_BookNum - 1 && this->m_BookArray[i]->m_Id != Id) { cout << "未查询到该书信息!" << endl; } if (this->m_BookArray[i]->m_Id == Id) { cout << "该书本信息为:" << endl; this->m_BookArray[i]->showInfo(); cout << endl; break; } } break; } case 2: { cout << "请输入书本名称:" << endl; cin >> book_name; for (int i = 0; i < this->m_BookNum; i++) { if (i == this->m_BookNum - 1 && this->m_BookArray[i]->m_Name != book_name) { cout << "未查询到该书信息!" << endl; } if (this->m_BookArray[i]->m_Name == book_name) { cout << "该书本信息为:" << endl; this->m_BookArray[i]->showInfo(); cout << endl; break; } } break; } default: break; } } system("pause"); system("cls"); } //清空文档函数 void CLIbraryManager::clear_Txt() { if (!this->m_isFile) { cout << "文件不存在!" << endl; } else { int select; cout << "是否确定清空所有文档?" << endl << "1.是" << endl << "2.否" << endl; cin >> select; ofstream ofs; switch (select) { case 1: { ofs.open(FILENAME, ios::out | ios::trunc); ofs.close(); delete[]this->m_BookArray; this->m_BookArray = NULL; this->m_BookNum = 0; this->m_isFile = false; cout << "文档已清空!" << endl; break; } case 2: break; default: break; } } system("pause"); system("cls"); } //通过Id判断该书是否存在,存在则返回数组下标 int CLIbraryManager::isBook(int Id) { int index = -1; for (int i = 0; i < this->m_BookNum; i++) { if (this->m_BookArray[i]->m_Id == Id) { index = i; break; } } return index; } //通过name判断该书是否存在,存在则返回数组下标(重载) int CLIbraryManager::isBook(string name) { int index = -1; for (int i = 0; i < this->m_BookNum; i++) { if (this->m_BookArray[i]->m_Name == name) { index = i; break; } } return index; } //删除书本信息 void CLIbraryManager::delete_Book() { //判断文件或记录是否存在 if (!this->m_isFile) { cout << "文件不存在或记录为空!" << endl; } else { int Id; string name; int select;//接収用户选择 int index = -1;//接收返回的数组下标 cout << "请输入通过什么删除:" << endl << "1.书本编号" << endl << "2.书本名称" << endl; cin >> select; switch (select) { case 1: { cout << "请输入书本编号:" << endl; cin >> Id; index = this->isBook(Id); break; } case 2: { cout << "请输入书本名称:" << endl; cin >> name; index = this->isBook(name); break; } default: break; } //若index的值不为-1,则说明该书存在 if (index != -1) { cout << "该书本信息为:" << endl; this->m_BookArray[index]->showInfo();//显示该书信息 cout << endl << endl << "是否确认删除该书信息?" << endl << "1.是" << endl << "2.否" << endl; cin >> select; switch (select) { case 1: { for (int i = index; i < this->m_BookNum - 1 ; i++) { this->m_BookArray[i] = this->m_BookArray[i + 1]; } this->m_BookNum--; cout << "删除成功!" << endl; this->Save(); break; } case 2: cout << "已取消删除!" << endl; break; default: break; } } else { cout << "删除失败,无该书信息!" << endl; } } system("pause"); system("cls"); } //排序实现 void CLIbraryManager::sort_BookArray() { if (!this->m_isFile) { cout << "文件不存在或记录为空!" << endl; } else { for (int i = 0; i < this->m_BookNum; i++) { for (int j = 0; j < this->m_BookNum; j++) { if (this->m_BookArray[i]->m_Id < this->m_BookArray[j]->m_Id) { CColective* Tbook = this->m_BookArray[i];//创建Tbook中间变量 this->m_BookArray[i] = this->m_BookArray[j]; this->m_BookArray[j] = Tbook; } } } this->Save(); cout << "排序成功!" << endl; } system("pause"); system("cls"); } CColective* CLIbraryManager::find_By_Num(int Id) { for (int i = 0; i < this->m_BookNum; i++) { if (i == this->m_BookNum - 1 && this->m_BookArray[i]->m_Id != Id) { return nullptr; break; } if (this->m_BookArray[i]->m_Id == Id) { return m_BookArray[i]; break; } } } CColective* CLIbraryManager::find_By_Name(string name) { for (int i = 0; i < this->m_BookNum; i++) { if (i == this->m_BookNum - 1 && this->m_BookArray[i]->m_Name != name) { return nullptr; break; } if (this->m_BookArray[i]->m_Name == name) { return m_BookArray[i]; break; } } return nullptr; } //析构函数 CLIbraryManager::~CLIbraryManager() { if (this->m_BookArray != NULL) { delete[] this->m_BookArray; this->m_BookArray = NULL; } }
个人能力有限,仅供参考,共同学习!



