本文实例为大家分享了c++实现宾馆房间管理系统的具体代码,供大家参考,具体内容如下
一、问题描述
设计一个程序实现对宾馆房间的基本管理,可以实现:客房信息的录入功能;客人入住登记、客人退房结算;客房信息浏览功能,浏览全部客户的信息,客房信息和客户信息分别保存于不同文件;客房信息查询,查询空房间情况,实现按房间号查询等。
二、基本要求
(1)使用面向对象编程思想编写开发过程中需要用到的类,比如:至少包含四个类:日期类,客房类,主要包含客房信息(房号类型,是否有客人等)及相关操作;客人类,主要完 成客户信息(身份证,入住时间,姓名,性别等)的相关操作;管理类实现对客房的管理。
(2)输入和输出可以使用文本文件重定向输入(保存数据为磁盘文件);也可以使用标准输入输出进行(提交时需要提交txt格式输入数据)。比如:room.txt 的文件,文件中应包含 20 条以上记录(房间的初始状态),guest.txt 的文本文件,包含 10 条以上客人记录。 在运行程序时自动载入。
(3)基本功能要求具有增、删、改、查。
基本流程图
#include<iostream> #include<fstream> #include<string> #include<iomanip> #include<windows.h> #include<conio.h> #define max 100 using namespace std; class data//日期类,记录交易时间 { public: data(){}//缺省构造函数 ~data(){}//析构函数 void setdate(int year,int month,int day)//接收输入的日期 { this->year=year; this->month=month; this->day=day; } int getyear(){ return year; } int getmonth(){ return month; } int getday(){ return day; } private: int year; int month; int day; }; class room { public: room *r[max];//房间对象指针数组 int room_count; //记录房间数量 room(int number,string type,double price,string whether)//构造函数 { this->number=number; this->type=type; this->whether=whether; this->price=price; } int inputnumber() {return number;} string inputtype(){return type;} string inputwhether(){return whether;} double inputprice(){return price;} void setwether(string _state) {whether=_state;} void show() {cout<<"房号: "<<number<<"\t"<<"房间类型: "<<type<<"\t"<<"房间状态: "<<whether<<"\t"<<"价格: "<<price<<endl;} protected: int number; //房号 string type;//类型 string whether;//是否有客人 double price;//价格 }; class guest { public: guest *g[max]; //客人对象指针数组 int guest_count; //记录客人数量 guest(int number,string name,int id,string sex,string intime,int days) //构造函数 { this->name=name; this->id=id; this->sex=sex; this->number=number; this->intime=intime; this->days=days; } int inputnumber(){return number;} string inputname(){return name;} string inputsex(){return sex;} int inputdays(){return days;} string inputintime(){return intime;} int inputid(){return id;} void show() { cout<<"顾客姓名: "<<name<<"\t 身份证号: "<<id<<"\t性别: "<<sex<<"\t入住时间: "<<intime<<"\t入住天数: "<<days<<endl; } protected: int number;//房号 string name;//顾客姓名 int id;//身份证号 string sex;//性别 string intime;//入住时间 int days; //入住天数 }; class manage { public: guest *g[max]; //客人对象指针数组 int guest_count; //记录客人数量 room *r[max];//房间对象指针数组 int room_count; //记录房间数量 /*操作函数*/ void increaseroom();//添加客房信息 void check_in(); //删除客房信息,办理入住 void check_out(); //退房 int payment();//结账 void display(int n);//浏览所有信息(1浏览房间,2浏览顾客) void readdata(); //从文件中获取房间和顾客的信息 void writedata(int n);//向文件中写入所有的信息 void writeroom(room *r);//客房信息写入 void writeguest(guest *g);//顾客信息写入 /*查询菜单 */ void searchmenu();//查询主菜单 void searchtype();//查询所有空房间; void searchnumber();//按房间号查询 }; static int i=0; void manage::searchmenu() { int n; system("cls"); cout<<"===================================="<<endl; cout<<"= 查 询 菜 单 ="<<endl; cout<<"===================================="<<endl; cout<<"========= 1、查 询 空 房 ======="<<endl; cout<<"========= 2、按房间号查询 ======="<<endl; cout<<"===================================="<<endl; cout<<endl<<"请选择: "; cin>>n; switch(n) { case 1:searchtype(); break; case 2:searchnumber();break; } } void manage::increaseroom()//添加房间 { string type,whether; double price; int number; cout<<"请输入房号: "; cin>>number; cout<<"请输入房间类型: "; cin>>type; cout<<"请输入价格: "; cin>>price; cout<<"请输入房间状态: "; cin>>whether; writeroom(new room(number,type,price,whether)); } void manage::check_in()//删除房间信息,即入房登记 { readdata(); searchtype(); string name,intime,sex,type; int days,number; int id; cout<<"请输入房号: "; cin>>number; cout<<"请输入顾客的姓名: "; cin>>name; cout<<"请输入顾客的身份证号: "; cin>>id; cout<<"请输入顾客的性别: "; cin>>sex; cout<<"请输入入住日期: "; cin>>intime; cout<<"请输入入住天数: "; cin>>days; for(i=0;i<room_count;i++) { if(number==r[i]->inputnumber()) { writeguest(new guest(number,name,id,sex,intime,days)); r[i]->setwether("有"); writedata(1); cout<<"住房登记成功!"<<endl; } } } int manage::payment()//退房结账 { readdata(); display(2); int number; cout<<"请输入房号: "; cin>>number; for(i=0;i<guest_count;i++) { if(number==g[i]->inputnumber()) { return i; } } } void manage::check_out() { int x=payment(); readdata(); for(i=0;i<room_count;i++) { if(g[x]->inputnumber()==r[i]->inputnumber()) { r[i]->setwether("无"); cout<<"退房成功,您一共消费了 "<<g[x]->inputdays() *r[i]->inputprice()<<" 元"<<endl; writedata(1); } } g[x]=null; writedata(2); } void manage::display(int n)//浏览所有房间信息 { readdata(); switch(n){ case 1: for(i=0; i<room_count-1; i++) { cout<<"房号:"<<r[i]->inputnumber()<<"\t房间类型: "<<r[i]->inputtype()<<"\t房间价格: "<<r[i]->inputprice()<<"\t房间状态: "<<r[i]->inputwhether()<<endl<<endl; } break; case 2: for(i=0;i<guest_count-1;i++) { cout<<"房间号: "<<g[i]->inputnumber()<<"\t顾客姓名: "<<g[i]->inputname()<<"\t身份证号: "<<g[i]->inputid()<<"\t顾客性别:"<<g[i]->inputsex()<<"\t入住时间: "<<g[i]->inputintime()<<"\t入住天数: "<<g[i]->inputdays()<<endl<<endl; } break; } } void manage::readdata() { fstream rin,gin; rin.open("room.txt",ios::in);//打开文件 if(!rin) { cout<<"未找到room文件,请先建立文件!"<<endl; return; } room_count=0; while(!rin.eof()){ string type,whether; double price; int number; rin>>number>>type>>price>>whether; r[room_count++]=new room(number,type,price,whether); } rin.close();//关闭文件 gin.open("guest.txt",ios::in); if(!gin) { cout<<"未找到guest文件,请先建立文件!"<<endl; return; } guest_count=0; while(!gin.eof()){ string name,intime,sex; int days,number; int id; gin>>number>>name>>id>>sex>>intime>>days; g[guest_count++]=new guest(number,name,id,sex,intime,days); } gin.close(); } void manage::writedata(int n) { switch(n) { case 1: { ofstream rout("room.txt",ios::trunc); //用二进制的方法打开顾客文件 ,覆盖掉之前的所有信息重新写入 for(i=0; i<room_count-1; i++) //根据顾客数量判断输入几组信息 { if(r[i]!=null) { writeroom(r[i]);//调用构造函数来创建顾客信息 } } rout.close(); break;} case 2:{ ofstream gout("guest.txt",ios::trunc); //用二进制的方法打开顾客文件 ,覆盖掉之前的所有信息重新写入 for(i=0; i<guest_count-1; i++) //根据顾客数量判断输入几组信息 { if(g[i]!=null) { writeguest(g[i]);//调用构造函数来创建顾客信息 } } gout.close();break;} } } void manage::writeroom(room *r)//储存单个信息 { ofstream rout("room.txt",ios::app);//打开房间文件,追加读写,不会覆盖掉之前的所有信息 rout<<r->inputnumber()<<"\t"<<r->inputtype()<<"\t"<<r->inputprice()<<"\t"<<r->inputwhether()<<endl; rout.close(); } void manage::writeguest(guest *g)//储存单个信息 { ofstream gout("guest.txt",ios::app);//打开顾客文件,追加读写,不会覆盖掉之前的所有信息 gout<<g->inputnumber()<<"\t"<<g->inputname()<<"\t"<<g->inputid()<<"\t"<<g->inputsex()<<"\t"<<g->inputintime()<<"\t"<<g->inputdays()<<endl; gout.close(); } void manage::searchtype() { readdata(); for(i=0;i<room_count;i++) { if(r[i]->inputwhether()=="无") { r[i]->show();} } } void manage::searchnumber() { readdata(); int number,n; cout<<"请输出要查询的房间号: "; cin>>number; for(i=0;i<room_count-1;i++) { if(number==r[i]->inputnumber()) r[i]->show(); } for(i=0;i<guest_count-1;i++) { if(g[i]->inputnumber()==number) g[i]->show(); } } int main() { manage m; int n; while(1) { system("cls"); cout<<endl<<endl<<endl<<"\t\t\t宾 馆 房 间 管 理 系 统 "<<endl<<endl; cout<<"\t\t\t1、房 间 信 息 的 录 入"<<endl<<endl; cout<<"\t\t\t2、顾 客 入 住 房 间 登 记"<<endl<<endl; cout<<"\t\t\t3、顾 客 退 房 结 账"<<endl<<endl; cout<<"\t\t\t4、所 有 房 间 信 息 显 示"<<endl<<endl; cout<<"\t\t\t5、所 有 顾 客 的 显 示"<<endl<<endl; cout<<"\t\t\t6、查 询 所 有 空 房 间"<<endl<<endl; cout<<"\t\t\t7、查 询 指 定 的 房 间 号"<<endl<<endl; cout<<"\t\t\t8、退 出 系 统"<<endl<<endl; cout<<endl<<"请选择: "; cin>>n; cout<<endl<<endl; switch(n) { case 1:m.increaseroom();getch();break; case 2:m.check_in();getch();break; case 3:m.check_out();getch();break; case 4:m.display(1);getch();break; case 5:m.display(2);getch();break; case 6: m.searchtype();getch();break; case 7: m.searchnumber();getch();break; case 8:exit(0); } } return 0; }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。