本文档为C++控制台商业级项目实战解析,核心分享「酒店客房入住管理系统」的开发思路、完整功能实现、核心代码及部署指南。该系统适配中小型酒店的轻量化管理需求,无需数据库依赖,采用本地文件存储数据,支持Windows全版本离线运行。

文档作者长期专注C++控制台项目开发,可提供多场景技术定制服务(学生作业/课程设计、教师教学工具、中小型商业管理系统),若需个性化开发方案或技术优化建议,可通过CSDN私信沟通(点击作者头像进入主页,找到「发私信」按钮即可)。

一、 服务方向说明(合规版)

基于C++控制台开发技术,聚焦「轻量、高效、无依赖」的项目需求,核心服务方向如下,均提供完整代码、编译指导及使用文档:

  • 学生场景:C++控制台作业、课程设计(如算法实现、小型管理系统),代码规范、注释详尽,贴合教学要求;

  • 教师场景:教学辅助工具(点名器、成绩统计、作业批改工具),适配课堂离线使用,操作极简;

  • 商业场景:中小型商业轻量化管理系统(库存、记账、客房、客户管理等),本地文件存储,无额外环境配置,编译为.exe即可部署。

二、 酒店客房入住管理系统(中型商业项目)核心解析

2.1 项目开发背景与核心目标

针对中小型酒店「客房状态难同步、入住退房流程繁琐、客户信息管理混乱、数据易丢失」的痛点,开发轻量化客房入住管理系统。核心目标是实现「客房管理、客户入住/退房、订单记录、数据备份」全流程自动化,无需专业IT维护,前台员工简单培训即可上手。

技术优势:基于C++标准库开发,无外部依赖,编译后仅5MB左右,U盘即可携带部署;数据采用加密文件存储,兼顾安全性与便捷性;支持多用户操作(管理员/前台),权限分级管理。

2.2 核心功能模块

系统分为4大核心模块,覆盖酒店客房管理全流程:

  1. 用户权限模块:支持管理员(全权限)、前台员工(有限权限)登录,区分操作权限(如管理员可修改客房类型,前台仅可操作入住退房);

  2. 客房管理模块:客房信息增删改查(编号、类型、价格、状态),支持批量导入/导出客房信息,实时显示客房状态(空闲/入住/维修);

  3. 入住退房模块:客户信息录入(姓名、身份证号、联系方式)、客房分配、押金记录、入住时间登记;退房时自动计算费用、清空客房状态、生成结算清单;

  4. 数据管理模块:订单记录查询(按日期、客房号、客户姓名)、数据自动备份、历史数据恢复,支持导出订单记录为TXT/CSV格式。

2.3 核心技术选型与设计思路

2.3.1 技术选型
  • 开发语言:C++(兼容C++11及以上标准);

  • 存储方案:本地文件存储(客房信息文件、客户订单文件、用户权限文件),采用简单加密算法(Base64+自定义密钥)保护敏感数据;

  • 编译环境:支持Dev-C++、Visual Studio 2019+、Code::Blocks,全Windows系统兼容(XP/7/10/11);

  • 核心技术:结构体封装数据、vector容器管理内存、文件IO操作、权限分级逻辑、简单数据加密。

2.3.2 数据结构设计

采用结构体封装核心数据,清晰区分不同模块的数据属性:

#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <iomanip>
#include <ctime>
#include <algorithm>
#include <limits>
using namespace std;

// 日期时间工具函数:获取当前时间
string getCurrentDateTime() {
    time_t now = time(0);
    tm* ltm = localtime(&now);
    string dt = to_string(1900 + ltm->tm_year) + "-"
              + (ltm->tm_mon + 1 < 10 ? "0" + to_string(ltm->tm_mon + 1) : to_string(ltm->tm_mon + 1)) + "-"
              + (ltm->tm_mday < 10 ? "0" + to_string(ltm->tm_mday) : to_string(ltm->tm_mday)) + " "
              + (ltm->tm_hour < 10 ? "0" + to_string(ltm->tm_hour) : to_string(ltm->tm_hour)) + ":"
              + (ltm->tm_min < 10 ? "0" + to_string(ltm->tm_min) : to_string(ltm->tm_min));
    return dt;
}

