设计思路

  • 一个酒店管理系统有Guest旅客类、Room房间类、Hotel酒店类、HotelMangerSystem酒店操作系统管理类
  • Guest旅客类
  • 属性: 姓名、id、联系方式(本文仅写了姓名)
  • Room房间类
  • 属性:房间号、旅客指针,初始化为nullptr,表示房间为空
  • 方法:判断房间是否为空、登记旅客入住、管理旅客退房、查询旅客信息
  • Hotel酒店类
  • 属性:很多房间、有n层、每层m间,用二维的vector向量存储
  • 方法:查询房间号
  • HotelMangerSystem酒店操作系统管理类
  • 属性:一个酒店成员
  • 方法:展示操作界面、旅客入住、旅客退房、显示所有房间信息

设计各个类的构造函数

Guest旅客类

旅客类的构造函数比较简单,本文只写了name属性,所以只用初始化name属性就够了

// 旅客类
class Guest{
private:
    string name;
public:
    Guest(string name=""):name(name){

    }
};

Room房间类

房间类的构造函数只用给no房间号这个形参就行了,旅客指针pg默认初始化为空,表示该房间没有旅客入住

// 房间类
class Room{
private:
    int no;
    Guest *pg;
public:
    // 构造函数
    Room(int no=0):no(no),pg(nullptr){

    }
};

Hotel酒店类

酒店类的构造函数稍微复杂了点,需要给定酒店的层数layer和每层房间数count,然后根据这两个参数构造二维向量,来存储酒店的所有房间

// 酒店类
class Hotel{
private:
    size_t layer;   // 层数
    size_t count;   // 每层房间数
    vector<vector<Room> > rooms;    // 二维向量,存储多层房间
public:
    Hotel(size_t layer = 5,size_t count = 8):layer(layer),count(count){
        for(int i=0;i<layer;i++)
        {
            vector<Room> rc;
            for(int j=0;j<count;j++)
            {
                rc.push_back(Room((i+1)*100+j+1)); // 使用匿名对象
            }
            rooms.push_back(rc);
        }
    }
   
};

HotelMangerSystem酒店操作系统管理类

酒店操作系统类只定义了一个酒店类的成员对象,所以它的构造函数可以不写,系统默认会调用酒店类的无参构造函数

// 酒店操作系统类
class HotelMangerSystem{
private:
    Hotel h;
    enum OPER{QUIT=0,IN,OUT,SHOW};
public:
    
};

代码实现

设计完各个类后,我们就可以根据功能来实现各个类的方法,最后在main函数中运行即可

#include <iostream>
#include <vector>
#include <iomanip>
#include <string>
using namespace std;

// 旅客类
class Guest{
private:
    string name;
public:
    Guest(string name=""):name(name){

    }
    string getName(void)const{
        return name;
    }
    void setName(const string& name){
        this->name = name;
    }
    // 为了方便名字的获取和显示,重载输入输出运算符
    friend ostream& operator<<(ostream &os,const Guest& g);
    friend istream& operator>>(istream &is,Guest& g);
};

ostream& operator<<(ostream &os,const Guest& g)
{
    return os << g.name;
}

istream& operator>>(istream &is,Guest& g){
    return is >> g.name;
}

// 房间类
class Room{
private:
    int no;
    Guest *pg;
public:
    // 构造函数
    Room(int no=0):no(no),pg(nullptr){

    }
    // 析构函数
    ~Room(void){
        if(pg){
            delete pg;
            pg = nullptr;
        }
    }
    // 拷贝构造
    Room(const Room &r){
        if(r.pg)
        {
            pg = new Guest(*r.pg);
        }
        else{
            pg = nullptr;
        }
        no = r.no;
    }
    // 拷贝赋值
    Room& operator=(const Room &r){
        if(this != &r)
        {
            Room tmp(r);
            swap(pg,tmp.pg);
            no = r.no;
        }
        return *this;
    }
    // 判断房间是否为空
    bool Empty(void)const{
        return pg == nullptr;
    }
    // 登记旅客入住,让pg指向新的guest对象
    void check_in(const Guest &g){
        if(!Empty()){
            throw string("The Room is already checked in!");
        }
        pg = new Guest(g);
    }
    // 管理旅客退房,pg指向空
    Guest check_out(void){
        if(Empty()){
            throw string("The Room is Empty!");
        }
        Guest g(*pg);
        delete pg;
        pg=nullptr;
        return g;
    }
    int getNo(void)const{
        return no;
    }
    void setNo(int no){
        this->no = no;
    }
    // 返回房间旅客信息
    Guest *getGuest()const{
        return pg;
    }
};

