主頁 >  其他 > C++入門——機房預約系統

C++入門——機房預約系統

2021-09-20 09:29:31 其他

參考鏈接

  1. https://www.bilibili.com/video/BV1et411b73Z?p=282

機房預約系統

機房預約系統需求

系統簡介

學校現有幾個規格不同的機房,由于使用時經常出現撞車現象,現開發一套機房預約系統,解決這一問題

身份簡介

分別有三種身份使用該程式

  1. 學生代表:申請使用機房
  2. 教師:審核學生的預約申請
  3. 管理員:給學生、教師創建賬號

機房簡介

機房共有三間:

  1. 1號機房,最大容量20人
  2. 2號機房,最大容量50人
  3. 3號機房,最大容量100人

申請簡介

  1. 申請的訂單每周由管理員負責清空
  2. 學生可以預約未來一周內的機房使用,預約的日期為周一至周五,預約時需要預約時段(上午、下午)
  3. 教師來審核預約,依據實際情況審核預約通過或者不通過

系統具體需求

首先進入登錄界面,可選登錄身份有:

  1. 學生代表
  2. 老師
  3. 管理員
  4. 退出

每個身份都需要進行驗證后,進入子選單:

  1. 學生需要輸入:學號、姓名、登錄密碼
  2. 老師需要輸入:職工號、姓名、登錄密碼
  3. 管理員需要輸入:管理員姓名、登錄密碼

學生具體功能:

  1. 申請預約:預約機房
  2. 查看自身的預約:查看自己的預約狀態
  3. 查看所有預約:查看全部預約資訊以及預約狀態
  4. 取消預約:取消自身的預約,預約成功或審核中的預約均可取消

教師具體功能:

  1. 查看所有預約:查看全部預約資訊以及預約狀態
  2. 審核預約:對學生的預約進行審核
  3. 注銷登錄:退出登錄

管理員具體功能:

  1. 添加賬號:添加學生或教師的賬號,需要檢測學生編號或教師職工號是否重復
  2. 查看賬號:可以選擇查看學生或教師的全部資訊
  3. 查看機房:查看所有機房的資訊
  4. 清空預約:清空所有預約記錄
  5. 注銷登錄:退出登錄

創建主選單

int main()
{
    int select = 0; //用于接收用戶的選擇
    while (true)
    {
        cout << "====================  歡迎來到機房預約系統  ===================" << endl;
        cout << endl << 請輸入您的身份 << 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|          0.退    出           |\n";
        cout << "\t\t|                              |\n";
        cout << "\t\t ------------------------------\n";
        cout << "輸入您的選擇:";

        cin >> select;

        switch (select)
        {
        case 1: // 學生身份
            break;
        case 2: // 老師身份
            break;
        case 3: // 管理員身份
            break;
        case 0: // 退出
            break;
        default:
            cout << "輸入有誤,請重新選擇!" << endl;\
            system("pause");
            system("cls");
            break;
        }
    }


    system("pause");
    return 0;
}

退出功能實作

在main函式分支0選項中,添加退出程式的代碼

cout << "歡迎下一次使用" << endl;
system("pause");
return 0;

創建身份類

身份的基類

在整個系統中,有三種身份,分別為學生代表、老師以及管理員
三種身份有其共性也有其特性,因此我們可以將三種身份抽象出一個身份基類identity
在頭檔案下創建Identity.h檔案

Identity.h中添加如下代碼:

#pragma once
#include <iostream>
using namespace std;

// 身份身份抽象類
class Identity
{
public:
    // 操作選單
    virtual void operMenu() = 0;

    string m_Name; // 用戶名
    string m_Pwd; // 密碼
};

學生類

功能分析

學生類的主要功能是可以通過類中的成員函式,實作預約實驗室操作
學生類中主要功能有:

  1. 顯示學生操作的選單界面
  2. 申請預約
  3. 查看自身預約
  4. 查看所有預約
  5. 取消預約
類的創建

在頭檔案以及源檔案下創建student.h和student.cpp檔案

student.h中添加如下代碼:

#pragma once
#include <iostream>
using namespace std;
#include "Identity.h"

// 學生類
class Student :public Identity
{
public:
    // 默認構造
    Student();

    // 有參構造(學號、姓名、密碼)
    Student(int id, string name, string pwd);

    // 選單界面
    virtual void operMenu();

    // 申請預約
    void applyOrder();

    // 查看我的預約
    void showMyOrder();

    // 查看所有預約
    void showAllOrder();

    // 取消預約
    void cancelOrder();

    //學生學號
    int m_Id;
};

student.cpp中添加如下代碼:

#include "student.h"

// 默認構造
Student::Student()
{

}

// 有參構造
Student::Student(int id, string name, string pwd)
{

}

// 選單界面
void Student::operMenu()
{

}

// 申請預約
void Student::applyOrder()
{

}