// 用户权限结构体
struct User {
    string username;       // 用户名
    string password;       // 密码(简单加密存储)
    string role;           // 角色:admin(管理员)/reception(前台)
};

// 客房信息结构体
struct Room {
    string roomId;         // 客房编号(如101、202)
    string type;           // 客房类型(单人间/双人间/豪华间)
    double price;          // 单价(元/天)
    string status;         // 状态:free(空闲)/occupied(入住)/maintain(维修)
    string remark;         // 备注(如带窗、无烟)
};

// 客户与订单结构体(入住时生成)
struct Order {
    string orderId;        // 订单编号(自动生成:日期+序号)
    string roomId;         // 客房编号
    string guestName;      // 客户姓名
    string idCard;         // 身份证号(加密存储)
    string phone;          // 联系方式
    string checkInTime;    // 入住时间
    string checkOutTime;   // 退房时间(未退房时为空)
    double deposit;        // 押金(元)
    double totalCost;      // 总费用(退房时计算)
    bool isCheckedOut;     // 是否退房:true(已退)/false(未退)
};

// 全局数据存储
vector<User> userList;          // 用户列表
vector<Room> roomList;          // 客房列表
vector<Order> orderList;        // 订单列表

// 核心文件路径定义
const string USER_FILE = "hotel_users.txt";
const string ROOM_FILE = "hotel_rooms.txt";
const string ORDER_FILE = "hotel_orders.txt";
const string BACKUP_FILE = "hotel_backup_";
2.3.3 核心设计思路

1. 权限分级:管理员可操作所有功能(用户管理、客房类型修改、数据备份),前台仅可操作入住、退房、订单查询等基础功能;

2. 数据安全:客户身份证号、用户密码采用简单加密(Base64+自定义密钥)存储,避免明文泄露;系统启动时自动加载历史数据,退出时自动保存;

3. 操作便捷性:采用菜单式交互,所有操作无需输入命令,按数字选择即可;订单编号自动生成,减少人工输入错误;

4. 可扩展性:模块间低耦合,后续可新增「会员管理、餐饮消费记录」等功能,无需大幅修改核心代码。

2.4 核心功能实现代码(全流程)

2.4.1 数据加密与解密工具函数(保护敏感数据)
// 简单Base64加密(简化版,用于敏感数据存储)
string base64Encode(const string& str) {
    const string base64Table = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    string result;
    int i = 0, len = str.size();
    while (i < len) {
        // 第一个字节
        result += base64Table[(str[i] & 0xfc) >> 2];
        // 第二个字节
        if (i + 1 < len) {
            result += base64Table[((str[i] & 0x03) << 4) | ((str[i + 1] & 0xf0) >> 4)];
            // 第三个字节
            if (i + 2 < len) {
                result += base64Table[((str[i + 1] & 0x0f) << 2) | ((str[i + 2] & 0xc0) >> 6)];
                result += base64Table[str[i + 2] & 0x3f];
            } else {
                result += base64Table[((str[i + 1] & 0x0f) << 2)];
                result += "=";
            }
        } else {
            result += base64Table[((str[i] & 0x03) << 4)];
            result += "==";
        }
        i += 3;
    }
    return result;
}

