基于c++面向对象的简单小型图书管理系统

Book.h

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

class Book {
private:
	string BookName;
	string ISBN = "12345678";
	string AuthorName;
	string publisher;
	double value;
	int number = 1;
public:
	Book();
	Book(string ISBN, string BookName, string AuthorName, string publisher,double value);
	~Book();
	string GetBookname();
	string GetISBN()const;
	string GetAuthorname();
	string GetPublisher();
	int GetNumber();
	double GetValue();
	void SetNumber1();
	void SetNumber2();
	void SetBookName(string s);
	void SetISBN(string s);
	void SetAuthorName(string s);
	void SetPublisher(string s);
	void SetValue(double v);
};

Book.cpp

#include "Book.h"

Book::Book() {

}
Book::Book(string ISBN, string BookName, string AuthorName, string publisher,double value) {
	this->ISBN = ISBN;
	this->BookName = BookName;
	this->AuthorName = AuthorName;
	this->publisher = publisher;
	this->value = value;
}
Book::~Book() {

}
string Book::GetBookname(){
	return this->BookName;
};
string Book::GetISBN() const {
	return this->ISBN;
};
string Book::GetAuthorname(){
	return this->AuthorName;
};
string Book::GetPublisher(){
	return this->publisher;
};
int Book::GetNumber(){
	return this->number;
};
double Book::GetValue() {
	return this->value;
}
void Book::SetNumber1() {
	this->number++;
}
void Book::SetNumber2() {
	this->number-=1;
}

void Book::SetBookName(string s)
{
	this->BookName = s;
}

void Book::SetISBN(string s)
{
	this->ISBN = s;
}

void Book::SetAuthorName(string s)
{
	this->AuthorName = s;
}

void Book::SetPublisher(string s)
{
	this->publisher = s;
}

void Book::SetValue(double v)
{
	this->value = v;
}

 Library.h

#include <string>
#include <algorithm>
#include <vector>
#include <map>
#include "Book.h"
#include "LibraryManager.h"
#include <fstream>
using namespace std;
extern vector<Book>library;
extern string paixu[];
class Library {
public:
	void Insert(Book a);
	void Delete(Book a);
	void Update(Book a);
	static bool cmp(const Book& a,const Book& b);
	void Search(Book a);
	bool Search(string BookName);
	Book Search1(string BookName);
	bool Search2(string ISBN);
};

Library.cpp(对图书馆的操作)

#include "Library.h"
vector<Book>library;
void Library::Insert(Book a) {
	if (Search(a.GetBookname())) {
		for (auto it = library.begin(); it < library.end(); it++) {
			if (it->GetBookname() == a.GetBookname()) {
				it->SetNumber1();
			}
		}
	}
	else {
		library.push_back(a);
	}
}

void Library::Delete(Book a) {
	for (auto it = library.begin(); it != library.end();) {
		if (it->GetBookname() == a.GetBookname()) {
			it->SetNumber2();
			if (it->GetNumber() == 0) {
				it = library.erase(it);
			}
			else {
				it++;
			}
		}
		else {
			it++;
		}
	}
}

bool Library::Search(string BookName) {
	if (library.empty()) {
		return false;
	}
	else{
		sort(library.begin(), library.end(), cmp);
		for (auto it = library.begin(); it < library.end(); it++) {
			if (it->GetBookname() == BookName) {
				return true;
			}
		}//顺序查找
		return false;
	}
}

Book Library::Search1(string BookName) {
	sort(library.begin(), library.end(), cmp);
	for (auto it = library.begin(); it < library.end(); it++) {
		if (it->GetBookname() == BookName) {
			return *it;
		}
	}//顺序查找
}

void Library::Search(Book a) {
	for (auto it = library.begin(); it < library.end(); it++) {
		if (it->GetBookname() == a.GetBookname()) {
			cout << "现暂存该书本数为:" << it->GetNumber() << "本" << endl;
			return;
		}
	}//顺序查找
}

