服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - 基于C++实现酒店管理系统

基于C++实现酒店管理系统

2022-10-25 13:37q996852067 C/C++

这篇文章主要为大家详细介绍了基于C++实现酒店管理系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

现今大多数宾馆所提供的服务样式都各式各样,规模大小也是各有不同,但是归总下来,不可或缺的两类模块还是顾客和工作人员。由于对宾馆行业内部没有很深刻的理解,此次系统设计包括数据库和功能模块都是根据网上收集到的材料和个人认知上,简单模仿和具体实现的。

为满宾馆管理的实际需求,本系统主要实现以下功能:

1、入住登记:登记所入住房间号码,登记顾客入住时间,退房时间,个人信息(身份证号,手机号,姓名)
2、退房办理:输入已经入住的房间号,确认完毕即可退房。
3、房间查询:管理员输入正确的密码后即可对房间状态查询,和具体入住信息查询。
4、密码修改:管理员对自身密码进行修改,前提是先输入正确密码后才能实现。
5、以txt文档的形式存储信息数据。
6、使用类封装。

基于C++实现酒店管理系统

注:代码使用前需要先向代码中自定义路径下的Input.txt文档中预存信息

功能截图

基于C++实现酒店管理系统

基于C++实现酒店管理系统

基于C++实现酒店管理系统

代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
#include<iostream>
#include<iomanip>
#include<string>
#include<fstream>
#include<sstream>
#include<windows.h>
#include<stdexcept>  
#include<conio.h>
using namespace std;
 
class room
{
private:
    int roomnumber = 0;        //房间号
    int price = 0;            //价格
    int start_date = 0;        //开始 结束 日期
    int end_date = 0;
    bool order = 0;            //房间状态 0/1
    string name;            //个人信息
    string ID;
    string phone;
public:
 
    void getnumber(int _number)
    {
        roomnumber = _number;
    }
    int returnnumber() const
    {
        return roomnumber;
    }
    void getprice(int _price)
    {
        price = _price;
    }
    int returnprice() const
    {
        return price;
    }
    void getdate(int s, int e)
    {
        if (s < 1 || s > 31 || e < 1 || e > 31 || s >= e) throw runtime_error("错误的日期!");
        start_date = s;
        end_date = e;
    }
    int returnstartdate() const
    {
        return start_date;
    }
    int returnenddate() const
    {
        return end_date;
    }
    void getorder(bool _order)
    {
        if (!(_order == 0 || _order == 1)) throw out_of_range("房间状态错误!");
        order = _order;
    }
    bool returnorder() const
    {
        return order;
    }
    void getname(string _name)
    {
        name = _name;
    }
    string returnname() const
    {
        return name;
    }
    void getID(string id)
    {
        if (id.size() < 18 || id.size() > 19) throw runtime_error("您的身份证号输入有误,请重新输入!(18位)");
        ID = id;
    }
    string returnID() const
    {
        return ID;
    }
    void getphone(string ph)
    {
        if (ph.size() != 11)
            throw runtime_error("您的手机号输入有误,请重新输入(11位)!");
        phone = ph;
    }
    string returnphone() const
    {
        return phone;
    }
    int sumprice()
    {
        return price * (end_date - start_date);
    }
    friend ostream& operator<<(ostream& os, const room* u)//输出流重载
    {
        os << u->returnnumber() << '\n';
        os << u->returnprice() << '\n';
        os << u->returnstartdate() << '\n';
        os << u->returnenddate() << '\n';
        os << u->returnorder() << '\n';
        os << u->returnname() << '\n';
        os << u->returnID() << '\n';
        os << u->returnphone() << '\n';
        return os;
    }
};
 
class standard :public room
{
};
 
class suite :public room
{
};
 
class kingsize :public room
{
};
 
void nomorememory()
{
    cerr << "unable to satisfy request for memory\n";
    abort();
}
//new分配异常
 
int check(int a[], int size, int suspicion)
{
    int judge = 0;
    for (int i = 0; i < size; i++)
    {
        if (suspicion == a[i])
            judge = 1;
    }
    if (judge == 0)
        throw suspicion;
    return 0;
}
 