// 简单Base64解密
string base64Decode(const string& str) {
    const string base64Table = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    string result;
    int i = 0, len = str.size();
    while (i < len) {
        // 找到对应Base64编码的索引
        int idx1 = base64Table.find(str[i]);
        int idx2 = base64Table.find(str[i + 1]);
        // 解码第一个字节
        result += (char)(((idx1 & 0x3f) << 2) | ((idx2 & 0x30) >> 4));
        if (i + 2 < len && str[i + 2] != '=') {
            int idx3 = base64Table.find(str[i + 2]);
            // 解码第二个字节
            result += (char)(((idx2 & 0x0f) << 4) | ((idx3 & 0x3c) >> 2));
            if (i + 3 < len && str[i + 3] != '=') {
                int idx4 = base64Table.find(str[i + 3]);
                // 解码第三个字节
                result += (char)(((idx3 & 0x03) << 6) | (idx4 & 0x3f));
            }
        }
        i += 4;
    }
    return result;
}
2.4.2 数据加载与保存函数(系统启动/退出核心)
// 加载用户数据(启动时调用)
void loadUserData() {
    ifstream fin(USER_FILE);
    if (!fin.is_open()) return;
    userList.clear();
    User u;
    while (getline(fin, u.username)) {
        getline(fin, u.password);
        getline(fin, u.role);
        // 解密密码(存储时已加密)
        u.password = base64Decode(u.password);
        userList.push_back(u);
    }
    fin.close();
}

// 保存用户数据(退出时/修改用户后调用)
void saveUserData() {
    ofstream fout(USER_FILE);
    for (const auto& u : userList) {
        fout << u.username << endl;
        // 加密密码后存储
        fout << base64Encode(u.password) << endl;
        fout << u.role << endl;
    }
    fout.close();
}

// 加载客房数据
void loadRoomData() {
    ifstream fin(ROOM_FILE);
    if (!fin.is_open()) return;
    roomList.clear();
    Room r;
    while (getline(fin, r.roomId)) {
        getline(fin, r.type);
        fin >> r.price;
        fin.ignore();
        getline(fin, r.status);
        getline(fin, r.remark);
        roomList.push_back(r);
    }
    fin.close();
}

// 保存客房数据
void saveRoomData() {
    ofstream fout(ROOM_FILE);
    for (const auto& r : roomList) {
        fout << r.roomId << endl;
        fout << r.type << endl;
        fout << r.price << endl;
        fout << r.status << endl;
        fout << r.remark << endl;
    }
    fout.close();
}

// 加载订单数据
void loadOrderData() {
    ifstream fin(ORDER_FILE);
    if (!fin.is_open()) return;
    orderList.clear();
    Order o;
    while (getline(fin, o.orderId)) {
        getline(fin, o.roomId);
        getline(fin, o.guestName);
        getline(fin, o.idCard);
        getline(fin, o.phone);
        getline(fin, o.checkInTime);
        getline(fin, o.checkOutTime);
        fin >> o.deposit;
        fin.ignore();
        fin >> o.totalCost;
        fin.ignore();
        fin >> o.isCheckedOut;
        fin.ignore();
        // 解密身份证号
        o.idCard = base64Decode(o.idCard);
        orderList.push_back(o);
    }
    fin.close();
}

// 保存订单数据
void saveOrderData() {
    ofstream fout(ORDER_FILE);
    for (const auto& o : orderList) {
        fout << o.orderId << endl;
        fout << o.roomId << endl;
        fout << o.guestName << endl;
        // 加密身份证号后存储
        fout << base64Encode(o.idCard) << endl;
        fout << o.phone << endl;
        fout << o.checkInTime << endl;
        fout << o.checkOutTime << endl;
        fout << o.deposit << endl;
        fout << o.totalCost << endl;
        fout << o.isCheckedOut << endl;
    }
    fout.close();
}

// 系统初始化(启动时调用,加载所有数据)
void systemInit() {
    loadUserData();
    loadRoomData();
    loadOrderData();
    // 初始化默认管理员(若无用户数据)
    if (userList.empty()) {
        User admin;
        admin.username = "admin";
        admin.password = "admin123";
        admin.role = "admin";
        userList.push_back(admin);
        saveUserData();
    }
    cout << "系统初始化完成!" << endl;
}
2.4.3 登录与权限校验函数
// 登录函数(返回登录成功的用户角色,失败返回空)
string login() {
    string username, password;
    cout << "\n===== 酒店客房管理系统 - 登录 =====" << endl;
    cout << "用户名:";
    getline(cin, username);
    cout << "密码:";
    getline(cin, password);

    // 校验用户信息
    for (const auto& u : userList) {
        if (u.username == username && u.password == password) {
            cout << "登录成功!欢迎您," << (u.role == "admin" ? "管理员" : "前台员工") << "!" << endl;
            return u.role;
        }
    }
    cout << "用户名或密码错误!登录失败。" << endl;
    return "";
}