bool Library::Search2(string ISBN) {
	sort(library.begin(), library.end(), cmp);
	for (auto it = library.begin(); it < library.end(); it++) {
		if (it->GetISBN () == ISBN) {
			return true;
		}
	}//顺序查找
	return false;
}


void Library::Update(Book a){
	for (auto it = library.begin(); it < library.end(); it++) {
		if (it->GetISBN() == a.GetISBN()) {
			if (it->GetAuthorname()!=a.GetAuthorname()) {
				it->SetAuthorName(a.GetAuthorname());
			}
			if (it->GetBookname() != a.GetBookname()) {
				it->SetBookName(a.GetBookname());
			}
			if (it->GetPublisher() != a.GetPublisher()) {
				it->SetPublisher(a.GetPublisher());
			}
			if (it->GetValue() != a.GetValue()) {
				it->SetValue(a.GetValue());
			}
		}
	}
}

bool Library::cmp(const Book& a,const Book& b) {
	return a.GetISBN() < b.GetISBN();
}

LibraryManager.h

#pragma once
#include <iostream>
#include <string>
#include <iomanip>
#include <sstream>
using namespace std;
class Manager {
public:
	Manager();
	void Showmenu();
	~Manager();
	void Showmenu0();
	void Showmenu1();
	void Showmenu2();
	void Showmenu3();
	void Showmenu4();
	void InitLibrary();
	void StoreLibrary();
};

LibraryManager.cpp(主要的控制层面,用户与代码交互)

#include "LibraryManager.h"
#include "Library.h"

string paixu[30] = {" ","A","B","C","D","E","F","G","H","I","J"};

Manager::Manager() {

}

void Manager::Showmenu() {
	cout << "***********1.总览可借书籍**********" << endl;
	cout << "***********2.添加可借书籍**********" << endl;
	cout << "***********3.查找可借书籍**********" << endl;
	cout << "***********4.借阅可借书籍**********" << endl;
	cout << "***********5.更新可借书籍**********" << endl;
	cout << "*********6.退出图书管理系统********" << endl;
}//总目录

Manager::~Manager() {

}

void Manager::Showmenu1() {
	string BookName,ISBN,AuthorName,publisher;
	double value;
	cout << "请输入你要添加图书的名称:" << endl;
	cin >> BookName;
	/*cout << "请输入你要添加图书的ISBN号:" << endl;
	cin >> ISBN;*/
	cout << "请输入你要添加图书的作者:" << endl;
	cin >> AuthorName;
	cout << "请输入你要添加图书的出版社:" << endl;
	cin >> publisher;
	cout << "请输入你要添加图书的价格:" << endl;
	cin >> value;//若输入不是数字会出错
	ISBN = paixu[library.size()%10+1] + "_12345678_";
	ISBN += to_string(library.size()+1);
	Book book(ISBN ,BookName, AuthorName, publisher, value);
	Library l;
	l.Insert(book);
	cout << "添加成功!" << endl;
	system("pause");
	system("cls");
}//插入操作目录

void Manager::Showmenu2() {
	string BookName;
	cout << "请输入你要查找图书的名称:" << endl;
	cin >> BookName;
	Library l;
	if (l.Search(BookName)) {
		Book book = l.Search1(BookName);
		l.Search(book);
	}
	else {
		cout << "该藏书系统无该书" << endl;
	}
	system("pause");
	system("cls");
}//查找操作目录

void Manager::Showmenu3() {
	string BookName;
	cout << "请输入你要借阅图书的名称:" << endl;
	cin >> BookName;
	Library l;
	if (l.Search(BookName)) {
		Book book = l.Search1(BookName);
		l.Delete(book);
		cout << "借阅成功!" << endl;
	}
	else {
		cout << "无法借阅该书" << endl;
	}
	system("pause");
	system("cls");
}//借阅图书目录

