当前位置:   article > 正文

C++学习笔记_基础完结篇_机房预约管理项目_bool insertappointment(const int& date):尝试租借小会议室,如

bool insertappointment(const int& date):尝试租借小会议室,如果租借成功返回tru

一个结课级别的C++项目,模拟学校机房的运行和管理。

项目需求如下:

使用者身份

机房简介

申请规则

系统按身份登录,每个身份通过账号密码的验证之后进入子菜单:

1. 学生菜单

2. 教师菜单

3. 管理员菜单

1. 主页面

  1. int main() {
  2. while (true)
  3. {
  4. int select = 0;
  5. cout << "================== Welcome to Computer Room Reserving System ==================" << endl;
  6. cout << endl << "Please Enter Your Identity" << endl;
  7. cout << "\t\t ----------------------------------------------\n";
  8. cout << "\t\t| |\n";
  9. cout << "\t\t| 1. Student |\n";
  10. cout << "\t\t| |\n";
  11. cout << "\t\t| |\n";
  12. cout << "\t\t| 2. Teacher |\n";
  13. cout << "\t\t| |\n";
  14. cout << "\t\t| |\n";
  15. cout << "\t\t| 3. Administrator |\n";
  16. cout << "\t\t| |\n";
  17. cout << "\t\t| |\n";
  18. cout << "\t\t| 0. Quit |\n";
  19. cout << "\t\t| |\n";
  20. cout << "\t\t| |\n";
  21. cout << "\t\t ----------------------------------------------\n";
  22. cout << "Please enter: ";
  23. cin >> select;
  24. switch (select) {
  25. case 1:
  26. LogIn(STUDENT_FILE, 1);
  27. break;
  28. case 2:
  29. LogIn(TEACHER_FILE, 2);
  30. break;
  31. case 3:
  32. LogIn(ADMIN_FILE, 3);
  33. break;
  34. case 0:
  35. cout << "Thanks! Bye" << endl;
  36. system("pause");
  37. return 0;
  38. break;
  39. default:
  40. cout << "Wrong input! Please re-enter !" << endl;
  41. system("cls");
  42. break;
  43. }
  44. }
  45. system("pause");
  46. return 0;
  47. }

 死循环+switch实现界面选择,算是基操了

身份类创建

基类

  1. #pragma once
  2. #include <iostream>
  3. // 身份抽象类
  4. class Identity {
  5. public:
  6. // 操作菜单 纯虚函数
  7. virtual void openMenu() = 0;
  8. // 用户名
  9. std::string m_name;
  10. // 密码
  11. std::string m_password;
  12. };

 学生类

  1. #pragma once
  2. #include <iostream>
  3. #include "identity.h"
  4. class Student : public Identity {
  5. public:
  6. int student_id;
  7. // 默认构造
  8. Student();
  9. // 有参构造 学号、姓名、密码
  10. Student(int student_id, std::string name, std::string pwd);
  11. // 菜单界面
  12. virtual void openMenu();
  13. // 申请预约
  14. void applyOrder();
  15. // 查看自身预约
  16. void showMyOrder();
  17. // 查看所有预约
  18. void showAllOrder();
  19. // 取消预约
  20. void cancelOrder();
  21. };

教师类

  1. #pragma once
  2. #include <iostream>
  3. #include "identity.h"
  4. class Teacher : public Identity {
  5. public:
  6. int teacher_id;
  7. // 默认构造
  8. Teacher();
  9. // 有参构造 工号、姓名、密码
  10. Teacher(int teacher_id, std::string name, std::string pwd);
  11. // 菜单界面
  12. virtual void openMenu();
  13. // 查看所有预约
  14. void showAllOrder();
  15. // 审核预约
  16. void validOrder();
  17. };

管理员类

  1. #pragma once
  2. #include <iostream>
  3. #include "identity.h"
  4. class Administrator : public Identity {
  5. public:
  6. // 默认构造
  7. Administrator();
  8. // 有参构造 账号、密码
  9. Administrator(std::string name, std::string pwd);
  10. // 菜单界面
  11. virtual void openMenu();
  12. // 添加账号
  13. void addAccount();
  14. // 查看账号
  15. void showAccount();
  16. // 查看机房
  17. void showRoom();
  18. // 清空预约
  19. void clearOrder();
  20. };

登录模块

在头文件中定义所有的使用到的文件名方便不同文件的文件操作

  1. #pragma once
  2. // 管理员文件
  3. #define ADMIN_FILE "admin.txt"
  4. // 学生文件
  5. #define STUDENT_FILE "student.txt"
  6. // 教师文件
  7. #define TEACHER_FILE "teacher.txt"
  8. // 机房信息文件
  9. #define COMPUTER_FILE "computerRoom.txt"
  10. // 预约文件
  11. #define ORDER_FILE "order.txt"