// 输入合法性校验(整数)
bool isValidInt(int& input) {
    cin >> input;
    if (cin.fail()) {
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        return false;
    }
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    return true;
}

// 输入合法性校验(浮点数)
bool isValidDouble(double& input) {
    cin >> input;
    if (cin.fail()) {
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        return false;
    }
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    return input > 0;
}
2.4.4 客房管理核心功能(增删改查)
// 新增客房
void addRoom(const string& role) {
    // 仅管理员可操作
    if (role != "admin") {
        cout << "权限不足!仅管理员可新增客房。" << endl;
        return;
    }

    Room r;
    cout << "\n===== 新增客房 =====" << endl;
    cout << "客房编号(如101、202):";
    getline(cin, r.roomId);
    // 校验客房编号是否重复
    for (const auto& room : roomList) {
        if (room.roomId == r.roomId) {
            cout << "该客房编号已存在!请重新输入。" << endl;
            return;
        }
    }
    cout << "客房类型(单人间/双人间/豪华间):";
    getline(cin, r.type);
    cout << "客房单价(元/天):";
    while (!isValidDouble(r.price)) {
        cout << "输入错误!请输入正数:";
    }
    cout << "客房状态(free-空闲/occupied-入住/maintain-维修):";
    getline(cin, r.status);
    // 状态合法性校验
    while (r.status != "free" && r.status != "occupied" && r.status != "maintain") {
        cout << "输入错误!请输入free/occupied/maintain:";
        getline(cin, r.status);
    }
    cout << "备注(如带窗、无烟,无则回车):";
    getline(cin, r.remark);

    roomList.push_back(r);
    saveRoomData();
    cout << "客房新增成功!" << endl;
}

// 查看所有客房状态
void showAllRooms() {
    cout << "\n===== 酒店客房状态列表 =====" << endl;
    cout << left << setw(10) << "客房编号" << setw(12) << "客房类型" << setw(10) << "单价(元)" << setw(12) << "状态" << setw(20) << "备注" << endl;
    cout << string(70, '-') << endl;
    for (const auto& r : roomList) {
        string statusDesc = (r.status == "free" ? "空闲" : (r.status == "occupied" ? "入住" : "维修"));
        cout << left << setw(10) << r.roomId << setw(12) << r.type << setw(10) << fixed << setprecision(2) << r.price << setw(12) << statusDesc << setw(20) << r.remark << endl;
    }
    cout << "共" << roomList.size() << "间客房。" << endl;
}