void Manager::Showmenu4() {
	string BookName, ISBN, AuthorName, publisher;
	double value;
	cout << "请输入你要更新图书的ISBN号:" << endl;
	cin >> ISBN;
	Library l;
	if (l.Search2(ISBN)) {
		cout << "请输入该图书新名称:" << endl;
		cin >> BookName;
		cout << "请输入该图书的新作者:" << endl;
		cin >> AuthorName;
		cout << "请输入该图书的新出版社:" << endl;
		cin >> publisher;
		cout << "请输入该图书的新价格:" << endl;
		cin >> value;
		Book book(ISBN, BookName, AuthorName, publisher, value);
		l.Update(book);
	}
	else {
		cout << "该藏书系统没有该书,无法完成更新操作!" << endl;
	}
	system("pause");
	system("cls");
}

void Manager::Showmenu0() {
	int num = library.size();
	if (num != 0) {
		cout << "图书名称" << setw(20)<< "图书ISBN号" << setw(20) << "图书作者" << setw(20) << "图书出版社" << setw(20) << "图书价格" << endl;
	}
	else {
		cout << "当前无可借阅图书" << endl;
	}
	for (auto it = library.begin(); it < library.end(); it++) {
		cout << it->GetBookname() << setw(20) << it->GetISBN() << setw(20) << it->GetAuthorname() << setw(20) << it->GetPublisher() << setw(20) << it->GetValue() << endl;
	}
	system("pause");
	system("cls");
}//总览目录

void Manager::InitLibrary() {
	/*Library l;
	Book book1("A_12345678_1","三体", "刘慈欣", "重庆出版社", 40);
	l.Insert(book1);
	Book book2( "A_12345678_1","三体", "刘慈欣", "重庆出版社", 40);
	l.Insert(book2);
	Book book3("B_12345678_2", "活着", "余华", "长江文艺出版社", 35);
	l.Insert(book3);*/
	string BookName, ISBN, AuthorName, publisher;
	double value;
	ifstream ifs("data.txt");
	if (!ifs.is_open()) {
		cout << "文件打开失败!" << endl;
	}
	else {
		string line;
		while (getline(ifs, line)) {
			istringstream s(line);
			s >> BookName >> ISBN >> AuthorName >> publisher >> value;
			Book book(ISBN, BookName, AuthorName, publisher, value);
			Library l;
			l.Insert(book);
		}
	}
	ifs.close();
}


void Manager::StoreLibrary() {
	ofstream ofs("data.txt");
	if (!ofs.is_open()) {
		cout << "文件打开失败!" << endl;
	}
	else {
		for (auto it = library.begin(); it != library.end(); it++) {
			ofs << it->GetBookname() << " " << it->GetISBN() << " " << it->GetAuthorname() << " " << it->GetPublisher() << " " << it->GetValue() << endl;
		}
	}
	ofs.close();
}

main.cpp(主函数)

#include<iostream>
#include "LibraryManager.h"
#include "Library.h"
#include <vector>
#include <map>
using namespace std;
int main() {
	Manager manager;
	vector<Book>library;
	manager.InitLibrary();
	int choice;
	while (1) {
		manager.Showmenu();
		cout << "请输入你的选择:" << endl;
		cin >> choice;
		switch (choice) {
			case 1:
				manager.Showmenu0();
				break;
			case 2:
				manager.Showmenu1();
				break;
			case 3:
				manager.Showmenu2();
				break;
			case 4:
				manager.Showmenu3();
				break;
			case 5:
				manager.Showmenu4();
				break;
			case 6:
				cout << "欢迎下次使用!" << endl;
				exit(0);
				break;
			default:
				system("cls");
				cout << "请重新输入:" << endl;
				break;
		}
		manager.StoreLibrary();
	}
}
//测试数据
//三体 A_12345678_1 刘慈欣 重庆出版社 40
// 三体 A_12345678_1 刘慈欣 重庆出版社 40
//活着 B_12345678_2 余华 长江文艺出版社 35

注意自己创建data.txt文件(资源文件,注意该文件位置与代码中打开的位置)

(一天半肝完,还有很多bug,后续可能更新一个用数据库代替文件IO流,并且推出更多的功能)

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

。。391

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值