*主要功能:1.打印已经被预定的房号,用于客户订房
2.客户退房,结算时间和消费
特点:使用person和client类,文件,引用,全局变量*
#include<iostream>
#include<string.h>
#include<iomanip>
#include<fstream>
using namespace std;
class person {
private:
int roomnum;
char *name;
char *begintime, *pretime;//开房时间,预计开房时间
public:
void getnroomnum(int num)
{
roomnum = num;
}
void getname(char* p)
{
name = new char[100];
strcpy_s(name, strlen(p) +1, p);
}
void getbegintime(char* p)
{
begintime = new char[100];
strcpy_s(begintime, strlen(p) +1, p);
}
void getpretime(char* p)
{
pretime = new char[100];
strcpy_s(pretime, strlen(p) + 1, p);
}
int cnum()
{
return roomnum;
}
char* cname()
{
return name;
}
char* ctime1()
{
return begintime;
}
char* ctime2()
{
return pretime;
}
int rmonth();
int rday();
int year();
};int person:: rmonth()//返回入住月日时整数
{
int k, j=0;
for (k = 0; k < strlen(begintime); k++)
if (begintime[k] == 'M')
{
j= (begintime[k - 2] - '0') * 10 + begintime[k - 1] - '0';
break;
}
return j;
}
int person::rday()
{
int k, j=0;
for (k = 0; k < strlen(begintime); k++)
if (begintime[k] == 'D')
{
j= (begintime[k - 2] - '0') * 10 + begintime[k - 1] - '0';
break;
}
return j;
}
int person::year()
{
int k, j=0;
for (k = 0; k < strlen(begintime); k++)
if (begintime[k] == 'Y')
{
j = (begintime[k - 4] - '0') * 1000+ (begintime[k- 3] - '0') * 100 + (begintime[k - 2] - '0') * 10 + begintime[k - 1] - '0';
break;
}
return j;
}
class client {
private:
int other;
char *canceltime;
public:
void inother(int x) { other = x; }
void incanceltime(char* p)
{
canceltime= new char[100];
strcpy_s(canceltime, strlen(p) + 1, p);
}
int outother()
{
return other;
}
char* outcanceltime()
{
return canceltime;
}
int remonth();
int reday();
};int client:: remonth()//返回退房月日时
{
int k, j=0;
for (k = 0; k < strlen(canceltime); k++)
if (canceltime[k] == 'M')
{
j = (canceltime[k - 2] - '0') * 10 + canceltime[k - 1] - '0';
break;
}
return j;
}
int client::reday()
{
int k, j=0;
for (k = 0; k < strlen(canceltime); k++)
if (canceltime[k] == 'D')
{
j = (canceltime[k - 2] - '0') * 10 + canceltime[k - 1] - '0';
break;
}
return j;
}
person room[100];
client roomm[100];
int i,n;
char ti[100], begint[100];
void menu() {
cout << setw(60)<<"a.订房" << endl;
cout <<setw(60)<< "b.退房" << endl;
cout << setw(60)<<"c.退出" << endl;
}
int regis() {
int num;
char ch,ch2[4];
cout << "请选择房号(1-100 格式:xxx):";
cin.getline(ch2,4);//以字符形式输入房号,以便存入文件,
fstream housefile,file;
housefile.open("存房号.txt", ios::app);//存入登记的房号
file.open("酒店入住人员信息.txt", ios::app);
if (!file)
{
cout << "打开文件失败!";
exit(0);
}
if (!housefile)
{
cout << "打开文件失败!";
exit(0);
}
file << "房号:"<<ch2;
housefile << ch2<<"\n";//多存入一个换行符,格式整齐
housefile.close();
num=(ch2[0]-48)*100+(ch2[1]-48)*10+(ch2[2]-48);
return num;//返回房号的整数,用于做数组下标
}
void order(person& p)
{
char nam[20],ch[4];
char pret[100];
fstream file;
int num;
file.open("酒店入住人员信息.txt", ios::app);
if (!file)
{
cout << "打开文件失败!";
exit(0);
}
p.getnroomnum(i);
cout << "请输入姓名:";
cin >> nam;
file <<"姓名:" <<nam<<" ";
cin.ignore();
p.getname(nam);
cout << "请输入开房时间(格式:xxY(年) xxM(月) xxD(日);";
cin.getline(begint,100);
file <<"开房时间:"<< begint<<" ";
p.getbegintime(begint);
cout << "请输入预计订房时间(格式:xxY(年) xxM(月) xxD(日):";
cin.getline(pret, 100);
file <<"预计订房时间:"<< pret<<" ";
file << '\n';
file.close();
p.getpretime(pret);
cout << setw(60) << "你的信息为:" << endl;
cout << setw(60) <<"房号:" << p.cnum() << endl;
cout << setw(60) << "姓名:" << p.cname() << endl;
cout << setw(60) << "开房时间:" << p.ctime1() << endl;
cout << setw(60) << "预计订房时间:" << p.ctime2() << endl;
}
int irregis() {
fstream delfile;
int num;
char information[100][100],ch2[4];
cout << "请输入您的房号(1-100):";
cin.getline(ch2,4);
num = (ch2[0] - 48) * 100 + (ch2[1] - 48) * 10 + (ch2[2] - 48);
int a = 0,c=0;
while (c != 1)
{
delfile.open("酒店入住人员信息.txt", ios::in);//判断输入的房号是否有效或是否有该客户信息,若输入错误,持续弹出输出界面。提供再次输入的机会
if (!delfile)
{
cout << "打开文件失败!";
exit(0);
}
while (!delfile.eof())
{
delfile.getline(information[a], 100);
if ((information[a][6]-'0') * 100 + (information[a][7]-'0') * 10 + (information[a][8]-'0') == num)
{
c = 1;
break;
}
a++;
}
if (c == 0)
{
delfile.close();
cout << "请输入正确的房号:";
a = 0;
cin >> num;
}
}
delfile.close();
return num;
}
void withdraw(client&q,person&p)
{
fstream delfile,del2;
char information[100][100] = { "0" }, information2[100][100] = { "\0" };
delfile.open("酒店入住人员信息.txt", ios::in);
if(!delfile)
{
cout << "打开文件失败!";
exit(0);
}
del2.open("酒店副本.txt", ios::out);//利用多文件,删除信息(删除的信息不复制)
if (!del2)
{
cout << "打开文件失败!";
exit(0);
}int a = 0;
while (!delfile.eof())
{
delfile.getline(information[a], 100);
if ((information[a][6] - '0') * 100 + (information[a][7] - '0') * 10 + (information[a][8] - '0') == n)
{
p.getbegintime(information[a]);//把需要删除的信息存到类变量,起到了一一对应的作用,否则begintime无值
a++;
continue;
}
else
del2 << information[a]<<"\n";
a++;
}
int caltime(person&,client&);//计算消费时间
int money;//额外消费
cout << "请输入其他消费金额总额:";
cin>>money;
cin.ignore();
q.inother(money);
cout << "请输入退房时间(格式:xxY(年) xxM(月) xxD(日)):";
cin.getline(ti, 100);
q.incanceltime(ti);
cout << setw(60) << "您的信息为:" << endl;
cout<< setw(60) << "额外消费:"<<q.outother() <<"元"<< endl;
cout<< setw(60) << "退房时间为:"<<q.outcanceltime() << endl;
cout << setw(60) << "您的总消费金额:" << caltime(room[i], roomm[n]) + q.outother() << "元" << endl;;
delfile.close();
del2.close();
delfile.open("酒店入住人员信息.txt", ios::out);
char ch[4]="\0";
if (!delfile)
{
cout << "打开文件失败!";
exit(0);
}
del2.open("酒店副本.txt", ios::in);
if (!del2)
{
cout << "打开文件失败!";
exit(0);
}a = 0;
while (!del2.eof())
{
del2.getline(information2[a], 100);
delfile << information2[a]<<"\n";
a++;
}
del2.close();
delfile.close();
fstream f1,f2;//更新文件中的房号
f1.open("存房号.txt", ios::in);
f2.open("存房号副本.txt", ios::out);
if (!f1)
{
cout << "打开文件失败!" ;
exit(0);
}
if (!f2)
{
cout << "打开文件失败!" ;
exit(0);
}
while (!f1.eof())
{
f1.getline(ch, 4);
if (((ch[0] - '0') * 100 + (ch[1] - '0') * 10 + ch[2] - '0')==n)
continue;
else
f2 << ch << "\n";
}
f1.close();
f2.close();
f1.open("存房号.txt", ios::out);
f2.open("存房号副本.txt", ios::in);
if (!f1)
{
cout << "打开文件失败!";
exit(0);
}
if (!f2)
{
cout << "打开文件失败!";
exit(0);
}
while (!f2.eof())
{
f2.getline(ch, 4);
f1<< ch << "\n";
}
f1.close();
f2.close();
}
int caltime(person&p,client&c)
{
int mon(int mo,int Feb);//计算每个月的天数
int month, sum = 0, b = 28, d = 0, sumday=0;
if (p.year() % 4 == 0 && p.year() % 100 != 0)//判断闰年平年
b = 29;
else
if (p.year() % 400 == 0)
b = 29;
month = p.rmonth();
d=mon(month, b);
for (month=month+1; month < c.remonth(); month++)
{
sum=sum+mon(month, b);
}
sumday = d - p.rday() + 1 + sum + c.reday();
cout << setw(60) << "您的消费时间为:" << sumday<< "天"<<endl;
return sumday * 158;
}
int mon(int mo,int Feb)
{
int sum = 0;
switch (mo) {
case 1:sum = 31; break;
case 2:sum = Feb; break;
case 3:sum = 31; break;
case 4:sum = 30; break;
case 5:sum = 31; break;
case 6:sum = 30; break;
case 7:sum = 31; break;
case 8:sum = 31; break;
case 9:sum = 30; break;
case 10:sum = 31; break;
case 11:sum = 30; break;
case 12:sum = 31; break;
}
return sum;
}
void pri_house()
{
char a[4];
cout << "已经被选的房号:" << endl;
fstream house;//打印出已经被选的房号名单
house.open("存房号.txt", ios::in);
if (!house)
{
cout << "打开文件失败!";
exit(0);
}
while (!house.eof())
{
house.getline(a, 4);
cout<<a<<endl ;
}
house.close();
}
int main()
{
cout << setw(79)<<"*****************欢迎光临******************"<<endl;
cout<< setw(82) << "*****************请选择服务类型******************" << endl;
void pri_house();
void corresponding(person&);
void menu();
void order(person&);
void withdraw(client&,person&);
int irregis();//同下
int regis();//输入房号作为数组名
char choice;
int j = 0;
do {
menu();
cin.get(choice);
cin.ignore();
switch (choice)
{
case 'a':pri_house(); i = regis(), order(room[i]); break;
case 'b':n=irregis(),i=n,withdraw(roomm[n],room[i]),cout << "退房成功!欢迎下次光临!"<<endl; break;
}
} while (choice != 'C' && choice != 'c');
}
简易酒店管理系统(c++)
最新推荐文章于 2024-07-19 00:05:14 发布