美文网首页
C++ STL容器实现机房预约系统

C++ STL容器实现机房预约系统

作者: FredricZhu | 来源:发表于2021-11-26 17:05 被阅读0次

需求分析


image.png

程序目录结构


image.png

CMakeLists.txt

cmake_minimum_required(VERSION 2.6)

if(APPLE)
    message(STATUS "This is Apple, do nothing.")
elseif(UNIX)
    message(STATUS "This is linux, set CMAKE_PREFIX_PATH.")
    set(CMAKE_PREFIX_PATH /vcpkg/ports/cppwork/vcpkg_installed/x64-linux/share)
endif(APPLE)

project(book_mach)

add_definitions(-std=c++17)

add_definitions(-g)

find_package(ZLIB)

find_package(glog REQUIRED)

find_package(OpenCV REQUIRED )

find_package(Boost REQUIRED COMPONENTS
    system
    filesystem
    serialization
    program_options
    thread
    )

find_package(DataFrame REQUIRED)

if(APPLE)
    MESSAGE(STATUS "This is APPLE, set INCLUDE_DIRS")
set(INCLUDE_DIRS ${Boost_INCLUDE_DIRS} /usr/local/include /usr/local/iODBC/include /opt/snowflake/snowflakeodbc/include/ ${CMAKE_CURRENT_SOURCE_DIR}/../../include/ ${CMAKE_CURRENT_SOURCE_DIR}/include/)
elseif(UNIX)
    MESSAGE(STATUS "This is linux, set INCLUDE_DIRS")
    set(INCLUDE_DIRS ${Boost_INCLUDE_DIRS} /usr/local/include ${CMAKE_CURRENT_SOURCE_DIR}/../../include/ ${CMAKE_CURRENT_SOURCE_DIR}/include/)
endif(APPLE)


if(APPLE)
    MESSAGE(STATUS "This is APPLE, set LINK_DIRS")
    set(LINK_DIRS /usr/local/lib /usr/local/iODBC/lib /opt/snowflake/snowflakeodbc/lib/universal)
elseif(UNIX)
    MESSAGE(STATUS "This is linux, set LINK_DIRS")
    set(LINK_DIRS ${Boost_INCLUDE_DIRS} /usr/local/lib /vcpkg/ports/cppwork/vcpkg_installed/x64-linux/lib)
endif(APPLE)

if(APPLE)
    MESSAGE(STATUS "This is APPLE, set ODBC_LIBS")
    set(ODBC_LIBS iodbc iodbcinst)
elseif(UNIX)
    MESSAGE(STATUS "This is linux, set LINK_DIRS")
    set(ODBC_LIBS odbc odbcinst ltdl)
endif(APPLE)

include_directories(${INCLUDE_DIRS})
LINK_DIRECTORIES(${LINK_DIRS})

file( GLOB main_file_list ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp) 