// 查看我的預約
void Student::showMyOrder()
{

}

// 查看所有預約
void Student::showAllOrder()
{

}

// 取消預約
void Student::cancelOrder()
{
    
}

老師類

功能分析

教師類主要功能是查看學生的預約,并進行審核
教師類中主要功能有:

  1. 顯示教師操作的選單界面
  2. 查看所有預約
  3. 審核預約
類的創建

在頭檔案以及源檔案下創建teacher.h和teacher.cpp檔案
teacher.h中添加如下代碼:

#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
#include "Identity.h"

class Teacher :public Identity
{
public:
    // 默認構造
    Teacher();

    // 有參構造(職工編號、姓名、密碼)
    Teacher(int empId, string name, string pwd);

    // 選單界面
    virtual void operMenu();

    // 查看所有預約
    void showAllOrder();

    // 審核預約
    void validOrder();

    int m_EmpId; // 教師編號
};

teacher.cpp中添加如下代碼:

#include "teacher.h"

// 默認構造
Teacher::Teacher()
{

}

// 有參構造(職工編號、姓名、密碼)
Teacher::Teacher(int empId, string name, string pwd)
{

}

// 選單界面
void Teacher::operMenu()
{

}

// 查看所有預約
void Teacher::showAllOrder()
{

}

// 審核預約
void Teacher::validOrder()
{
    
}

管理員類

功能分析

管理員類主要功能是對學生和老師賬號進行管理,查看機房資訊以及情況預約記錄
管理員類中主要功能有:

  1. 顯示管理員操作的選單界面
  2. 添加賬號
  3. 查看賬號
  4. 查看機房資訊
  5. 清空預約記錄
類的創建

在頭檔案以及源檔案下創建manager.h和manager.cpp檔案
manager.h中添加如下代碼:

#pragma once
#include <iostream>
using namespace std;
#include "Identity.h"

class Manager :public Identity
{
public:
    // 默認構造
    Manager();

    // 有參構造 管理員資訊、密碼
    Manager(string name, string pwd);

    // 選擇選單
    virtual void operMenu();

    // 添加賬號
    void addPerson();

    // 查看賬號
    void showPerson();

    // 查看機房資訊
    void showComputer();

    // 清空預約記錄
    void cleanFile();
};

manager.cpp中添加如下代碼:

#include "manager.h"

// 默認構造
Manager::Manager()
{

}

// 有參構造
Manager::Manager(string name, string pwd)
{

}

// 選擇選單
void Manager::operMenu()
{

}

// 添加賬號
void Manager::addPerson()
{

}

// 查看賬號
void Manager::showPerson()
{

}

// 查看機房資訊
void Manager::showComputer()
{

}

// 清空預約記錄
void Manager::cleanFile()
{
    
}

登錄模塊

全域檔案添加

功能描述:

  1. 不同的身份可能會用到不同的檔案操作,將所有檔案名定義到一個全域的檔案中
  2. 在頭檔案中添加globalFile.h檔案
  3. 并添如下代碼:
#pragma once

// 管理員檔案
#define ADMIN_FILE "admin.txt"

// 學生檔案
#define STUDENT_FILE "student.txt"

// 教師檔案
#define TEACHER_FILE "teacher.txt"

// 機房資訊檔案
#define COMPUTER_FILE "computerRoom.txt"

// 訂單檔案
#define ORDER_FILE "order.txt"

登錄函式封裝

功能描述:
根據用戶的選擇,進入不同的身份登錄

在預約系統的.cpp檔案中添加全域函式void LoginIn(string fileName, int type)
引數:

  1. fileName:操作的檔案名
  2. type:登錄的身份(1代表學生、2代表老師、3代表管理員)

LoginIn中添加如下代碼:

#include "globalFile.h"
#include <fstream>
#include <string>

// 登錄功能
void LoginIn(string fileName, int type)
{
    Identity* person = NULL;

    ifstream ifs;
    ifs.open(fileName, ios::in);

    // 檔案不存在情況
    {
        cout << "檔案不存在" << endl;
        ifs.close();
        return;
    }

    int id = 0;
    string name;
    string pwd;

    if (type == 1) // 學生登錄
    {
        cout << "請輸入你的學號" << endl;
        cin >> id;
    }
    else if (type == 2) // 教師登錄
    {
        cout << "請輸入你的職工號" << endl;
        cin >> id;
    }

    cout << "請輸入用戶名:" << endl;
    cin >> name;

    cout << "請輸入密碼:" << endl;
    cin >> pwd;

    if (type == 1)
    {
        // 學生登錄驗證
    }
    else if (type == 2)
    {
        // 教師登錄驗證
    }
    else if (type == 3)
    {
        // 管理員登錄驗證
    }

    cout << "驗證登錄失敗!" << endl;

    system("pause");
    system("cls");
    return;
}