注:显示中文的话,文件格式采用ansi编码格式保存,utf-8会读出乱码

三种身份的登录及其验证

  1. // 登录功能
  2. void LogIn(string fileName, int type) {
  3. // 父类指针,用于指向子类对象
  4. Identity* person = NULL;
  5. ifstream ifs;
  6. ifs.open(fileName, ios::in);
  7. if (!ifs.is_open()) {
  8. cout << "No File!" << endl;
  9. ifs.close();
  10. return;
  11. }
  12. // 初始化id、名字和密码
  13. int id = 0;
  14. string name;
  15. string pwd;
  16. // 身份判断
  17. if (type == 1) {
  18. cout << "Please enter student_ID: " << endl;
  19. cin >> id;
  20. }
  21. else if (type == 2) {
  22. cout << "Please enter staff_ID: " << endl;
  23. cin >> id;
  24. }
  25. cout << "Please enter your name: " << endl;
  26. cin >> name;
  27. cout << "Please enter your password: " << endl;
  28. cin >> pwd;
  29. if (type == 1) {
  30. // 学生身份验证
  31. int fId;
  32. string fName;
  33. string fPwd;
  34. while (ifs >> fId && ifs >> fName && ifs >> fPwd) {
  35. /* cout << fId << endl;
  36. cout << fName << endl;
  37. cout << fPwd << endl; */
  38. if (fId == id && fName == name && fPwd == pwd) {
  39. cout << "student log in successfully!" << endl;
  40. system("pause");
  41. system("cls");
  42. // 创建学生对象
  43. person = new Student(id, name, pwd);
  44. studentMenu(person);
  45. return;
  46. }
  47. }
  48. }
  49. else if (type == 2) {
  50. // 教师身份验证
  51. int fId;
  52. string fName;
  53. string fPwd;
  54. while (ifs >> fId && ifs >> fName && ifs >> fPwd) {
  55. if (fId == id && fName == name && fPwd == pwd) {
  56. cout << "teacher log in successfully!" << endl;
  57. system("pause");
  58. system("cls");
  59. // 创建教师对象
  60. person = new Teacher(id, name, pwd);
  61. teacherMenu(person);
  62. return;
  63. }
  64. }
  65. }
  66. else {
  67. // 管理员身份验证
  68. int fId;
  69. string fName;
  70. string fPwd;
  71. while (ifs >> fName && ifs >> fPwd){
  72. if (fName == name && fPwd == pwd) {
  73. cout << "admin log in successfully!" << endl;
  74. system("pause");
  75. system("cls");
  76. // 创建管理员对象
  77. person = new Administrator(name, pwd);
  78. adminMenu(person);
  79. return;
  80. }
  81. }
  82. }
  83. cout << "Login Fail!" << endl;
  84. system("pause");
  85. system("cls");
  86. ifs.close();
  87. }

管理员身份登录,功能模块:

  1. void adminMenu(Identity*& admin) {
  2. while (true) {
  3. // 父类指针调用子类共同接口
  4. admin->openMenu();
  5. // 将父类指针转为子类指针,调用子类其他接口
  6. Administrator* ad = (Administrator*)admin;
  7. int select = 0;
  8. cin >> select;
  9. // 添加账号
  10. if (select == 1) {
  11. cout << " add account " << endl;
  12. ad->addAccount();
  13. }
  14. // 查看账号
  15. else if (select == 2) {
  16. cout << " show account " << endl;
  17. ad->showAccount();
  18. }
  19. // 查看机房
  20. else if (select == 3) {
  21. cout << " show room " << endl;
  22. ad->showRoom();
  23. }
  24. // 清空预约
  25. else if (select == 4) {
  26. cout << " clear order " << endl;
  27. ad->clearOrder();
  28. }
  29. // 注销登录
  30. else {
  31. cout << "log out" << endl;
  32. return;
  33. // delete admin;
  34. }
  35. }
  36. }

先写管理员部分,因为学生和老师的账号都由其创建,其职能如下:

头文件定义如下:

  1. #pragma once
  2. #include <iostream>
  3. #include <fstream>
  4. #include "identity.h"
  5. #include "globalFile.h"
  6. #include <vector>
  7. #include "student.h"
  8. #include "teacher.h"
  9. #include "computerRoom.h"
  10. #include <algorithm>
  11. void printStudent(Student& s);
  12. void printTeacher(Teacher& t);
  13. class Administrator : public Identity {
  14. public:
  15. // 默认构造
  16. Administrator();
  17. // 有参构造 账号、密码
  18. Administrator(std::string name, std::string pwd);
  19. // 菜单界面
  20. virtual void openMenu();
  21. // 添加账号
  22. void addAccount();
  23. // 查看账号
  24. void showAccount();
  25. // 查看机房
  26. void showRoom();
  27. // 清空预约
  28. void clearOrder();
  29. // 初始化容器
  30. void initVector();
  31. // 检测重复,参数1是学号/工号,参数2是检测类型
  32. bool checkRepeat(int id, int type);
  33. // 学生容器
  34. std::vector<Student> vStu;
  35. // 老师容器
  36. std::vector<Teacher> vTea;
  37. // 机房容器
  38. std::vector<computerRoom> vCom;
  39. };

