C++:图书管理系统

图书管理系统

效果图:

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

内容

Book 头文件(抽象类)

Book.h

#pragma once
#include<iostream>
using namespace std;
#include<cstring>

class Book
{
public:

	virtual void showInfo() = 0;

	virtual string GetDeptName() = 0;


	int m_ID;

	string m_name;

	int m_DeptId;

	double m_price;

	int m_num;
};

BookManager 头文件(管理系统功能)

#pragma once  
#include<iostream>
using namespace std;
#include"Book.h"
#include"ScBook.h"
#include"Material.h"
#include"Novel.h"
#include<fstream>


class BookManager
{
public:
	BookManager();


	void Show_Menu();

	void exitSystem();

	int m_BookNum;

	Book** m_BookArry;

	bool m_FileIsEmpty;

	void Add();

	void BorrowOrReturn();

	void Save();

	int get_BookNum();

	void init_Book();

	void Show_Book();

	void Del_Book();

	int IsExist(string name);

	void Mod_Book();

	void Find_Book();

	void Sort_Book();

	void Clam_File();

	~BookManager();
};


BookManager.cpp

#include"BookManager.h"

BookManager::BookManager()
{
	//初始化属性
	ifstream ifs;
	ifs.open("系统.txt", ios::in);

	if (!ifs.is_open()) //文件不存在
	{
		cout << "文件不存在 \n";
		this->m_BookNum = 0;
		this->m_FileIsEmpty = true;
		this->m_BookArry = NULL;
		ifs.close();
		return;
	}

	//文件存在
	char ch;
	ifs >> ch;
	if (ifs.eof())//判断文件是否为空(返回真)
	{
		cout << "文件为空! \n";
		this->m_BookNum = 0;
		this->m_FileIsEmpty = true;
		this->m_BookArry = NULL;
		ifs.close();
		return;
	}

	//当文件存在且文件有内容
	int num = this->get_BookNum();
	cout << "图书人数为:" << num << endl;
	this->m_BookNum = num;

	//根据图书数创建数组
	//开辟空间
	this->m_BookArry = new Book * [this->m_BookNum];

	//文件数据放入数组中
	this->init_Book();

}

void BookManager::Add()
{
	cout << "请输入添加图书的数量: \n";

	int addNum = 0;

	cin >> addNum;
	if (addNum > 0)
	{
		//添加
		int newSize = this->m_BookNum + addNum;

		//开辟新空间

		Book** newSpace = new Book * [newSize];
		if (this->m_BookArry != NULL)
		{
			for (int i = 0; i < this->m_BookNum; ++i)
			{
				newSpace[i] = this->m_BookArry[i];
			}
		}

		//添加新数据

		for (int i = 0; i < addNum; ++i)
		{
			int id;
			string name;
			int dSelect;
			double price;
			int num;

			cout << "请输入第 " << i + 1 << " 个新图书的编号: \n";
			cin >> id;
			cout << "请输入第 " << i + 1 << " 个新图书名: \n";
			cin >> name;
			cout << "请选择该图书的类别:\n" 
				<< "1、教材 \n"
				<< "2、小说 \n"
				<< "3、科普 \n";
			cin >> dSelect;

			cout << "请输入第 " << i + 1 << " 个新图书的价格: \n";
			cin >> price;

			cout << "请输入第 " << i + 1 << " 个新图书的库存: \n";
			cin >> num;

			Book* Book = NULL;
			switch (dSelect)
			{
			case 1:
				Book = new Material(id, name, 1, price, num);
				break;
			case 2:
				Book = new Novel(id, name, 2, price, num);
				break;
			case 3:
				Book = new ScBook(id, name, 3, price, num);
				break;
			default:
				break;
			}
			//创建的指针保存在数组中
			newSpace[this->m_BookNum + i] = Book;
		}
		//释放原有的空间
		delete[]this->m_BookArry;

		//改变空间指向
		this->m_BookArry = newSpace;

		//更改图书不为空标志
		this->m_FileIsEmpty = false;

		//改变指针长度
		this->m_BookNum = newSize;

		//添加到文件中

		cout << "成功添加 " << addNum << " 名新图书 \n";

		this->Save();
	}
	else
	{
		cout << "输入数据有误! \n";
	}

	system("pause");
	system("cls");
}

void BookManager::BorrowOrReturn()
{
	if (this->m_FileIsEmpty)
	{
		cout << "文件不存在或记录为空! \n";
		system("pause");
		system("cls");
	}
	else
	{
		cout << "1.借书 \n";
		cout << "2.还书 \n";
		int select = 0;
		cin >> select;

		cout << "请输入书名: \n";
		string name;
		cin >> name;
		int ret = this->IsExist(name);
		if (ret != -1)
		{
			this->m_BookArry[ret]->showInfo();
			if (select == 1)
			{
				if (this->m_BookArry[ret]->m_num != 0)
				{
					this->m_BookArry[ret]->m_num--;
					cout << "借书成功! \n";
				}
				else
					cout << "该书已被借完! \n";
			}
			else if (select == 2)
			{
				this->m_BookArry[ret]->m_num++;
				cout << "还书成功! \n";
			}
			else
				cout << "错误操作! \n";
			this->m_BookArry[ret]->showInfo();
		}
		else
			cout << "该书不属于图书馆! \n";
	}
	this->Save();
	system("pause");
	system("cls");
}

void BookManager::Show_Menu()
{
	cout << "—————————————————— \n"
		<< "————欢迎使用图书管理系统———— \n"
		<< "————— 0.退出管理系统 ————— \n"
		<< "————— 1.增加图书信息 ————— \n"
		<< "————— 2. 借书/还书   ————— \n"
		<< "————— 3.显示图书信息 ————— \n"
		<< "————— 4.删除离库图书 ————— \n"
		<< "————— 5.修改图书信息 ————— \n"
		<< "————— 6.查找图书信息 ————— \n"
		<< "————— 7.按照编号排序 ————— \n"
		<< "————— 8.清空所有文档 ————— \n";
	cout << endl;
}

void BookManager::exitSystem()
{
	cout << "欢迎下次使用 \n";
	system("pause");
	exit(0);
	//不管是在哪个部分调用这个函数 退出程序 0 - 退出参数
}
BookManager::~BookManager()
{
	if (this->m_BookArry != NULL)
	{
		delete[]this->m_BookArry;
		this->m_BookArry = NULL;
	}
}

//保存文件
void BookManager::Save()
{
	ofstream ofs;
	ofs.open("系统.txt", ios::out);

	for (int i = 0; i < this->m_BookNum; ++i)
	{
		ofs << this->m_BookArry[i]->m_ID << " "
			<< this->m_BookArry[i]->m_name << " "
			<< this->m_BookArry[i]->m_DeptId << " "
			<< this->m_BookArry[i]->m_price << " "
			<< this->m_BookArry[i]->m_num << endl;
	}
	ofs.close();
}
//统计人数
int BookManager::get_BookNum()
{
	ifstream ifs;
	ifs.open("系统.txt", ios::in);

	int id;
	string name;
	int did;
	double price;
	int number;

	int num = 0;

	while (ifs >> id && ifs >> name && ifs >> did && ifs >> price && ifs >> number)
	{
		num++;
	}
	ifs.close();

	return num;
}

void BookManager::init_Book()
{
	ifstream ifs;
	ifs.open("系统.txt", ios::in);

	int id;
	string name;
	int did;
	double price;
	int number;

	int index = 0;
	while (ifs >> id && ifs >> name && ifs >> did && ifs >> price && ifs >> number)
	{
		Book* Book = NULL;
		if (did == 1)
			Book = new Material(id, name, did, price, number);
		else if (did == 2)
			Book = new Novel(id, name, did, price, number);
		else
			Book = new ScBook(id, name, did, price, number);
		this->m_BookArry[index] = Book;
		index++;
	}
	ifs.close();
}

void BookManager::Show_Book()
{
	if (this->m_FileIsEmpty)
		cout << "文件不存在或已为空! \n";
	else
	{
		for (int i = 0; i < m_BookNum; ++i)
		{
			this->m_BookArry[i]->showInfo();
		}
	}

	system("pause");
	system("cls");
}

int BookManager::IsExist(string name)
{
	int index = -1;

	for (int i = 0; i < m_BookNum; ++i)
	{
		if (this->m_BookArry[i]->m_name.find(name) != string::npos)
		{
			index = i;
			break;
		}
	}
	return index;
}

void BookManager::Del_Book() //数组数据前移(找到位置的后一位)
{
	if (this->m_FileIsEmpty)
		cout << "文件不存在或文件为空! \n";
	else
	{
		//按照图书的姓名删除
		cout << "请输入想要删除的图书名字: \n";
		string name = "0";
		cin >> name;

		int index = this->IsExist(name);

		if (index != -1)
		{
			//m_BookNum = 1时不进入循坏

			for (int i = 0; i < this->m_BookNum - 1; ++i)
			{
				//数据前移
				this->m_BookArry[i] = this->m_BookArry[i + 1];
			}
			//数量更新
			this->m_BookNum--;

			//数据同步到文件中
			this->Save();

			cout << "删除成功! \n";
		}
		else
			cout << "删除失败,未找到该图书! \n";
	}
	system("pause");
	system("cls");
}