在main函式的不同分支中,填入不同的登錄介面

switch (select)
{
case 1: // 學生身份
    LoginIn(STUDENT_FILE, 1);
    break;
case 2: // 老師身份
    LoginIn(TEACHER_FILE, 2);
    break;
case 3: // 管理員身份
    LoginIn(ADMIN_FILE, 3);
    break;
case 0: // 退出
    cout << "歡迎下一次使用" << endl;
    system("pause");
    return 0;
    break;
default:
    cout << "輸入有誤,請重新選擇!" << endl;\
    system("pause");
    system("cls");
    break;
}

學生登錄實作

在LoginIn函式的學生分支中加入如下代碼:

// 學生登錄驗證
int fId;
string fName;
string fPwd;
while (ifs >> fId && ifs >> fName && ifs >> fPwd)
{
    if (id == fId && name == fName && pwd == fPwd)
    {
        cout << "學生驗證登錄成功!" << endl;
        system("pause");
        system("cls");
        person = new Student(id, name, pwd);

        return;
    }
}

教師登錄實作

在LoginIn函式的教師分支中加入如下代碼,驗證教師身份

// 教師登錄驗證
int fId;
string fName;
string fPwd;
while (ifs >> fId && ifs >> fName && ifs >> fPwd)
{
    if (id == fId && name == fName && pwd == fPwd)
    {
        cout << "教師驗證登錄成功!" << endl;
        system("pause");
        system("cls");
        person = new Teacher(id, name, pwd);
        return;
    }
}

管理員登錄實作

在Login函式的管理員分支中加入如下代碼,驗證管理員身份:

// 管理員登錄驗證
string fName;
string fPwd;
while (ifs >> fName && ifs >> fPwd)
{
    if (name == fName && pwd == fPwd)
    {
        cout << "驗證登錄成功!" << endl;
        // 登錄成功后,按任意鍵進入管理員界面
        system("pause");
        system("cls");
        // 創建管理員物件
        person = new Manager(name, pwd);
        return;
    }
}

管理員模塊

管理員登錄和注銷

建構式

在Manager類的建構式中,初始化管理員資訊,代碼如下:

// 有參構造
Manager::Manager(string name, string pwd)
{
    this->m_Name = name;
    this->m_Pwd = pwd;
}
管理員子選單

(1)在機房預約系統.cpp中,當用戶登錄的是管理員,添加管理員選單介面
(2)將不同的分支提供處理

  1. 添加賬號
  2. 查看賬號
  3. 查看機房
  4. 清空預約
  5. 注銷登錄
    (3)實作注銷功能

添加全域函式void managerMenu(Identity* &manager),代碼如下:

// 管理員選單
void managerMenu(Identity* &manager)
{
    while (true)
    {
        // 管理員選單
        manager->operMenu();

        Manager* man = (Manager*)manager;
        int select = 0;

        cin >> select;

        if (select == 1) // 添加賬號
        {
            cout << "添加賬號" << endl;
            man->addPerson();
        }
        else if (select == 2) // 查看賬號
        {
            cout << "查看賬號" << endl;
            man->showPerson();
        }
        else if (select == 3) // 查看機房
        {
            cout << "查看機房" << endl;
            man->showComputer();
        }
        else if (select == 4) // 清空預約
        {
            cout << "清空預約" << endl;
            man->cleanFile();
        }
        else
        {
            delete manager;
            cout << "注銷成功" << endl;
            system("pause");
            system("cls");
            return;
        }
    }
}
選單功能實作

在實作成員函式void Manager::operMenu()代碼如下:

// 選擇選單
void Manager::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 << "\t\t -----------------------------------\n";
    cout << "請選擇您的操作:" << endl;
}
介面對接

管理員成功登錄后,呼叫管理員子選單界面
在管理員登錄驗證分支后,添加代碼:

添加賬號

添加功能實作

在Manager的addPerson成員函式中,實作添加新賬號功能,代碼如下:

// 添加賬號
void Manager::addPerson()
{
    cout << "請輸入添加賬號的型別" << endl;
    cout << "1、添加學生" << endl;
    cout << "2、添加老師" << endl;

    string fileName;
    string tip;
    ofstream ofs;

    int select = 0;
    cin >> select;

    if (select == 1)
    {
        fileName = STUDENT_FILE;
        tip = "請輸入學號:";
    }
    else
    {
        fileName = TEACHER_FILE;
        tip = "請輸入職工編號:";
    }

    ofs.open(fileName, ios::out | ios::app);
    int id;
    string name;
    string pwd;
    cout << tip << endl;
    cin >> id;

    cout << "請輸入姓名:" << endl;
    cin >> name;

    cout << "請輸入密碼:" << endl;
    cin >> pwd;

    ofs << id << " " << name << " " << pwd << " " << endl;
    cout << "添加成功" << endl;

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

    ofs.close();
}
去重操作