administrator.cpp 管理员各功能函数实现:

  1. #include "administrator.h"
  2. #include <iostream>
  3. using namespace std;
  4. // 默认构造
  5. Administrator::Administrator() {
  6. }
  7. // 有参构造 账号、姓名、密码
  8. Administrator::Administrator(string name, string pwd) {
  9. this->m_name = name;
  10. this->m_password = pwd;
  11. //初始化容器, 获取文件中所有学生和老师的账号信息
  12. this->initVector();
  13. //初始化机房信息
  14. ifstream ifs;
  15. ifs.open(COMPUTER_FILE, ios::in);
  16. if (!ifs.is_open()) {
  17. cout << "fail to open " << endl;
  18. return;
  19. }
  20. computerRoom com;
  21. while (ifs >> com.m_ID && ifs >> com.m_Volume) {
  22. vCom.push_back(com);
  23. }
  24. ifs.close();
  25. // cout << "Room num: " << vCom.size() << endl;
  26. }
  27. // 菜单界面
  28. void Administrator::openMenu() {
  29. cout << "================== Welcome Administrator==================" << endl;
  30. cout << "\t\t ----------------------------------------------\n";
  31. cout << "\t\t| |\n";
  32. cout << "\t\t| 1. Add Acounts |\n";
  33. cout << "\t\t| |\n";
  34. cout << "\t\t| |\n";
  35. cout << "\t\t| 2. Show Accounts |\n";
  36. cout << "\t\t| |\n";
  37. cout << "\t\t| |\n";
  38. cout << "\t\t| 3. Show Room |\n";
  39. cout << "\t\t| |\n";
  40. cout << "\t\t| |\n";
  41. cout << "\t\t| 4. Clear Order |\n";
  42. cout << "\t\t| |\n";
  43. cout << "\t\t| Other. Log Out |\n";
  44. cout << "\t\t ----------------------------------------------\n";
  45. cout << "Please enter: ";
  46. }
  47. // 添加账号
  48. void Administrator::addAccount() {
  49. cout << "Please enter the type of account you want to add:" << endl;
  50. cout << "1. student" << endl;
  51. cout << "2. teacher" << endl;
  52. string fileName; // 操作文件名
  53. string tip; // 协助显示信息
  54. ofstream ofs;
  55. int select = 0;
  56. cin >> select;
  57. if (select == 1) {
  58. fileName = STUDENT_FILE;
  59. tip = "Please enter student_id: ";
  60. }
  61. else {
  62. fileName = TEACHER_FILE;
  63. tip = "Please enter staff_id: ";
  64. }
  65. ofs.open(fileName, ios::out | ios::app);
  66. int id; //学号/工号
  67. string name; // 姓名
  68. string pwd; // 密码
  69. while(true){
  70. cout << tip << endl;
  71. cin >> id;
  72. bool is_repeat = checkRepeat(id, select);
  73. if (is_repeat) {
  74. cout << "repeat id input, please re-enter! " << endl;
  75. }
  76. else {
  77. break;
  78. }
  79. }
  80. cout << "Please enter your name: " << endl;
  81. cin >> name;
  82. cout << "Please enter your password: " << endl;
  83. cin >> pwd;
  84. ofs << id << " " << name << " " << pwd << " " << endl;
  85. cout << "Add Successfully! " << endl;
  86. system("pause");
  87. system("cls");
  88. ofs.close();
  89. // 添加完毕后重新初始化维持重复检测状态
  90. this->initVector();
  91. }
  92. // 查看账号
  93. void Administrator::showAccount() {
  94. cout << "Please choose what to show: " << endl;
  95. cout << "1. show all students " << endl;
  96. cout << "2. show all teachers" << endl;
  97. int select = 0;
  98. cin >> select;
  99. if (select == 1) {
  100. cout << "message of all students is below: " << endl;
  101. for_each(vStu.begin(), vStu.end(), printStudent);
  102. }
  103. else {
  104. cout << "message of all teachers is below:" << endl;
  105. for_each(vTea.begin(), vTea.end(), printTeacher);
  106. }
  107. system("pause");
  108. system("cls");
  109. }
  110. // 查看机房
  111. void Administrator::showRoom() {
  112. cout << "The message of computer room is below: " << endl;
  113. for (vector<computerRoom>::iterator it = vCom.begin(); it != vCom.end(); it++) {
  114. cout << "Room number: " << it->m_ID << "Room volume: " << it->m_Volume << endl;
  115. }
  116. system("pause");
  117. system("cls");
  118. }
  119. // 清空预约
  120. void Administrator::clearOrder() {
  121. ofstream ofs(ORDER_FILE, ios::trunc);
  122. ofs.close();
  123. cout << "Clear successfully! " << endl;
  124. system("pause");
  125. system("cls");
  126. }
  127. // 初始化操作
  128. void Administrator::initVector() {
  129. vStu.clear();
  130. vTea.clear();
  131. ifstream ifs;
  132. // 读取学生信息和老师信息存入容器,用于去重操作
  133. // 读取学生
  134. ifs.open(STUDENT_FILE, ios::in);
  135. if (!ifs.is_open()) {
  136. cout << "read file fail! " << endl;
  137. return;
  138. }
  139. Student s;
  140. while (ifs >> s.student_id && ifs >> s.m_name && ifs >> s.m_password) {
  141. vStu.push_back(s);
  142. }
  143. cout << "The amount of students is: " << vStu.size() << endl;
  144. ifs.close();
  145. // 读取老师
  146. ifs.open(TEACHER_FILE, ios::in);
  147. if (!ifs.is_open()) {
  148. cout << "read file fail! " << endl;
  149. return;
  150. }
  151. Teacher t;
  152. while (ifs >> t.teacher_id && ifs >> t.m_name && ifs >> t.m_password) {
  153. vTea.push_back(t);
  154. }
  155. cout << "The amount of teachers is: " << vTea.size() << endl;
  156. ifs.close();
  157. }
  158. // 检测重复
  159. bool Administrator::checkRepeat(int id, int type) {
  160. // 检测学生
  161. if (type == 1) {
  162. for (vector<Student>::iterator it = vStu.begin(); it != vStu.end(); it++) {
  163. if (id == it->student_id) {
  164. return true;
  165. }
  166. }
  167. }
  168. // 检测老师
  169. else {
  170. for (vector<Teacher>::iterator it = vTea.begin(); it != vTea.end(); it++) {
  171. if (id == it->teacher_id) {
  172. return true;
  173. }
  174. }
  175. }
  176. return false;
  177. }
  178. // 打印账号信息
  179. void printStudent(Student &s) {
  180. cout << "student_id: " << s.student_id << " student_name: " << s.m_name << endl;
  181. }
  182. void printTeacher(Teacher &t) {
  183. cout << "teacher_id: " << t.teacher_id << " teacher_name: " << t.m_name << endl;
  184. }