// 修改客房信息
void modifyRoom(const string& role) {
    if (role != "admin") {
        cout << "权限不足!仅管理员可修改客房信息。" << endl;
        return;
    }

    string roomId;
    cout << "\n===== 修改客房信息 =====" << endl;
    cout << "请输入要修改的客房编号:";
    getline(cin, roomId);

    for (auto& r : roomList) {
        if (r.roomId == roomId) {
            cout << "当前客房类型:" << r.type << ",如需修改请输入(否则回车):";
            string newType;
            getline(cin, newType);
            if (!newType.empty()) r.type = newType;

            cout << "当前客房单价:" << r.price << "元,如需修改请输入(否则回车):";
            string newPriceStr;
            getline(cin, newPriceStr);
            if (!newPriceStr.empty()) {
                double newPrice = stod(newPriceStr);
                if (newPrice > 0) r.price = newPrice;
                else cout << "单价输入无效,保留原数据。" << endl;
            }

            cout << "当前客房状态:" << r.status << ",如需修改请输入(free/occupied/maintain,否则回车):";
            string newStatus;
            getline(cin, newStatus);
            if (!newStatus.empty() && (newStatus == "free" || newStatus == "occupied" || newStatus == "maintain")) {
                r.status = newStatus;
            } else if (!newStatus.empty()) {
                cout << "状态输入无效,保留原数据。" << endl;
            }

            cout << "当前备注:" << r.remark << ",如需修改请输入(否则回车):";
            string newRemark;
            getline(cin, newRemark);
            if (!newRemark.empty()) r.remark = newRemark;

            saveRoomData();
            cout << "客房信息修改成功!" << endl;
            return;
        }
    }
    cout << "未找到编号为" << roomId << "的客房!" << endl;
}
2.4.5 入住与退房核心功能(核心业务流程)
// 生成订单编号(规则:日期+3位序号)
string generateOrderId() {
    string date = getCurrentDateTime().substr(0, 10);
    // 替换日期中的'-'为''
    replace(date.begin(), date.end(), '-', '');
    int seq = 1;
    // 查找当前最大序号
    for (const auto& o : orderList) {
        if (o.orderId.substr(0, 8) == date) {
            int currentSeq = stoi(o.orderId.substr(8));
            if (currentSeq > seq) seq = currentSeq + 1;
        }
    }
    // 序号补零为3位
    return date + setfill('0') + setw(3) + to_string(seq);
}

// 客户入住
void checkIn() {
    string roomId;
    cout << "\n===== 客户入住 =====" << endl;
    cout << "请输入入住客房编号:";
    getline(cin, roomId);

    // 校验客房是否存在且空闲
    auto roomIt = find_if(roomList.begin(), roomList.end(), [&](const Room& r) {
        return r.roomId == roomId;
    });
    if (roomIt == roomList.end()) {
        cout << "未找到编号为" << roomId << "的客房!" << endl;
        return;
    }
    if (roomIt->status != "free") {
        cout << "该客房当前状态为" << (roomIt->status == "occupied" ? "已入住" : "维修中") << ",无法办理入住!" << endl;
        return;
    }

    // 录入客户信息
    Order o;
    o.orderId = generateOrderId();
    o.roomId = roomId;
    cout << "客户姓名:";
    getline(cin, o.guestName);
    cout << "客户身份证号:";
    getline(cin, o.idCard);
    cout << "客户联系方式:";
    getline(cin, o.phone);
    cout << "入住押金(元):";
    while (!isValidDouble(o.deposit)) {
        cout << "输入错误!请输入正数:";
    }
    o.checkInTime = getCurrentDateTime();
    o.checkOutTime = "";
    o.totalCost = 0.0;
    o.isCheckedOut = false;

    // 更新客房状态为入住
    roomIt->status = "occupied";
    // 新增订单
    orderList.push_back(o);
    // 保存数据
    saveRoomData();
    saveOrderData();

    cout << "\n入住办理成功!" << endl;
    cout << "订单编号:" << o.orderId << endl;
    cout << "入住时间:" << o.checkInTime << endl;
    cout << "客房类型:" << roomIt->type << ",单价:" << roomIt->price << "元/天" << endl;
    cout << "押金金额:" << o.deposit << "元" << endl;
}