(1)要去除重復的賬號,首先要先將學生和教師的賬號資訊獲取到程式中
(2)在manager.h中,添加兩個容器,用于存放學生和教師的資訊
(3)添加一個新的成員函式void initVector()初始化容器

// 初始化容器
void initVector();

// 學生容器
vector<Student> vStu;

// 教師容器
vector<Teacher> vTea;

在Manager的有參建構式中,獲取目前的學生和教師資訊
代碼如下:

void Manager::initVector()
{
    // 確保容器清空狀態
    vStu.clear();
    vTea.clear();
    
    // 獲取學生檔案中資訊
    ifstream ifs;
    ifs.open(STUDENT_FILE, ios::in);
    if (!ifs.is_open())
    {
        cout << "檔案讀取失敗" << endl;
        return;
    }

    Student s;
    while (ifs >> s.m_Id && ifs >> s.m_Name && ifs >> s.m_Pwd)
    {
        vStu.push_back(s);
    }
    cout << "當前學生數量為:" << vStu.size() << endl;
    ifs.close(); // 學生初始化

    // 讀取老師檔案資訊
    ifs.open(TEACHER_FILE, ios::in);

    Teacher t;
    while (ifs >> t.m_EmpId && ifs >> t.m_Name && ifs >> t.m_Pwd)
    {
        vTea.push_back(t);
    }
    cout << "當前教師數量為:" << vTea.size() << endl;

    ifs.close();
}

在有參建構式中,呼叫初始化容器函式

// 初始化容器
this->initVector();

在manager.h檔案中添加成員函式bool checkRepeat(int id, int type);

// 檢測重復  引數:(傳入id, 傳入型別)  回傳值:(true 代表有重復, false 代表沒有重復)
bool checkRepeat(int id, int type);

在manager.cpp中實作成員函式bool checkRepeat(int id, int type);

bool Manager::checkRepeat(int id, int type)
{
    if (type == 1)
    {
        for (vector<Student>::iterator it = vStu.begin(); it != vStu.end(); it++)
        {
            if (id == it->m_Id)
            {
                return true;
            }
        }
    }
    else
    {
        for (vector<Teacher>::iterator it = vTea.begin(); it != vTea.end(); it++)
        {
            if (id == it->m_EmpId)
            {
                return true;
            }
        }
    }
    return false;
}

在添加學生編號或教師職工號時,檢測是否有重復,代碼如下:

string errorTip; // 重復錯誤提示

if (select == 1)
{
    fileName = STUDENT_FILE;
    tip = "請輸入學號:";
    errorTip = "學號重復,請重新輸入";
}
else
{
    fileName = TEACHER_FILE;
    tip = "請輸入職工編號:";
    errorTip = "職工號重復,請重新輸入";
}

ofs.open(fileName, ios::out | ios::app);
int id;
string name;
string pwd;
cout << tip << endl;

while (true)
{
    cin >> id;

    bool ret = this->checkRepeat(id, select);
    if (ret) // 有重復
    {
        cout << errorTip << endl;
    }
    else
    {
        break;
    }
}

顯示賬號

顯示功能實作

在Manager的showPerson成員函式中,實作顯示賬號功能,代碼如下:

void printStudent(Student& s)
{
    cout << "學號:" << s.m_Id << " 姓名:" << s.m_Name << " 密碼:" << s.m_Pwd << endl;
}

void printTeacher(Teacher& t)
{
    cout << "職工號:" << t.m_EmpId << " 姓名:" << t.m_Name << " 密碼:" << t.m_Pwd << endl;
}

// 查看賬號
void Manager::showPerson()
{
    cout << "請選擇查看內容:" << endl;
    cout << "1、查看所有學生" << endl;
    cout << "2、查看所有老師" << endl;

    int select = 0;

    cin >> select;
    
    if (select == 1)
    {
        cout << "所有學生資訊如下:" << endl;
        for_each(vStu.begin(), vStu.end(), printStudent);
    }
    else
    {
        cout >> "所有老師資訊如下:" << endl;
        for_each(vTea.begin(), vTea.end(), printTeacher);
    }
    system("pause");
    system("cls");
}

查看機房

添加機房資訊

需求案例中,機房一個三個,其中1號機房容量20臺機器,2號50臺,3號100臺

將資訊錄入到computerRoom.txt中

機房類創建

在頭檔案下,創建新的檔案computerRoom.h
并添加如下代碼:

#pragma once
#include <iostream>
using namespace std;

// 機房類
class computerRoom
{
public:
    int m_ComId; // 機房id號

    int m_MaxNum; // 機房最大容量
};
初始化機房資訊

在Manager管理員類下,添加機房的容器,用于保存機房資訊

// 機房容器
vector<ComputerRoom> vCom;

