一个简单酒店住宿信息管理系统(C++)

住宿信息包括:住宿编号、姓名、身份证号码、联系电话、入住时间、房号、单价、入住接待人姓名、退房时间、退房接待人姓名、优惠费用、使用房内小物品费用、应付费用、备注。

本酒店住宿信息管理系统具有以下功能:

(1)系统以菜单方式工作,执行一次操作后重新显示一次菜单;

(2)住宿信息利用文件保存;

(3)住宿信息的录入;

(4)修改住宿信息;

(5)删除住宿信息;

(6)按条件(住宿编号、姓名、身份证号码、联系电话、入住时间、房号)查询住宿信息;

(7)退房时根据退房时间与单价自动计算应付费用,应付费用=单价*入住天数﹣优惠费用+使用房内小物品费用,入住天数根据入住时间和退房时间自动计算;

(8)按天、周和月统计酒店的营业额。

cpp 复制代码
#include <iostream>
#include <fstream>
#include <cstring>
#include <time.h>
 
using namespace std;
 
// 定义住宿信息结构体
struct Accommodation {
    int id;  // 住宿编号
    char name[50];  // 姓名
    char idNumber[20];  // 身份证号码
    char phoneNumber[20];  // 联系电话
    char checkInDate[20];  // 入住时间
    char roomNumber[10];  // 房号
    float price;  // 单价
    char receptionistName[50];  // 入住接待人姓名
    char checkOutDate[20];  // 退房时间
    char checkOutReceptionistName[50];  // 退房接待人姓名
    float discount;  // 优惠费用
    float additionalCost;  // 使用房内小物品费用
    float totalCost;  // 应付费用
    char remarks[100];  // 备注
};
 
 
//显示菜单
void displayMenu() {
    cout << "========== 酒店住宿信息管理系统 ==========" << endl;
    cout << "1. 录入住宿信息" << endl;
    cout << "2. 修改住宿信息" << endl;
    cout << "3. 删除住宿信息" << endl;
    cout << "4. 查询住宿信息" << endl;
    cout << "5. 退房结算" << endl;
    cout << "6. 统计营业额" << endl;
    cout << "0. 退出系统" << endl;
    cout << "========================================" << endl;
    cout << "请输入操作编号:";
}
// 添加住宿信息到文件
void addAccommodation(Accommodation& accommodation) {
    ofstream file("accommodations.txt", ios::app);
    if (!file) {
        cout << "打开文件失败" << endl;
        return;
    }
    
    file.write(reinterpret_cast<char*>(&accommodation), sizeof(Accommodation));
    file.close();
    
    cout << "住宿信息已录入" << endl;
}
// 输入住宿信息
void inputAccommodationInfo(Accommodation& accommodation) {
    cout << "请输入住宿编号:";
    cin >> accommodation.id;
    cout << "请输入姓名:";
    cin >> accommodation.name;
    cout << "请输入身份证号码:";
    cin >> accommodation.idNumber;
    cout << "请输入联系电话:";
    cin >> accommodation.phoneNumber;
    cout << "请输入入住时间:";
    cin >> accommodation.checkInDate;
    cout << "请输入房号:";
    cin >> accommodation.roomNumber;
    cout << "请输入单价:";
    cin >> accommodation.price;
    cout << "请输入入住接待人姓名:";
    cin >> accommodation.receptionistName;
    cout << "请输入备注:";
    cin.ignore();
    cin.getline(accommodation.remarks, 100);
}
// 修改住宿信息
void modifyAccommodation() {
    int id;
    cout << "请输入要修改的住宿编号:";
    cin >> id;
    
    fstream file("accommodations.txt", ios::in | ios::out);
    if (!file) {
        cout << "打开文件失败" << endl;
        return;
    }
    
    Accommodation accommodation;
    bool found = false;
    while (!found && file.read(reinterpret_cast<char*>(&accommodation), sizeof(Accommodation))) {
        if (accommodation.id == id) {
            inputAccommodationInfo(accommodation);
            
            file.seekp(-sizeof(Accommodation), ios::cur);
            file.write(reinterpret_cast<char*>(&accommodation), sizeof(Accommodation));
            
            cout << "住宿信息已修改" << endl;
            found = true;
        }
    }
    
    if (!found) {
        cout << "未找到对应的住宿信息" << endl;
    }
    
    file.close();
}
// 删除住宿信息
void deleteAccommodation() {
    int id;
    cout << "请输入要删除的住宿编号:";
    cin >> id;
    
    ifstream inFile("accommodations.txt");
    if (!inFile) {
        cout << "打开文件失败" << endl;
        return;
    }
    
    ofstream outFile("temp.txt");
    if (!outFile) {
        cout << "创建临时文件失败" << endl;
        return;
    }
    
    Accommodation accommodation;
    bool found = false;
    while (inFile.read(reinterpret_cast<char*>(&accommodation), sizeof(Accommodation))) {
        if (accommodation.id != id) {
            outFile.write(reinterpret_cast<char*>(&accommodation), sizeof(Accommodation));
        } else {
            found = true;
        }
    }
    
    inFile.close();
    outFile.close();
    
    if (found) {
        remove("accommodations.txt");
        rename("temp.txt", "accommodations.txt");
        cout << "住宿信息已删除" << endl;
    } else {
        remove("temp.txt");
        cout << "未找到对应的住宿信息" << endl;
    }
}
// 显示住宿信息
void displayAccommodation(const Accommodation& accommodation) {
    cout << "住宿编号:" << accommodation.id << endl;
    cout << "姓名:" << accommodation.name << endl;
    cout << "身份证号码:" << accommodation.idNumber << endl;
    cout << "联系电话:" << accommodation.phoneNumber << endl;
    cout << "入住时间:" << accommodation.checkInDate << endl;
    cout << "房号:" << accommodation.roomNumber << endl;
    cout << "单价:" << accommodation.price << endl;
    cout << "入住接待人姓名:" << accommodation.receptionistName << endl;
    cout << "备注:" << accommodation.remarks << endl;
}
// 查询住宿信息
void queryAccommodation() {
    int choice;
    cout << "1. 按住宿编号查询" << endl;
    cout << "2. 按姓名查询" << endl;
    cout << "3. 按身份证号码查询" << endl;
    cout << "4. 按联系电话查询" << endl;
    cout << "5. 按入住时间查询" << endl;
    cout << "6. 按房号查询" << endl;
    cout << "请输入查询方式:";
    cin >> choice;
    
    ifstream file("accommodations.txt");
    if (!file) {
        cout << "打开文件失败" << endl;
        return;
    }
    
    Accommodation accommodation;
    bool found = false;
    switch (choice) {
        case 1:
            int id;
            cout << "请输入要查询的住宿编号:";
            cin >> id;
            
            while (file.read(reinterpret_cast<char*>(&accommodation), sizeof(Accommodation))) {
                if (accommodation.id == id) {
                    displayAccommodation(accommodation);
                    found = true;
                    break;
                }
            }
            break;
        case 2:
            char name[50];
            cout << "请输入要查询的姓名:";
            cin >> name;
            
            while (file.read(reinterpret_cast<char*>(&accommodation), sizeof(Accommodation))) {
                if (strcmp(accommodation.name, name) == 0) {
                    displayAccommodation(accommodation);
                    found = true;
                    break;
                }
            }
            break;
        case 3:
            char idNumber[20];
            cout << "请输入要查询的身份证号码:";
            cin >> idNumber;
            
            while (file.read(reinterpret_cast<char*>(&accommodation), sizeof(Accommodation))) {
                if (strcmp(accommodation.idNumber, idNumber) == 0) {
                    displayAccommodation(accommodation);
                    found = true;
                    break;
                }
            }
            break;
        case 4:
            char phoneNumber[20];
            cout << "请输入要查询的联系电话:";
            cin >> phoneNumber;
            
            while (file.read(reinterpret_cast<char*>(&accommodation), sizeof(Accommodation))) {
                if (strcmp(accommodation.phoneNumber, phoneNumber) == 0) {
                    displayAccommodation(accommodation);
                    found = true;
                    break;
                }
            }
            break;
        case 5:
            char checkInDate[20];
            cout << "请输入要查询的入住时间:";
            cin >> checkInDate;
            
            while (file.read(reinterpret_cast<char*>(&accommodation), sizeof(Accommodation))) {
                if (strcmp(accommodation.checkInDate, checkInDate) == 0) {
                    displayAccommodation(accommodation);
                    found = true;
                    break;
                }
            }
            break;
        case 6:
            char roomNumber[10];
            cout << "请输入要查询的房号:";
            cin >> roomNumber;
            
            while (file.read(reinterpret_cast<char*>(&accommodation), sizeof(Accommodation))) {
                if (strcmp(accommodation.roomNumber, roomNumber) == 0) {
                    displayAccommodation(accommodation);
                    found = true;
                    break;
                }
            }
            break;
        default:
            cout << "无效的选择" << endl;
    }
    
    if (!found) {
        cout << "未找到对应的住宿信息" << endl;
    }
    
    file.close();
}
// 退房结算
void checkOut() {
    int id;
    cout << "请输入要退房的住宿编号:";
    cin >> id;
    
    fstream file("accommodations.txt", ios::in | ios::out);
    if (!file) {
        cout << "打开文件失败" << endl;
        return;
    }
    
    Accommodation accommodation;
    bool found = false;
    while (!found && file.read(reinterpret_cast<char*>(&accommodation), sizeof(Accommodation))) {
        if (accommodation.id == id) {
            displayAccommodation(accommodation);
            
            cout << "请输入退房时间:";
            cin >> accommodation.checkOutDate;
            cout << "请输入退房接待人姓名:";
            cin >> accommodation.checkOutReceptionistName;
            cout << "请输入优惠费用:";
            cin >> accommodation.discount;
            cout << "请输入使用房内小物品费用:";
            cin >> accommodation.additionalCost;
            
            // 计算入住天数和应付费用
            // 假设日期格式为 yyyy-mm-dd
            int checkInYear, checkInMonth, checkInDay;
            int checkOutYear, checkOutMonth, checkOutDay;
            sscanf(accommodation.checkInDate, "%d-%d-%d", &checkInYear, &checkInMonth, &checkInDay);
            sscanf(accommodation.checkOutDate, "%d-%d-%d", &checkOutYear, &checkOutMonth, &checkOutDay);
            int totalDays = (checkOutYear - checkInYear) * 365 + (checkOutMonth - checkInMonth) * 30 + (checkOutDay - checkInDay);
            accommodation.totalCost = accommodation.price * totalDays - accommodation.discount + accommodation.additionalCost;
            
            file.seekp(-sizeof(Accommodation), ios::cur);
            file.write(reinterpret_cast<char*>(&accommodation), sizeof(Accommodation));
            
            cout << "住宿信息已退房结算" << endl;
            cout << "入住天数:" << totalDays << endl;
            cout << "应付费用:" << accommodation.totalCost << endl;
            found = true;
        }
    }
    
    if (!found) {
        cout << "未找到对应的住宿信息" << endl;
    }
    
    file.close();
}
// 统计营业额
void calculateRevenue() {
    ifstream file("accommodations.txt");
    if (!file) {
        cout << "打开文件失败" << endl;
        return;
    }
    
    Accommodation accommodation;
    float dailyRevenue = 0.0;
    float weeklyRevenue = 0.0;
    float monthlyRevenue = 0.0;
    
    while (file.read(reinterpret_cast<char*>(&accommodation), sizeof(Accommodation))) {
        int checkInYear, checkInMonth, checkInDay;
        sscanf(accommodation.checkInDate, "%d-%d-%d", &checkInYear, &checkInMonth, &checkInDay);
 
        time_t now = time(0);
        tm* currentTime = localtime(&now);
        int currentYear = currentTime->tm_year + 1900;
        int currentMonth = currentTime->tm_mon + 1;
        int currentDay = currentTime->tm_mday;
 
        int totalDays = (currentYear - checkInYear) * 365 + (currentMonth - checkInMonth) * 30 + (currentDay - checkInDay);
        
        if (totalDays <= 1) {
            dailyRevenue += accommodation.totalCost;
        }
        
        if (totalDays <= 7) {
            weeklyRevenue += accommodation.totalCost;
        }
        
        if (totalDays <= 30) {
            monthlyRevenue += accommodation.totalCost;
        }
    }
    
    cout << "当日营业额:" << dailyRevenue << endl;
    cout << "近一周营业额:" << weeklyRevenue << endl;
    cout << "近一月营业额:" << monthlyRevenue << endl;
    
    file.close();
}
// 主函数
int main() {
    int choice;
    
    while (true) {
        displayMenu();
        cin >> choice;
        
        switch (choice) {
            case 1: {
                Accommodation accommodation;
                inputAccommodationInfo(accommodation);
                addAccommodation(accommodation);
                break;
            }
            case 2:
                modifyAccommodation();
                break;
            case 3:
                deleteAccommodation();
                break;
            case 4:
                queryAccommodation();
                break;
            case 5:
                checkOut();
                break;
            case 6:
                calculateRevenue();
                break;
            case 0:
                cout << "感谢使用,再见!" << endl;
                return 0;
            default:
                cout << "无效的选择,请重新输入" << endl;
        }
        
        cout << endl;
    }
}
 
相关推荐
ULTRA??22 分钟前
C加加中的结构化绑定(解包,折叠展开)
开发语言·c++
凌云行者1 小时前
OpenGL入门005——使用Shader类管理着色器
c++·cmake·opengl
凌云行者1 小时前
OpenGL入门006——着色器在纹理混合中的应用
c++·cmake·opengl
~yY…s<#>1 小时前
【刷题17】最小栈、栈的压入弹出、逆波兰表达式
c语言·数据结构·c++·算法·leetcode
可均可可2 小时前
C++之OpenCV入门到提高004:Mat 对象的使用
c++·opencv·mat·imread·imwrite
白子寰3 小时前
【C++打怪之路Lv14】- “多态“篇
开发语言·c++
小芒果_013 小时前
P11229 [CSP-J 2024] 小木棍
c++·算法·信息学奥赛
gkdpjj3 小时前
C++优选算法十 哈希表
c++·算法·散列表
王俊山IT3 小时前
C++学习笔记----10、模块、头文件及各种主题(一)---- 模块(5)
开发语言·c++·笔记·学习
-Even-3 小时前
【第六章】分支语句和逻辑运算符
c++·c++ primer plus