C++:机房预约系统(四)——学生模块

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

//菜单界面
void Student::operMenu()
{
	cout << "欢迎同学" << this->m_Name << "登录" << endl;
	cout << "\t\t -----------------------------------\n";
	cout << "\t\t |                                  |\n";
	cout << "\t\t |            1 申请预约            |\n";
	cout << "\t\t |                                  |\n";
	cout << "\t\t |            2 查看自身预约        |\n";
	cout << "\t\t |                                  |\n";
	cout << "\t\t |            3 查看所有人预约      |\n";
	cout << "\t\t |                                  |\n";
	cout << "\t\t |            4 取消预约            |\n";
	cout << "\t\t |                                  |\n";
	cout << "\t\t |            0 注销登录            |\n";
	cout << "\t\t -----------------------------------\n";
	cout << "请输入您的选择:";
}

在这里插入图片描述


	//机房预约
	vector<ComputerRoom> vCom;

在这里插入图片描述

//有参构造  学号,姓名,密码
Student::Student(int id, string name, string pwd)
{
	//初始化属性
	this->m_Id = id;
	this->m_Name = name;
	this->m_Pwd = pwd;

	//初始化机房信息
	ifstream ifs;
	ifs.open(COMPUTER_FILE, ios::in);

	ComputerRoom com;
	while (ifs >> com.m_ComId && ifs >> com.m_MaxNum)
	{
		vCom.push_back(com);//将读取的信息放入到 容器中

		
	}
	ifs.close();

}
//申请预约
void Student::applyOrder()
{
	cout << "机房开放时间为周一至周五" << endl;
	cout << "请输入申请预约时间:" << endl;
	cout << " 1 周一" << endl;
	cout << " 2 周二" << endl;
	cout << " 3 周三" << endl;
	cout << " 4 周四" << endl;
	cout << " 5 周五" << endl;

	int date = 0;//日期
	int interval= 0;//时间段
	int room = 0;//机房编号

	while (true)
	{
		cin >> date;
		if (date >= 1 && date <= 5)
		{
			break;
		}
		cout << "输入有误,请重新输入:" << endl;
	}

	cout << "请输入时间段:" << endl;
	cout << " 1 上午" << endl;
	cout << " 2 下午" << endl;

	while (true)
	{
		cin >> interval;
		if (interval >= 1 && interval <= 2)
		{
			break;
		}
		cout << "输入有误,请重新输入:" << endl;

	}

	cout << "请选择机房" << endl;
	for (int i = 0; i < vCom.size(); i++)
	{
		cout << vCom[i].m_ComId << "号机房的容量为:" << vCom[i].m_MaxNum << endl;
	}

	while (true)
	{
		cin >> room;
		if (room >= 1 && room <= 3)
		{
			break;
		}
		cout << "输入有误,请重新输入:" << endl;
	}

	cout << "预约成功!审核中..." << endl;

	ofstream ofs;
	ofs.open(ORDER_FILE, ios::app);

	ofs << "date:" << date << " ";
	ofs << "interval:" << interval << " ";
	ofs << "StuId:" << this->m_Id << " ";
	ofs << "StuName:" << this->m_Name << " ";
	ofs << "roomId:" << room << " ";
	ofs << "status:" << 1 << endl;

	ofs.close();

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

}

在这里插入图片描述

#pragma once
#include<iostream>
#include"globalFile.h"
#include<map>
using namespace std;
#include<fstream>
#include<string>

class OrderFile
{
public:

	//构造函数
	OrderFile();

	//更新预约记录
	void updateOrder();

	//预约记录条数
	int m_size;

	//记录所有预约信息的内容 key记录条数 value 具体记录键值对信息
	map<int, map<string, string>>m_orderData;
};

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

