C++大作业源文件(2)

1.Log.cpp

#include"Log.h"
#include<iostream>
#include<string>
using namespace std;


Log::Log()
{
	number = 0;
}

Log::~Log()
{

}

//注册账号,不过这里我们设置只能注册读者
void Log::rigister(User* pu)
{
	cout << "****************************************************************************" << endl;
	cout << "下面开始注册!" << endl;
	this->rig_success = 0;
	string info_n; //接收用户输入的姓名信息
	int info_a; //接收用户输入的账号信息
	string info_p1, info_p2; //接收用户前后两次输入密码信息
	while (true)
	{
		cout << "请输入用户名:" << endl;
		cin >> info_n;
		//判断该用户名是否已经存在
		for (int i = 0; i < User::User_num; i++)
		{
			if (pu[i].get_name() == info_n)
			{
				cout << "该用户名已经存在,您的输入无效!" << endl;
				return;
			}
		}
		cout << "请输入账号(一个整数):" << endl;
		cin >> info_a;
		//判断该账号是否存在
		for (int i = 0; i < User::User_num; i++)
		{
			if (pu[i].get_account() == info_a)
			{
				cout << "该账号已经存在,您的输入无效!" << endl;
				return;
			}
		}
		cout << "请输入密码:" << endl;
		cin >> info_p1;
		cout << "请再次输入密码:" << endl;
		cin >> info_p2;
		if (info_p1 != info_p2)
		{
			cout << "两次输入的密码不一致,密码无效!" << endl;
			return;
		}
		cout << "注册账号成功!" << endl;
		cout << "您当前的用户类型是读者。" << endl;
		cout << "************************************************************************" << endl;
		this->rig_success = 1;
		//当上述过程完毕,将新的用户添加到User数组中
		(pu + User::User_num)->change_account(info_a);
		(pu + User::User_num)->change_name(info_n);
		(pu + User::User_num)->set_pass(info_p1);  //不能用change_pass
		(pu + User::User_num)->change_mode(Reader); //默认只能注册读者
		this->number = User::User_num;
		User::User_num++;
		//然后改变本对象的信息
		this->us.change_account(info_a);
		this->us.change_name(info_n);
		this->us.change_mode(Reader);
		this->us.set_pass(info_p1);
		break;//注册成功后退出循环
	}
}

//账号登陆,读者和图书馆理员都能登陆
void Log::login(User* pu)
{
	this->Log_success = 0;
	cout << "***************************************************************************" << endl;
	cout << "下面开始用户登陆:" << endl;
	int info_a; //接收用户输入的账号信息
	string info_p; //接收用户输入密码信息
	cout << "请输入您的账号信息(一个整数):" << endl;
	cin >> info_a;
	cout << "请输入您的密码:" << endl;
	cin >> info_p;
	for (int i = 0; i < User::User_num; i++)
	{
		if (pu[i].get_account() == info_a)
		{
			if (pu[i].get_password() == info_p)
			{
				this->Log_success = 1; //表示登陆成功
				this->number = i;
				break;
			}
		}
	}
	if (this->Log_success == 1)
	{
		//改变登陆对象对应的User信息
		this->us.change_account((pu+this->number)->get_account());
		this->us.change_name((pu + this->number)->get_name());
		this->us.set_pass((pu + this->number)->get_password());
		this->us.change_mode((pu + this->number)->get_mode());

		cout << "登陆成功!" << endl;
		cout << "尊敬的" << this->us.get_name() << ",您好!" << endl;
		switch (this->us.get_mode())
		{
		case Reader:
			cout << "您的身份是读者。" << endl;
			break;
		case Librarian:
			cout << "您的身份是图书管理员。" << endl;
			break;
		default:
			break;
		}
	}
	else
	{
		cout<<"您输入的账号或者密码有误,请重新登陆!"<<endl;
	}
}

2.Reader.cpp

#include"Reader.h"
#include<iostream>
#include<string>
using namespace std;

//读者类的注册实现
void Reader_user::zc(User* u, Log lo, Reader_user* r)
{
	lo.rigister(u);
	cout << "注册成功!" << endl;
	u[lo.number].change_mode(Reader);
	r[Reader_user::Reader_num].change_name(lo.us.get_name());
	Reader_user::Reader_num++;
}

//构造函数实现
Reader_user::Reader_user(string name, int account, string password, User* pu)
{
	//处理本对象以及User数组
	this->book_lend = 0;
	this->name = name;
	this->account = account;
	this->password = password;
	pu[User::User_num].change_name(name);
	pu[User::User_num].change_account(account);
	pu[User::User_num].change_mode(Reader);
	pu[User::User_num].set_pass(password);
	User::User_num++;
}