对于每次添加账号的查重,需要及时保持管理员类中负责学生和老师账号容器的重新初始化,使得重复检测有效。

然后完成学生部分,因为老师是用来审核预约的,因此需要学生主体先创建预约,学生功能如下:

学生登录部分和管理员类似:

  1. // 学生身份登录
  2. void studentMenu(Identity*& student) {
  3. while (true) {
  4. // 父类指针调用子类共同接口
  5. student->openMenu();
  6. // 将父类指针转为子类指针,调用子类其他接口
  7. Student* stu = (Student*)student;
  8. int select = 0;
  9. cin >> select;
  10. // 申请预约
  11. if (select == 1) {
  12. cout << "add order " << endl;
  13. stu->applyOrder();
  14. }
  15. // 查看自身预约
  16. else if (select == 2) {
  17. cout << "show order " << endl;
  18. stu->showMyOrder();
  19. }
  20. // 查看所有预约
  21. else if (select == 3) {
  22. cout << "show all orders " << endl;
  23. stu->showAllOrder();
  24. }
  25. // 取消预约
  26. else if (select == 4) {
  27. cout << "clear order " << endl;
  28. stu->cancelOrder();
  29. }
  30. // 注销登录
  31. else {
  32. cout << "log out" << endl;
  33. return;
  34. //delete admin;
  35. }
  36. }
  37. }

学生部分的功能涉及到预约的显示操作,因此定义orderFile类用于处理预约记录和更新。

orderFile.h:

  1. #pragma once
  2. #include <iostream>
  3. using namespace std;
  4. #include "globalFile.h"
  5. #include <fstream>
  6. #include <string>
  7. #include <map>
  8. class orderFile {
  9. public:
  10. orderFile();
  11. // 预约数量
  12. int m_Size;
  13. // 预约记录,使用嵌套map存储
  14. map<int, map<string, string>> m_Data;
  15. // 更新预约记录
  16. void updateOrder();
  17. };