// 客户退房(核心:计算费用、更新状态)
void checkOut() {
    string orderId;
    cout << "\n===== 客户退房 =====" << endl;
    cout << "请输入订单编号:";
    getline(cin, orderId);

    // 查找订单
    auto orderIt = find_if(orderList.begin(), orderList.end(), [&](const Order& o) {
        return o.orderId == orderId;
    });
    if (orderIt == orderList.end()) {
        cout << "未找到编号为" << orderId << "的订单!" << endl;
        return;
    }
    if (orderIt->isCheckedOut) {
        cout << "该订单已办理退房!" << endl;
        return;
    }

    // 查找对应客房
    auto roomIt = find_if(roomList.begin(), roomList.end(), [&](const Room& r) {
        return r.roomId == orderIt->roomId;
    });
    if (roomIt == roomList.end()) {
        cout << "订单对应客房不存在!" << endl;
        return;
    }

    // 计算入住天数(简化版:按天计算,不足一天按一天算)
    string checkInDate = orderIt->checkInTime.substr(0, 10);
    string checkOutDate = getCurrentDateTime().substr(0, 10);
    // 此处简化计算,实际可通过日期工具类精确计算
    int days = 1; // 默认至少1天
    if (checkInDate != checkOutDate) {
        // 简单处理:按日期差计算(实际项目需优化)
        int inYear = stoi(checkInDate.substr(0, 4)), inMonth = stoi(checkInDate.substr(5, 2)), inDay = stoi(checkInDate.substr(8, 2));
        int outYear = stoi(checkOutDate.substr(0, 4)), outMonth = stoi(checkOutDate.substr(5, 2)), outDay = stoi(checkOutDate.substr(8, 2));
        days = (outYear - inYear) * 365 + (outMonth - inMonth) * 30 + (outDay - inDay) + 1;
    }

    // 计算总费用
    orderIt->totalCost = days * roomIt->price;
    orderIt->checkOutTime = getCurrentDateTime();
    orderIt->isCheckedOut = true;
    // 更新客房状态为空闲
    roomIt->status = "free";

    // 保存数据
    saveRoomData();
    saveOrderData();

    // 打印结算清单
    cout << "\n===== 退房结算清单 =====" << endl;
    cout << "订单编号:" << orderIt->orderId << endl;
    cout << "客户姓名:" << orderIt->guestName << endl;
    cout << "客房编号:" << orderIt->roomId << "(" << roomIt->type << ")" << endl;
    cout << "入住时间:" << orderIt->checkInTime << endl;
    cout << "退房时间:" << orderIt->checkOutTime << endl;
    cout << "入住天数:" << days << "天" << endl;
    cout << "单价:" << roomIt->price << "元/天" << endl;
    cout << "押金:" << orderIt->deposit << "元" << endl;
    cout << "总费用:" << fixed << setprecision(2) << orderIt->totalCost << "元" << endl;
    cout << "应退金额:" << fixed << setprecision(2) << (orderIt->deposit - orderIt->totalCost) << "元" << endl;
    cout << "==========================" << endl;
    cout << "退房办理完成!感谢您的光临~" << endl;
}
2.4.6 数据备份与订单查询功能
// 数据备份(管理员权限)
void backupData(const string& role) {
    if (role != "admin") {
        cout << "权限不足!仅管理员可进行数据备份。" << endl;
        return;
    }

    string backupTime = getCurrentDateTime();
    replace(backupTime.begin(), backupTime.end(), '-', '');
    replace(backupTime.begin(), backupTime.end(), ' ', '_');
    replace(backupTime.begin(), backupTime.end(), ':', '');
    string backupPath = BACKUP_FILE + backupTime + ".txt";

    ofstream fout(backupPath);
    if (!fout.is_open()) {
        cout << "备份文件创建失败!" << endl;
        return;
    }

    // 写入备份时间
    fout << "===== 酒店管理系统数据备份 =====" << endl;
    fout << "备份时间:" << getCurrentDateTime() << endl;
    fout << "\n【用户数据】" << endl;
    for (const auto& u : userList) {
        fout << "用户名:" << u.username << ",角色:" << u.role << endl;
    }

    fout << "\n【客房数据】" << endl;
    for (const auto& r : roomList) {
        fout << "客房编号:" << r.roomId << ",类型:" << r.type << ",单价:" << r.price << ",状态:" << r.status << endl;
    }

    fout << "\n【订单数据】" << endl;
    for (const auto& o : orderList) {
        fout << "订单编号:" << o.orderId << ",客户:" << o.guestName << ",客房:" << o.roomId << ",入住时间:" << o.checkInTime << ",退房状态:" << (o.isCheckedOut ? "已退" : "未退") << endl;
    }

    fout.close();
    cout << "数据备份成功!备份文件路径:" << backupPath << endl;
}