在Manager有參建構式中,追加如下代碼,初始化機房資訊

// 獲取機房資訊
ifstream ifs;

ComputerRoom c;
while (ifs >> c.m_ComId && ifs >> c.m_MaxNum)
{
    vCom.push_back(c);
}
cout << "當前機房數量為:" << vCom.size() << endl;

ifs.close();
顯示機房資訊

在Manager類的showComputer成員函式中添加如下代碼:

void Manager::showComputer()
{
    cout << "機房資訊如下:" << endl;
    for (vector<ComputerRoom>::iterator it = vCom.begin(); it != vCom.end(); it++)
    {
        cout << "機房編號:" << it->m_ComId << " 機房最大容量:" << it->m_MaxNum << endl;
    }
    system("pause");
    system("cls");
}

清空預約

功能描述:清空生成的order.txt預約檔案

清空功能實作

在Manager的cleanFile成員函式下添加如下代碼:

// 清空預約記錄
void Manager::cleanFile()
{
    ofstream ofs(ORDER_FILE, ios::trunc);
    ofs.close();

    cout << "清空成功!" << endl;
    system("pause");
    system("cls");
}

學生模塊

學生登錄和注銷

建構式

在Student類的建構式中,初始化學生新,代碼如下:

// 有參構造
Student::Student(int id, string name, string pwd)
{
    // 初始化屬性
    this->m_Id = id;
    this->m_Name = name;
    this->m_Pwd = pwd;
}
管理員子選單

(1)在機房預約系統.cpp中,當用戶登錄的是學生時,添加學生選單介面
(2)將不同的分支提供出來

  1. 申請預約
  2. 查看我的預約
  3. 查看所有預約
  4. 取消預約
  5. 注銷登錄

(3)實作注銷功能

添加全域函式void studentMenu(Identiy* &student)代碼如下:

// 學生選單
void studentMenu(Identity* &student)
{
    while (true)
    {
        // 學生選單
        student->operMenu();

        Student* stu = (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 ----------------------------------\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 << "\t\t ----------------------------------\n";
    cout << "請選擇您的操作:" << endl;
}
介面對接

(1)學生成功登錄后,呼叫學生的子選單界面
(2)在學生登錄分支中,添加代碼:

// 進入學生子選單
studentMenu(person);

申請預約

獲取機房資訊

在申請預約時,學生可以看到機房的資訊,因此我們需要讓學生獲取到機房的資訊

在student.h中添加新的成員函式如下:

// 機房容器
vector<ComputerRoom> vCom;

在學生的有參建構式中追加如下代碼:

// 獲取機房資訊
ifstream ifs;
ifs.open(COMPUTER_FILE, ios::in);

ComputerRoom c;
while (ifs >> c.m_ComId && ifs >> c.m_MaxNum)
{
    vCom.push_back(c);
}

ifs.close();
預約功能實作

在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;
    cout << "1號機房容量:" << vCom[0].m_MaxNum << endl;
    cout << "2號機房容量:" << vCom[1].m_MaxNum << endl;
    cout << "3號機房容量:" << vCom[2].m_MaxNum << endl;

    while (true)
    {
        cin >> room;
        if (room >= 1 && room <= 3)
        {
            break;
        }
        cout << "輸入有誤,請重新輸入" << endl;
    }

    cout << "預約成功!審核中" << endl;

    ofstream ofs(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"); 
}

顯示預約

創建預約類

功能描述:顯示預約記錄時,需要從檔案中獲取到所有記錄,用來顯示,創建預約的類來管理記錄以及更新

在頭檔案以及源檔案下分別創建orderFile.h和orderFile.cpp檔案

orderFile.h中添加如下代碼:

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

class OrderFile
{
public:
    // 建構式
    OrderFile();

    // 更新預約記錄
    void updateOrder();

    // 記錄的容器 key--記錄的條數   value--具體記錄的鍵值對資訊
    map<int, map<string, string>> m_orderData;

    // 預約記錄條數
    int m_Size;
};

建構式中獲取所有資訊,并存放在容器中,添加如下代碼:

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)
    {
        string key;
        string value;
        map<string, string> m;

        int pos = date.find(":");
        if (pos != -1)
        {
            key = date.substr(0, pos);
            value = data.substr(pos + 1, date.size() - pos);
            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);
            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);
            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);
            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);
            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);
            m.insert(make_pair(key, value));
        }

        this->m_orderData.insert(make_pair(this->m_Size, m));
        this->m_Size++;
    }
}

更新預約記錄的成員函式updateOrder代碼如下:

void OrderFile::updateOrder()
{
    if (this->m_Size == 0)
    {
        return;
    }

    ofstream ofs(ORDER_FILE, ios::out | ios::trunc);
    for (int i = 0; i < 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"] << " ";
    }
    ofs.close();
}
顯示自身預約