class file_exception {
    string filename;
public:
    file_exception(const string& filename) :filename(filename) {}
    ~file_exception() {}
    const string& get_filename()const { return filename; }
};
 
 
void update(room* p[], const string& filename)//覆盖/更新原有文件内容
{
    ofstream os(filename, ios_base::binary);
    if (os)
    {
        for (int i = 0; i < 6; i++)
        {
            os << p[i];
        }
    }
    else
        throw file_exception(filename);
    os.close();
}
 
void addRecord(const string& filename, room* current)//以追加模式打开文件,用于记录
{
    ofstream file(filename, ios_base::app);
    if (file)
    {
        file << current;
        file.close();
    }
    else
        throw file_exception(filename);
    file.close();
}
 
void password(string rightpassword)
{
part4:
    string password;
    cout << "请输入 密码: " << endl;
    int i = 0;
    char ch;
    while ((ch = _getch()) != 13)
    {
        password += ch; //字符串拼接
        cout << "*";
    }
    if (password != rightpassword)
    {
        cout << "密码错误! 请重新输入" << endl;
        goto part4;
    }
    cout << "密码正确!" << endl;
    fflush(stdin);
}
 
void checkin(room* p[], int size, const string& filename1, const string& filename2)
{
    HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);//句柄
    cout.width(120);
    SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN);
    cout << "欢迎来到 小刘 酒店!\n本酒店设有 标准房, 商务套房 和 大床房. \n目前可入住房间为: " << endl;
    int vacant_room[6];
    int j = 0;
    int temp, temp1;
    string str;
    for (int i = 0; i < 6; i++)
    {
        if (p[i]->returnorder() == 0)
        {
            temp1 = p[i]->returnnumber();
            cout << temp1 << "、";
            vacant_room[j] = temp1;
            j++;
        }
    }
    cout << endl << "101-102 是标准间. 价格为 100 CNY 每晚" << endl;
    cout << "103-104 是商务套间. 价格为 200 CNY 每晚" << endl;
    cout << "105-106 是大床房. 价格为 300 CNY 每晚" << endl;
part1:
    try
    {
        cout << '\n' << setiosflags(ios_base::left) << " 请输入你选择的房间号." << endl;
        cout << "共有 " << j << " 间空房." << endl;
        cin >> temp;
        check(vacant_room, j, temp);
    }
    catch (int e)
    {
        cout << "房间 " << e << " 不可选择入住,请重新选择." << endl;
        goto part1;
    }
    room* current = NULL;
    for (int i = 0; i < 6; i++)
    {
        if (p[i]->returnnumber() == temp)
        {
            current = p[i];
            break;
        }
    }
part2:
    try
    {
        cout << "请输入 入住 日期" << endl;
        cin >> temp;
 
        cout << "请输入 离店 日期" << endl;
        cin >> temp1;
        current->getdate(temp, temp1);
 
        cout << "请输入您的 姓名 " << endl;
        cin >> str;
        current->getname(str);
 
        cout << "请输入您的 身份证号 " << endl;
        cin >> str;
        current->getID(str);
 
        cout << "请输入您的 手机号 " << endl;
        cin >> str;
        current->getphone(str);
 
        current->getorder(static_cast<bool>(1));
 
        cout << "您的 消费金额 ";
        temp = current->sumprice();
        cout << temp << endl;
    }
    catch (runtime_error& e)
    {
        cout << e.what() << endl;
        goto part2;
    }
    catch (out_of_range& e)
    {
        cout << e.what() << endl;
        goto part2;
    }
    cout << resetiosflags(ios_base::left);
    try
    {
        update(p, filename1);
        addRecord(filename2, current);
    }
    catch (file_exception& e)
    {
        cout << "Fail to open " << e.get_filename() << endl;
    }
}
 
void checkout(room* p[], int size, const string& filename)
{
    int temp;
    HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);//句柄
    SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE);
    cout << " 感谢您的光临,欢迎您下次入住! " << endl;
    cout << endl;
