用c++写一个酒店管理系统
时间: 2023-08-04 09:14:49 浏览: 45
好的,这是一个简单的酒店管理系统的C++代码,包括了客房信息的查看、预订和取消预订功能。
```cpp
#include<iostream>
#include<conio.h>
#include<fstream>
#include<string>
using namespace std;
class hotel
{
int room_no;
string name;
string address;
string phone;
string nationality;
public:
void menu();
void checkin();
void cancellation();
void room_status();
};
void hotel::menu()
{
cout<<"\n\n\t\t\t\t\t\t\tWELCOME TO HOTEL MANAGEMENT SYSTEM"<<endl;
cout<<"\t\t\t\t\t\t\t--------------------------------"<<endl;
cout<<"\n\n\t\t\t\t\t\t\t1. Check-in a room"<<endl;
cout<<"\t\t\t\t\t\t\t2. Cancellation of booking"<<endl;
cout<<"\t\t\t\t\t\t\t3. Room status"<<endl;
cout<<"\t\t\t\t\t\t\t4. Exit"<<endl;
}
void hotel::checkin()
{
ofstream fout;
fout.open("hotel.txt",ios::app);
cout<<"\n\n\t\t\t\t\t\t\tEnter the room number: ";
cin>>room_no;
cout<<"\n\n\t\t\t\t\t\t\tEnter the name of the customer: ";
cin>>name;
cout<<"\n\n\t\t\t\t\t\t\tEnter the address of the customer: ";
cin>>address;
cout<<"\n\n\t\t\t\t\t\t\tEnter the phone number of the customer: ";
cin>>phone;
cout<<"\n\n\t\t\t\t\t\t\tEnter the nationality of the customer: ";
cin>>nationality;
fout<<room_no<<"\t"<<name<<"\t"<<address<<"\t"<<phone<<"\t"<<nationality<<endl;
fout.close();
cout<<"\n\n\t\t\t\t\t\t\tRoom booking done successfully!"<<endl;
}
void hotel::cancellation()
{
int r_no;
string line;
ifstream fin;
ofstream fout;
fin.open("hotel.txt");
fout.open("temp.txt",ios::trunc);
cout<<"\n\n\t\t\t\t\t\t\tEnter the room number to be cancelled: ";
cin>>r_no;
while(getline(fin,line))
{
int room=stoi(line.substr(0,line.find("\t")));
if(room!=r_no)
{
fout<<line<<endl;
}
}
fin.close();
fout.close();
remove("hotel.txt");
rename("temp.txt","hotel.txt");
cout<<"\n\n\t\t\t\t\t\t\tRoom booking cancelled successfully!"<<endl;
}
void hotel::room_status()
{
string line;
ifstream fin;
fin.open("hotel.txt");
cout<<"\n\n\t\t\t\t\t\t\tRoom No."<<"\t"<<"Name"<<"\t\t"<<"Address"<<"\t\t"<<"Phone No."<<"\t"<<"Nationality"<<endl;
cout<<"\t\t\t\t\t\t\t-----------------------------------------------------------"<<endl;
while(getline(fin,line))
{
cout<<"\t\t\t\t\t\t\t"<<line<<endl;
}
fin.close();
}
int main()
{
hotel h;
int choice;
while(true)
{
h.menu();
cout<<"\n\n\t\t\t\t\t\t\tEnter your choice: ";
cin>>choice;
switch(choice)
{
case 1:
h.checkin();
break;
case 2:
h.cancellation();
break;
case 3:
h.room_status();
break;
case 4:
exit(0);
default:
cout<<"\n\n\t\t\t\t\t\t\tInvalid choice! Please enter a valid choice."<<endl;
}
getch();
system("cls");
}
return 0;
}
```
这个程序使用了文件I/O,将房间预订信息保存在hotel.txt文件中。你可以根据需要修改程序来满足你的需求。