前言
衔接上一篇“c++学习笔记-STL案例-机房预约系统4-管理员模块”,本文主要设计学生模块,从学生登录和注销、申请预约、显示预约、取消预约四个功能进行分析和实现。
目录
在student类的构造函数中,初始化学生信息,代码如下:
//有参构造 参数:学号、姓名、密码
Student::Student(int id, string name, string pwd)
{
//初始化属性
this->m_Id = id;
this->m_Name = name;
this->m_Pwd = pwd;
}
?添加位置如图:
? ? ? ? 申请预约
? ? ? ? 查看我的预约
? ? ? ? 查看所有预约
? ? ? ? 取消预约
? ? ? ? 注销登录
添加全局函数void studentMenu(Identity* &student):
//进入学生的子菜单
void studentMenu(Identity* &student)
{
while(true)
{
//调用学生的子菜单
student->operMenu();
//将父类指针 转为子类对象指针,调用子类的其他接口
Student* stu =(Student *) student; // (Student *)是强制转换为学生类
int select = 0;
cin >> select;//接收用户选择
if (select == 1) //申请预约
{
stu->applyOrder();
}
else if (select == 2) //查看自身预约
{
stu->showMyOrder();
}
else if (select == 3) //查看所以人预约
{
stu->showAllOrder();
}
else if (select == 4) //取消预约
{
stu->cancelOrder();
}
else
{
//注销登录
delete student;
cout << "注销成功" << endl;
system("pause");
system("cls");
return;
}
}
}
?添加位置如图:
在实现成员函数void Student::operMenu()代码如下:
//菜单界面
void Student::operMenu()
{
cout << "欢迎学生代表" << this->m_Name << "登录!" << endl;
cout << "\t\t -------------------------------------------------" << endl;
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 << "\t\t -------------------------------------------------" << endl;
cout << "请选择您的操作:" << endl;
}
??添加位置如图:
//进入学生身份的子菜单
studentMenu(person);
???添加位置如图:
登录成功测试:
?注销成功测试:
在student.h中添加新头文件和新的成员函数如下:
#include"computerRoom.h"
#include<vector>
//机房容器
vector<ComputerRoom> vCom;
在student.cpp学生的有参构造函数中追加如下代码:
//有参构造 参数:学号、姓名、密码
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();
}
???添加位置如图:
至此,vCom中保存了所有机房信息。
在student.cpp中实现成员函数void Student::applyOrder()
//申请预约
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");
}
????添加位置如图:
在order.txt文件中生成如下 内容:
map容器嵌套map容器,外层map容器可以值是第几条信息,内层map是每条信息具体内容(data、interval、stuId、stuName、roomId、status)据结构展示:
功能描述:显示预约记录时,需要从文件中获取到所以记录,用来显示,创建预约的类来管理记录以及更新
(1)在头文件以及源文件下分别创建orderFile.h和orderFile.cpp文件
(2)orderFile.h中添加如下代码:
#pragma once
#include<iostream>
using namespace std;
#include"globalFile.h"
#include<fstream>
#include<map>
class OrderFile
{
public:
//构造函数
OrderFile();
//更新预约记录
void updateOrder();
//记录预约条数
int m_Size;
//记录所有预约信息的容器 key 记录条数 value 记录键值对信息
map<int, map<string, string>> m_orderDate;
};
????添加位置如图:?
?(3)orderFile.cpp中添加如下代码:
#include "orderFile.h"
//构造函数
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;
//cout << endl;
//date:1111
string key;
string value;
map<string, string>m;
//截取日期
int pos = date.find(":");
if (pos != -1)
{
key = date.substr(0, pos);
value = date.substr(pos + 1, date.size() - pos - 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_orderDate.insert(make_pair(this->m_Size, m));
m_Size++;
}
//测试大map
for (map<int, map<string, string>>::iterator it = m_orderDate.begin(); it != m_orderDate.end(); it++)
{
cout << "条数为:" << it->first << " value = " << endl;
for (map<string, string>::iterator mit = (*it).second.begin(); mit != it->second.end(); mit++)
{
cout << " key = " << mit->first << " value = " << mit->second << " ";
}
cout << endl;
}
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_orderDate[i]["date"] << " ";
ofs << "interval" << this->m_orderDate[i]["interval"] << " ";
ofs << "stuId" << this->m_orderDate[i]["stuId"] << " ";
ofs << "stuName" << this->m_orderDate[i]["stuName"] << " ";
ofs << "roomId" << this->m_orderDate[i]["roomId"] << " ";
ofs << "status" << this->m_orderDate[i]["status"] << endl;
}
}
(1)首先我们先添加几条预约记录,order.txt文件内容如下,四条预约信息:
(2)在Student类的void Student::showOrder()成员函数中,添加如下代码:
//查看我的预约
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
//string 利用.c_str() 转const char *
//利用 atoi(const char *) 转int
if (this->m_Id == atoi(of.m_orderDate[i]["stuId"].c_str()))//找到自身预约
{
cout << "预约日期:周" << of.m_orderDate[i]["date"] << " ";
cout << "时间段:" << (of.m_orderDate[i]["interval"] == "1" ? "上午" : "下午") << " ";
cout << "机房号:" << of.m_orderDate[i]["roomId"] << " ";
string status = "状态:";
//1 审核中 2 已预约 -1 预约失败 0 取消预约
if (of.m_orderDate[i]["status"] == "1")
{
status += "审核中";
}
else if (of.m_orderDate[i]["status"] == "2")
{
status += "预约成功";
}
else if (of.m_orderDate[i]["status"] == "-1")
{
status += "预约失败,审核未通过";
}
else
{
status += "预约已取消";
}
cout << status << endl;
}
else
{
cout << "无预约记录!" << endl;
system("pause");
system("cls");
return;
}
}
system("pause");
system("cls");
}
????添加位置如图:?
其中将string 转int:
?? ??? ?第一步:string 利用.c_str() 转const char *
? ? ? ? 第二步:利用 atoi(const char *) 转int?
(3)测试结果
张三查看自己的预约:
李四查看自己的预约:?
王五(并无预约记录)查看自己的预约:?
(1)在Student类的void Student::showAllOrder()成员函数中,添加如下代码:
//查看所有预约
void Student::showAllOrder()
{
OrderFile of;
if (of.m_Size == 0)
{
cout << "无预约记录" << endl;
system("pause");
system("cls");
}
for (int i = 0; i < of.m_Size; i++)
{
cout << i + 1 << "、 ";
cout << "预约日期:周" << of.m_orderDate[i]["date"] << " ";
cout << "时段:" << (of.m_orderDate[i]["interval"]=="1"?"上午":"下午") << " ";
cout << "学号:" << of.m_orderDate[i]["stuId"] << " ";
cout << "姓名:" << of.m_orderDate[i]["stuName"] << " ";
cout << "机房号" << of.m_orderDate[i]["roomId"] << " ";
string status = "状态:";
//1 审核中 2 已预约 -1 预约失败 0 取消预约
if (of.m_orderDate[i]["status"] == "1")
{
status += "审核中";
}
else if (of.m_orderDate[i]["status"] == "2")
{
status += "预约成功";
}
else if (of.m_orderDate[i]["status"] == "-1")
{
status += "预约失败,审核未通过";
}
else
{
status += "取消预约";
}
cout << status << endl;
}
system("pause");
system("cls");
}
????添加位置如图:?
(2)测试结果
?登录学生代表张三账号后,查看所有预约如下:
筛选出该学号的预约→筛选出状态为“审核中”或“预约成功”的预约→记录符合条件预约的index于vector<int>v中→根据用户选择select-1取得实际下标→更改选中下标的状态为“0”取消预约
在Student类的void Student::cancelOrder()成员函数中,添加如下代码:
//取消预约
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_orderDate[i]["stuId"].c_str()))
{
//再筛选状态 审核中或预约成功
if (of.m_orderDate[i]["status"] == "1" || of.m_orderDate[i]["status"] == "2")
{
v.push_back(i);
cout << index++ << "、 ";
cout << "预约日期:周" << of.m_orderDate[i]["date"]<<" ";
cout << "时间段:" << (of.m_orderDate[i]["interval"] == "1" ? "上午" : "下午") << " ";
cout << "机房号:" << of.m_orderDate[i]["roomId"] << " ";
string status = "状态:";
if (of.m_orderDate[i]["status"] == "1")
{
status += "审核中";
}
else if (of.m_orderDate[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_orderDate[v[select - 1]]["status"] = "0";
of.updateOrder();
cout << "已经取消预约" << endl;
break;
}
}
cout << "输入有误,请重新输入" << endl;
}
system("pause");
system("cls");
}
?????添加位置如图:?
查看张三的预约有如下:
选择取消预约功能:
查看预约,取消张三的第四条预约成功:
?