part3:
    cout << " 请输入你想要 退房 的房间号." << endl;
    cin >> temp;
    room* current = NULL;
    cout << setiosflags(ios_base::left);
    int orderroom[6];
    int j = 0;
    try
    {
        for (int i = 0; i < 6; i++)
        {
            if (p[i]->returnnumber() == temp)
            {
                current = p[i];
            }
            if (p[i]->returnorder() == 1)
            {
                orderroom[j] = p[i]->returnnumber();
                j++;
            }
        }
        check(orderroom, j, temp);
    }
    catch (int e)
    {
        cout << "房间 " << e << " 错误,请重新输入!" << endl;
        goto part3;
    }
    cout << resetiosflags(ios_base::left);
    current->getorder(static_cast<bool>(0));
    try
    {
        update(p, filename);
    }
    catch (file_exception& e)
    {
        cout << "Fail to open " << e.get_filename() << endl;
    }
}
 
void searchfor(room* p[], int size, string& rightpassword)
{
    int temp1;
    password(rightpassword);
    cout << "如果您想要修改密码 请输入 1 , 2 键继续" << endl;
    cin >> temp1;
    fflush(stdin);
    if (temp1 == 1)
    {
        password(rightpassword);
        cout << "请输入新的密码 " << endl;
        rightpassword = ' ';
        cin >> rightpassword;
    }
    HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);//句柄
    SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_BLUE);
    cout << "请输入想要查询的房间号 ." << endl;
    cin >> temp1;
    for (int i = 0; i < 6; i++)
    {
        if (p[i]->returnnumber() == temp1)
        {
            cout << p[i] << endl;
            break;
        }
    }
}
 
int main()
{
    room* p[10];
    set_new_handler(nomorememory);//处理new分配异常
    for (int i = 0; i < 2; i++)
    {
        p[i] = new standard();
    }
    for (int i = 2; i < 4; i++)
    {
        p[i] = new suite();
    }
    for (int i = 4; i < 6; i++)
    {
        p[i] = new kingsize();
    }
    const string filename1 = "此处填写路径   \\Input.txt";
    const string filename2 = "此处填写路径   \\Output.txt";
    try
    {
        ifstream ifs;//构建输入流对象,以二进制形式打开,得到文件内容
        ifs.open(filename1, ios_base::binary);
        if (ifs)
        {
            for (int i = 0; i < 6; i++)
            {
                int roomnumber, price, start_date, end_date;
                bool order;
                string name;
                string ID;
                string phone;
                ifs >> roomnumber >> price >> start_date >> end_date >> order >> name >> ID >> phone;
                p[i]->getnumber(roomnumber);
                p[i]->getprice(price);
                p[i]->getdate(start_date, end_date);
                p[i]->getorder(order);
                p[i]->getname(name);
                p[i]->getID(ID);
                p[i]->getphone(phone);
            }
        }
        else
            throw file_exception(filename1);
        ifs.close();
    }
    catch (file_exception& e)
    {
        cout << "Fail to open " << e.get_filename() << endl;
    }
    catch (runtime_error& e)
    {
        cout << e.what() << endl;
    }
    catch (out_of_range& e)
    {
        cout << e.what() << endl;
    }
 
    int temp;
    HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);//句柄
    SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_BLUE);
    printf("▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓\n\n");
    cout << "\t\t\t小刘 酒店管理系统        \n\n\t\t游客 please input 1. 管理员 please input 2." << endl;
    printf("\n▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓\n\n");
    cin >> temp;
    system("cls");
    if (temp == 1)
    {
        while (1)
        {
            SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_BLUE);//设置背景和字体颜色
            cout << "欢迎来到 小刘 酒店!(游客)\n ";
 
            SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN);
            cout << "若你想要安全退出 ,please input 0. \n ";
 
            SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY | FOREGROUND_BLUE);
            cout << "若你想要入住酒店, please input 1.\n ";
 
            SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE);
            cout << "若你想要办理退房, please input 2.\n";
            int temp2;
            string str;
            cin >> temp2;
            system("cls");
            if (temp2 == 0)
                break;
            if (temp2 == 1)
            {
                checkin(p, 6, filename1, filename2);
            }
            if (temp2 == 2)
            {
                checkout(p, 6, filename1);
            }
            cout << "succeed!" << endl;
            system("pause");
            system("cls");
        }
    }
    if (temp == 2)
    {
        while (1)
        {
            SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_BLUE);//设置背景和字体颜色
            cout << "欢迎来到 小刘 酒店!(管理员)\n ";
 
            SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN);
            cout << "若你想要安全退出 ,please input 0. \n ";
 
            SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY | FOREGROUND_BLUE);
            cout << "若你想要入住酒店, please input 1.\n ";
 
            SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE);
            cout << "若你想要办退房, please input 2.\n ";
 
            SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_BLUE);
            cout << "若你想要查询房间信息, please input 3.\n";
            int temp2;
            string str;
            cin >> temp2;
            system("cls");
            if (temp2 == 0)
                break;
            if (temp2 == 1)
            {
                checkin(p, static_cast<int>(6), filename1, filename2);
            }
            if (temp2 == 2)
            {
                checkout(p, static_cast<int>(6), filename1);
            }
            if (temp2 == 3)
            {
                string rightpassword = "123456";            //默认初始密码
                searchfor(p, 6, rightpassword);
            }
            system("pause");
            system("cls");
        }
    }
    return 0;
}

注:代码使用前需要先向代码中自定义路径下的Input.txt文档中预存信息

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/weixin_43811333/article/details/107361540

延伸 · 阅读

精彩推荐
  • C/C++VC实现五子棋游戏的一个算法示例

    VC实现五子棋游戏的一个算法示例

    这篇文章主要介绍了VC实现五子棋游戏的一个算法示例,对于学习数据结构与算法的朋友有一定的借鉴价值,需要的朋友可以参考下...

    C语言程序设计9062021-01-27
  • C/C++C语言SQLite3事务和锁的操作实例

    C语言SQLite3事务和锁的操作实例

    这篇文章主要介绍了C语言SQLite3事务和锁的操作,结合完整实例形式分析了C语言针对SQLite3数据库的事务与锁相关操作技巧,需要的朋友可以参考下...

    lifan512432021-05-25
  • C/C++利用C/C++实现贪吃蛇游戏

    利用C/C++实现贪吃蛇游戏

    这篇文章主要为大家详细介绍了利用C/C++实现贪吃蛇游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    what how when why5782022-02-10
  • C/C++位运算实现十进制转换为二进制

    位运算实现十进制转换为二进制

    这篇文章主要介绍了位运算实现十进制转换为二进制的相关资料,需要的朋友可以参考下...

    C语言教程网4452021-02-23
  • C/C++C语言 使用qsort函数来进行快速排序

    C语言 使用qsort函数来进行快速排序

    排序方法有很多种:选择排序,冒泡排序,归并排序,快速排序等。 看名字都知道快速排序是目前公认的一种比较好的排序算法。因为他速度很快,所以系...

    诚挚的乔治4732022-09-16
  • C/C++C语言中的初阶指针详解

    C语言中的初阶指针详解

    这篇文章主要介绍了C语言中的初阶指针,介绍了其相关概念,具有一定参考价值。需要的朋友可以了解下,希望能够给你带来帮助...

    地火轰雷7022022-01-19
  • C/C++c++ 巧开平方的实现代码

    c++ 巧开平方的实现代码

    开平方指一种数学的运算方式,求一个数a的平方根的运算,叫做开平方,开平方是平方的逆运算。如果没有计算器,我们如何求2的平方根...

    C++教程网4372020-11-26
  • C/C++C++实现航空订票系统课程设计

    C++实现航空订票系统课程设计

    这篇文章主要为大家详细介绍了C++实现航空订票系统课程设计,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    蓉蓉兔686411472022-10-19