//构造函数
OrderFile::OrderFile()
{
	ifstream ifs;
	ifs.open(ORDER_FILE, ios::in);

	string date;//日起
	string interval;//时间段
	string StuId;//学生编号
	string StuName;//姓名
	string roomId;//机房编号
	string status;//预约状态

	this->m_size = 0;//记录条数

	while (ifs>>date && ifs >> interval && ifs>>StuId && ifs 
		>> StuName && ifs >> roomId && ifs >> status )
	{
	/*	cout << date << endl;
		cout << interval << endl;
		cout << StuId << endl;
		cout << StuName << endl;
		cout << roomId << endl;
		cout << status << endl;*/
		//date:1111
		string key;
		string value;
		map<string, string>m;

		//截取日起
		int pos = date.find(":");//4
		if (pos != -1)
		{
			key = date.substr(0, pos);//从0 开始截取,截取4个,pos为截取个数
			value = date.substr(pos + 1, date.size() - pos - 1);//size = 9 pos = 4 size - pos = 5 - 1

			m.insert(make_pair(key, value));
		}

		//截取时间段
		pos = interval.find(":");
		if (pos != -1)
		{
			key = interval.substr(0, pos);
			value = interval.substr(pos + 1, interval.size() - pos - 1);

			m.insert(make_pair(key, value));
		}

		//截取学号
		pos = StuId.find(":");
		if (pos != -1)
		{
			key = StuId.substr(0, pos);
			value = StuId.substr(pos + 1, StuId.size() - pos - 1);

			m.insert(make_pair(key, value));
		}

		//截取姓名
		pos = StuName.find(":");
		if (pos != -1)
		{
			key = StuName.substr(0, pos);
			value = StuName.substr(pos + 1, StuName.size() - pos - 1);

			m.insert(make_pair(key, value));
		}

		//截取机房
		pos = roomId.find(":");
		if (pos != -1)
		{
			key = roomId.substr(0, pos);
			value = roomId.substr(pos + 1, roomId.size() - pos - 1);

			m.insert(make_pair(key, value));
		}

		//截取预约状态
		pos = status.find(":");
		if (pos != -1)
		{
			key = status.substr(0, pos);
			value = status.substr(pos + 1, status.size() - pos - 1);

			m.insert(make_pair(key, value));
		}
		//将小map放入大map中
		this->m_orderData.insert(make_pair(this->m_size, m));
		this->m_size++;
	}

	ifs.close();

}

在这里插入图片描述

//更新预约记录
void OrderFile::updateOrder()
{
	if (this->m_size == 0)
	{
		return;//预约记录0条,直接return 
	}

	ofstream ofs(ORDER_FILE, ios::out | ios::trunc);
	for (int i = 0; i < this->m_size; i++)
	{
		ofs << "date:" << this->m_orderData[i]["date"] << " ";
		ofs << "interval:" << this->m_orderData[i]["interval"] << " ";
		ofs << "StuId:" << this->m_orderData[i]["StuId"] << " ";
		ofs << "StuName:" << this->m_orderData[i]["StuName"] << " ";
		ofs << "roomId:" << this->m_orderData[i]["roomId"] << " ";
		ofs << "status:" << this->m_orderData[i]["status"] <<endl;
	}
	ofs.close();
	
}

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

在这里插入图片描述

//查看自身预约
void Student::showMyOrder()
{
	OrderFile of;
	if (of.m_size == 0)
	{
		cout << "暂无预约记录!" << endl;
		system("pause");
		system("cls");
		return;
	}

	for (int i = 0; i < of.m_size; i++)
	{
		//string 转int
		//stirng 利用 .c_str() 转const char *
		//利用atoi(const char *)转int
		if (this->m_Id == atoi(of.m_orderData[i]["StuId"].c_str()))//找到自身预约
		{
			cout << "预约日期:周" << of.m_orderData[i]["date"];
			cout << " 时间段:" << (of.m_orderData[i]["interval"] == "1" ? "上午" : "下午");
			cout << " 机房号:" << of.m_orderData[i]["roomId"];
			string status = " 状态: ";
			//1 审核中  2  已预约 -1 预约失败 0 取消预约
			if (of.m_orderData[i]["status"] == "1")
			{
				status += "审核中";
			}
			else if (of.m_orderData[i]["status"] == "2")
			{
				status += "预约成功";
			}
			else if (of.m_orderData[i]["status"] == "-1")
			{
				status += "预约失败,审核未通过";
			}
			else
			{
				status += "预约已取消";
			}
			cout << status << endl;
		}
	}

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


}

