酒店管理系统项目:
旅客类:
姓名、性别、身份证号
构造函数
show
setName\setSex\setId (可选)
房间类:
房号、房型、房价、入住数、旅客数组
构造函数
入住
退房
show
酒店类:(单例模式)
层数、每层房数、房间对象(三级指针)
构造函数 (读酒店信息文件进行构造)
入住
退房
查看空房
查询房间
升级
管理员类:(选做)
5 层数
4 第一层房间数
1001 2 200
1002 3 200
1003 2 200
1005 2 200
2
1.guest.h
- #pragma once
-
- #include <iostream>
- #include <string>
- #include <fstream>
-
- using namespace std;
-
- class Guest
- {
- string name;
- char sex;
- string id;
- public:
- Guest(const string name="", char sex='m', const string id="") : name(name), sex(sex), id(id) {}
- //显示客人信息
- void show()
- {
- cout<<name<<" "<<sex<<" "<<id<<endl;
- }
- // friend ostream& operator<<(ostream& os, const Guest& g)
- // {
- // return os<<g.name<<" "<<g.sex<<" "<<g.id;
- // }
- };
2.room.h
- #pragma once
-
- #include "guest.h"
-
- class Room
- {
- char type;
- short id;
- float price;
- int guest_cnt;
- Guest** guests;
-
- public:
- Room(char type, short id, float price):type(type), id(id), price(price)
- {
- guests = new Guest*[100];
- guest_cnt = 0;
- }
-
- //显示房间,客人信息
- void show(void)
- {
- cout<<id<<" "<<(int)type<<" "<<price<<endl;
- for(int i=0; i<guest_cnt; i++)
- {
- cout<<"旅客"<<i+1<<": "<<endl;
- guests[i]->show();
- }
- }
- //运算符重载写法
- // friend ostream& operator<<(ostream& os, const Room& r)
- // {
- // os<<r.id<<" "<<r.type<<" "<<r.price<<endl;
- // for(int i=0; i<r.guest_cnt; i++)
- // {
- // os<<"旅客"<<i+1<<": "<<endl;
- // os<<r.guests[i];
- // }
- // return os;
- // }
-
- bool cmp_room_id(int room_id)
- {
- return id==room_id;
- }
-
- bool in(Guest* guest)
- {
- if(guest_cnt >= type)
- {
- return false;
- }
- guests[guest_cnt++] = guest;
- return true;
- }
- //重载==运算符,对比房号
- bool operator==(int room_id)
- {
- return id==room_id;
- }
-
- //是否空房
- bool empty(void)
- {
- return guest_cnt==0;
- }
-
- //退房
- void out(void)
- {
- if(0==guest_cnt)
- {
- cout<<"房间"<<id<<"已空"<<endl;
- }
- for(int i=0; i<guest_cnt; i++)
- {
- delete guests[i];
- }
- guest_cnt = 0;
- cout<<"退房成功"<<endl;
- system("pause");
- }
- };
3.hotel.h
- #pragma once
-
- #include "room.h"
- #include "tools.h"
-
- class Hotel
- {
- static Hotel hotel;
- int level_cnt;
- int *level_room;
- Room ***rooms;
-
- Hotel(void){}
- Hotel(const Hotel& that){}
- public:
- static Hotel& getHotel(void)
- {
- return hotel;
- }
- void load(const char *path); //加载房间信息
- void start(void); //程序运行
- int menu(void); //显示菜单并返回选择
- void in(void); //入住
- void list(void); //显示空房
- void query(void); //查询已入住房间,房客信息
- void out(void); //退房
- void upgrade(void); //升级房间
- Room *find(int room_id); //根据房间号查找房间,因为入住或退房等都需要房间号,所以提供一个接口
- };
4.hotel.cpp
- #include "hotel.h"
-
- Hotel Hotel::hotel;
-
- void Hotel::load(const char *path)
- {
- FILE *fp = fopen(path, "r");
- if (NULL == fp)
- {
- perror("fopen");
- return;
- }
-
- fscanf(fp, "%d", &level_cnt);
- level_room = new int[level_cnt];
- rooms = new Room **[level_cnt];
- for (int i = 0; i < level_cnt; i++)
- {
- fscanf(fp, "%d", &level_room[i]);
- rooms[i] = new Room *[level_room[i]];
- for (int j = 0; j < level_room[i]; j++)
- {
- char type;
- short id;
- float price;
- fscanf(fp, "%hd %hhd %f", &id, &type, &price);
- rooms[i][j] = new Room(type, id, price);
- rooms[i][j]->show();
- }
- }
-
- // C++写法
- // ifstream ifs(path);
- // if(!ifs.good())
- // {
- // cout<<"errno"<<endl;
- // return;
- // }
- // ifs>>level_cnt;
- // level_room = new int[level_cnt];
- // rooms = new Room **[level_cnt];
- // for(int i=0;i<level_cnt;i++)
- // {
- // ifs>>level_room[i];
- // rooms[i] = new Room *[level_room[i]];
- // for(int j=0;j<level_room[i];j++)
- // {
- // char type;
- // short id;
- // float price;
- // ifs>>id>>type>>price;
- // rooms[i][j]=new Room(type, id, price);
- // }
- // }
-
- // cout<<__func__<<endl;
- }
-
- void Hotel::start(void)
- {
- while (true)
- {
- switch (menu())
- {
- case '1':in(); break;
- case '2':query(); break;
- case '3':out(); break;
- case '4':list(); break;
- case '0':
- cout << "欢迎下次光临!" << endl;
- return;
- break;
- default:break;
- }
- }
-
- //cout << __func__ << endl;
- }
-
- int Hotel:: menu(void) // 显示菜单并返回选择
- {
- system("cls");
- cout << "***欢迎使用XXX酒店管理系统***" << endl;
- cout << "1.入住 2.查询" << endl;
- cout << "3.退房 4.查看" << endl;
- cout << "0.退出" << endl;
- return get_cmd('0','4');
- }
-
- void Hotel:: in(void)
- {
- string name,id;
- char sex;
- cout << "请输入姓名、性别、身份证号:";
- cin >> name >> sex >> id;
- Guest *guest = new Guest(name, sex, id);
- int room_id;
- cout << "请输入要入住的房间:";
- cin >> room_id;
- Room *room = find(room_id);
- if (room == NULL)
- {
- cout << "未找到该房间:" <<room_id <<",请检查房间号"<< endl;
- return;
- }
- if(room->in(guest))
- {
- cout << "入住成功!" << endl;
- system("pause");
- }
- else
- {
- cout << "该房间已满!" << endl;
- delete guest;
- }
- }
- void Hotel::list(void)
- {
- for(int i=0;i<level_cnt;i++)
- {
- for(int j=0;j<level_room[i];j++)
- {
- if(rooms[i][j]->empty())
- {
- rooms[i][j]->show();
- }
- }
- }
- system("pause");
- }
- void Hotel::query(void)
- {
- for(int i=0;i<level_cnt;i++)
- {
- for(int j=0;j<level_room[i];j++)
- {
- if(!rooms[i][j]->empty())
- {
- rooms[i][j]->show();
- }
- }
- }
- system("pause");
- }
- void Hotel::out(void)
- {
- int room_id;
- cout << "请输入要退房的房间号:";
- cin >> room_id;
- Room *room = find(room_id);
- if (room == NULL)
- {
- cout << "未找到该房间:" << room_id << ",请检查房间号" << endl;
- return;
- }
- room->out();
- }
- void Hotel::upgrade(void)
- {
-
- }
- Room *Hotel::find(int room_id)
- {
- for (int i = 0; i < level_cnt; i++)
- {
- for (int j = 0; j < level_room[i]; j++)
- {
- if (rooms[i][j]->cmp_room_id(room_id))
- {
- return rooms[i][j];
- }
- }
- }
- return NULL;
- }
5.tools.h
- #pragma once
-
- #include "hotel.h"
-
- char get_cmd(char a, char b);
-
- void anykey_continue(void);
6.tool.cpp
- #include "hotel.h"
-
- char get_cmd(char a, char b) // 输入命令
- {
- char cmd;
- while (true)
- {
- cout << "请输入命令(1-" << b << "):";
- cin >> cmd;
- if (cmd >= a && cmd <= b)
- {
- return cmd;
- }
- cout << "输入错误,请重新输入!" << endl;
- }
- }
-
- // //按任意键继续(Linux)
- // void anykey_continue(void)
- // {
- // stdin->IO_read_ptr = stdin->IO_read_end;
- // cout << "按任意键继续..." << endl;
- // getch();
- // cout << endl;
- // }
7.main.cpp
- #include "hotel.h"
-
- int main(int argc,const char* argv[])
- {
- if(2!=argc)
- {
- cout<<"User: ./hotel [hotel_name]"<<endl;
- return 0;
- }
-
- Hotel &hotel=Hotel::getHotel();
- hotel.load(argv[1]);
- hotel.start();
-
- return 0;
- }
8.hotel.txt
- 5
- 5
- 1001 2 200
- 1002 3 200
- 1003 2 200
- 1004 2 200
- 1005 2 200
- 6
- 2001 2 300
- 2002 3 300
- 2003 2 300
- 2004 2 300
- 2005 2 300
- 2006 2 300
- 7
- 3001 3 400
- 3002 3 400
- 3003 3 400
- 3004 3 400
- 3005 3 400
- 3006 3 400
- 3007 3 400
- 8
- 4001 4 500
- 4002 4 500
- 4003 4 500
- 4004 4 500
- 4005 4 500
- 4006 4 500
- 4007 4 500
- 4008 4 500