在Student類的void Student::showMyOrder()成員函式中,添加如下代碼:

// 查看我的預約
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 (atoi(of.m_orderData[i]["stuId"].c_str()) == this->m_Id)
        {
            cout << "預約日期: 周" << of.m_orderData[i]["date"];
            cout << " 時段:" << (of.m_orderData[i]["interval"] == "1" ? "上午" : "下午");
            cout << " 機房:" << of.m_orderData[i]["roomId"];
            string status = " 狀態:"; // 0 取消的預約  1 審核中   2 已預約  -1 預約失敗
            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");
}
顯示所有預約

在Student類的void Student::showAllOrder()成員函式中,添加如下代碼:

// 查看所有預約
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 = " 狀態:"; // 0 取消的預約  1 審核中  2 已預約  -1 預約失敗
        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");
}

取消預約

在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 (atoi(of.m_orderData[i][stuId].c_str()) == this->m_Id)
        {
            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");
}

教師模塊

教師登錄和注銷

建構式

在Teacher類的建構式中,初始化教師資訊,代碼如下:

// 有參構造(職工編號、姓名、密碼)
Teacher::Teacher(int empId, string name, string pwd)
{
    // 初始化屬性
    this->m_EmpId = empId;
    this->m_Name = name;
    this->m_Pwd = pwd;
}
教師子選單

(1)在機房預約系統.cpp中,當用戶登錄的是教師,添加教師選單介面
(2)將不同的分支提供出來

  1. 查看所有預約
  2. 審核預約
  3. 注銷登錄

(3)實作注銷功能

添加全域函式void TeacherMenu(Person* &teacher),代碼如下:

// 教師選單
void TeacherMenu(Identity* &teacher)
{
    while (true)
    {
        // 教師選單
        teacher->operMenu();

        Teacher* tea = (Teacher*)teacher;
        int select = 0;

        cin >> select;

        if (select == 1)
        {
            // 查看所有預約
            tea->showAllOrder();
        }
        else if (select == 2)
        {
            // 審核預約
            tea->validOrder();
        }
        else
        {
            delete teacher;
            cout << "注銷成功" << endl;
            system("pause");
            system("cls");
            return;
        }
    }
}
選單功能實作

實作成員函式void Teacher::operMenu(),代碼如下:

// 選單界面
void Teacher::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|           0.注銷登錄              |\n";
    cout << "\t\t|                                  |\n";
    cout << "\t\t ----------------------------------\n";
    cout << "請選擇您的操作:" << endl;
}
介面對接

教師成功登錄后,呼叫教師的子選單界面
在教師登錄分支中,添加代碼:

// 進入教師子選單
TeacherMenu(person);

查看所有預約

所有預約功能實作

該功能與學生身份的查看所有預約功能相似,用于顯示所有預約記錄

在Teacher.cpp中實作成員函式void Teacher::showAllOrder()

// 查看所有預約
void Teacher::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 = " 狀態:"; // 0 取消的預約   1 審核中   2 已預約   -1 預約失敗
        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");
}

審核預約

審核功能實作

功能描述:教師審核學生的預約,依據實際情況審核預約

在Teacher.cpp中實作成員函式void Teacher::validOrder(),代碼如下:

