📄 hotelbooking.cpp
字号:
{ cout << "\n\n\tCould not open Booking.dat file.";
return;
}
// Check whether the booking is for a hotel or a resort.
check='n';
while(check=='n')
{
cout << "\nBooking for Hotel or Resort? (H/R): ";
cin >> book_type;
if(book_type=='h')
book_type='H';
else if(book_type=='r')
book_type='R';
if(book_type != 'H' && book_type != 'R')
cout << "\n\n\tPLEASE ENTER 'H' FOR HOTEL OR 'R' FOR RESORT BOOKING";
else
check='y';
}
if(book_type == 'H') // If the booking is for a hotel...
{
// Declare an object array of the hotel class.
hotel hotelarr[100];
// Open Hotel.dat file for reading.
finout1.open("Hotel.dat", ios::in);
if(!finout1)
{ cout << "\n\n\tCould not open Hotel.dat file.";
return;
}
// Read the hotel records into the object array.
for(i=0;(finout1.read((char *)&hotelarr[i], sizeof(hotelarr[i])));i++) {}
// Accept a valid hotel code.
check='n';
while(check=='n')
{
cout << "\nEnter Hotel code: ";
cin >> hcd1;
for(j=0;j<i;j++)
{
// Obtain the hotel code of each object.
hcd2=hotelarr[j].getcode();
// Check whether it matches the input hotel code.
if(hcd1==hcd2)
{
check='y';
break;
}
}
if(check=='n')
cout << "\n\n\tINVALID HOTEL CODE! Please re-enter.";
}
// Accept a valid room type.
check='n';
while(check=='n')
{
cout << "\nEnter required room type('R' for regular, 'D' for Deluxe): ";
cin >> rtype;
cin.ignore();
if(rtype=='r')
rtype='R';
else if(rtype=='d')
rtype='D';
if(rtype != 'R' && rtype != 'D')
cout << "\n\n\tPLEASE ENTER 'R' FOR REGULAR OR 'D' FOR DELUXE";
else
check='y';
}
// Obtain the number of rooms available for the specified room type.
if(rtype=='R')
vavail=hotelarr[j].getregno();
else
vavail=hotelarr[j].getdlxno();
if(vavail==0) // If rooms are not available, return to menu.
{
cout << "\n\n\tNo rooms available. Press enter to return to menu.";
cin >> check;
return;
}
/* If rooms are available, accept the booking and invoke functions
to reduce the available number. */
else
{
if(rtype=='R')
hotelarr[j].reducereg();
else
hotelarr[j].reducedlx();
}
// Close the Hotel.dat file.
finout1.close();
/* Reopen Hotel.dat in write mode to write the updated
object array back to the file. */
finout1.open("Hotel.dat", ios::out);
if(!finout1)
{ cout << "Could not open Hotel.dat file for updating.";
return;
}
// Write the object array back to the file.
for(j=0;j<i;j++)
finout1.write((char *)&hotelarr[j], sizeof(hotelarr[j]));
// Close the Hotel.dat file.
finout1.close();
}
else // If the booking is for a resort...
{
// Declare an object array of the resort class.
resort resortarr[100];
// Open Resort.dat file for reading.
finout1.open("Resort.dat", ios::in);
if(!finout1)
{ cout << "\n\n\tCould not open Resort.dat file.";
return;
}
// Read the resort records into the object array.
for(i=0;(finout1.read((char *)&resortarr[i], sizeof(resortarr[i])));i++) {}
// Accept a valid resort code.
check='n';
while(check=='n')
{
cout << "\nEnter Resort code: ";
cin >> hcd1;
cin.ignore();
for(j=0;j<i;j++)
{
// Obtain the resort code of each object.
hcd2=resortarr[j].getcode();
// Check whether it matches the input resort code.
if(hcd1==hcd2)
{
check='y';
break;
}
}
if(check=='n')
cout << "\n\n\tINVALID RESORT CODE! Please re-enter.";
}
// Set the room type to 'C' for cottage.
rtype='C';
// Obtain the number of cottages available.
vavail=resortarr[j].getcottno();
if(vavail==0) // If cottages are not available, return to menu.
{
cout << "\n\n\tNo cottages available. Press M and Enter keys to return to menu.";
cin >> check;
return;
}
/* If cottages are available, accept the booking and invoke
function to reduce the available number. */
else
resortarr[j].reducecott();
// Close the Resort.dat file.
finout1.close();
/* Reopen Resort.dat in write mode to write the updated
object array back to the file. */
finout1.open("Resort.dat", ios::out);
if(!finout1)
{ cout << "Could not open Resort.dat file for updating.";
return;
}
// Write the object array back to the file.
for(j=0;j<i;j++)
finout1.write((char *)&resortarr[j], sizeof(resortarr[j]));
// Close the Resort.dat file.
finout1.close();
}
// Obtain the booking number of the last booking in the file.
finout.seekg(0);
while(finout.read((char *)&booking1, sizeof(booking1))){}
lastnum=booking1.getnum();
/* If the details of the first booking are being entered,
set last booking number to 0.*/
if(lastnum<0)
lastnum=0;
// Close the Booking.dat file.
finout.close();
/* Invoke the function to accept booking details and pass the last
booking number, hotel type, hotel code and room type as
parameters to it. */
booking2.getdata(lastnum, book_type, hcd1, rtype);
// Reopen Booking.dat file for adding the new record.
finout.open("Booking.dat", ios::app);
if(!finout)
{ cout << "Could not open Booking.dat file.";
return;
}
// Write the new record to the Booking.dat file.
finout.write((char *)&booking2, sizeof(booking2));
// Close the Booking.dat file.
finout.close();
return;
}
/* Function to record cancellations. */
void rec_cancel(void)
{
// Declare an object array of the booking class.
booking bookingarr[100];
int i, j, bno1, bno2, hcd;
char check, htype, rtype;
// Open Booking.dat for reading.
fstream finout("Booking.dat", ios::in);
if(!finout)
{ cout << "Could not open Booking.dat file for reading.";
return;
}
// Read the file contents into the object array.
for(i=0;(finout.read((char *)&bookingarr[i], sizeof(bookingarr[i])));i++) {}
system("cls");
cout << "\nRECORD CANCELLATION DETAILS\n";
// Accept a valid booking number.
check='n';
while(check=='n')
{
cout << "\nEnter Booking number: ";
cin >> bno1;
for(j=0;j<i;j++)
{
// Obtain the booking number of each object.
bno2=bookingarr[j].getnum();
// Check whether it matches the input booking number.
if(bno1==bno2)
{
// If found, obtain the hotel type, code and room type.
htype=bookingarr[j].gethoteltype();
hcd=bookingarr[j].gethotelcd();
rtype=bookingarr[j].getroomtype();
check='y';
break;
}
}
if(check=='n')
cout << "\n\n\tINVALID BOOKING NUMBER! Please re-enter.";
}
// Close the Booking.dat file.
finout.close();
// Open Booking.dat file for writing.
finout.open("Booking.dat", ios::out);
if(!finout)
{ cout << "Could not open Booking.dat file for writing.";
return;
}
/* Write the contents of the object array into the file,
excluding the record for the booking to be cancelled. */
for(j=0;j<i;j++)
{
bno2=bookingarr[j].getnum();
if(bno1!=bno2)
finout.write((char *)&bookingarr[j], sizeof(bookingarr[j]));
}
// Close the Booking.dat file.
finout.close();
if(htype=='H') // If the cancelled booking was for a hotel...
{
// Declare an object array of the hotel class.
hotel hotelarr[100];
int hotelcd;
// Open Hotel.dat file for reading.
fstream finout1("Hotel.dat", ios::in);
if(!finout1)
{ cout << "\n\n\tCould not open Hotel.dat file.";
return;
}
// Read the hotel records into the object array.
for(i=0;(finout1.read((char *)&hotelarr[i], sizeof(hotelarr[i])));i++)
{
hotelcd=hotelarr[i].getcode();
/* If the object's hotel code matches the cancelled
booking hotel code, invoke function to increase
the room availability. */
if(hotelcd==hcd)
{
if(rtype=='R')
hotelarr[i].increasereg();
else
hotelarr[i].increasedlx();
}
}
// Close the Hotel.dat file.
finout1.close();
// Open the Hotel.dat file for writing.
finout1.open("Hotel.dat", ios::out);
if(!finout1)
{ cout << "Could not open Hotel.dat file for updating.";
return;
}
// Write the object array back to the file.
for(j=0;j<i;j++)
finout1.write((char *)&hotelarr[j], sizeof(hotelarr[j]));
// Close the Hotel.dat file.
finout1.close();
}
else // If the cancelled booking was for a resort...
{
// Declare an object array of the resort class.
resort resortarr[100];
int resortcd;
// Open Resort.dat file for reading.
fstream finout1("Resort.dat", ios::in);
if(!finout1)
{ cout << "\n\n\tCould not open Resort.dat file.";
return;
}
// Read the resort records into the object array.
for(i=0;(finout1.read((char *)&resortarr[i], sizeof(resortarr[i])));i++)
{
/* If the object's resort code matches the cancelled
booking resort code, invoke function to increase
the cottage availability. */
resortcd=resortarr[i].getcode();
if(resortcd==hcd)
resortarr[i].increasecott();
}
// Close the Resort.dat file.
finout1.close();
// Open the Resort.dat file for writing.
finout1.open("Resort.dat", ios::out);
if(!finout1)
{ cout << "Could not open Resort.dat file for updating.";
return;
}
// Write the object array back to the file.
for(j=0;j<i;j++)
finout1.write((char *)&resortarr[j], sizeof(resortarr[j]));
// Close the Resort.dat file.
finout1.close();
}
cout << "\n\n\tBooking has been cancelled. Press M and Enter keys to return to menu.";
cin >> check;
}
/* Function to display the hotels and resorts in a specified city. */
void viewhotel(void)
{
// Declare objects for the hotel and resort class.
hotel vhotel;
resort vresort;
fstream finout1, finout2;
int foundctr, i;
char vcitynm[30], *pcitynm, check;
// Open the Hotel.dat file for reading.
finout1.open("Hotel.dat", ios::in);
if(!finout1)
{ cout << "Could not open Hotel.dat file for reading.";
return;
}
// Open the Resort.dat file for reading.
finout2.open("Resort.dat", ios::in);
if(!finout2)
{ cout << "Could not open Resort.dat file for reading.";
return;
}
system("cls");
cout << "\nVIEW HOTELS AND RESORTS IN A CITY";
// Accept the city name.
cout << "\n\nEnter the city name: ";
cin.getline(vcitynm, 30);
cout << "\n\nLIST OF HOTELS\n";
foundctr=0;
// Loop to display the hotels in the specified city.
for(i=0;(finout1.read((char *)&vhotel, sizeof(vhotel)));i++)
{
pcitynm=vhotel.getcitynm();
if((strcmp(vcitynm, pcitynm))==0)
{ vhotel.dispdata();
foundctr=1;
}
}
if(foundctr!=1)
cout << "\n\tThere are no hotels in the specified city.";
cout << "\n\n\nLIST OF RESORTS\n";
foundctr=0;
// Loop to display the resorts in the specified city.
for(i=0;(finout2.read((char *)&vresort, sizeof(vresort)));i++)
{
pcitynm=vresort.getcitynm();
if((strcmp(vcitynm, pcitynm))==0)
{ vresort.dispdata();
foundctr=1;
}
}
if(foundctr!=1)
cout << "\n\tThere are no resorts in the specified city.";
// Close the files
finout1.close();
finout2.close();
cout << "\n\n\tPress M and Enter keys to return to menu.";
cin >> check;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -