圖書管理系統
效果圖:
内容
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檔案會使得整個系統可讀性更高。