orderFile.cpp:

  1. #include "orderFile.h"
  2. orderFile::orderFile() {
  3. ifstream ifs;
  4. ifs.open(ORDER_FILE, ios::in);
  5. string date; // 日期
  6. string interval; // 时间段
  7. string stuID; // 学号
  8. string stuName; // 学生姓名
  9. string roomID; // 机房号码
  10. string status; // 审核状态
  11. this->m_Size = 0;
  12. while (ifs >> date && ifs >> interval && ifs >> stuID && ifs >> stuName && ifs >> roomID && ifs >> status ){
  13. /* cout << date << endl;
  14. cout << interval << endl;
  15. cout << stuID << endl;
  16. cout << stuName << endl;
  17. cout << roomID << endl;
  18. cout << status << endl;
  19. cout << endl;*/
  20. string key;
  21. string value;
  22. map<string, string> m;
  23. // 截取日期
  24. int pos = date.find(":");
  25. if (pos != -1) {
  26. key = date.substr(0, pos);
  27. value = date.substr(pos + 1, date.size()- pos -1);
  28. m.insert(make_pair(key, value));
  29. //cout << value << endl;
  30. //switch(stoi(value)){
  31. // case 1:
  32. // m.insert(make_pair(key, "Monday"));
  33. // break;
  34. // case 2:
  35. // m.insert(make_pair(key, "Tuesday"));
  36. // break;
  37. // case 3:
  38. // m.insert(make_pair(key, "Wednesday"));
  39. // break;
  40. // case 4:
  41. // m.insert(make_pair(key, "Thursday"));
  42. // break;
  43. // case 5:
  44. // m.insert(make_pair(key, "Friday"));
  45. // break;
  46. // default:
  47. // break;
  48. //}
  49. }
  50. // 截取时间段
  51. pos = interval.find(":");
  52. // cout << "interval " << interval << endl;
  53. if (pos != -1) {
  54. key = interval.substr(0, pos);
  55. value = interval.substr(pos + 1, interval.size() - pos - 1);
  56. m.insert(make_pair(key, value));
  57. }
  58. // 截取学号
  59. // cout << "stuID " << stuID << endl;
  60. pos = stuID.find(":");
  61. if (pos != -1) {
  62. key = stuID.substr(0, pos);
  63. value = stuID.substr(pos + 1, stuID.size() - pos - 1);
  64. m.insert(make_pair(key, value));
  65. }
  66. // 截取姓名
  67. pos = stuName.find(":");
  68. if (pos != -1) {
  69. key = stuName.substr(0, pos);
  70. value = stuName.substr(pos + 1, stuName.size() - pos - 1);
  71. m.insert(make_pair(key, value));
  72. }
  73. // 截取机房号
  74. pos = roomID.find(":");
  75. if (pos != -1) {
  76. key = roomID.substr(0, pos);
  77. value = roomID.substr(pos + 1, roomID.size() - pos - 1);
  78. m.insert(make_pair(key, value));
  79. }
  80. // 截取审核状态
  81. pos = status.find(":");
  82. if (pos != -1) {
  83. key = status.substr(0, pos);
  84. value = status.substr(pos + 1, status.size() - pos - 1);
  85. m.insert(make_pair(key, value));
  86. }
  87. // 将小map容器放到大map容器中
  88. this->m_Data.insert(make_pair(this->m_Size, m));
  89. this->m_Size++;
  90. }
  91. ifs.close();
  92. // test
  93. /*for (map<int, map<string, string>>::iterator it = m_Data.begin(); it != m_Data.end(); it++) {
  94. cout << "No." << it->first << " value: " << endl;
  95. for (map<string, string>::iterator mit = it->second.begin(); mit != it->second.end(); mit++) {
  96. cout << "key = " << mit->first << " value = " << mit->second << " ";
  97. }
  98. cout << endl;
  99. }*/
  100. }
  101. void orderFile::updateOrder() {
  102. if (this->m_Size == 0) {
  103. return; // 无预约记录直接返回
  104. }
  105. ofstream ofs(ORDER_FILE, ios::out | ios::trunc); // 删除并重新写入
  106. for (int i = 0; i < this->m_Size; i++) {
  107. ofs << "date:" << this->m_Data[i]["date"] << " ";
  108. ofs << "interval:" << this->m_Data[i]["interval"] << " ";
  109. ofs << "stuID:" << this->m_Data[i]["stuID"] << " ";
  110. ofs << "stuName:" << this->m_Data[i]["stuName"] << " ";
  111. ofs << "roomID:" << this->m_Data[i]["roomID"] << " ";
  112. ofs << "status:" << this->m_Data[i]["status"] << " ";
  113. }
  114. ofs.close();
  115. }

关键点在于字符串的切分以及重新的存取,更新