// 订单查询(按多种条件)
void queryOrder() {
    cout << "\n===== 订单查询 =====" << endl;
    cout << "查询条件:1-按订单编号  2-按客房编号  3-按客户姓名  4-按入住日期  5-查看所有订单" << endl;
    int choice;
    cout << "请选择查询条件:";
    while (!isValidInt(choice) || choice < 1 || choice > 5) {
        cout << "输入错误!请输入1-5:";
    }

    vector<Order> result;
    switch (choice) {
        case 1: {
            string orderId;
            cout << "请输入订单编号:";
            getline(cin, orderId);
            for (const auto& o : orderList) {
                if (o.orderId == orderId) result.push_back(o);
            }
            break;
        }
        case 2: {
            string roomId;
            cout << "请输入客房编号:";
            getline(cin, roomId);
            for (const auto& o : orderList) {
                if (o.roomId == roomId) result.push_back(o);
            }
            break;
        }
        case 3: {
            string guestName;
            cout << "请输入客户姓名:";
            getline(cin, guestName);
            for (const auto& o : orderList) {
                if (o.guestName.find(guestName) != string::npos) result.push_back(o);
            }
            break;
        }
        case 4: {
            string targetDate;
            cout << "请输入入住日期(格式:年-月-日):";
            getline(cin, targetDate);
            for (const auto& o : orderList) {
                if (o.checkInTime.substr(0, 10) == targetDate) result.push_back(o);
            }
            break;
        }
        case 5:
            result = orderList;
            break;
    }

    // 显示查询结果
    cout << "\n===== 查询结果(共" << result.size() << "条)=====" << endl;
    cout << left << setw(15) << "订单编号" << setw(10) << "客房编号" << setw(12) << "客户姓名" << setw(20) << "入住时间" << setw(20) << "退房时间" << setw(12) << "退房状态" << endl;
    cout << string(100, '-') << endl;
    for (const auto& o : result) {
        cout << left << setw(15) << o.orderId << setw(10) << o.roomId << setw(12) << o.guestName << setw(20) << o.checkInTime << setw(20) << (o.checkOutTime.empty() ? "未退房" : o.checkOutTime) << setw(12) << (o.isCheckedOut ? "已退" : "未退") << endl;
    }

    // 导出选项
    cout << "\n是否导出查询结果?(1-是  0-否):";
    int exportChoice;
    while (!isValidInt(exportChoice) || (exportChoice != 0 && exportChoice != 1)) {
        cout << "输入错误!请输入1或0:";
    }
    if (exportChoice == 1) {
        string exportPath = "order_query_result_" + getCurrentDateTime().substr(0, 10) + ".txt";
        ofstream fout(exportPath);
        fout << "===== 订单查询结果 =====" << endl;
        fout << "查询时间:" << getCurrentDateTime() << endl;
        fout << left << setw(15) << "订单编号" << setw(10) << "客房编号" << setw(12) << "客户姓名" << setw(20) << "入住时间" << setw(20) << "退房时间" << setw(12) << "退房状态" << endl;
        fout << string(100, '-') << endl;
        for (const auto& o : result) {
            fout << left << setw(15) << o.orderId << setw(10) << o.roomId << setw(12) << o.guestName << setw(20) << o.checkInTime << setw(20) << (o.checkOutTime.empty() ? "未退房" : o.checkOutTime) << setw(12) << (o.isCheckedOut ? "已退" : "未退") << endl;
        }
        fout.close();
        cout << "结果已导出到:" << exportPath << endl;
    }
}
2.4.7 主函数与菜单交互(系统入口)
// 管理员菜单
void adminMenu() {
    int choice;
    while (true) {
        cout << "\n===== 酒店客房管理系统(管理员版)=====" << endl;
        cout << "1. 客房管理(增删改查)  2. 客户入住  3. 客户退房" << endl;
        cout << "4. 订单查询  5. 数据备份  6. 用户管理  7. 退出系统" << endl;
        cout << "请选择操作:";
        while (!isValidInt(choice) || choice < 1 || choice > 7) {
            cout << "输入错误!请输入1-7:";
        }

        switch (choice) {
            case 1: {
                int roomChoice;
                cout << "\n客房管理子菜单:1-新增客房  2-查看所有客房  3-修改客房信息  4-返回上一级" << endl;
                cout << "选择操作:";
                while (!isValidInt(roomChoice) || roomChoice < 1 || roomChoice > 4) {
                    cout << "输入错误!请输入1-4:";
                }
                if (roomChoice == 1) addRoom("admin");
                else if (roomChoice == 2) showAllRooms();
                else if (roomChoice == 3) modifyRoom("admin");
                break;
            }
            case 2: checkIn(); break;
            case 3: checkOut(); break;
            case 4: queryOrder(); break;
            case 5: backupData("admin"); break;
            case 6: cout << "用户管理功能(新增/删除用户)可后续拓展~" << endl; break;
            case 7: 
                cout << "退出系统成功!" << endl;
                return;
        }
    }
}