//借书函数实现
void Reader_user::lend_book(book* B)
{
	cout << "***********下面开始借书程序**********" << endl;
	string info;  //接受读者输入书籍信息
	int booknumber;  //当前找的书在书籍库中的位置
	bool flag = false;  //如果可借则为true
	string confirm;   //再度确认是否借阅
	if (book::book_num == 0)
	{
		cout << "抱歉,图书馆当前没有图书!" << endl;
		return;
	}
	cout << "请输入需要借阅的图书名或者编号:" << endl;
	cin >> info;
	//寻找图书
	for (int i = 0; i < book::book_num; i++)
	{
		if (info == B[i].get_code() || info == B[i].get_name())
		{
			if (B[i].get_available() == false) continue;  //因为一种书可能有好几本,第一本被借走了还有第二本
			booknumber = i;
			flag = true;
			break;
		}
	}
	if (flag == true)		//如果找到了,先看是否可借,再确认是否借阅
	{
		if (B[booknumber].get_available() == true)
		{
			cout << "已找到当前图书,可借." << endl;
			cout << "请确认是否借阅(y or n):" << endl;
			cin >> confirm;
			if (confirm == "y")
			{
				B[booknumber].lend_or_return(false);  //表示这本书已经被借阅,别人不能再借
				cout << "您已经成功借阅了当前图书!" << endl;
				cout << "借书时间为" << B[booknumber].get_lendtime() << ",请按时归还。" << endl;
				//将这本书的信息添加到读者的借书库中
				this->borrow[this->book_lend].set_code(B[booknumber].get_code());
				this->borrow[this->book_lend].set_name(B[booknumber].get_name());
				this->borrow[this->book_lend].set_author(B[booknumber].get_author());
				this->borrow[this->book_lend].set_price(B[booknumber].get_price());
				this->borrow[this->book_lend].set_lend_time(B[booknumber].get_lendtime());
				this->borrow[this->book_lend].set_time(B[booknumber].get_time());
				this->borrow[this->book_lend].lend_or_return(false);
				this->book_lend++;
			}
			else if (confirm == "n")
			{
				cout << "您已经取消了借阅。" << endl;
			}
		}
		else
		{
			cout << "已找到当前图书,但是不可借!" << endl;
		}
	}
	else if (flag == false)
	{
		cout << "抱歉,图书馆未找到您输入的图书!" << endl;
	}
	return;

}

//还书函数实现
void Reader_user::return_book(book* B)
{
	cout << "**********下面开始还书**********" << endl;
	string info;  //接受读者输入书籍信息
	int booknumber;  //当前找的书在书籍库中的位置
	int booknumber_local = -1; //当前的书在读者借书库中的位置
	bool flag = false;  //如果图书在图书馆记录内则可还,flag为true
	string confirm;   //再度确认是否还书
	if (book::book_num == 0)
	{
		cout << "抱歉,图书馆当前没有图书!" << endl;
		return;
	}
	if (this->book_lend == 0)
	{
		cout << "您没有借书记录,不能还书!" << endl;
		return;
	}
	cout << "请输入需要还书的图书名或者编号:" << endl;
	cin >> info;
	//寻找图书在本人记录中的编号
	for (int i = 0; i < 10; i++)
	{
		if (info == borrow[i].get_code() || info == borrow[i].get_name())
		{
			booknumber_local = i;
			break;
		}
	}
	if (booknumber_local != -1)  //清除这本书在本人记录的信息
	{
		this->book_lend--;
		for (int j = booknumber_local; j < book_lend; j++)
		{
			borrow[j] = borrow[j + 1];
		}
	}
	else if (booknumber_local == -1)
	{
		cout << "您没有借过这本书或者这本书在图书馆中不存在!" << endl;
	}
	//寻找图书在图书馆的编号
	for (int i = 0; i < book::book_num; i++)
	{
		if (info == B[i].get_code() || info == B[i].get_name())
		{
			if (B[i].get_available() == true) continue; //因为一种书可能有好几本,第一本已经还了还有第二本
			booknumber = i;
			flag = true;
			break;
		}
	}
	if (flag == true)
	{
		B[booknumber].lend_or_return(true); //这本书可以借给别人看了
		cout << "还书成功!" << endl;
	}
	
}

void Reader_user::lend_show()
{
	cout << "您的借书信息为:" << endl;
	for (int i = 0; i < this->book_lend; i++)
	{
		cout << borrow[i];
	}
	cout << "您目前已经借了" << this->book_lend << "本书," << endl;
	cout << "您还可以借" << (10 - this->book_lend) << "本书。" << endl;
}

void Reader_user::book_show(book* B)
{
	cout << "下面是当前图书馆所有书的信息:" << endl;
	for (int i = 0; i < book::book_num; i++)
	{
		cout << B[i];
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值