// 審核預約
void Teacher::validOrder()
{
    OrderFile of;
    if (of.m_Size == 0)
    {
        cout << "無預約記錄" << endl;
        system("pause");
        system("cls");
        return;
    }
    cout << "待審核的預約記錄如下:" << endl;

    vector<int> v;
    int index = 0;
    for (int i = 0; i < of.m_Size; i++)
    {
        if (of.m_orderData[i]["status"] == "1")
        {
            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 = " 狀態:";  // 0 取消的預約   1 審核中   2 已預約   -1 預約失敗
            if (of.m_orderData[i]["status"] == "1")
            {
                status += "審核中";
            }
            cout << status << endl;
        }
    }
    cout << "請輸入審核的預約記錄,0代表回傳" << endl;
    int select = 0;
    int ret = 0;
    while (true)
    {
        cin >> select;
        if (select >= 0 && select <= v.size())
        {
            if (select == 0)
            {
                break;
            }
            else
            {
                cout << "請輸入審核結果" << endl;
                cout << "1、通過" << endl;
                cout << "2、不通過" << endl;
                cin >> ret;

                if (ret == 1)
                {
                    of.m_orderData[v[select - 1]]["status"] = "2";
                }
                else
                {
                    of.m_orderData[v[select - 1]]["status"] = "-1";
                }
                of.updateOrder();
                cout << "審核完畢!" << endl;
                break;
            }
        }
        cout << "輸入有誤,請重新輸入" << endl;
    }

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

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/301437.html

標籤:其他

上一篇:黑馬javaweb353集復雜的條件查詢-引數名sql注入案例

下一篇:科技云報道:你的密碼還安全嗎?探究密碼發展的“冰火兩重天”

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • 網閘典型架構簡述

    網閘架構一般分為兩種:三主機的三系統架構網閘和雙主機的2+1架構網閘。 三主機架構分別為內端機、外端機和仲裁機。三機無論從軟體和硬體上均各自獨立。首先從硬體上來看,三機都用各自獨立的主板、記憶體及存盤設備。從軟體上來看,三機有各自獨立的作業系統。這樣能達到完全的三機獨立。對于“2+1”系統,“2”分為 ......

    uj5u.com 2020-09-10 02:00:44 more
  • 如何從xshell上傳檔案到centos linux虛擬機里

    如何從xshell上傳檔案到centos linux虛擬機里及:虛擬機CentOs下執行 yum -y install lrzsz命令,出現錯誤:鏡像無法找到軟體包 前言 一、安裝lrzsz步驟 二、上傳檔案 三、遇到的問題及解決方案 總結 前言 提示:其實很簡單,往虛擬機上安裝一個上傳檔案的工具 ......

    uj5u.com 2020-09-10 02:00:47 more
  • 一、SQLMAP入門

    一、SQLMAP入門 1、判斷是否存在注入 sqlmap.py -u 網址/id=1 id=1不可缺少。當注入點后面的引數大于兩個時。需要加雙引號, sqlmap.py -u "網址/id=1&uid=1" 2、判斷文本中的請求是否存在注入 從文本中加載http請求,SQLMAP可以從一個文本檔案中 ......

    uj5u.com 2020-09-10 02:00:50 more
  • Metasploit 簡單使用教程

    metasploit 簡單使用教程 浩先生, 2020-08-28 16:18:25 分類專欄: kail 網路安全 linux 文章標簽: linux資訊安全 編輯 著作權 metasploit 使用教程 前言 一、Metasploit是什么? 二、準備作業 三、具體步驟 前言 Msfconsole ......

    uj5u.com 2020-09-10 02:00:53 more
  • 游戲逆向之驅動層與用戶層通訊

    驅動層代碼: #pragma once #include <ntifs.h> #define add_code CTL_CODE(FILE_DEVICE_UNKNOWN,0x800,METHOD_BUFFERED,FILE_ANY_ACCESS) /* 更多游戲逆向視頻www.yxfzedu.com ......

    uj5u.com 2020-09-10 02:00:56 more
  • 北斗電力時鐘(北斗授時服務器)讓網路資料更精準

    北斗電力時鐘(北斗授時服務器)讓網路資料更精準 北斗電力時鐘(北斗授時服務器)讓網路資料更精準 京準電子科技官微——ahjzsz 近幾年,資訊技術的得了快速發展,互聯網在逐漸普及,其在人們生活和生產中都得到了廣泛應用,并且取得了不錯的應用效果。計算機網路資訊在電力系統中的應用,一方面使電力系統的運行 ......

    uj5u.com 2020-09-10 02:01:03 more
  • 【CTF】CTFHub 技能樹 彩蛋 writeup

    ?碎碎念 CTFHub:https://www.ctfhub.com/ 筆者入門CTF時時剛開始刷的是bugku的舊平臺,后來才有了CTFHub。 感覺不論是網頁UI設計,還是題目質量,賽事跟蹤,工具軟體都做得很不錯。 而且因為獨到的金幣制度的確讓人有一種想去刷題賺金幣的感覺。 個人還是非常喜歡這個 ......

    uj5u.com 2020-09-10 02:04:05 more
  • 02windows基礎操作

    我學到了一下幾點 Windows系統目錄結構與滲透的作用 常見Windows的服務詳解 Windows埠詳解 常用的Windows注冊表詳解 hacker DOS命令詳解(net user / type /md /rd/ dir /cd /net use copy、批處理 等) 利用dos命令制作 ......

    uj5u.com 2020-09-10 02:04:18 more
  • 03.Linux基礎操作

    我學到了以下幾點 01Linux系統介紹02系統安裝,密碼啊破解03Linux常用命令04LAMP 01LINUX windows: win03 8 12 16 19 配置不繁瑣 Linux:redhat,centos(紅帽社區版),Ubuntu server,suse unix:金融機構,證券,銀 ......

    uj5u.com 2020-09-10 02:04:30 more
  • 05HTML

    01HTML介紹 02頭部標簽講解03基礎標簽講解04表單標簽講解 HTML前段語言 js1.了解代碼2.根據代碼 懂得挖掘漏洞 (POST注入/XSS漏洞上傳)3.黑帽seo 白帽seo 客戶網站被黑帽植入劫持代碼如何處理4.熟悉html表單 <html><head><title>TDK標題,描述 ......

    uj5u.com 2020-09-10 02:04:36 more
最新发布
  • 2023年最新微信小程式抓包教程

    01 開門見山 隔一個月發一篇文章,不過分。 首先回顧一下《微信系結手機號資料庫被脫庫事件》,我也是第一時間得知了這個訊息,然后跟蹤了整件事情的經過。下面是這起事件的相關截圖以及近日流出的一萬條資料樣本: 個人認為這件事也沒什么,還不如關注一下之前45億快遞資料查詢渠道疑似在近日復活的訊息。 訊息是 ......

    uj5u.com 2023-04-20 08:48:24 more
  • web3 產品介紹:metamask 錢包 使用最多的瀏覽器插件錢包

    Metamask錢包是一種基于區塊鏈技術的數字貨幣錢包,它允許用戶在安全、便捷的環境下管理自己的加密資產。Metamask錢包是以太坊生態系統中最流行的錢包之一,它具有易于使用、安全性高和功能強大等優點。 本文將詳細介紹Metamask錢包的功能和使用方法。 一、 Metamask錢包的功能 數字資 ......

    uj5u.com 2023-04-20 08:47:46 more
  • vulnhub_Earth

    前言 靶機地址->>>vulnhub_Earth 攻擊機ip:192.168.20.121 靶機ip:192.168.20.122 參考文章 https://www.cnblogs.com/Jing-X/archive/2022/04/03/16097695.html https://www.cnb ......

    uj5u.com 2023-04-20 07:46:20 more
  • 從4k到42k,軟體測驗工程師的漲薪史,給我看哭了

    清明節一過,盲猜大家已經無心上班,在數著日子準備過五一,但一想到銀行卡里的余額……瞬間心情就不美麗了。最近,2023年高校畢業生就業調查顯示,本科畢業月平均起薪為5825元。調查一出,便有很多同學表示自己又被平均了。看著這一資料,不免讓人想到前不久中國青年報的一項調查:近六成大學生認為畢業10年內會 ......

    uj5u.com 2023-04-20 07:44:00 more
  • 最新版本 Stable Diffusion 開源 AI 繪畫工具之中文自動提詞篇

    🎈 標簽生成器 由于輸入正向提示詞 prompt 和反向提示詞 negative prompt 都是使用英文,所以對學習母語的我們非常不友好 使用網址:https://tinygeeker.github.io/p/ai-prompt-generator 這個網址是為了讓大家在使用 AI 繪畫的時候 ......

    uj5u.com 2023-04-20 07:43:36 more
  • 漫談前端自動化測驗演進之路及測驗工具分析

    隨著前端技術的不斷發展和應用程式的日益復雜,前端自動化測驗也在不斷演進。隨著 Web 應用程式變得越來越復雜,自動化測驗的需求也越來越高。如今,自動化測驗已經成為 Web 應用程式開發程序中不可或缺的一部分,它們可以幫助開發人員更快地發現和修復錯誤,提高應用程式的性能和可靠性。 ......

    uj5u.com 2023-04-20 07:43:16 more
  • CANN開發實踐:4個DVPP記憶體問題的典型案例解讀

    摘要:由于DVPP媒體資料處理功能對存放輸入、輸出資料的記憶體有更高的要求(例如,記憶體首地址128位元組對齊),因此需呼叫專用的記憶體申請介面,那么本期就分享幾個關于DVPP記憶體問題的典型案例,并給出原因分析及解決方法。 本文分享自華為云社區《FAQ_DVPP記憶體問題案例》,作者:昇騰CANN。 DVPP ......

    uj5u.com 2023-04-20 07:43:03 more
  • msf學習

    msf學習 以kali自帶的msf為例 一、msf核心模塊與功能 msf模塊都放在/usr/share/metasploit-framework/modules目錄下 1、auxiliary 輔助模塊,輔助滲透(埠掃描、登錄密碼爆破、漏洞驗證等) 2、encoders 編碼器模塊,主要包含各種編碼 ......

    uj5u.com 2023-04-20 07:42:59 more
  • Halcon軟體安裝與界面簡介

    1. 下載Halcon17版本到到本地 2. 雙擊安裝包后 3. 步驟如下 1.2 Halcon軟體安裝 界面分為四大塊 1. Halcon的五個助手 1) 影像采集助手:與相機連接,設定相機引數,采集影像 2) 標定助手:九點標定或是其它的標定,生成標定檔案及內參外參,可以將像素單位轉換為長度單位 ......

    uj5u.com 2023-04-20 07:42:17 more
  • 在MacOS下使用Unity3D開發游戲

    第一次發博客,先發一下我的游戲開發環境吧。 去年2月份買了一臺MacBookPro2021 M1pro(以下簡稱mbp),這一年來一直在用mbp開發游戲。我大致分享一下我的開發工具以及使用體驗。 1、Unity 官網鏈接: https://unity.cn/releases 我一般使用的Apple ......

    uj5u.com 2023-04-20 07:40:19 more