// 前台员工菜单
void receptionMenu() {
    int choice;
    while (true) {
        cout << "\n===== 酒店客房管理系统(前台版)=====" << endl;
        cout << "1. 查看所有客房  2. 客户入住  3. 客户退房  4. 订单查询  5. 退出系统" << endl;
        cout << "请选择操作:";
        while (!isValidInt(choice) || choice < 1 || choice > 5) {
            cout << "输入错误!请输入1-5:";
        }

        switch (choice) {
            case 1: showAllRooms(); break;
            case 2: checkIn(); break;
            case 3: checkOut(); break;
            case 4: queryOrder(); break;
            case 5: 
                cout << "退出系统成功!" << endl;
                return;
        }
    }
}

// 主函数(系统入口)
int main() {
    cout << "===== 欢迎使用酒店客房入住管理系统 =====" << endl;
    // 系统初始化(加载数据)
    systemInit();

    // 登录流程
    string role;
    int loginCount = 0;
    while (role.empty() && loginCount < 3) {
        role = login();
        loginCount++;
        if (role.empty() && loginCount < 3) {
            cout << "还有" << 3 - loginCount << "次登录机会。" << endl;
        }
    }
    if (role.empty()) {
        cout << "登录失败次数过多,系统退出!" << endl;
        return 0;
    }

    // 进入对应角色菜单
    if (role == "admin") {
        adminMenu();
    } else if (role == "reception") {
        receptionMenu();
    }

    return 0;
}

2.5 系统部署与使用指南

2.5.1 编译环境要求
  • 开发工具:支持Dev-C++ 5.11、Visual Studio 2019+、Code::Blocks 20.03,兼容C++11及以上标准;

  • 运行环境:Windows XP/7/10/11(32位/64位),无需安装额外库文件;

  • 部署方式:编译生成.exe文件后,直接拷贝到目标电脑即可运行,无需配置环境变量。

2.5.2 首次使用流程
  1. 编译运行.exe文件,系统自动初始化,生成默认管理员账号(用户名:admin,密码:admin123);

  2. 登录后(管理员身份),通过「客房管理」模块新增客房信息(编号、类型、价格等);

  3. 前台员工登录(需管理员提前创建账号),即可操作「入住/退房/订单查询」等基础功能;

  4. 建议首次使用后,通过管理员账号修改默认密码,并进行一次数据备份。

2.5.3 数据安全与维护
  • 数据存储:系统运行时自动生成3个核心文件(hotel_users.txt、hotel_rooms.txt、hotel_orders.txt),存储用户、客房、订单数据,请勿手动修改;

  • 数据备份:管理员定期通过「数据备份」功能备份数据,备份文件建议存储在U盘或云盘;

  • 故障处理:若运行时闪退,检查核心数据文件是否存在且未被损坏,可通过备份文件恢复数据

Logo

Agent 垂直技术社区,欢迎活跃、内容共建。

更多推荐