这是我自己用c++写的比较简易的一个酒店管理系统问题的答案。
它能实现的功能如下:
主要基于c++面向对象的特点,通过对文件的输入输出,实现信息的及时更新。同时我加入了c++对各种异常的处理,并通过函数将控制台字体和背景颜色做了调整。
代码如下:
#include<iostream>
#include<iomanip>
#include<string>
#include<fstream>
#include<sstream>
#include<windows.h>
#include<stdexcept>
#include<conio.h>
using namespace std;
class room
{
private:
int roomnumber = 0;
int price = 0;
int start_date = 0;
int end_date = 0;
bool order = 0;
string name;
string ID;
string phone;
public:
void getnumber(int _number)
{
roomnumber = _number;
}
int returnnumber() const
{
return roomnumber;
}
void getprice(int _price)
{
price = _price;
}
int returnprice() const
{
return price;
}
void getdate(int s, int e)
{
if (s < 1 || s>31 || e < 1 || e>31 || s >= e) throw runtime_error("Invalid date!");
start_date = s;
end_date = e;
}
int returnstartdate() const
{
return start_date;
}
int returnenddate() const
{
return end_date;
}
void getorder(bool _order)
{
if (!(_order == 0 || _order == 1)) throw out_of_range("Room's state is error!");
order = _order;
}
bool returnorder() const
{
return order;
}
void getname(string _name)
{
name = _name;
}
string returnname() const
{
return name;
}
void getID(string id)
{
if (id.size() < 18 || id.size() > 19) throw runtime_error("Your ID is error! Please input correct ID!");
ID = id;
}
string returnID() const
{
return ID;
}
void getphone(string ph)
{
if (ph.size()!=11) throw runtime_error("Your mobilephone's number is error! Please input correct number!");
phone = ph;
}
string returnphone() const
{
return phone;
}
int sumprice()
{
return price * (end_date - start_date);
}
friend ostream& operator<<(ostream& os, const room* u)//输出流重载
{
os << u->returnnumber() << '\n';
os << u->returnprice() << '\n';
os << u->returnstartdate() << '\n';
os << u->returnenddate() << '\n';
os << u->returnorder() << '\n';
os << u->returnname() << '\n';
os << u->returnID() << '\n';
os << u->returnphone() << '\n';
return os;
}
};
class standard :public room
{
};
class suite :public room
{
};
class kingsize :public room
{
};
void nomorememory()
{
cerr << "unable to satisfy request for memory\n";
abort();
}
//new分配异常
int check(int a[], int size,int suspicion)
{
int judge=0;
for (int i = 0; i < size; i++)
{
if (suspicion == a[i])
judge = 1;
}
if (judge == 0)
throw suspicion;
return 0;
}
class file_exception {
string filename;
public:
file_exception(const string &filename):filename(filename){}
~file_exception(){}
const string& get_filename()const { return filename; }
};
void update(room* p[], const string& filename)//覆盖/更新原有文件内容
{
ofstream os(filename, ios_base::binary);
if (os)
{
for (int i = 0; i < 6; i++)
{
os << p[i];
}
}
else
throw file_exception(filename);
os.close();
}
void addRecord(const string& filename, room* current)//以追加模式打开文件,用于记录
{
ofstream file(filename, ios_base::app);
if (file)
{
file << current;
file.close();
}
else
throw file_exception(filename);
file.close();
}
void password(string rightpassword)
{
part4:
string password;
cout << "please input password: " << endl;
int i = 0;
char ch;
while ((ch = _getch()) != 13)
{
password += ch;//string对象重载了+=
cout << "*";
}
if (password!= rightpassword)
{
cout << "Error password! please input again!" << endl;
goto part4;
}
cout << "correct!" << endl;
}
void checkin(room* p[], int size, const string& filename1, const string& filename2)
{
HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);//句柄
cout.width(120);
SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_BLUE);
cout << "Welcome to this hotel!\nWe have standard room, suite room and kingsize room. \nThe reliable rooms are: " << endl;
int vacant_room[6];
int j = 0;
int temp, temp1;
string str;
for (int i = 0; i < 6; i++)
{
if (p[i]->returnorder() == 0)
{
temp1 = p[i]->returnnumber();
cout << temp1 << "、";
vacant_room[j] = temp1;
j++;
}
}
cout << endl << "room 101-102 are standard room. Those price is 100 dollar per night" << endl;
cout << "room 103-104 are suite room. Those price is 200 dollar per night" << endl;
cout << "room 105-106 are kingsize room. Those price is 300 dollar per night" << endl;
part1:
try
{
cout << '\n' << setiosflags(ios_base::left) << " Please input the room number you expected." << endl;
cout << "There are " << j << " vancant rooms altogether." << endl;
cin >> temp;
check(vacant_room, j, temp);
}
catch (int e)
{
cout << "The room" << e << " can't been chosen.Please choose again." << endl;
goto part1;
}
room* current = NULL;
for (int i = 0; i < 6; i++)
{
if (p[i]->returnnumber() == temp)
{
current = p[i];
break;
}
}
part2:
try
{
cout << "please input the date you cheak in" << endl;
cin >> temp;
cout << "please input the date you leave" << endl;
cin >> temp1;
current->getdate(temp, temp1);
cout << "please input your name" << endl;
cin >> str;
current->getname(str);
cout << "please input your ID" << endl;
cin >> str;
current->getID(str);
cout << "please input your mobile phone" << endl;
cin >> str;
current->getphone(str);
current->getorder(static_cast<bool>(1));
cout << "The price is ";
temp = current->sumprice();
cout << temp << endl;
}
catch (runtime_error& e)
{
cout << e.what() << endl;
goto part2;
}
catch (out_of_range& e)
{
cout << e.what() << endl;
goto part2;
}
cout << resetiosflags(ios_base::left);
try
{
update(p, filename1);
addRecord(filename2, current);
}
catch (file_exception& e)
{
cout << "Fail to open " << e.get_filename() << endl;
}
}
void checkout(room* p[], int size, const string& filename)
{
int temp;
HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);//句柄
cout.width(110);
SetConsoleTextAttribute(hout, 176 + 1);
cout << "Thanks for coming and hope to see you again!" << endl;
part3:
cout << " please input the room number you expected to quit." << endl;
cin >> temp;
room* current = NULL;
cout << setiosflags(ios_base::left);
int orderroom[6];
int j = 0;
try
{
for (int i = 0; i < 6; i++)
{
if (p[i]->returnnumber() == temp)
{
current = p[i];
}
if (p[i]->returnorder() == 1)
{
orderroom[j] = p[i]->returnnumber();
j++;
}
}
check(orderroom, j, temp);
}
catch (int e)
{
cout << "The room number" << e << " is error.Please input again!" << endl;
goto part3;
}
cout << resetiosflags(ios_base::left);
current->getorder(static_cast<bool>(0));
try
{
update(p, filename);
}
catch (file_exception& e)
{
cout << "Fail to open " << e.get_filename() << endl;
}
}
void searchfor(room* p[], int size, string &rightpassword)
{
int temp1;
password(rightpassword);
cout << "If you want to change password ,please input 1,or input 2" << endl;
cin >> temp1;
if (temp1 == 1)
{
password(rightpassword);
cout << "please input new password. " << endl;
rightpassword = ' ';
cin >> rightpassword;
}
HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);//句柄
SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_BLUE);
cout << "please input the room's number you want to search for." << endl;
cin >> temp1;
for (int i = 0; i < 6; i++)
{
if (p[i]->returnnumber() == temp1)
{
cout << p[i] << endl;
break;
}
}
}
int main()
{
room* p[10];
set_new_handler(nomorememory);//处理new分配异常
for (int i = 0; i < 2; i++)
{
p[i] = new standard();
}
for (int i = 2; i < 4; i++)
{
p[i] = new suite();
}
for (int i = 4; i < 6; i++)
{
p[i] = new kingsize();
}
const string filename1 = "D:\\大一下学期\\计导实验\\实验十四\\in.txt";
const string filename2 = "D:\\大一下学期\\计导实验\\实验十四\\record.txt";
try
{
ifstream ifs;//构建输入流对象,以二进制形式打开,得到文件内容
ifs.open(filename1, ios_base::binary);
if (ifs)
{
for (int i = 0; i < 6; i++)
{
int roomnumber,price,start_date,end_date;
bool order;
string name;
string ID;
string phone;
ifs >> roomnumber >> price >> start_date >> end_date >> order >> name >> ID >> phone;
p[i]->getnumber(roomnumber);
p[i]->getprice(price);
p[i]->getdate(start_date, end_date);
p[i]->getorder(order);
p[i]->getname(name);
p[i]->getID(ID);
p[i]->getphone(phone);
}
}
else
throw file_exception(filename1);
ifs.close();
}
catch (file_exception &e)
{
cout << "Fail to open " << e.get_filename() << endl;
}
catch (runtime_error& e)
{
cout << e.what()<< endl;
}
catch (out_of_range& e)
{
cout << e.what() << endl;
}
int temp;
HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);//句柄
SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_BLUE);
cout << "Hi~ This is hotel management system integration service!\nIf you are passengers,please input 1.If you are managers,please input 2." << endl;
cin >> temp;
if (temp == 1)
{
while (1)
{
SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_BLUE);//设置背景和字体颜色
cout << "Welcome to this hotel!.\n ";
SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN);
cout << "if you want to quit safely ,please input 0. \n ";
SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY | FOREGROUND_BLUE);
cout << "if you want to check in, please input 1.\n ";
SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE);
cout << "if you want to depart, please input 2.\n";
int temp2;
string str;
cin >> temp2;
system("cls");
if (temp2 == 0)
break;
if (temp2 == 1)
{
checkin(p, 6, filename1, filename2);
}
if (temp2 == 2)
{
checkout(p, 6, filename1);
}
cout << "succeed!" << endl;
system("pause");
system("cls");
}
}
if (temp == 2)
{
while (1)
{
SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_BLUE);//设置背景和字体颜色
cout << "Welcome to this hotel!.\n ";
SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN);
cout << "if you want to quit safely ,please input 0. \n ";
SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY | FOREGROUND_BLUE);
cout << "if you want to check in, please input 1.\n ";
SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE);
cout << "if you want to depart, please input 2.\n";
SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
cout << "if you want to search for the information about any room,please input 3.\n";
int temp2;
string str;
cin >> temp2;
system("cls");
if (temp2 == 0)
break;
if (temp2 == 1)
{
checkin(p, static_cast<int>(6), filename1, filename2);
}
if (temp2 == 2)
{
checkout(p, static_cast<int>(6), filename1);
}
if (temp2 == 3)
{
string rightpassword= "123456";
searchfor(p, 6, rightpassword);
}
system("pause");
system("cls");
}
}
return 0;
}
biubiubiu~