// 酒店类
class Hotel{
private:
    size_t layer;   // 层数
    size_t count;   // 每层房间数
    vector<vector<Room> > rooms;    // 二维向量,存储多层房间
public:
    Hotel(size_t layer = 5,size_t count = 8):layer(layer),count(count){
        for(int i=0;i<layer;i++)
        {
            vector<Room> rc;
            for(int j=0;j<count;j++)
            {
                rc.push_back(Room((i+1)*100+j+1)); // 使用匿名对象
            }
            rooms.push_back(rc);
        }
    }
    // 根据房间号查找房间
    Room& Find(int rno){
        for(int i=0;i<layer;++i)
        {
            for(int j=0;j<count;++j)
            {
                if(rooms[i][j].getNo() == rno)
                {
                    return rooms[i][j];
                }
            }
        }
        throw string("Not found the room number!");
    }
    // 返回二维向量,即所有房间
    const vector<vector<Room> >& getRooms()const {
        return rooms;
    }

};

// 酒店操作系统类
class HotelMangerSystem{
private:
    Hotel h;
    enum OPER{QUIT=0,IN,OUT,SHOW};
public:
    void menu(void){
        cout << "===========================================================================" << endl;
        cout << "-----------------------------Hotel management system-----------------------" << endl;
        cout << "--"<< QUIT << ".quit" << endl;
        cout << "--"<< IN << ".in" << endl;
        cout << "--"<< OUT << ".out" << endl;
        cout << "--"<< SHOW << ".show" << endl;
        cout << ">>>" ;
    }
    // 旅客入住
    void check_in(){
        int no = 0;
        cout << "input the room's number:";
        cin >> no;
        try{
            Room& room = h.Find(no);
            // 房间不为空,已有旅客入住
            if(!room.Empty())
            {
                cout << "The room is already checked in!" << endl;
                return ;
            }
            Guest g;
            cout << "input the guest's name:";
            cin >> g;
            // 登记旅客入住
            room.check_in(g);
            cout << "welcome " << g << ",the room's number is:" << no << endl;
        }catch(string &e){
            cout << "Not fount the room!" << endl;
            return ;
        }
        
    }
    // 旅客退房
    void check_out(){
        int no = 0;
        cout << "input the room's number:";
        cin >> no;
        try{
            Room& room = h.Find(no);
            // 房间为空,没有旅客入住
            if(room.Empty())
            {
                cout << "The room is Empty!" << endl;
                return ;
            }
            // 管理旅客退房
            Guest g = room.check_out();
            cout << "welcome " << g << " next come !" << endl;
        }catch(string &e){
            cout << "Not fount the room!" << endl;
            return ;
        }
    }
    // 展示所有房间信息
    void show(){
        const vector<vector<Room>>& rooms = h.getRooms();   // 获取二维数组
        for(int i=0;i<rooms.size();++i)
        {
            for(int j=0;j<rooms[i].size();++j)
            {
                // 显示房间号
                cout << setw(5) << rooms[i][j].getNo() << "  ";
            }
            cout << endl;
            
            for(int j=0;j<rooms[i].size();++j)
            {
                // 显示旅客信息
                if(rooms[i][j].Empty())
                {
                    cout << "-----" << "  ";
                }
                else
                {
                    cout << setw(5) << right << rooms[i][j].getGuest()->getName() << "  "; // right表示右对齐
                }
            }
            cout << endl;
            
        }
    }
    void run(){
        while(true){
            menu();
            int in=0;
		    cin >> in;
            switch(in){
                case QUIT:
                    cout << "GoodBye!" << endl;
                    return ;
                case IN:check_in(); break;
                case OUT:check_out(); break;
                case SHOW:show(); break;
                default: 
                    cout << "Error!" << endl;
            }
        }
    }
};

int main()
{
    HotelMangerSystem hms;
    hms.run();
}

效果展示

酒店管理系统 element plus_c++

酒店管理系统 element plus_c++_02

 

酒店管理系统 element plus_酒店管理系统 element plus_03

 

酒店管理系统 element plus_构造函数_04

  

酒店管理系统 element plus_酒店管理系统 element plus_05

酒店管理系统 element plus_酒店管理系统 element plus_06

 

酒店管理系统 element plus_二维_07

  

酒店管理系统 element plus_二维_08

酒店管理系统 element plus_酒店管理系统 element plus_09

 

酒店管理系统 element plus_酒店管理系统 element plus_10