file( GLOB APP_SOURCES  ${CMAKE_CURRENT_SOURCE_DIR}/*.h ${CMAKE_CURRENT_SOURCE_DIR}/*.hpp ${CMAKE_CURRENT_SOURCE_DIR}/../../include/http/impl/*.cpp ${CMAKE_CURRENT_SOURCE_DIR}/../../include/yaml/impl/*.cpp ${CMAKE_CURRENT_SOURCE_DIR}/../../include/df/impl/*.cpp ${CMAKE_CURRENT_SOURCE_DIR}/../../include/death_handler/impl/*.cpp  ${CMAKE_CURRENT_SOURCE_DIR}/include/utils/*.h ${CMAKE_CURRENT_SOURCE_DIR}/include/utils/*.hpp ${CMAKE_CURRENT_SOURCE_DIR}/impl/utils/*.cpp ${CMAKE_CURRENT_SOURCE_DIR}/impl/beans/*.cpp)

add_library(${PROJECT_NAME}_lib SHARED ${APP_SOURCES})
target_link_libraries(${PROJECT_NAME}_lib ${Boost_LIBRARIES} ZLIB::ZLIB glog::glog DataFrame::DataFrame ${OpenCV_LIBS})
target_link_libraries(${PROJECT_NAME}_lib  ssl crypto libgtest.a pystring libyaml-cpp.a libgmock.a ${ODBC_LIBS} libnanodbc.a pthread dl backtrace)

foreach( main_file ${main_file_list} )
    file(RELATIVE_PATH filename ${CMAKE_CURRENT_SOURCE_DIR} ${main_file})
    string(REPLACE ".cpp" "" file ${filename})
    add_executable(${file}  ${main_file})
    target_link_libraries(${file} ${PROJECT_NAME}_lib)
endforeach( main_file ${main_file_list})

main.cpp

#include <cstdlib>
#include <iostream>

#include "utils/func.h"
#include "utils/consts.h"

int main(int argc, char* argv[]) {
    int select = 0;

    while (true) {
        std::cout << "===================欢迎来到Fredric的机房预约系统========="
                     "=============="
                  << std::endl;
        std::cout << std::endl << "请输入您的身份" << std::endl;
        std::cout << "\t\t---------------------------------------------\n";
        std::cout << "\t\t|                                            |\n";
        std::cout << "\t\t|               1.学生代表                    |\n";
        std::cout << "\t\t|                                            |\n";
        std::cout << "\t\t|               2.老师                        |\n";
        std::cout << "\t\t|                                            |\n";
        std::cout << "\t\t|               3.管理员                      |\n";
        std::cout << "\t\t|                                            |\n";
        std::cout << "\t\t|               0.退出                        |\n";
        std::cout << "\t\t---------------------------------------------\n";
        std::cout << "请输入您的选择: ";

        std::cin >> select;
        switch(select) {
            case 1:  // 学生身份
                Funcs::login_in(student_file, 1);
                break;
            case 2:  // 老师身份
                Funcs::login_in(teacher_file, 2);
                break;
            case 3:  //  管理员
                Funcs::login_in(admin_file, 3);
                break;
            case 0:  //  退出系统
                std::cout << "欢迎下次使用" << std::endl;
                Funcs::pause();
                return EXIT_SUCCESS;
                break;
            default:
                std::cout << "输入有误,请重新选择!" << std::endl;
                Funcs::pause();
                Funcs::clear();
                break;
        }

    }

    Funcs::pause();
    return EXIT_SUCCESS;
}

include/utils/consts.h

#ifndef _FREDRIC_CONSTS_H_
#define _FREDRIC_CONSTS_H_
#include <string>

extern std::string admin_file;
extern std::string student_file;
extern std::string teacher_file;
extern std::string computer_file;
extern std::string s_order_file;

#endif

include/utils/func.h

#ifndef _FREDIRC_FUNCS1_H_
#define _FREDIRC_FUNCS1_H_

#include <string>
#include "beans/identity.h"

namespace Funcs {
    void pause();
    void clear();
    void login_in(std::string file_name, int type);
    void manager_menu(identity* & man_);
    void student_menu(identity* & stu_);
    void teacher_menu(identity* & tea_);
};

#endif

include/beans/computer_room.h

#ifndef _FREDRIC_COMPUTER_ROOM_H_
#define _FREDRIC_COMPUTER_ROOM_H_

struct computer_room {
    int m_room_id;  // 机房编号
    int m_max_num;  // 最大容量
};
#endif

include/beans/identity.h

#ifndef _FREDRIC_IDENTITY_H_
#define _FREDRIC_IDENTITY_H_

#include <string>

struct identity {
    // 操作菜单
    virtual void oper_menu() = 0;
    
    std::string m_name;
    std::string m_pwd;
}; 
#endif

include/beans/manager.h

#ifndef _FREDRIC_MANAGER_H_
#define _FREDRIC_MANAGER_H_
#include "beans/identity.h"
#include "beans/student.h"
#include "beans/teacher.h"
#include "beans/computer_room.h"

#include <vector>

struct manager: public identity {
    manager();
    manager(std::string name, std::string pwd);
    
    virtual void oper_menu();
    // 添加账号
    void add_person();
    // 查看账号
    void show_person();
    // 查看机房信息
    void show_computer();
    // 清空预约记录
    void clean_file();

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

    bool check_repeat(int id, int type);

    std::vector<student> v_stus;
    std::vector<teacher> v_teas;
    std::vector<computer_room> v_coms;
};
#endif

include/beans/order_file.h

#ifndef _FREDRIC_ORDER_FILE_H_
#define _FREDRIC_ORDER_FILE_H_

#include <map>
#include <string>

struct order_file {
    order_file();

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

    // key 记录的条数   value 具体记录的键值对
    std::map<int, std::map<std::string, std::string>> m_order_data;

    // 预约记录的条数
    int m_size;
};
#endif

include/beans/student.h

#ifndef _FREDRIC_STUDENT_H_
#define _FREDRIC_STUDENT_H_

#include "beans/identity.h"
#include "beans/computer_room.h"

#include <vector>


struct student: public identity {
    student();
    student(int id, std::string name, std::string pwd);
    virtual void oper_menu();

    // 申请预约
    void apply_order();
    // 查看我的预约
    void show_my_order();
    // 查看所有预约
    void show_all_order();
    // 取消预约
    void cancel_order();
    // 学生学号
    int m_id;

    std::vector<computer_room> v_coms;
};
#endif

include/beans/teacher.h

#ifndef _FREDRIC_TEACHER_H_
#define _FREDRIC_TEACHER_H_

#include <string>
#include "beans/identity.h"

struct teacher: public identity {
    teacher();
    teacher(int emp_id, std::string name, std::string pwd);
    virtual void oper_menu();

    // 查看所有预约
    void show_all_order();
    // 审核预约
    void valid_order();

    int m_emp_id;
};
#endif

impl/utils/consts.h

#include "utils/consts.h"

std::string admin_file = "../datas/admin.txt";
std::string student_file = "../datas/student.txt";
std::string teacher_file = "../datas/teacher.txt";
std::string computer_file = "../datas/computer_room.txt";
std::string s_order_file = "../datas/order.txt";

impl/utils/func.cpp

#include "utils/func.h"
#include "beans/identity.h"
#include "beans/student.h"
#include "beans/teacher.h"
#include "beans/manager.h"

#include <iostream>
#include <memory>
#include <fstream>

void Funcs::pause() {
#ifdef WINDOWS
    (void)std::system("pause");
#else
    (void)std::system("read");
#endif
}

void Funcs::clear() {
#ifdef WINDOWS
    (void)std::system("cls");
#else
    (void)std::system("clear");
#endif
}

void Funcs::login_in(std::string file_name, int type) {
    std::unique_ptr<identity> person = nullptr;
    std::ifstream ifs;
    ifs.open(file_name, std::ios::in);

    // 文件不存在的情况
    if(!ifs.is_open()) {
        std::cout << "文件不存在"  << std::endl;
        ifs.close();
        return;
    }

    int id = 0;
    std::string name;
    std::string pwd;

    if(type == 1) {
        std::cout << "请输入你的学号" << std::endl;
        std::cin >> id;
    } else if(type == 2) {
        std::cout << "请输入你的职工号" << std::endl;
        std::cin >> id;
    }
    std::cout << "请输入用户名:" << std::endl;
    std::cin >> name;
    std::cout << "请输入密码:" << std::endl;
    std::cin >> pwd;

    // 学生登录验证
    if(type == 1) {
        int f_id;
        std::string f_name;
        std::string f_pwd;
        while(ifs >> f_id && ifs >> f_name && ifs >> f_pwd) {
            if(id == f_id && name == f_name && pwd == f_pwd) {
                std::cout << "学生验证登录成功!" << std::endl;
                Funcs::pause();
                Funcs::clear();
                person = std::make_unique<student>(id, name, pwd);
                auto person_p = person.get();
                student_menu(person_p);
                return;
            }
        }
    } else if(type == 2) {
        // 教师登录验证
        int f_id;
        std::string f_name;
        std::string f_pwd;
        while(ifs >> f_id && ifs >> f_name && ifs >> f_pwd) {
            if(id == f_id && name == f_name && pwd == f_pwd) {
                std::cout << "教师验证登录成功!" << std::endl;
                Funcs::pause();
                Funcs::clear();
                person = std::make_unique<teacher>(id, name, pwd);
                auto person_p = person.get();
                teacher_menu(person_p);
                return;
            }
        }
    } else if(type == 3) {
        // 管理员登录验证
        std::string f_name;
        std::string f_pwd;
        while(ifs >> f_name && ifs >> f_pwd) {
            if(name == f_name && pwd == f_pwd) {
                std::cout << "管理员验证登录成功!" << std::endl;
                Funcs::pause();
                Funcs::clear();
                person = std::make_unique<manager>(name, pwd);
                auto person_p = person.get();
                manager_menu(person_p);
                return;
            }
        }
    }
    Funcs::pause();
    Funcs::clear();
    return;
}

void Funcs::manager_menu(identity* & man_) {
    while(true) {
        // 管理员菜单虚函数
        man_->oper_menu();
        manager* man = dynamic_cast<manager*>(man_);
        int select = 0;
        std::cin >> select;
        switch(select) {
            case 1: // 添加账号
                std::cout << "添加账号" << std::endl;
                man->add_person();
                break;
            case 2: // 查看账号
                std::cout << "查看账号" << std::endl;
                man->show_person();
                break;
            case 3: // 查看机房
                std::cout << "查看机房" << std::endl;
                man->show_computer();
                break;
            case 4:
                std::cout << "清空预约" << std::endl;
                man->clean_file();
                break;
            default:    
                std::cout << "注销成功" << std::endl;
                Funcs::pause();
                Funcs::clear();
                return;
        }
    }
}

void Funcs::student_menu(identity* & stu_) {
    while(true) {
        // 学生菜单
        stu_->oper_menu();

        student* stu = dynamic_cast<student*>(stu_);
        int select = 0;

        std::cin >> select;

        switch (select)
        {
        case 1: // 申请预约
            stu->apply_order();
            break;
        case 2: // 查看自身预约
            stu->show_my_order();
            break;
        case 3: // 查看所有预约
            stu->show_all_order();
            break;
        case 4: // 取消预约
            stu->cancel_order();
            break;
        default:
            std::cout << "注销成功" << std::endl;
            Funcs::pause();
            Funcs::clear();
            return;
        }
    }
}

void Funcs::teacher_menu(identity* & tea_) {
    while(true) {
        // 教师菜单
        tea_->oper_menu();
        teacher* tea = dynamic_cast<teacher*>(tea_);

        int select = 0;

        std::cin >> select;

        switch(select) {
            case 1: // 查看所有预约
                tea->show_all_order();
                break;
            case 2: // 审核预约
                tea->valid_order();
                break;
            default: // 退出
                std::cout << "注销成功" << std::endl;
                Funcs::pause();
                Funcs::clear();
                return;
        }
    }
}

impl/beans/manager.cpp

#include "beans/manager.h"
#include "utils/consts.h"
#include "utils/func.h"

#include <iostream>
#include <fstream>
#include <algorithm>

manager::manager() {}

manager::manager(std::string name, std::string pwd) {
    m_name = name;
    m_pwd = pwd;
    
    init_vector();

    // 读取机房信息
    std::ifstream ifs;
    ifs.open(computer_file, std::ios::in);
    computer_room c;
    while(ifs >> c.m_room_id && ifs >> c.m_max_num) {
        v_coms.emplace_back(std::move(c));
    }
    std::cout << "当前机房数量为: " << v_coms.size() << std::endl;
    ifs.close();
}

void manager::init_vector() {
    std::ifstream ifs;
    ifs.open(student_file, std::ios::in);
    if(!ifs.is_open()) {
        std::cout << "读取文件失败" << std::endl;
        return;
    }

    v_stus.clear();
    v_teas.clear();

    student s;
    while(ifs >> s.m_id && ifs >> s.m_name && ifs >> s.m_pwd) {
        v_stus.emplace_back(std::move(s));
    }

    std::cout << "当前学生数量为: " << v_stus.size() << std::endl;
    ifs.close();

    ifs.open(teacher_file, std::ios::in);

    if(!ifs.is_open()) {
        std::cout << "读取教师文件失败" << std::endl;
        return;
    }
    teacher t;
    while(ifs >> t.m_emp_id && ifs >> t.m_name && ifs >> t.m_pwd) {
        v_teas.emplace_back(std::move(t));
    }
    
    std::cout << "当前教师数量为: " << v_teas.size() << std::endl;
    ifs.close();
}

void manager::oper_menu() {
    std::cout << "欢迎管理员: " << m_name << "登录!" << std::endl;
    std::cout << "\t\t---------------------------------------------\n";
    std::cout << "\t\t|                                            |\n";
    std::cout << "\t\t|               1.添加账号                    |\n";
    std::cout << "\t\t|                                            |\n";
    std::cout << "\t\t|               2.查看账号                     |\n";
    std::cout << "\t\t|                                            |\n";
    std::cout << "\t\t|               3.查看机房                     |\n";
    std::cout << "\t\t|                                            |\n";
    std::cout << "\t\t|               4.清空预约                     |\n";
    std::cout << "\t\t|                                            |\n";
    std::cout << "\t\t|               5.注销登录                    |\n";
    std::cout << "\t\t|                                            |\n";
    std::cout << "\t\t---------------------------------------------\n";
    std::cout << "请选择您的操作: " << std::endl;
}

// 添加账号
void manager::add_person() {
    std::cout << "请输入添加账号的类型" << std::endl;
    std::cout << "1.添加学生" << std::endl;
    std::cout << "2.添加老师" << std::endl;

    std::string error_tip;
    std::string filename;
    std::string tip;
    std::ofstream ofs;

    int select = 0;
    std::cin >> select;

    if(select == 1) {
        filename = student_file;
        tip = "请输入学号:";
        error_tip = "学号重复,清重新输入";
    } else {
        filename = teacher_file;
        tip = "请输入职工编号:";
        error_tip = "职工号重复,请重新输入";
    }

    ofs.open(filename, std::ios::out | std::ios::app);
    int id;
    std::string name;
    std::string pwd;
   
    std::cout << tip << std::endl; 
    while(true) {
        std::cin >> id;
        bool ret = check_repeat(id, select);
        if(ret) { // 有重复
            std::cout << error_tip << std::endl;
        } else {
            break;
        }
    }

    std::cout << "请输入姓名:" << std::endl; 
    std::cin >> name;
    
    std::cout << "请输入密码:" << std::endl; 
    std::cin >> pwd;

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

    ofs.close();
    Funcs::pause();
    Funcs::clear();

    init_vector();
}

// 查看账号
void manager::show_person() {
    std::cout << "请选择查看内容:" << std::endl;
    std::cout << "1.查看所有学生" << std::endl;
    std::cout << "2.查看所有老师" << std::endl;

    int select = 0;
    std::cin >> select;
    if(select == 1) { //查看所有学生信息
        std::for_each(v_stus.begin(), v_stus.end(), [](const student& s){
            std::cout << "学号: " << s.m_id << " 姓名: " << s.m_name << " 密码:" << s.m_pwd << std::endl;
        });
    }else { //查看所有老师信息
        std::for_each(v_teas.begin(), v_teas.end(), [](const teacher& t){
            std::cout << "职工号: " << t.m_emp_id << " 姓名: " << t.m_name << " 密码: " << t.m_pwd << std::endl;
        });
    }
    Funcs::pause();
    Funcs::clear();
}

// 查看机房信息
void manager::show_computer() {
    std::cout << "机房信息如下: " << std::endl;
    for(auto&& com: v_coms) {
        std::cout << "机房编号: " << com.m_room_id << " 机房最大容量: " << com.m_max_num << std::endl;
    }
    Funcs::pause();
    Funcs::clear();
}

// 清空预约记录
void manager::clean_file() {
    std::ofstream ofs {s_order_file, std::ios::trunc};
    ofs.close();
    std::cout << "清空成功!" << std::endl;
    Funcs::pause();
    Funcs::clear();
}

bool manager::check_repeat(int id, int type) {
    if(type == 1) {
        for(auto&& s: v_stus) {
            if(s.m_id == id) {
                return true;
            }
        }
    } else {
        for(auto&& t: v_teas) {
            if(t.m_emp_id == id) {
                return true;
            }
        }
    }
    return false;
}

impl/beans/order_file.cpp

#include "beans/order_file.h"
#include "utils/consts.h"
#include "pystring/pystring.h"

#include <fstream>
#include <vector>

order_file::order_file() {
    std::ifstream ifs;
    ifs.open(s_order_file, std::ios::in);
    
    std::string date, interval, stu_id, stu_name, room_id, status;

    m_size = 0;

    while(ifs >> date && ifs >> interval && ifs >> stu_id && ifs >> stu_name && ifs >> room_id && ifs >> status) {
        std::map<std::string, std::string> m;

        std::vector<std::string> res_arr{};
        pystring::split(date, res_arr, ":");
        if(res_arr.size() == 2) {
            m.insert({res_arr[0], res_arr[1]});
        }

        pystring::split(interval, res_arr, ":");
        if(res_arr.size() == 2) {
            m.insert({res_arr[0], res_arr[1]});
        }

        pystring::split(stu_id, res_arr, ":");
        if(res_arr.size() == 2) {
            m.insert({res_arr[0], res_arr[1]});
        }

        pystring::split(stu_name, res_arr, ":");
        if(res_arr.size() == 2) {
            m.insert({res_arr[0], res_arr[1]});
        }

        pystring::split(room_id, res_arr, ":");
        if(res_arr.size() == 2) {
            m.insert({res_arr[0], res_arr[1]});
        }

        pystring::split(status, res_arr, ":");
        if(res_arr.size() == 2) {
            m.insert({res_arr[0], res_arr[1]});
        }

        m_order_data.insert({m_size, std::move(m)});
        ++m_size;
    }
}

// 更新预约记录
void order_file::update_order() {
    if(m_size == 0) {
        return;
    }

    std::ofstream ofs {s_order_file, std::ios::out | std::ios::trunc};
    for(int i=0; i<m_size; ++i) {
        ofs << "date:" << m_order_data[i]["date"] << " ";
        ofs << "interval:" << m_order_data[i]["interval"] << " ";
        ofs << "stu_id:" <<  m_order_data[i]["stu_id"] << " ";
        ofs << "stu_name:" << m_order_data[i]["stu_name"]  << " ";
        ofs << "room_id:" << m_order_data[i]["room_id"]  << " ";
        ofs << "status:" << m_order_data[i]["status"]  << std::endl;
    }
    ofs.close();
}

impl/beans/student.cpp

#include "beans/student.h"
#include "utils/consts.h"
#include "utils/func.h"
#include "beans/order_file.h"

#include <iostream>
#include <fstream>
#include <sstream>

student::student() {

}

student::student(int id, std::string name, std::string pwd) {
    m_id = id;
    m_name = name;
    m_pwd = pwd;

    std::ifstream ifs;
    ifs.open(computer_file, std::ios::in);

    computer_room c;
    while(ifs >> c.m_room_id && ifs >> c.m_max_num) {
        v_coms.emplace_back(std::move(c));
    }
    ifs.close();
}

void student::oper_menu() {
    std::cout << "欢迎学生代表: " << m_name << "登录!" << std::endl;
    std::cout << "\t\t---------------------------------------------\n";
    std::cout << "\t\t|                                            |\n";
    std::cout << "\t\t|               1.申请预约                    |\n";
    std::cout << "\t\t|                                            |\n";
    std::cout << "\t\t|               2.查看我的预约                 |\n";
    std::cout << "\t\t|                                            |\n";
    std::cout << "\t\t|               3.查看所有预约                 |\n";
    std::cout << "\t\t|                                            |\n";
    std::cout << "\t\t|               4.取消预约                     |\n";
    std::cout << "\t\t|                                            |\n";
    std::cout << "\t\t|               0.注销登录                    |\n";
    std::cout << "\t\t|                                            |\n";
    std::cout << "\t\t---------------------------------------------\n";
    std::cout << "请选择您的操作: " << std::endl;
}

// 申请预约
void student::apply_order() {
    std::cout << "机房开放时间为周一至周五!" << std::endl;
    std::cout << "请输入申请预约的时间:" << std::endl;

    std::cout << "1.周一" << std::endl;
    std::cout << "2.周二" << std::endl;
    std::cout << "3.周三" << std::endl;
    std::cout << "4.周四" << std::endl;
    std::cout << "5.周五" << std::endl;

    int date = 0;  // 周一 --> 周五
    int interval = 0; // 上午,下午
    int room = 0;  // 房间号

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

    std::cout << "请输入申请预约的时间段:" << std::endl;

    std::cout << "1.上午" << std::endl;
    std::cout << "2.下午" << std::endl;

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

    std::cout << "请选择机房:" << std::endl;

    std::cout << "1号机房容量" << v_coms[0].m_max_num << std::endl;
    std::cout << "2号机房容量" << v_coms[1].m_max_num << std::endl;
    std::cout << "3号机房容量" << v_coms[2].m_max_num << std::endl;

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

    std::cout << "预约成功!审核中" << std::endl;
    
    std::ofstream ofs(s_order_file, std::ios::app);
    ofs << "date:" << date << " ";
    ofs << "interval:" << interval << " ";
    ofs << "stu_id:" << m_id << " ";
    ofs << "stu_name:" << m_name << " ";
    ofs << "room_id:" << room << " ";
    ofs << "status:" << 1 << std::endl;

    ofs.close();

    Funcs::pause();
    Funcs::clear();
}

// 查看我的预约
void student::show_my_order() {
    order_file of;
    if(of.m_size == 0) {
        std::cout << "无预约记录" << std::endl;
        Funcs::pause();
        Funcs::clear();
        return;
    }

    for(int i=0; i<of.m_size; ++i) {
        // 是当前学生的订单
        if(std::atoi(of.m_order_data[i]["stu_id"].c_str()) == m_id) {
            std::cout << "预约日期: 周" << of.m_order_data[i]["date"];
            std::cout << " 时段: " << (of.m_order_data[i]["interval"] == "1" ? "上午": "下午");
            std::cout << " 机房: " << (of.m_order_data[i]["room_id"]);

            std::stringstream status_ss;
            status_ss << " 状态: "; // 0 取消预约 1 审核中 2 已预约 -1 预约失败
            if(of.m_order_data[i]["status"] == "1") {
                status_ss << "审核中";
            } else if(of.m_order_data[i]["status"] == "2") {
                status_ss << "预约成功";
            } else if(of.m_order_data[i]["status"] == "-1") {
                status_ss << "审核未通过,预约失败";
            } else {
                status_ss << "预约已取消";
            }

            std::cout << status_ss.str() << std::endl;
        }
    }

    Funcs::pause();
    Funcs::clear();
}

// 查看所有预约
void student::show_all_order() {
    order_file of;
    if(of.m_size == 0) {
        std::cout << "无预约记录" << std::endl;
        Funcs::pause();
        Funcs::clear();
        return;
    }

    for(int i=0; i<of.m_size; ++i) {
        std::cout << (i+1) << ".";
        std::cout << "预约日期: 周" << of.m_order_data[i]["date"];
        std::cout << " 时段: " << (of.m_order_data[i]["interval"] == "1" ? "上午": "下午");
        std::cout << " 学号: " << (of.m_order_data[i]["stu_id"]);
        std::cout << " 姓名: " << (of.m_order_data[i]["stu_name"]);
        std::cout << " 机房: " << (of.m_order_data[i]["room_id"]);

        std::stringstream status_ss;
        status_ss << " 状态: "; // 0 取消预约 1 审核中 2 已预约 -1 预约失败
        if(of.m_order_data[i]["status"] == "1") {
            status_ss << "审核中";
        } else if(of.m_order_data[i]["status"] == "2") {
            status_ss << "预约成功";
        } else if(of.m_order_data[i]["status"] == "-1") {
            status_ss << "审核未通过,预约失败";
        } else {
            status_ss << "预约已取消";
        }

        std::cout << status_ss.str() << std::endl;
    }

    Funcs::pause();
    Funcs::clear();
}

// 取消预约
void student::cancel_order() {
    order_file of;
    if(of.m_size == 0) {
        std::cout << "无预约记录" << std::endl;
        Funcs::pause();
        Funcs::clear();
        return;
    }

    std::cout << "审核中或者预约成功的记录可以取消,请输入取消的记录" << std::endl;

    // 所有能被取消的记录索引
    std::vector<int> v;
    int index = 1;

    for(int i=0; i<of.m_size; ++i) {
        // 是当前学生的订单
        if(std::atoi(of.m_order_data[i]["stu_id"].c_str()) == m_id) {
            if(of.m_order_data[i]["status"] == "1" || of.m_order_data[i]["status"] == "2") {
                v.push_back(i);
                std::cout << index ++ << "."; 
                std::cout << "预约日期: 周" << of.m_order_data[i]["date"];
                std::cout << " 时段: " << (of.m_order_data[i]["interval"] == "1" ? "上午": "下午");
                std::cout << " 机房: " << (of.m_order_data[i]["room_id"]);

                std::stringstream status_ss;
                status_ss << " 状态: "; // 0 取消预约 1 审核中 2 已预约 -1 预约失败
                if(of.m_order_data[i]["status"] == "1") {
                    status_ss << "审核中";
                } else if(of.m_order_data[i]["status"] == "2") {
                    status_ss << "预约成功";
                } 
                std::cout << status_ss.str() << std::endl;
            }
        }
    }

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

    int select = 0;
    while(true) {
        std::cin >> select;
        if(select >= 0 && select <= v.size()) {
            if(select == 0) {
                break;
            } else {
                of.m_order_data[v[select-1]]["status"] = "0";
                of.update_order();
                std::cout << "已取消预约" << std::endl;
                break;
            }
        }
        std::cout << "输入有误,请重新输入" << std::endl;
    }

    Funcs::pause();
    Funcs::clear();
}

impl/beans/teacher.cpp

#include "beans/teacher.h"
#include "beans/order_file.h"
#include "utils/func.h"

#include <iostream>
#include <sstream>
#include <vector>

teacher::teacher() {

}

teacher::teacher(int emp_id, std::string name, std::string pwd) {
    m_emp_id = emp_id;
    m_name = name;
    m_pwd = pwd;
}

void teacher::oper_menu() {
    std::cout << "欢迎教师: " << m_name << "登录!" << std::endl;
    std::cout << "\t\t---------------------------------------------\n";
    std::cout << "\t\t|                                            |\n";
    std::cout << "\t\t|               1.查看所有预约                 |\n";
    std::cout << "\t\t|                                            |\n";
    std::cout << "\t\t|               2.审核预约                    |\n";
    std::cout << "\t\t|                                            |\n";
    std::cout << "\t\t|               0.注销登录                    |\n";
    std::cout << "\t\t|                                            |\n";
    std::cout << "\t\t---------------------------------------------\n";
    std::cout << "请选择您的操作: " << std::endl;
}

// 查看所有预约
void teacher::show_all_order() {
    order_file of;
    if(of.m_size == 0) {
        std::cout << "无预约记录" << std::endl;
        Funcs::pause();
        Funcs::clear();
        return;
    }

    for(int i=0; i<of.m_size; ++i) {
        std::cout << (i+1) << ".";
        std::cout << "预约日期: 周" << of.m_order_data[i]["date"];
        std::cout << " 时段: " << (of.m_order_data[i]["interval"] == "1" ? "上午": "下午");
        std::cout << " 学号: " << (of.m_order_data[i]["stu_id"]);
        std::cout << " 姓名: " << (of.m_order_data[i]["stu_name"]);
        std::cout << " 机房: " << (of.m_order_data[i]["room_id"]);

        std::stringstream status_ss;
        status_ss << " 状态: "; // 0 取消预约 1 审核中 2 已预约 -1 预约失败
        if(of.m_order_data[i]["status"] == "1") {
            status_ss << "审核中";
        } else if(of.m_order_data[i]["status"] == "2") {
            status_ss << "预约成功";
        } else if(of.m_order_data[i]["status"] == "-1") {
            status_ss << "审核未通过,预约失败";
        } else {
            status_ss << "预约已取消";
        }

        std::cout << status_ss.str() << std::endl;
    }

    Funcs::pause();
    Funcs::clear();
}

// 审核预约
void teacher::valid_order() {
    order_file of;
    if(of.m_size == 0) {
        std::cout << "无预约记录" << std::endl;
        Funcs::pause();
        Funcs::clear();
        return;
    }

    std::cout << "待审核的预约记录如下:" << std::endl;

    // 所有能被取消的记录索引
    std::vector<int> v;
    int index = 1;

    for(int i=0; i<of.m_size; ++i) {
        // 不管是哪个学生的订单,都可以审核,只能审核 审核中的订单
        if(of.m_order_data[i]["status"] == "1") {
            v.push_back(i);
            std::cout << index ++ << "."; 
            std::cout << "预约日期: 周" << of.m_order_data[i]["date"];
            std::cout << " 时段: " << (of.m_order_data[i]["interval"] == "1" ? "上午": "下午");
            std::cout << " 机房: " << (of.m_order_data[i]["room_id"]);

            std::stringstream status_ss;
            status_ss << " 状态: "; // 0 取消预约 1 审核中 2 已预约 -1 预约失败
            if(of.m_order_data[i]["status"] == "1") {
                status_ss << "审核中";
            }
            std::cout << status_ss.str() << std::endl;
        }
        
    }

    std::cout << "请输入审核的预约记录,0代表返回" << std::endl;

    int select = 0;
    int ret = 0;
    while(true) {
        std::cin >> select;
        if(select >= 0 && select <= v.size()) {
            if(select == 0) {
                break;
            } else {
                std::cout << "请输入审核结果" << std::endl;
                std::cout << "1.通过" << std::endl;
                std::cout << "2.不通过" << std::endl;

                std::cin >> ret;
                if(ret == 1) {
                    of.m_order_data[v[select-1]]["status"] = "2";
                } else {
                    of.m_order_data[v[select-1]]["status"] = "-1";
                }
                of.update_order();
                std::cout << "审核完毕" << std::endl;
                break;
            }
        }
        std::cout << "输入有误,请重新输入" << std::endl;
    }

    Funcs::pause();
    Funcs::clear();
}

程序输出如下,


image.png

相关文章

  • C++ STL容器实现机房预约系统

    需求分析 程序目录结构 CMakeLists.txt main.cpp include/utils/consts....

  • [C++] STL 容器

    参考:[C++] STL 容器 (一) - 基本介紹[C++] STL 容器 (二) - Iterator 部分示例:

  • STL源码解析(1)-红黑树

    STL源码解析(1)-红黑树 STL容器之红黑树实现 C++中map和set都是基于红黑树的实现, 其迭代器也是红...

  • C++ STL(1)

    C++ STL(1) from my csdn blog C++标准模板库 容器C++标准模板库提供了10种容器基...

  • c++ STL

    一.STL: standard template library(C++标准模板库) STL共有六大组件:容器、算...

  • 浅析STL allocator

    STL allocator是做什么用? 在学习STL中containers会发现C++ STL里定义了很多的容器(...

  • C++ STL 之 vectot(三)

    今天我们继续更新 C++ STL 中 vector 容器的使用 vector 容器增加元素 vector 容器增加...

  • STL与泛型编程 第一周 博览网

    重要的C++参考网站:cplusplus.com CppReference STL六大容器 容器containe...

  • C++ STL 之 vectot(四)

    今天我们继续更新 C++ STL 中 vector 容器的使用 vector 容器删除元素 使用 clear() ...

  • C++ STL 之 vectot(一)

    今天我们将更新 C++ STL 中 vector 容器的使用,之前我们介绍了 array 容器的使用,其实容器之间...

网友评论

      本文标题:C++ STL容器实现机房预约系统

      本文链接:https://www.haomeiwen.com/subject/ablxxrtx.html