一、简介
看了两个不同的C++培训课程,最后的实习程序都是这个项目。不看老师的讲解视频,自己试着写了一下,在写的过程中发现当项目需求不固定时,会越写越乱,总是想要添加更高的需求,导致写了删,删了写。还好没有放弃,全部拿下了。而且,获益良多。自我感觉这个系统功能还是很强大的,包括输入数据的正确性检查,输入过程中的返回等。
1、管理员和普通用户分别登陆
2、管理员可对普通用户、会议室、所有预约进行维护
3、普通用户只有删插增改自己的预约
4、使用文件储存数据
二、知识点
封装:封装的思想更加成熟,UI界面和数据完全隔离,只能通过管理员或者用户调用数据中心的方法实现访问;其次,UI界面之间的并列或者层级关系更清晰了,利用返回值控制返回界面。
继承:管理员类继承了用户类,基础功能在用户类中实现,管理员类直接调用。
多态:同一种功能函数,满足不同需求,利用虚函数实现了多态。
文件读写:文件读写时,基础数据类型可以直接读写;非基础数据类型需要自行实现读写方法;容器不能直接读写,需要逐一读写每个元素。
时间:time_t 和tm 两种时间格式的转换。
以及对组作为返回值的便利,容器的排序函数对象和查找函数对象需要一致等等。
三、上代码
代码比较多,还好主要注释都写了。欢迎大佬批评指正,欢迎同学交流学习。
OAUtils.h 提供了数字读取、字符串读取功能,提供文件读写字符串功能
#pragma once
#include<iostream>
using namespace std;
#include<string>
#include<fstream>
class OAUtils
{
public:static int getNum();static string getStr();static void save_str(ofstream& ofs,string& str);static void read_str(ifstream& ifs, string& str);
};
OAUtils.cpp
#include "OAUtils.h"int OAUtils::getNum()
{int res = -1;cin >> res;while (cin.fail()) {cin.clear();cin.ignore(numeric_limits<streamsize>::max(), '\n');cin >> res;}return res;
}string OAUtils::getStr()
{string str;cin >> str;while (cin.fail() || str.size() > 16 || str.size() == 0) {cin.clear();cin.ignore(numeric_limits<streamsize>::max(), '\n');cout << "输入格式有误,请重新输入:";cin >> str;}return str;
}void OAUtils::save_str(ofstream & ofs, string & str)
{size_t size = str.size();ofs.write((char*) &(size), sizeof(size));for (auto it = str.begin(); it != str.end(); it++) {ofs.write((char*)&(*it), sizeof(*it));}
}void OAUtils::read_str(ifstream & ifs, string & str)
{size_t size = 0;ifs.read((char*)&size, sizeof(size));str.resize(size);for (auto it = str.begin(); it != str.end(); it++) {ifs.read((char*)&(*it), sizeof(*it));}
}
OAsystemUI.h 提供的所有界面函数,利用枚举标记返回界面
#pragma once
#include"OADataCenter.h"
//UI界面flag
enum Page {Exit, //退出MainPage, //主界面LoginPageAdmin, //管理员登录界面LoginPageUser, //用户登录界面AdminPage, //管理员界面UserPage, //用户界面AddUserPage, //添加用户界面——管理员DelUserPage, //删除用户界面——管理员AddRoomPage, //添加会议室界面——管理员DelRoomPage, //删除会议室界面——管理员AdminAddAppPage, //管理员添加预约UserAddAppPage, //用户添加预约AdminDelAppPage, //管理员删除预约UserDelAppPage, //用户删除预约CleAppPage, //清空所有预约——管理员
};
//UI系统
class OAsystemUI {
public:static int displayMainPage(); //主界面函数static int displayLoginPage(AdminData** ptr_admin); //管理员登录界面函数static int displayLoginPage(UserData** ptr_user); //用户登录界面函数static int LoginPage(string& name, string& password); //登陆界面函数,主要用于获取name,passwordstatic int displayAdminPage(AdminData* ptr_admin); //管理员界面函数static int displayUserPage(UserData* ptr_user); //用户界面函数static int displayAddUserPage(AdminData* ptr_admin); //添加用户界面函数——管理员static int displayDelUserPage(const AdminData* ptr_admin); //删除用户界面函数——管理员static int displayAddRoomPage(AdminData* ptr_admin); //添加会议室界面函数——管理员static int displayDelRoomPage(AdminData* ptr_admin); //删除会议室界面函数——管理员static int displayAddAppPage(AdminData* ptr_admin); //管理员添加预约static int displayAddAppPage(UserData* ptr_user); //用户添加预约static int AddAppPage(UserData* ptr_data); //预约界面static int displayDelAppPage(AdminData* ptr_admin); //管理员删除预约static int displayDelAppPage(UserData* ptr_user); //用户删除预约static void DelAppPage(UserData* pt_data); //删除预约static int displayCleAppPage(AdminData* ptr_admin); //清空所有预约界面函数——管理员
};
OAsystemUI.cpp 所有界面函数的实现
#include "OAsystemUI.h"
#include<iostream>
using namespace std;
#include"OAUtils.h"
#include<string>
#include"OADataCenter.h"//主界面
int OAsystemUI::displayMainPage()
{int Pageflag = MainPage;int choice = -1;cout << "****欢迎使用会议室预约系统****" << endl;cout << "******************************" << endl;cout << " 1.管理员登录" << endl;cout << " 2.普通用户登录" << endl;cout << " 0.退出系统" << endl;cout << "******************************" << endl;cout << "请输入您的选择:";choice = OAUtils::getNum();enum MainChoice { exit = 0, adminlogin, userlogin };switch (choice) {case MainChoice::exit:cout << "欢迎下次使用" << endl;Pageflag = Exit;system("pause");break;case MainChoice::adminlogin:Pageflag = LoginPageAdmin;break;case MainChoice::userlogin:Pageflag = LoginPageUser;break;default:cout << "输入错误,请重新输入" << endl;system("pause");Pageflag = MainPage;break;}return Pageflag;
}
//管理员登录界面
int OAsystemUI::displayLoginPage(AdminData** ptr_admin)
{cout << "**********管理员登录**********" << endl;string name, password, depart;int pageflag = LoginPage(name, password);if (pageflag != Page::MainPage) {AdminData admin(name, password, "管理员");*ptr_admin = admin.Login();if (*ptr_admin) {pageflag = Page::AdminPage;}else {cout << "登陆失败,请重新登陆" << endl;system("pause");pageflag = Page::LoginPageAdmin;}}return pageflag;
}
//用户登录界面
int OAsystemUI::displayLoginPage(UserData ** ptr_user)
{cout << "***********用户登录***********" << endl;string name, password, depart;int pageflag = LoginPage(name, password);if (pageflag != Page::MainPage) {UserData user(name, password, depart);*ptr_user = user.Login();if (*ptr_user) {pageflag = Page::UserPage;}else {cout << "登陆失败,请重新登陆" << endl;system("pause");pageflag = Page::LoginPageUser;}}return pageflag;
}
//登陆界面,用于获取name和passwo,由管理员和用户登录界面调用
int OAsystemUI::LoginPage(string& name, string& password)
{int pageflag = Page::MainPage;cout << "******************************" << endl;cout << "请输入您的账号(输入0返回上级):";name = OAUtils::getStr();if (name != "0") {cout << "请输入您的密码(输入0返回上级):";password = OAUtils::getStr();if (password != "0") {pageflag = -1;}}return pageflag;
}
//管理员界面
int OAsystemUI::displayAdminPage(AdminData* ptr_admin)
{int choice = -1, pageflag = Page::AdminPage;cout << "************管理员************" << endl;cout << "******************************" << endl;cout << " 1.添加用户" << endl;cout << " 2.删除用户" << endl;cout << " 3.添加会议室" << endl;cout << " 4.删除会议室" << endl;cout << " 5.添加预约" << endl;cout << " 6.查看预约" << endl;cout << " 7.删除预约" << endl;cout << " 8.清空预约" << endl;cout << " 0.退出登录" << endl;cout << "******************************" << endl;cout << "请输入您的选择:";choice = OAUtils::getNum();enum AdminChoice { Exit, Adduser, Deluser, Addroom, Delroom, Addapp, Showapp, Delapp, Cleapp };switch (choice) {case Exit:pageflag = Page::MainPage;break;case Adduser:pageflag = Page::AddUserPage;break;case Deluser:pageflag = Page::DelUserPage;break;case Addroom:pageflag = Page::AddRoomPage;break;case Delroom:pageflag = Page::DelRoomPage;break;case Addapp:pageflag = Page::AdminAddAppPage;break;case Showapp:ptr_admin->showApp();system("pause");pageflag = Page::AdminPage;break;case Delapp:pageflag = Page::AdminDelAppPage;break;case Cleapp:pageflag = Page::CleAppPage;break;}return pageflag;
}
//用户界面
int OAsystemUI::displayUserPage(UserData* ptr_user)
{int choice = -1, pageflag = Page::UserPage;cout << "*************用户*************" << endl;cout << "******************************" << endl;cout << " 1.添加预约" << endl;cout << " 2.删除预约" << endl;cout << " 3.查看预约" << endl;cout << " 0.退出登录" << endl;cout << "******************************" << endl;cout << "请输入您的选择:";choice = OAUtils::getNum();enum { Exit, AddApp, DelApp, ShowApp };switch (choice) {case Exit:pageflag = Page::MainPage;break;case AddApp:pageflag = Page::UserAddAppPage;break;case DelApp:pageflag = Page::UserDelAppPage;break;case ShowApp:ptr_user->showApp();pageflag = Page::UserPage;break;}return pageflag;
}
//添加用户界面——管理员
int OAsystemUI::displayAddUserPage(AdminData* ptr_admin)
{cout << "******管理员——添加用户******" << endl;cout << "******************************" << endl;cout << "请输入要添加的用户名(输入0返回上级):";string name = OAUtils::getStr();while (name != "0"&&ptr_admin->checkName(name)) {cout << "该用户名已被占用,请重新输入:";name = OAUtils::getStr();}if (name != "0") {cout << "请输入密码(输入0返回上级):";string password = OAUtils::getStr();if (password != "0") {cout << "请输入部门(输入0返回上级):";string depart = OAUtils::getStr();if (depart != "0") {ptr_admin->AddUser(UserData(name, password, depart));cout << "添加用户成功" << endl;system("pause");}}}return Page::AdminPage;
}
//删除用户界面——管理员
int OAsystemUI::displayDelUserPage(const AdminData * ptr_admin)
{cout << "******管理员——删除用户******" << endl;cout << "******************************" << endl;if (ptr_admin->showUser()) {cout << "请输入要删除的用户名(输入0返回上级):";string name = OAUtils::getStr();while (name != "0"&&!ptr_admin->checkName(name)) {cout << "该用户不存在,请重新输入(输入0返回上级):";name = OAUtils::getStr();}if (name != "0") {cout << "请输入管理员密码确定删除(输入0取消):";string password = OAUtils::getStr();while (password != "0"&&password != ptr_admin->getPasw()) {cout << "管理员密码错误,请重新输入(输入0取消):";password = OAUtils::getStr();}if (password != "0") {ptr_admin->DelUser(name);cout << "删除用户成功" << endl;}}}system("pause");return Page::AdminPage;
}
//添加会议室界面——管理员
int OAsystemUI::displayAddRoomPage(AdminData * ptr_admin)
{cout << "*****管理员——添加会议室*****" << endl;cout << "******************************" << endl;cout << "请输入会议室门牌号:";int id = OAUtils::getNum();if (ptr_admin->AddMeetingRoom(id)) {cout << "会议室添加成功" << endl;}else {cout << "该会议室已存在" << endl;}system("pause");return Page::AdminPage;
}
//删除会议室界面——管理员
int OAsystemUI::displayDelRoomPage(AdminData * ptr_admin)
{cout << "*****管理员——删除会议室*****" << endl;cout << "******************************" << endl;if (ptr_admin->showRoom()) {cout << "请输入会议室门牌号:";int id = OAUtils::getNum();if (ptr_admin->DelMeetingRoom(id)) {cout << "会议室删除成功" << endl;}else {cout << "该会议室不存在" << endl;}}system("pause");return Page::AdminPage;
}
//管理员添加预约界面
int OAsystemUI::displayAddAppPage(AdminData* ptr_admin)
{int choice = -1, pageflag = Page::AdminPage;cout << "**********管理员预约**********" << endl;cout << "******************************" << endl;AddAppPage(ptr_admin);return pageflag;
}
//用户添加预约界面
int OAsystemUI::displayAddAppPage(UserData * ptr_user)
{int choice = -1, pageflag = Page::UserPage;cout << "***********用户预约***********" << endl;cout << "******************************" << endl;AddAppPage(ptr_user);return pageflag;
}
//预约界面
int OAsystemUI::AddAppPage(UserData * ptr_data)
{tm t;cout << "请输入您要预约的年份(输入-1返回上级):";t.tm_year = OAUtils::getNum();if (t.tm_year != -1) {t.tm_year -= 1900;cout << "请输入您要预约的月份(输入-1返回上级):";t.tm_mon = OAUtils::getNum();if (t.tm_mon != -1) {t.tm_mon -= 1;cout << "请输入您要预约哪天(输入-1返回上级):";t.tm_mday = OAUtils::getNum();if (t.tm_mday != -1) {cout << "请输入您要预约几时(输入-1返回上级):";t.tm_hour = OAUtils::getNum();while (t.tm_hour < -1 || t.tm_hour >= 24) {cout << "输入有误,请重新输入预约几时(输入-1返回上级):";t.tm_hour = OAUtils::getNum();}if (t.tm_hour != -1) {cout << "请输入您要预约多少分(输入-1返回上级):";t.tm_min = OAUtils::getNum();while (t.tm_min < -1 || t.tm_min >= 60) {cout << "输入有误,请重新输入预约几时(输入-1返回上级):";t.tm_min = OAUtils::getNum();}if (t.tm_min != -1) {cout << "请输入您预约的会议时长(单位:分钟):";time_t duration = OAUtils::getNum();cout << "请输入您要预约的会议室:";int roomid = OAUtils::getNum();t.tm_sec = 0;int Addres = ptr_data->addApp(t, duration, roomid);if (Addres == 1) {OADataCenter::save_OADataCenter();cout << "添加预约成功" << endl;}else if (Addres == -1) {cout << "没有此会议室" << endl;}else {cout << "该会议室预约时间冲突" << endl;}system("pause");}}}}}return Page::AdminPage;
}
//管理员删除预约界面
int OAsystemUI::displayDelAppPage(AdminData * ptr_admin)
{int pageflag = Page::AdminPage;cout << "********管理员删除预约********" << endl;cout << "******************************" << endl;if (ptr_admin->showApp()) {DelAppPage(ptr_admin);}return pageflag;
}
//用户删除预约界面
int OAsystemUI::displayDelAppPage(UserData * ptr_user)
{int pageflag = Page::UserPage;cout << "*********用户删除预约*********" << endl;cout << "******************************" << endl;if (ptr_user->showApp()) {DelAppPage(ptr_user);}return pageflag;
}
//删除预约界面
void OAsystemUI::DelAppPage(UserData * pt_data)
{bool res = true;cout << "请输入要删除预约的序号:";int id = OAUtils::getNum();id -= 1;if (!pt_data->delApp(id)) {cout << "删除失败" << endl;res = false;}else {cout << "删除成功" << endl;}system("pause");
}
//清空预约界面
int OAsystemUI::displayCleAppPage(AdminData * ptr_admin)
{cout << "********管理员清空预约********" << endl;cout << "******************************" << endl;cout << "是否确定清空所有预约?" << endl;cout << " 1.是\t2.否" << endl;int choice = OAUtils::getNum();if (choice == 1) {ptr_admin->CleAppPage();cout << "已清空" << endl;}system("pause");return Page::AdminPage;
}
OADataCenter.h 数据控制中心,包含用户类、管理员类、预约类、控制中心类的声明
#pragma once
#include<iostream>
using namespace std;
#include<vector>
#include<ctime>
#include"OAUtils.h"using MeetingRoom = int;
using _It_room = vector<MeetingRoom>::iterator;
//用户信息类
class UserData {
private:string _name;string _password;string _depart;
public:UserData();UserData(string name, string password, string depart);string& getName();string& getPasw();string& getDepart();const string& getName()const;const string& getPasw()const;const string& getDepart()const;
//用户登陆函数——通过多态实现管理员的不同登陆功能virtual UserData* Login();bool showApp();
//添加预约——返回-1,会议室不存在,返回0时间冲突int addApp(tm& t, time_t duration, MeetingRoom roomid);bool delApp(int id);
//写入或读取文件void save(ofstream& ofs);void read(ifstream& ifs);
};
class AdminData :public UserData {
public:AdminData();AdminData(string name, string password, string depart);AdminData* Login()override;bool showUser()const;bool showRoom();void AddUser(const UserData& userdata);void DelUser(const string& name)const;bool AddMeetingRoom(int id);bool DelMeetingRoom(int roomid);bool checkName(const string& name)const;void CleAppPage();
};
//预约类
class Appointment {
private:tm _t;time_t _duration;int _roomnum;string _username;string _userdepart;
public:Appointment();Appointment(const tm& t, time_t duration, int roomid, string name, string depart);const time_t getAppTime()const;const time_t getAppDuration()const;const int getRoomID()const;const string& getAppName()const;const string& getAppdepart()const;void save(ofstream& ofs);void read(ifstream& ifs);
};using _It_user = vector<UserData>::iterator;
using _It_app = vector<Appointment>::iterator;//数据中心类
class OADataCenter {
private:static AdminData admin;static vector<UserData> user;static vector<MeetingRoom> room;static vector<Appointment> App;
public://删插增改和登陆功能函数static UserData* Login(const UserData& userdata);static AdminData* Login(const AdminData& admindata);static void AddUser(const UserData& userdata);static void DelUser(const string& name);static bool AddMeetingRoom(int id);static bool DelMeetingRoom(MeetingRoom roomid);static void AddApp(const Appointment& app);static bool DelApp(const UserData& data, int id);static void CleApp();static bool showApp(const string& name);static bool showUser();static bool showRoom();//数据读取和写入static void init_OADataCenter();static void save_OADataCenter();//核对姓名、密码、时间,获取用户和预约的迭代器static bool checkTime(time_t time, time_t duration, MeetingRoom roomid);static _It_user get_It_User(const string& name);static _It_room get_It_Room(const MeetingRoom& id);static pair<_It_user, bool> checkName(const string& name);static bool checkPasw(const _It_user& it,const string& password);static pair<_It_room, bool> checkRoom(const MeetingRoom& id);};
//排序谓词
struct CompareName {bool operator()(const Appointment& a1, const Appointment& a2);
};
struct CompareRoomID {bool operator()(const Appointment& a1, const Appointment& a2);
};
struct PrintApp {void operator()(Appointment&a);
};
OADataCenter.cpp
#include "OADataCenter.h"
#include<algorithm>
#include<iomanip>
#include<sstream>
#include<fstream>
//用户信息类构造
UserData::UserData() {}
UserData::UserData(string name, string password, string depart)
{_name = name;_password = password;_depart = depart;
}
//获取用户名、密码、部门
string& UserData::getName()
{return _name;
}
string& UserData::getPasw()
{return _password;
}
string & UserData::getDepart()
{return _depart;
}
const string& UserData::getName() const
{return _name;
}
const string& UserData::getPasw() const
{return _password;
}
const string & UserData::getDepart() const
{return _depart;
}
//用户登录
UserData * UserData::Login()
{return OADataCenter::Login(*this);
}
//用户添加预约——判断会议室是否存在,判断时间是否冲突
int UserData::addApp(tm& t, time_t duration, MeetingRoom roomid)
{int res = 1;time_t t_time = mktime(&t);pair<_It_room, bool> respair = OADataCenter::checkRoom(roomid);if (!(respair.second)) {res = -1;}else if (!OADataCenter::checkTime(t_time, duration, roomid)) {res = 0;}else {Appointment app(t, duration, roomid, _name, _depart);OADataCenter::AddApp(app);}return res;
}
//删除预约
bool UserData::delApp(int id)
{return OADataCenter::DelApp(*this,id);
}
//查看预约——用户查看的是个人的预约,管理员查看的是所有预约
bool UserData::showApp()
{bool res = OADataCenter::showApp(_name);return res;
}
//保存用户信息到文件
void UserData::save(ofstream & ofs)
{OAUtils::save_str(ofs, _name);OAUtils::save_str(ofs, _password);OAUtils::save_str(ofs, _depart);
}
//从文件读取用户信息
void UserData::read(ifstream & ifs)
{OAUtils::read_str(ifs, _name);OAUtils::read_str(ifs, _password);OAUtils::read_str(ifs, _depart);
}//管理员构造函数
AdminData::AdminData() {}
AdminData::AdminData(string name, string password, string depart) :UserData(name, password, depart) {}
//管理员登录
AdminData * AdminData::Login()
{return OADataCenter::Login(*this);
}
//管理员查看所有用户
bool AdminData::showUser()const
{return OADataCenter::showUser();
}
//管理员查看所有会议室
bool AdminData::showRoom()
{return OADataCenter::showRoom();
}
//管理员添加用户——调用数据中心的添加用户函数
void AdminData::AddUser(const UserData& userdata)
{OADataCenter::AddUser(userdata);
}
//管理员删除用户——调用数据中心的删除用户函数
void AdminData::DelUser(const string& name)const
{OADataCenter::DelUser(name);
}
//管理员添加会议室——调用数据中心的添加会议室函数
bool AdminData::AddMeetingRoom(int id)
{return OADataCenter::AddMeetingRoom(id);
}
//管理员删除会议室——调用数据中心的删除会议室函数
bool AdminData::DelMeetingRoom(int roomid)
{return OADataCenter::DelMeetingRoom(roomid);
}
//管理员核对用户姓名是否存在
bool AdminData::checkName(const string & name)const
{return OADataCenter::checkName(name).second;
}void AdminData::CleAppPage()
{OADataCenter::CleApp();
}//预约类构造函数
Appointment::Appointment()
{this->_t = { 0 };this->_roomnum = 0;this->_duration = 0;
}
Appointment::Appointment(const tm & t, time_t duration, int roomid, string name, string depart)
{_t = t;_duration = duration;_roomnum = roomid;_username = name;_userdepart = depart;
}
//获取预约的time_t格式时间
const time_t Appointment::getAppTime()const
{return mktime(&tm(_t));
}
//获取预约的时长分钟
const time_t Appointment::getAppDuration()const
{return _duration;
}
//获取预约的会议室
const int Appointment::getRoomID() const
{return _roomnum;
}
//获取预约用户
const string& Appointment::getAppName()const
{return _username;
}
//获取预约部门
const string& Appointment::getAppdepart()const
{return _userdepart;
}
//保存约信息到文件
void Appointment::save(ofstream& ofs)
{ofs.write((char*)&_t, sizeof(_t));ofs.write((char*)&_duration, sizeof(_duration));ofs.write((char*)&_roomnum, sizeof(_roomnum));OAUtils::save_str(ofs, _username);OAUtils::save_str(ofs, _userdepart);
}
//从文件读取预约信息
void Appointment::read(ifstream & ifs)
{ifs.read((char*)&_t, sizeof(_t));ifs.read((char*)&_duration, sizeof(_duration));ifs.read((char*)&_roomnum, sizeof(_roomnum));OAUtils::read_str(ifs, _username);OAUtils::read_str(ifs, _userdepart);
}
//用户和管理员登录函数重载
UserData * OADataCenter::Login(const UserData & userdata)
{UserData* res = nullptr;pair<_It_user, bool> checkpair = checkName(userdata.getName());if (checkpair.second) {if (checkPasw(checkpair.first, userdata.getPasw())) {res = &(*checkpair.first);}}return res;
}AdminData * OADataCenter::Login(const AdminData & admindata)
{AdminData* admin_ptr = nullptr;if (admin.getName() == admindata.getName() && admin.getPasw() == admindata.getPasw()) {admin_ptr = &admin;}return admin_ptr;
}void OADataCenter::AddUser(const UserData & userdata)
{user.push_back(userdata);OADataCenter::save_OADataCenter();
}
//数据中心删除用户
void OADataCenter::DelUser(const string & name)
{user.erase(OADataCenter::get_It_User(name));save_OADataCenter();
}
//数据中心添加会议室——添加前判断会议室是否存在
bool OADataCenter::AddMeetingRoom(int id)
{bool res = false;if (!checkRoom(id).second) {room.push_back(id);res = true;OADataCenter::save_OADataCenter();}return res;
}
//数据中心删除会议室——删除前判断会议室是否存在
bool OADataCenter::DelMeetingRoom(MeetingRoom roomid)
{bool res = false;_It_room it = get_It_Room(roomid);if (it != room.end()) {room.erase(it);cout << room.size() << endl;res = true;OADataCenter::save_OADataCenter();}return res;
}
//数据中心添加预约
void OADataCenter::AddApp(const Appointment & app)
{App.push_back(app);save_OADataCenter();
}
//数据中心删除预约
bool OADataCenter::DelApp(const UserData & data, int id)
{bool res = false;sort(App.begin(),App.end(),CompareName());pair<_It_app, _It_app> respair;if (&data != &admin) {respair = equal_range(App.begin(), App.end(), Appointment(tm(), 0, 0, data.getName(), ""),CompareName());}else {respair.first = App.begin();respair.second = App.end();}if (id < respair.second - respair.first&&id >= 0) {App.erase(respair.first + id);res = true;save_OADataCenter();}return res;
}
//数据中心清空预约
void OADataCenter::CleApp()
{App.clear();vector<Appointment>().swap(App);
}
//保存所有数据到文件
void OADataCenter::init_OADataCenter()
{ifstream ifs;ifs.open("DataCenter.txt", ios::in | ios::binary);if (ifs.is_open()) {size_t usersize = 0;ifs.read((char*)&usersize, sizeof(usersize));user.resize(usersize);for (_It_user it = user.begin(); it != user.end(); it++) {it->read(ifs);}size_t roomsize = 0;ifs.read((char*)&roomsize, sizeof(roomsize));room.resize(roomsize);for (_It_room it = room.begin(); it != room.end(); it++) {ifs.read((char*)&(*it), sizeof(*it));}size_t appsize = 0;ifs.read((char*)&appsize, sizeof(appsize));App.resize(appsize);for (_It_app it = App.begin(); it != App.end(); it++) {it->read(ifs);}ifs.close();}
}
//从文件写入数据
void OADataCenter::save_OADataCenter()
{ofstream ofs;ofs.open("DataCenter.txt", ios::out | ios::binary);if (ofs.is_open()) {size_t size = user.size();ofs.write((char*)&size, sizeof(size));for (auto it = user.begin(); it != user.end(); it++) {it->save(ofs);}size = room.size();ofs.write((char*)&size, sizeof(size));for (_It_room it = room.begin(); it != room.end(); it++) {ofs.write((char*)&(*it), sizeof(*it));}size = App.size();ofs.write((char*)&size, sizeof(size));for (auto it = App.begin(); it != App.end(); it++) {it->save(ofs);}ofs.close();}
}
//数据中心核对时间是否冲突
bool OADataCenter::checkTime(time_t t_time, time_t duration, MeetingRoom roomid)
{bool res = true;duration *= 60;sort(App.begin(), App.end(), CompareRoomID());auto roomidAppit = equal_range(App.begin(), App.end(), Appointment(tm(), 0, roomid, "", ""), CompareRoomID());for (_It_app it = roomidAppit.first; roomidAppit.first != App.end() && it != roomidAppit.second; it++) {if (it->getAppTime() == t_time ||(it->getAppTime() < t_time&&it->getAppTime() + it->getAppDuration()*60 > t_time) ||(it->getAppTime() > t_time&&it->getAppTime() < t_time + duration)) {res = false;break;}}return res;
}
//通过姓名获取用户的迭代器,不存在则返回end()
_It_user OADataCenter::get_It_User(const string & name)
{for (_It_user it = user.begin(); it != user.end(); it++) {if (it->getName() == name) {return it;}}return user.end();
}
//通过房间号获取会议室迭代器,不存在返回end
_It_room OADataCenter::get_It_Room(const MeetingRoom & id)
{return find(room.begin(), room.end(), id);
}
//数据中心核实是否存在姓名为name的用户
pair<_It_user, bool> OADataCenter::checkName(const string& name)
{pair<_It_user, bool> res;res.first = get_It_User(name);res.second = res.first != user.end();return res;
}
//数据中心核对密码——登陆时调用
bool OADataCenter::checkPasw(const _It_user& it, const string& password)
{if (it != user.end()) {return it->getPasw() == password;}else {return false;}
}
//数据中心判断是否存在id会议室
pair<_It_room, bool> OADataCenter::checkRoom(const MeetingRoom & id)
{pair<_It_room, bool> res;res.first = get_It_Room(id);res.second = res.first != room.end();return res;
}
//数据中心查看预约——用户查看个人预约,管理员查看所有预约
bool OADataCenter::showApp(const string & name)
{bool res = false;pair<_It_app, _It_app> myApp;if (name != admin.getName()) {sort(App.begin(), App.end(), CompareName());myApp = equal_range(App.begin(), App.end(), Appointment(tm(), 0, 0, name, ""), CompareName());}else {myApp.first = App.begin();myApp.second = App.end();} if (myApp.first == App.end()) {cout << "您没有预约" << endl;}else {res = true;cout << setw(10) << setiosflags(ios::left) << "序号"<< setw(10) << setiosflags(ios::left) << "房间"<< setw(30) << setiosflags(ios::left) << "预约时间"<< setw(10) << setiosflags(ios::left) << "预约时长"<< setw(10) << setiosflags(ios::left) << "预约人"<< setw(10) << setiosflags(ios::left) << "预约部门" << endl;int i = 1;for (_It_app it = myApp.first; it != myApp.second; it++, i++) {cout << setw(10) << setiosflags(ios::left) << i;PrintApp()(*it);}}return res;
}
//数据中心查看所有用户——由管理员删除用户时调用
bool OADataCenter::showUser()
{bool res = false;if (user.size() == 0) {cout << "当前没有用户" << endl;}else {res = true;cout << setw(10) << setiosflags(ios::left) << "序号"<< setw(10) << setiosflags(ios::left) << "用户名"<< setw(10) << setiosflags(ios::left) << "部门" << endl;int i = 1;for (_It_user it = OADataCenter::user.begin(); it != OADataCenter::user.end(); it++, i++) {cout << setw(10) << setiosflags(ios::left) << i<< setw(10) << setiosflags(ios::left) << it->getName()<< setw(10) << setiosflags(ios::left) << it->getDepart() << endl;}}return res;
}
//数据中心查看所有会议室——由管理员删除会议室时调用
bool OADataCenter::showRoom()
{bool res = false;if (OADataCenter::room.size() == 0) {cout << "当前没有会议室" << endl;}else {res = true;cout << setw(10) << setiosflags(ios::left) << "序号"<< setw(10) << setiosflags(ios::left) << "门牌号" << endl;int i = 1;for (_It_room it = OADataCenter::room.begin(); it != OADataCenter::room.end(); it++, i++) {cout << setw(10) << setiosflags(ios::left) << i<< setw(10) << setiosflags(ios::left) << *it << endl;}}return res;
}using MeetingRoom = int;
AdminData OADataCenter::admin("admin", "admin", "管理员");
vector<UserData> OADataCenter::user;
vector<MeetingRoom> OADataCenter::room;
vector<Appointment> OADataCenter::App;//排序谓词
bool CompareName::operator()(const Appointment& a1, const Appointment& a2)
{return a1.getAppName() < a2.getAppName();
}bool CompareRoomID::operator()(const Appointment & a1, const Appointment & a2)
{return a1.getRoomID() < a2.getRoomID();
}
//输出预约仿函数
void PrintApp::operator()(Appointment & a)
{time_t t = a.getAppTime();tm tm_t;localtime_s(&tm_t, &t);ostringstream oss;oss << tm_t.tm_year + 1900 << "年" << tm_t.tm_mon + 1 << "月" << tm_t.tm_mday << "日" << tm_t.tm_hour << "时" << tm_t.tm_min << "分";cout << setw(10) << a.getRoomID() << setw(30) << oss.str() << setw(10) << a.getAppDuration() << setw(10)<< a.getAppName() << setw(10) << a.getAppdepart() << endl;
}