在这里插入图片描述


//查看所有预约
void Student::showAllOrder()
{
	OrderFile of;
	if (of.m_size == 0)
	{
		cout << "暂无预约记录!" << endl;
		system("pause");
		system("cls");
		return;
	}

	for (int i = 0; i < of.m_size; i++)
	{
		cout << i + 1 << " 、";
		cout << "预约日期:周" << of.m_orderData[i]["date"];
		cout << " 时间段:" << (of.m_orderData[i]["interval"] == "1" ? "上午" : "下午");
		cout << " 学号:" << of.m_orderData[i]["StuId"];
		cout << " 姓名:" << of.m_orderData[i]["StuName"];
		cout << " 机房号:" << of.m_orderData[i]["roomId"];
		string status = " 状态: ";
		//1 审核中  2  已预约 -1 预约失败 0 取消预约
		if (of.m_orderData[i]["status"] == "1")
		{
			status += "审核中";
		}
		else if (of.m_orderData[i]["status"] == "2")
		{
			status += "预约成功";
		}
		else if (of.m_orderData[i]["status"] == "-1")
		{
			status += "预约失败,审核未通过";
		}
		else
		{
			status += "预约已取消";
		}
		cout << status << endl;
	}
	system("pause");
	system("cls");
		

}

在这里插入图片描述
在这里插入图片描述
注意上图,张三同学预约了三次,现在他想取消第三次。
首先,对于张三同学来说,他一共有三条记录,从1开始计数,一共1,2,3条
然后,对于所有同学的记录来说,张三同学的记录在全部记录中的编号为0,1,3
那么,我们先从头开始遍历全部记录,一旦找到张三符合条件的记录,就把当前的序号放入vector中。一共三条,我们知道这三条记录在容器中的下标为0,1,2,对应张三的第1,2,3条记录,即第一条记录是vector中的第0号元素,我们直接将张三同学眼中的记录编号减去1,就得到vector中元素的编号了。
最后,我们通过vector中的元素编号,去间接的取全部记录中张三同学记录的编号。
vector={0,1,3})(遍历出的张三符合条件的记录编号)
第三个记录编号为3,即在全部记录中这条记录的编号为3,且对张三来书这是他的第三条记录,但这个3,在vector中的下标为2,即vector[2]可以取到这个3,从而通过这个3,取到在全部记录中的位置。

//取消预约
void Student::cancelOrder()
{
	OrderFile of;
	if (of.m_size == 0)
	{
		cout << "暂无预约记录!" << endl;
		system("pause");
		system("cls");
		return;
	}

	cout << "审核中或预约成功的记录可取消" << endl;

	vector<int>v;//存放在最大容器中的下标编号
	int index = 1;
	for (int i = 0; i < of.m_size; i++)
	{
		//先判断是自身学号
		if (this->m_Id == atoi(of.m_orderData[i]["StuId"].c_str()))
		{
			//再筛选状态 审核中获预约成功
			if (of.m_orderData[i]["status"] == "1" || of.m_orderData[i]["status"] == "2")
			{
				v.push_back(i);
				cout << index++ << "、";
				cout << "预约日期:周" << of.m_orderData[i]["date"];
				cout << " 时间段:" << (of.m_orderData[i]["interval"] == "1" ? "上午" : "下午");
				cout << " 机房号:" << of.m_orderData[i]["roomId"];
				string status = " 状态: ";
				if (of.m_orderData[i]["status"] == "1")
				{
					status += "审核中";
				}
				else if (of.m_orderData[i]["status"] == "2")
				{
					status += "预约成功";
				}
				cout << status << endl;

			}
		}
	}

	cout << "请输入取消记录,0代表返回" << endl;

	int select = 0;
	while (true)
	{
		cin >> select;

		if (select >= 0 && select <= v.size())
		{
			if (select == 0)
			{
				break;
			}
			else
			{
				of.m_orderData[v[select - 1]]["status"] = "0";
				of.updateOrder();//更新

				cout << "已取消预约" << endl;
				break;
			}
		}
		cout << "输入有误请重新输入" << endl;
	}
	system("pause");
	system("cls");
}


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值