C++實(shí)現(xiàn)賓館房間管理系統(tǒng)
本文實(shí)例為大家分享了C++實(shí)現(xiàn)賓館房間管理系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
一、問(wèn)題描述
設(shè)計(jì)一個(gè)程序?qū)崿F(xiàn)對(duì)賓館房間的基本管理,可以實(shí)現(xiàn):客房信息的錄入功能;客人入住登記、客人退房結(jié)算;客房信息瀏覽功能,瀏覽全部客戶的信息,客房信息和客戶信息分別保存于不同文件;客房信息查詢(xún),查詢(xún)空房間情況,實(shí)現(xiàn)按房間號(hào)查詢(xún)等。
二、基本要求
(1)使用面向?qū)ο缶幊趟枷刖帉?xiě)開(kāi)發(fā)過(guò)程中需要用到的類(lèi),比如:至少包含四個(gè)類(lèi):日期類(lèi),客房類(lèi),主要包含客房信息(房號(hào)類(lèi)型,是否有客人等)及相關(guān)操作;客人類(lèi),主要完 成客戶信息(身份證,入住時(shí)間,姓名,性別等)的相關(guān)操作;管理類(lèi)實(shí)現(xiàn)對(duì)客房的管理。
(2)輸入和輸出可以使用文本文件重定向輸入(保存數(shù)據(jù)為磁盤(pán)文件);也可以使用標(biāo)準(zhǔn)輸入輸出進(jìn)行(提交時(shí)需要提交TXT格式輸入數(shù)據(jù))。比如:room.txt 的文件,文件中應(yīng)包含 20 條以上記錄(房間的初始狀態(tài)),guest.txt 的文本文件,包含 10 條以上客人記錄。 在運(yùn)行程序時(shí)自動(dòng)載入。
(3)基本功能要求具有增、刪、改、查。
基本流程圖