void BookManager::Mod_Book()
{
	if (this->m_FileIsEmpty)
		cout << "文件不存在或记录为空! \n";
	else
	{
		cout << "请输入修改图书的名字: \n";
		string name = "0";
		cin >> name;

		int ret = this->IsExist(name);
		if (ret != -1)
		{
			//查找
			delete this->m_BookArry[ret];

			int newId = 0;
			string newName = "";
			int dSelect = 0;
			double price;
			int number;

			this->m_BookArry[ret]->showInfo();

			cout << "请输入新图书的编号: \n";
			cin >> newId;

			cout << "请输入新图书名: \n";
			cin >> newName;

			cout << "请选择该图书的类别: \n"
				<< "1、教材 \n"
				<< "2、小说 \n"
				<< "3、科普 \n";
			cin >> dSelect;

			cout << "请输入新图书的价格: \n";
			cin >> price;

			cout << "请输入新图书的库存: \n";
			cin >> number;

			Book* Book = NULL;
			switch (dSelect)
			{
			case 1:
				Book = new Material(newId, newName, dSelect, price, number);
			case 2:
				Book = new Novel(newId, newName, dSelect, price, number);
			case 3:
				Book = new ScBook(newId, newName, dSelect, price, number);
			default:
				break;
			}

			//更改数据到数组中
			this->m_BookArry[ret] = Book;

			cout << "修改成功! \n";

			//保存到文件中
			this->Save();
		}
		else
		{
			cout << "修改失败,查无此人 \n";
		}
	}

	system("pause");
	system("cls");
}

void BookManager::Find_Book()
{
	if (this->m_FileIsEmpty)
		cout << "文件不存在或记录为空! \n";
	else
	{
		cout << "请输入查找的方式: \n";
		cout << "1.按图书编号查找 \n";
		cout << "2.按图书姓名查找 \n";

		int select = 0;
		cin >> select;

		if (select == 2)
		{
			string name;
			cout << "请输入查找的图书姓名: \n";
			cin >> name;

			int ret = 1;
			ret = this->IsExist(name);

			if (ret != -1)
			{
				cout << "查找成功!该图书信息如下: \n";
				this->m_BookArry[ret]->showInfo();
			}
			else
				cout << "查找失败,查无此书 \n";
		}
		else if (select == 1)
		{
			int id = 0;
			cout << "请输入查找的图书编号: \n";
			cin >> id;

			bool flag = false;

			for (int i = 0; i < this->m_BookNum; ++i)
			{
				if (this->m_BookArry[i]->m_ID == id)
				{
					cout << "查找成功,图书编号为: "
						<< this->m_BookArry[i]->m_ID
						<< "号图书信息如下: \n";

					flag = true;

					this->m_BookArry[i]->showInfo();
				}
			}
			if (flag == false)
			{
				cout << "查找失败,查无此人! \n";
			}
		}
		else
			cout << "输入选项有误! \n";
	}

	system("pause");
	system("cls");
}

void BookManager::Sort_Book()
{
	if (this->m_FileIsEmpty)
	{
		cout << "文件不存在或记录为空! \n";
		system("pause");
		system("cls");
	}
	else
	{
		cout << "请选择排序方式: \n";
		cout << "1.按图书编号进行升序 \n";
		cout << "2.按图书编号进行降序 \n";

		int select = 0;
		cin >> select;

		for (int i = 0; i < m_BookNum; ++i)
		{
			int minOrMax = i;
			for (int j = i + 1; j < m_BookNum; ++j)
			{
				if (select == 1)//升序
				{
					if (m_BookArry[minOrMax]->m_ID > m_BookArry[j]->m_ID)
						minOrMax = j;
				}
				else  //降序
				{
					if (m_BookArry[minOrMax]->m_ID < m_BookArry[j]->m_ID)
						minOrMax = j;
				}
			}
			if (i != minOrMax)
			{
				Book* temp = m_BookArry[i];
				m_BookArry[i] = m_BookArry[minOrMax];
				m_BookArry[minOrMax] = temp;
			}
		}
		cout << "排序成功!排序后结果为: \n";

		this->Save();
		this->Show_Book();
	}
}