student.cpp:

  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include "student.h"
  5. #include "globalFile.h"
  6. #include "computerRoom.h"
  7. using namespace std;
  8. // 将数字转换为星期
  9. string day[5] = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" };
  10. // 默认构造
  11. Student::Student() {
  12. }
  13. // 有参构造 学号、姓名、密码
  14. Student::Student(int student_id, std::string name, std::string pwd) {
  15. this->student_id = student_id;
  16. this->m_name = name;
  17. this->m_password = pwd;
  18. //初始化机房信息
  19. ifstream ifs;
  20. ifs.open(COMPUTER_FILE, ios::in);
  21. if (!ifs.is_open()) {
  22. cout << "fail to open " << endl;
  23. return;
  24. }
  25. computerRoom com;
  26. while (ifs >> com.m_ID && ifs >> com.m_Volume) {
  27. vCom.push_back(com);
  28. }
  29. ifs.close();
  30. // cout << "Room num: " << vCom.size() << endl;
  31. }
  32. // 菜单界面
  33. void Student::openMenu() {
  34. cout << "\t\t================== Welcome " << this->m_name << "==================" <<endl;
  35. cout << "\t\t ----------------------------------------------\n";
  36. cout << "\t\t| |\n";
  37. cout << "\t\t| 1. Add Orders |\n";
  38. cout << "\t\t| |\n";
  39. cout << "\t\t| |\n";
  40. cout << "\t\t| 2. Show Orders |\n";
  41. cout << "\t\t| |\n";
  42. cout << "\t\t| |\n";
  43. cout << "\t\t| 3. Show All Orders |\n";
  44. cout << "\t\t| |\n";
  45. cout << "\t\t| |\n";
  46. cout << "\t\t| 4. Cancel Order |\n";
  47. cout << "\t\t| |\n";
  48. cout << "\t\t| Other. Log Out |\n";
  49. cout << "\t\t ----------------------------------------------\n";
  50. cout << "Please enter: ";
  51. }
  52. // 申请预约
  53. void Student::applyOrder() {
  54. cout << "The open time of the computer room is from Monday to Friday" << endl;
  55. cout << "Please enter the time of your order: " << endl;
  56. cout << "1. Monday " << endl;
  57. cout << "2. Tuesday " << endl;
  58. cout << "3. Wednesday " << endl;
  59. cout << "4. Thursday " << endl;
  60. cout << "5. Friday " << endl;
  61. int date = 0; // 日期
  62. int interval = 0; // 时间段
  63. int room = 0; // 机房编号
  64. while (true) {
  65. cin >> date;
  66. if (date >= 1 && date <= 5) {
  67. break;
  68. }
  69. cout << "date out of range, please re-enter" << endl;
  70. }
  71. cout << "please enter the period of the order: " << endl;
  72. cout << "1. morning " << endl;
  73. cout << "2. afternoon " << endl;
  74. while (true) {
  75. cin >> interval;
  76. if (interval >= 1 && interval <= 2) {
  77. break;
  78. }
  79. cout << "interval out of range, please re-enter" << endl;
  80. }
  81. cout << "please choose the room: " << endl;
  82. // 改进点:机房剩余容量的计算
  83. while(true) {
  84. cin >> room;
  85. if (room >= 1 && room <= 3) {
  86. break;
  87. }
  88. cout << "wrong enter, please re-enter: " << endl;
  89. }
  90. cout << "order successfully! under the validation...." << endl;
  91. ofstream ofs;
  92. ofs.open(ORDER_FILE, ios::app);
  93. ofs << "date:" << date << " ";
  94. ofs << "interval:" << interval << " ";
  95. ofs << "stuID:" << this->student_id << " ";
  96. ofs << "stuName:" << this->m_name << " ";
  97. ofs << "roomID:" << room << " ";
  98. ofs << "status:" << 1 << endl;
  99. ofs.close();
  100. std::system("pause");
  101. std::system("cls");
  102. }
  103. // 查看自身预约
  104. void Student::showMyOrder() {
  105. // cout << "********************************" << endl;
  106. orderFile od;
  107. if (od.m_Size == 0) {
  108. cout << "no order record!" << endl;
  109. std::system("pause");
  110. std::system("cls");
  111. return;
  112. }
  113. //cout << od.m_Data[0]["stuID"] << endl;
  114. //cout << od.m_Data[0]["date"] << endl;
  115. //cout << od.m_Data[0]["interval"] << endl;
  116. //cout << od.m_Data[0]["roomID"] << endl;
  117. for (int i = 0; i < od.m_Size; i++) {
  118. // string to int
  119. // string -> .c_str() -> const char* -> atoi -> int
  120. if (this->student_id == atoi(od.m_Data[i]["stuID"].c_str())) {
  121. cout << "date: " << day[(stoi(od.m_Data[i]["date"])-1)];
  122. cout << " time: " << (od.m_Data[i]["interval"] == "1" ? "morning" : "afternoon");
  123. cout << " roomID: " << od.m_Data[i]["roomID"];
  124. // 1. under validation 2. success -1. fail 0. cancel
  125. string status = " status: ";
  126. if (od.m_Data[i]["status"] == "1") {
  127. status += "under validation";
  128. }
  129. else if (od.m_Data[i]["status"] == "2") {
  130. status += "order success";
  131. }
  132. else if (od.m_Data[i]["status"] == "-1") {
  133. status += "order fail";
  134. }
  135. else {
  136. status += "order cancel";
  137. }
  138. cout << status << endl;
  139. }
  140. }
  141. std::system("pause");
  142. std::system("cls");
  143. }
  144. // 查看所有预约
  145. void Student::showAllOrder() {
  146. orderFile od;
  147. if (od.m_Size == 0) {
  148. cout << "No order recently " << endl;
  149. std::system("pause");
  150. std::system("cls");
  151. return;
  152. }
  153. for (int i = 0; i < od.m_Size; i++) {
  154. cout << i + 1 << ">> ";
  155. cout << "stuID: " << od.m_Data[i]["stuID"];
  156. cout << " name: " << od.m_Data[i]["stuName"];
  157. cout << " date: " << day[(stoi(od.m_Data[i]["date"]) - 1)];
  158. cout << " interval: " << (od.m_Data[i]["interval"] == "1" ? "morning" : "afternoon");
  159. cout << " roomID: " << od.m_Data[i]["roomID"];
  160. string status = " status: ";
  161. if (od.m_Data[i]["status"] == "1") {
  162. status += "under validation";
  163. }
  164. else if (od.m_Data[i]["status"] == "2") {
  165. status += "order success";
  166. }
  167. else if (od.m_Data[i]["status"] == "-1") {
  168. status += "order fail";
  169. }
  170. else {
  171. status += "order cancel";
  172. }
  173. cout << status << endl;
  174. }
  175. std::system("pause");
  176. std::system("cls");
  177. }
  178. // 取消预约
  179. void Student::cancelOrder() {
  180. orderFile od;
  181. // 取消的预约,其状态需为审核中或预约成功
  182. if (od.m_Size == 0) {
  183. cout << "No order recently " << endl;
  184. std::system("pause");
  185. std::system("cls");
  186. return;
  187. }
  188. cout << "Your can cancel your orders finished or under validation, please choose: " << endl;
  189. vector<int> v;
  190. int index = 1;
  191. int j = 0;
  192. for (int i = 0; i < od.m_Size; i++) {
  193. // 先判断自身学号
  194. if (this->student_id == stoi(od.m_Data[i]["stuID"])) {
  195. // 筛选状态
  196. if (od.m_Data[i]["status"] == "1" || od.m_Data[i]["status"] == "2") {
  197. v.push_back(i);
  198. j++;
  199. cout << j << ">> ";
  200. cout << "stuID: " << od.m_Data[i]["stuID"];
  201. cout << " name: " << od.m_Data[i]["stuName"];
  202. cout << " date: " << day[(stoi(od.m_Data[i]["date"]) - 1)];
  203. cout << " interval: " << (od.m_Data[i]["interval"] == "1" ? "morning" : "afternoon");
  204. cout << " roomID: " << od.m_Data[i]["roomID"];
  205. string status = " status: ";
  206. if (od.m_Data[i]["status"] == "1") {
  207. status += "under validation";
  208. }
  209. else if (od.m_Data[i]["status"] == "2") {
  210. status += "order success";
  211. }
  212. else if (od.m_Data[i]["status"] == "-1") {
  213. status += "order fail";
  214. }
  215. else {
  216. status += "order cancel";
  217. }
  218. cout << status << endl;
  219. }
  220. }
  221. }
  222. //
  223. cout << "Please enter the No. of order you want to cancel, 0 to back " << endl;
  224. int select = 0;
  225. while (true) {
  226. cin >> select;
  227. if (select >= 0 && select <= v.size()) {
  228. if (select == 0) {
  229. break;
  230. }
  231. else {
  232. od.m_Data[v[select - 1]]["status"] = "0";
  233. od.updateOrder();
  234. cout << "cancel successfully " << endl;
  235. break;
  236. }
  237. }
  238. cout << "wrong input, please re-enter" << endl;
  239. }
  240. std::system("pause");
  241. std::system("cls");
  242. }