#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
#include<windows.h>?
#include<conio.h>
#define Max 100?
using namespace std;
class Data//日期類(lèi),記錄交易時(shí)間?
{
?? ?public:
?? ??? ?Data(){}//缺省構(gòu)造函數(shù)
?? ??? ?~Data(){}//析構(gòu)函數(shù)
?? ??? ?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];//房間對(duì)象指針數(shù)組?? ??? ??? ??? ??? ??? ??? ??? ??? ?
?? ??? ?int Room_count;?? ?//記錄房間數(shù)量?? ?
?? ??? ?Room(int Number,string Type,double Price,string Whether)//構(gòu)造函數(shù)?
?? ??? ?{
?? ??? ??? ?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<<"房號(hào): ?"<<Number<<"\t"<<"房間類(lèi)型: ?"<<Type<<"\t"<<"房間狀態(tài): ?"<<Whether<<"\t"<<"價(jià)格: ? "<<Price<<endl;}?? ?
?? ?protected:
?? ??? ?int Number; //房號(hào)?
?? ??? ?string Type;//類(lèi)型?
?? ??? ?string Whether;//是否有客人?
?? ??? ?double Price;//價(jià)格?
?? ??? ??? ?
};
class Guest
{
?? ?public:
?? ??? ?Guest *g[Max];?? ?//客人對(duì)象指針數(shù)組?? ??? ??? ??? ??? ??? ??? ??? ?
?? ??? ?int Guest_count;?? ?//記錄客人數(shù)量?? ?
?? ??? ?Guest(int number,string Name,int Id,string sex,string Intime,int days) //構(gòu)造函數(shù)?
?? ??? ?{
?? ??? ??? ?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 身份證號(hào): ?"<<Id<<"\t性別: ?"<<sex<<"\t入住時(shí)間: ?"<<Intime<<"\t入住天數(shù): ?"<<days<<endl;
?? ??? ?}
?? ?protected:
?? ??? ?int number;//房號(hào)?
?? ??? ?string Name;//顧客姓名?
?? ??? ?int Id;//身份證號(hào)?
?? ??? ?string sex;//性別?
?? ??? ?string Intime;//入住時(shí)間?
?? ??? ?int days; //入住天數(shù)
};
class Manage?
{
?? ?public:?? ?
?? ??? ?Guest *g[Max];?? ?//客人對(duì)象指針數(shù)組?? ??? ??? ??? ??? ??? ??? ??? ?
?? ??? ?int Guest_count;?? ?//記錄客人數(shù)量?? ??? ?
?? ??? ?Room *r[Max];//房間對(duì)象指針數(shù)組?? ??? ??? ??? ??? ??? ??? ??? ??? ?
?? ??? ?int Room_count;?? ?//記錄房間數(shù)量
?? ?/*操作函數(shù)*/
?? ??? ?void IncreaseRoom();//添加客房信息?
?? ??? ?void Check_In();?? ?//刪除客房信息,辦理入住
?? ??? ?void Check_Out();?? ?//退房?? ?
?? ??? ?int Payment();//結(jié)賬?
?? ??? ?void Display(int n);//瀏覽所有信息(1瀏覽房間,2瀏覽顧客) ?? ??? ??? ??? ?
?? ??? ?void ReadData(); ?//從文件中獲取房間和顧客的信息
?? ??? ?void WriteData(int n);//向文件中寫(xiě)入所有的信息
?? ??? ?void WriteRoom(Room *r);//客房信息寫(xiě)入?
?? ??? ?void WriteGuest(Guest *g);//顧客信息寫(xiě)入?
?? ?/*查詢(xún)菜單 */?
?? ??? ?void SearchMenu();//查詢(xún)主菜單?
?? ??? ?void SearchType();//查詢(xún)所有空房間;?
?? ??? ?void SearchNumber();//按房間號(hào)查詢(xún)?? ??? ?
};?
static int i=0;?
void Manage::SearchMenu()
{
?? ?int n;
?? ?system("cls");
?? ?cout<<"===================================="<<endl;
?? ?cout<<"= ? ? ? ? 查 ? 詢(xún) ? 菜 ? 單 ? ? ? ?="<<endl;
?? ?cout<<"===================================="<<endl;
?? ?cout<<"========= ?1、查 詢(xún) 空 房 ? ?======="<<endl;
?? ?cout<<"========= ?2、按房間號(hào)查詢(xún) ? ======="<<endl;
?? ?cout<<"===================================="<<endl;
?? ?cout<<endl<<"請(qǐng)選擇: ";
?? ?cin>>n;
?? ?switch(n)
?? ?{
?? ??? ?case 1:SearchType(); break;?
?? ??? ?case 2:SearchNumber();break;
?? ?}
?}?
void Manage::IncreaseRoom()//添加房間?
{
?? ?string type,Whether;
?? ?double price;
?? ?int number;
?? ?cout<<"請(qǐng)輸入房號(hào): ";?? ?cin>>number;
?? ?cout<<"請(qǐng)輸入房間類(lèi)型: ";?? ?cin>>type;
?? ?cout<<"請(qǐng)輸入價(jià)格: ";?? ?cin>>price;
?? ?cout<<"請(qǐng)輸入房間狀態(tài): ";?? ?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<<"請(qǐng)輸入房號(hào): ";?? ?cin>>number;
?? ?cout<<"請(qǐng)輸入顧客的姓名: "; cin>>name;
?? ?cout<<"請(qǐng)輸入顧客的身份證號(hào): ";?? ?cin>>id;
?? ?cout<<"請(qǐng)輸入顧客的性別: "; cin>>sex;
?? ?cout<<"請(qǐng)輸入入住日期: ";?? ?cin>>intime;
?? ?cout<<"請(qǐng)輸入入住天數(shù): "; 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()//退房結(jié)賬?
{
?? ?ReadData();
?? ?Display(2);
?? ?int number;
?? ?cout<<"請(qǐng)輸入房號(hào): ";?? ??? ?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("無(wú)");
?? ??? ??? ?cout<<"退房成功,您一共消費(fèi)了 "<<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<<"房號(hào):"<<r[i]->InputNumber()<<"\t房間類(lèi)型: "<<r[i]->InputType()<<"\t房間價(jià)格: "<<r[i]->InputPrice()<<"\t房間狀態(tài): "<<r[i]->InputWhether()<<endl<<endl;?
?? ??? ?} break;
?? ?case 2:
?? ??? ?for(i=0;i<Guest_count-1;i++)
?? ??? ?{
?? ??? ??? ?cout<<"房間號(hào): "<<g[i]->InputNumber()<<"\t顧客姓名: "<<g[i]->InputName()<<"\t身份證號(hào): "<<g[i]->InputId()<<"\t顧客性別:"<<g[i]->InputSex()<<"\t入住時(shí)間: "<<g[i]->InputIntime()<<"\t入住天數(shù): "<<g[i]->InputDays()<<endl<<endl;?
?? ??? ?} break;
?? ?}
}
void Manage::ReadData()
{
?? ?fstream Rin,Gin;
?? ?Rin.open("room.txt",ios::in);//打開(kāi)文件?
?? ?if(!Rin)
?? ?{
?? ??? ?cout<<"未找到room文件,請(qǐng)先建立文件!"<<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();//關(guān)閉文件?
?? ?Gin.open("guest.txt",ios::in);
?? ?if(!Gin)
?? ?{
?? ??? ?cout<<"未找到guest文件,請(qǐng)先建立文件!"<<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); //用二進(jìn)制的方法打開(kāi)顧客文件 ,覆蓋掉之前的所有信息重新寫(xiě)入?
?? ??? ?for(i=0; i<Room_count-1; i++) //根據(jù)顧客數(shù)量判斷輸入幾組信息?
?? ??? ?{
?? ??? ??? ?if(r[i]!=NULL)
?? ??? ??? ?{
?? ??? ??? ??? ?WriteRoom(r[i]);//調(diào)用構(gòu)造函數(shù)來(lái)創(chuàng)建顧客信息?
?? ??? ??? ?}
?? ??? ?}
?? ??? ?Rout.close(); break;}
?? ??? ?case 2:{
?? ??? ?ofstream Gout("guest.txt",ios::trunc); //用二進(jìn)制的方法打開(kāi)顧客文件 ,覆蓋掉之前的所有信息重新寫(xiě)入?
?? ??? ?for(i=0; i<Guest_count-1; i++) //根據(jù)顧客數(shù)量判斷輸入幾組信息?
?? ??? ?{
?? ??? ??? ?if(g[i]!=NULL)
?? ??? ??? ?{?? ?
?? ??? ??? ??? ?WriteGuest(g[i]);//調(diào)用構(gòu)造函數(shù)來(lái)創(chuàng)建顧客信息?
?? ??? ??? ?}
?? ??? ?}
?? ??? ?Gout.close();break;}
?? ?}
}?
void Manage::WriteRoom(Room *r)//儲(chǔ)存單個(gè)信息?
{
?? ?ofstream Rout("room.txt",ios::app);//打開(kāi)房間文件,追加讀寫(xiě),不會(huì)覆蓋掉之前的所有信息?
?? ?Rout<<r->InputNumber()<<"\t"<<r->InputType()<<"\t"<<r->InputPrice()<<"\t"<<r->InputWhether()<<endl;
?? ?Rout.close();
}
void Manage::WriteGuest(Guest *g)//儲(chǔ)存單個(gè)信息?
{
?? ?ofstream Gout("guest.txt",ios::app);//打開(kāi)顧客文件,追加讀寫(xiě),不會(huì)覆蓋掉之前的所有信息?
?? ?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()=="無(wú)")
?? ??? ??? ?{?
?? ??? ??? ?r[i]->show();}
?? ??? ?}?? ?
}
void Manage::SearchNumber()
{
?? ?ReadData();
?? ?int number,n;
?? ?cout<<"請(qǐng)輸出要查詢(xún)的房間號(hào): "; 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賓 館 房 間 管 理 系 統(tǒng) ? ? "<<endl<<endl;
?? ??? ?cout<<"\t\t\t1、房 間 信 息 的 錄 入"<<endl<<endl;
?? ??? ?cout<<"\t\t\t2、顧 客 入 住 房 間 登 記"<<endl<<endl;
?? ??? ?cout<<"\t\t\t3、顧 客 退 房 結(jié) 賬"<<endl<<endl;
?? ??? ?cout<<"\t\t\t4、所 有 房 間 信 息 顯 示"<<endl<<endl;
?? ??? ?cout<<"\t\t\t5、所 有 顧 客 的 顯 示"<<endl<<endl;
?? ??? ?cout<<"\t\t\t6、查 詢(xún) 所 有 空 房 間"<<endl<<endl;
?? ??? ?cout<<"\t\t\t7、查 詢(xún) 指 定 的 房 間 號(hào)"<<endl<<endl;
?? ??? ?cout<<"\t\t\t8、退 出 系 統(tǒng)"<<endl<<endl;
?? ??? ?cout<<endl<<"請(qǐng)選擇: ?";
?? ??? ?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;
?}?以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C語(yǔ)言?模擬實(shí)現(xiàn)memcpy與memmove函數(shù)詳解
這篇文章主要介紹了C語(yǔ)言詳解如何模擬內(nèi)存函數(shù),用到了mencpy與memmove兩個(gè)函數(shù),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-04-04
深入線性時(shí)間復(fù)雜度求數(shù)組中第K大數(shù)的方法詳解
本篇文章是對(duì)線性時(shí)間復(fù)雜度求數(shù)組中第K大數(shù)的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
Qt+QListWidget實(shí)現(xiàn)氣泡聊天界面(附源碼)
由于最近的項(xiàng)目需要,做了些相關(guān)IM的工作。所以聊天框也是必不可少的一部分。本文以QListWidget+QPainter繪制的Item做了一個(gè)Demo。該Demo只是做一個(gè)示例,感興趣的可以了解一下2022-12-12
C++入門(mén)到精通之循環(huán)語(yǔ)句的使用教程
這篇文章主要給大家介紹了關(guān)于C++中循環(huán)語(yǔ)句的用法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05