void BookManager::Clam_File()
{
	cout << "确认清空? \n";
	cout << "1. 确认 \n";
	cout << "2. 返回 \n";

	int select = 0;
	cin >> select;

	if (select == 1)
	{
		//trunc 如果存在文件 则删除并重建
		ofstream ofs("系统.txt", ios::trunc);
		ofs.close();

		//清空内容
		if (this->m_BookArry != NULL)
		{
			for (int i = 0; i < this->m_BookNum; ++i)
			{
				if (this->m_BookArry[i] != NULL)
					delete this->m_BookArry[i];
			}
			this->m_BookNum = 0;
			delete[]this->m_BookArry;
			this->m_BookArry = NULL;
			this->m_FileIsEmpty = true;
		}
		cout << "清空成功! \n";
	}
	system("pause");
	system("cls");
}

mian函数

#include<iostream>
using namespace std;
#include"BookManager.h"
#include"Book.h"
#include"Novel.h"

//提供功能接口
int main()
{
	BookManager book;
	int choice = 0;
	while (true)
	{
		book.Show_Menu();
		cout << "请输入您的选择:" << endl;
		cin >> choice;

		switch (choice)
		{
		case 0: //退出系统
			book.exitSystem();
			break;
		case 1: //添加图书
			book.Add();
			break;
		case 2:
			book.BorrowOrReturn();
			break;
		case 3: //显示图书
			book.Show_Book();
			break;
		case 4: //删除图书
			//测试 要封装
		//{int ret = wm.IsExist("1");
		//if (ret != -1)
		//	cout << "图书存在" << endl;
		//else
		//	cout << "图书不存在" << endl;
		//}
			book.Del_Book();
			break;
		case 5: //修改图书
			book.Mod_Book();
			break;
		case 6: //查找图书
			book.Find_Book();
			break;
		case 7://排序
			book.Sort_Book();
			break;
		case 8: //清空文件
			book.Clam_File();
			break;
		default:
			system("cls"); // !!!(输入其他的)刷新屏幕
			break;
		}
	}

	return 0;
}

图书分类

1、教材

Material.h

#pragma once
#include<iostream>
using namespace std;
#include<cstring>
#include"Book.h"

class Material : public Book
{
public:
	Material(int id, string name, int did ,double price,int num);

	void showInfo();

	string GetDeptName();
};


Material.cpp

#include"Material.h"

Material::Material(int id, string name, int did, double price, int num)
{
	this->m_ID = id;
	this->m_name = name;
	this->m_DeptId = did;
	this->m_price = price;
	this->m_num = num;
}

//显示个人信息
void Material::showInfo()
{
	cout << "图书编号: " << this->m_ID
		<< "\t 图书名:" << this->m_name
		<< "\t 图书分类: " << this->GetDeptName()
		<< "\t 图书价格:" <<this->m_price
		<<"\t 图书库存:"<< this->m_num << endl;
}

//获取类别名称
string Material::GetDeptName()
{
	return string("教材");
}

2、小说

Novel.h

#pragma once
#include<iostream>
using namespace std;
#include<cstring>
#include"Book.h"

class Novel : public Book
{
public:
	Novel(int id, string name, int did, double price, int num);

	void showInfo();

	string GetDeptName();
};

Novel.cpp

#include"Novel.h"

Novel::Novel(int id, string name, int did, double price, int num)
{
	this->m_ID = id;
	this->m_name = name;
	this->m_DeptId = did;
	this->m_price = price;
	this->m_num = num;
}

//显示个人信息
void Novel::showInfo()
{
	cout << "图书编号: " << this->m_ID
		<< "\t 图书名:" << this->m_name
		<< "\t 图书分类: " << this->GetDeptName()
		<< "\t 图书价格:" << this->m_price
		<< "\t 图书库存:" << this->m_num << endl;
}

//获取类别名称
string Novel::GetDeptName()
{
	return string("小说");
}

3、科普

ScBook.h

#pragma once
#include<iostream>
using namespace std;
#include<cstring>
#include"Book.h"


class ScBook : public Book
{
public:
	ScBook(int id, string name, int did, double price, int num);

	void showInfo();

	string GetDeptName();

};


ScBook.cpp

#include"ScBook.h"

ScBook::ScBook(int id, string name, int did, double price, int num)
{
	this->m_ID = id;
	this->m_name = name;
	this->m_DeptId = did;
	this->m_price = price;
	this->m_num = num;
}

//显示个人信息
void ScBook::showInfo()
{
	cout << "图书编号: " << this->m_ID
		<< "\t 图书名:" << this->m_name
		<< "\t 图书分类: " << this->GetDeptName()
		<< "\t 图书价格:" << this->m_price
		<< "\t 图书库存:" << this->m_num << endl;
}

//获取类别名称
string ScBook::GetDeptName()
{
	return string("科普");
}


注意

系统分头文件和cpp文件会使得整个系统可读性更高。

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值