最后来编写教师模块,教师身份的职能如下:

教师身份登录:

  1. // 老师身份登录
  2. void teacherMenu(Identity*& teacher) {
  3. while (true) {
  4. // 父类指针调用子类共同接口
  5. teacher->openMenu();
  6. // 将父类指针转为子类指针,调用子类其他接口
  7. Teacher* tea = (Teacher*)teacher;
  8. int select = 0;
  9. cin >> select;
  10. // 查看所有预约
  11. if (select == 1) {
  12. cout << "show all orders " << endl;
  13. tea->showAllOrder();
  14. }
  15. // 审核预约
  16. else if (select == 2) {
  17. cout << "show order " << endl;
  18. tea->validOrder();
  19. }
  20. // 注销登录
  21. else {
  22. cout << "log out" << endl;
  23. return;
  24. //delete admin;
  25. }
  26. }
  27. }

老师模块的功能可以大量复用学生部分的代码。

teacher.cpp:

  1. #include "teacher.h"
  2. #include "student.h"
  3. #include "orderFile.h"
  4. #include <vector>
  5. using namespace std;
  6. // 将数字转换为星期
  7. string daylist[5] = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" };
  8. // 默认构造
  9. Teacher::Teacher() {
  10. }
  11. // 有参构造 学号、姓名、密码
  12. Teacher::Teacher(int id, std::string name, std::string pwd) {
  13. this->teacher_id = id;
  14. this->m_name = name;
  15. this->m_password = pwd;
  16. }
  17. // 菜单界面
  18. void Teacher::openMenu() {
  19. cout << "\t\t================= Welcome " << this->m_name << "! ==================" << endl;
  20. cout << "\t\t ----------------------------------------------\n";
  21. cout << "\t\t| 1. Show All Orders |\n";
  22. cout << "\t\t| |\n";
  23. cout << "\t\t| 2. Valid Order |\n";
  24. cout << "\t\t| |\n";
  25. cout << "\t\t| Other. Log Out |\n";
  26. cout << "\t\t ----------------------------------------------\n";
  27. cout << "Please enter: ";
  28. }
  29. // 查看所有预约
  30. void Teacher::showAllOrder() {
  31. orderFile od;
  32. if (od.m_Size == 0) {
  33. cout << "no order record!" << endl;
  34. std::system("pause");
  35. std::system("cls");
  36. return;
  37. }
  38. for (int i = 0; i < od.m_Size; i++) {
  39. cout << i + 1 << ">> ";
  40. cout << "stuID: " << od.m_Data[i]["stuID"];
  41. cout << " name: " << od.m_Data[i]["stuName"];
  42. cout << " date: " << daylist[(stoi(od.m_Data[i]["date"]) - 1)];
  43. cout << " interval: " << (od.m_Data[i]["interval"] == "1" ? "morning" : "afternoon");
  44. cout << " roomID: " << od.m_Data[i]["roomID"];
  45. string status = " status: ";
  46. if (od.m_Data[i]["status"] == "1") {
  47. status += "under validation";
  48. }
  49. else if (od.m_Data[i]["status"] == "2") {
  50. status += "order success";
  51. }
  52. else if (od.m_Data[i]["status"] == "-1") {
  53. status += "order fail";
  54. }
  55. else {
  56. status += "order cancel";
  57. }
  58. cout << status << endl;
  59. }
  60. system("pause");
  61. system("cls");
  62. }
  63. // 审核预约
  64. void Teacher::validOrder() {
  65. orderFile od;
  66. if (od.m_Size == 0) {
  67. cout << "no order record!" << endl;
  68. std::system("pause");
  69. std::system("cls");
  70. return;
  71. }
  72. cout << "Here are orders to be valided: " << endl;
  73. vector<int> v;
  74. int j = 0;
  75. for (int i = 0; i < od.m_Size; i++) {
  76. if (od.m_Data[i]["status"] == "1") {
  77. v.push_back(i);
  78. j++;
  79. cout << j << ">> ";
  80. cout << "stuID: " << od.m_Data[i]["stuID"];
  81. cout << " name: " << od.m_Data[i]["stuName"];
  82. cout << " date: " << daylist[(stoi(od.m_Data[i]["date"]) - 1)];
  83. cout << " interval: " << (od.m_Data[i]["interval"] == "1" ? "morning" : "afternoon");
  84. cout << " roomID: " << od.m_Data[i]["roomID"];
  85. cout << " under validation" << endl;
  86. }
  87. }
  88. cout << "Please enter the num of the order you want to do validation, 0 to back" << endl;
  89. int select = 0;
  90. int ret = 0;
  91. while (true) {
  92. cin >> select;
  93. if (select >= 0 && select <= v.size()) {
  94. if (select == 0) {
  95. break;
  96. }
  97. else {
  98. cout << "Please enter the result of validation " << endl;
  99. cout << "1. pass " << endl;
  100. cout << "2. no pass " << endl;
  101. cin >> ret;
  102. if (ret == 1) {
  103. od.m_Data[v[select - 1]]["status"] = "2";
  104. }
  105. else {
  106. od.m_Data[v[select - 1]]["status"] = "-1";
  107. }
  108. od.updateOrder();
  109. break;
  110. }
  111. }
  112. cout << "wrong input, please re-enter" << endl;
  113. }
  114. system("pause");
  115. system("cls");
  116. }

项目整体文件视图如下:

运行后登录菜单页面如下:

完整代码见下方链接

Github链接:https://github.com/Hakureiremu/ComputerRoom.git

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/182100
推荐阅读
  

闽ICP备14008679号