⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hotelbooking.cpp

📁 本人在学习C++阶段
💻 CPP
📖 第 1 页 / 共 2 页
字号:
#include <iostream.h>
#include <fstream.h>
#include <string.h>
#include <stdlib.h>

// The base class for hotels and resorts.

class hotel_cl
{
protected:
	int hotel_cd;
	char hotel_nm[30];
	char city_nm[30];

public:
	
	/* Function to accept the details common to both hotels and resorts. 
		It takes the last hotel code as a parameter. */

	void getdata(int hcd)
	{
		char check;

		hotel_cd=hcd+1;
		cout << "\n\nHotel/Resort code: " << hotel_cd;
		
		// Verify that the hotel name is not left blank.

		check='n';
		while(check=='n')
		{
			cout << endl << "\nEnter Hotel/Resort name: ";
			cin.getline(hotel_nm, 30);

			if((strlen(hotel_nm))==0)
				cout << "\n\n\tHotel/Resort name must be entered.";
			else
				check='y';
		}
		
		// Verify that the city name is not left blank.

		check='n';
		while(check=='n')
		{
			cout << "\nEnter City name: ";
			cin.getline(city_nm, 30);

			if((strlen(city_nm))==0)
				cout << "\n\n\tCity name must be entered.";
			else
				check='y';
		}
	}

	// Function to display data.

	void dispdata(void)
	{
		cout << "\nHotel code: " << hotel_cd;
		cout << "\tName: " << hotel_nm;
	}

	// Function that returns the hotel code.

	int getcode(void)
	{	
		return hotel_cd;
	}

	// Function that returns the city name.

	char *getcitynm(void)
	{	
		return city_nm;
	}
};


// The derived class for hotels.

class hotel:public hotel_cl
{
protected:
	int no_reg;
	float reg_tariff;
	int no_dlx;
	float dlx_tariff;
	int reg_avail;
	int dlx_avail;

public:
	
	/* Function to accept the hotel specific details. It takes the last hotel 
		code as a parameter. */

	void getdata(int hcd)
	{
		// Invoke the function in the base class.

		hotel_cl::getdata(hcd);

		// Accept data for the derived class.

		cout << "\nEnter the number of Regular rooms: ";
		cin >> no_reg;
	
		cout << "\nEnter the tariff for Regular rooms: ";
		cin >> reg_tariff;
		
		cout << "\nEnter the number of Deluxe rooms: ";
		cin >> no_dlx;
	
		cout << "\nEnter the tariff for Deluxe rooms: ";
		cin >> dlx_tariff;

		reg_avail=no_reg;
		dlx_avail=no_dlx;
	}

	// Function to display data.

	void dispdata(void)
	{
		// Invoke the function in the base class.

		hotel_cl::dispdata();

		// Display data for the derived class.

		cout << "\nRegular rooms: " << no_reg;
		cout << "\tTariff: " << reg_tariff;
		cout << "\tAvailable: " << reg_avail;
		cout << "\nDeluxe rooms: " << no_dlx;
		cout << "\tTariff: " << dlx_tariff;
		cout << "\tAvailable: " << dlx_avail;
	}

	// Function to return the number of regular rooms available.

	int getregno(void)
	{	
		return reg_avail;
	}

	// Function to return the number of deluxe rooms available.

	int getdlxno(void)
	{	
		return dlx_avail;
	}

	// Function to reduce the number of regular rooms available.

	void reducereg(void)
	{
		reg_avail=reg_avail-1;
	}

	// Function to reduce the number of deluxe rooms available.

	void reducedlx(void)
	{
		dlx_avail=dlx_avail-1;
	}

	// Function to increase the number of regular rooms available.

	void increasereg(void)
	{
		reg_avail=reg_avail+1;
	}

	// Function to increase the number of deluxe rooms available.

	void increasedlx(void)
	{
		dlx_avail=dlx_avail+1;
	}
};


// The derived class for resorts.

class resort:public hotel_cl
{
protected:
	int no_cott;
	float cott_tariff;
	int cott_avail;

public:
	
	/* Function to accept the resort specific details. It takes the last resort 
		code as a parameter. */

	void getdata(int rcd)
	{
		// Invoke the function in the base class.

		hotel_cl::getdata(rcd);

		cout << "\nEnter the number of Cottages: ";
		cin >> no_cott;
	
		cout << "\nEnter the tariff for Cottages: ";
		cin >> cott_tariff;

		cott_avail=no_cott;
	}
	
	// Function to display data.

	void dispdata(void)
	{
		// Invoke the function in the base class.

		hotel_cl::dispdata();

		cout << "\nTotal cottages: " << no_cott;
		cout << "\tTariff: " << cott_tariff;
		cout << "\tAvailable: " << cott_avail;
	}

	// Function to return the number of cottages available.

	int getcottno(void)
	{	
		return cott_avail;
	}

	// Function to reduce the number of cottages available.

	void reducecott(void)
	{
		cott_avail=cott_avail-1;
	}

	// Function to increase the number of cottages available.

	void increasecott(void)
	{
		cott_avail=cott_avail+1;
	}
};


// The class to store the booking details.

class booking
{
protected:

	int booking_no;
	char cust_nm[30];
	long int contact_no;
	char email_id[30];
	char hotel_type;
	int hotel_cd;
	char room_type;

public:

	/* Function to accept the booking details. It takes the last booking 
		number, hotel type, hotel code and room type as parameters. */

	void getdata(int bno, char htype, int hcd, char rtype)
	{
		char check;

		// Increment the last booking number to obtain the new number.

		booking_no=bno+1;

		cout << "\nBooking number: " << booking_no;
		
		// Verify that the customer name is not left blank.

		check='n';
		while(check=='n')
		{
			cout << "\n\nEnter name: ";
			cin.getline(cust_nm, 30);

			if((strlen(cust_nm))==0)
				cout << "\n\n\tCustomer name must be entered.";
			else
				check='y';
		}

		
		// Verify that the customer contact number is greater than 0.

		check='n';
		while(check=='n')
		{
			cout << "\nEnter contact number: ";
			cin >> contact_no;
			cin.ignore();

			if(contact_no<=0)
				cout << "\n\n\tContact number cannot be less than or equal to 0.";
			else
				check='y';
		}
		
		cout << "\nEnter Email ID: ";
		cin.getline(email_id, 30);

		hotel_type=htype;
		hotel_cd=hcd;
		room_type=rtype;
	}

	// Function that returns the booking number.

	int getnum(void)
	{	
		return booking_no;
	}

	// Function that returns the hotel type.

	char gethoteltype(void)
	{	
		return hotel_type;
	}

	// Function that returns the room type.

	char getroomtype(void)
	{	
		return room_type;
	}

	// Function that returns the hotel code.

	int gethotelcd(void)
	{	
		return hotel_cd;
	}
};

void main(void)
{
	int choice=0;

	// Function declarations.

	void addhotel(void);
	void addresort(void);
	void rec_book(void);
	void rec_cancel(void);
	void viewhotel(void);

	// Loop to display the menu.
	
	while(choice!=6)
	{
		system("cls");
		cout << "\n\n\tHOTEL BOOKING SYSTEM";
		cout << "\n\n1. Add new Hotel details";
		cout << "\n2. Add new Resort details";
		cout << "\n3. Record Booking details";
		cout << "\n4. Record Cancellation details";
		cout << "\n5. View details of Hotels and Resorts in a city"; 
		cout << "\n6. Exit";
		cout << "\n\n\tEnter choice: ";
		
		// Accept user input menu choice.
		
		cin >> choice;
		cin.ignore();

		// Check user input menu choice and invoke appropriate function.

		if(choice==1)
			addhotel();
		else if(choice==2)
			addresort();
		else if(choice==3)
			rec_book();
		else if(choice==4)
			rec_cancel();
		else if(choice==5)
			viewhotel();
		else if(choice==6)
			return;
		else
			cout << "\n\n\tINVALID CHOICE! Valid choices are 1 to 6.";
	}
}



/* Function to add new hotel details to Hotel.dat file. */

void addhotel(void)
{

	// Declare objects of the hotel class.

	hotel hotel1, hotel2;
	
	int lastcode;
	
	system("cls");
	
	// Open Hotel.dat file for reading.

	fstream finout("Hotel.dat", ios::in);
	if(!finout)
	{		cout << "Could not open Hotel.dat file.";
			return;
	}
	
	// Obtain the hotel code of the last hotel in the file.

	finout.seekg(0);
	while(finout.read((char *)&hotel1, sizeof(hotel1))){}
	lastcode=hotel1.getcode();
	
	/* If the details of the first hotel are being entered, set last 
		hotel code to 0.*/

	if(lastcode<0)
		lastcode=0;
	
	// Close the file.
	
	finout.close();

	cout << "\nADD NEW HOTEL DETAILS";

	/* Invoke the function to accept hotel details and pass the last hotel 
		code as a parameter to it. */

	hotel2.getdata(lastcode);

	// Reopen Hotel.dat file for adding the new record.

	finout.open("Hotel.dat", ios::app);
	if(!finout)
	{	cout << "Could not open Hotel.dat file.";
		return;
	}

	// Write the new record to the file.

	finout.write((char *)&hotel2, sizeof(hotel2));
	
	// Close the file.
	
	finout.close();
	return;
}



/* Function to add new resort details to Resort.dat file. */

void addresort(void)
{

	// Declare objects of the resort class.

	resort resort1, resort2;
	
	int lastcode;
	
	system("cls");
	
	// Open Resort.dat file for reading.

	fstream finout("Resort.dat", ios::in);
	if(!finout)
	{		cout << "Could not open Resort.dat file.";
			return;
	}
	
	// Obtain the resort code of the last resort in the file.

	finout.seekg(0);
	while(finout.read((char *)&resort1, sizeof(resort1))){}
	lastcode=resort1.getcode();
	
	/* If the details of the first resort are being entered, set last 
		resort code to 0.*/

	if(lastcode<0)
		lastcode=0;
	
	// Close the file.
	
	finout.close();

	cout << "\nADD NEW RESORT DETAILS";

	/* Invoke the function to accept resort details and pass the last resort 
		code as a parameter to it. */

	resort2.getdata(lastcode);

	// Reopen Resort.dat file for adding the new record.

	finout.open("resort.dat", ios::app);
	if(!finout)
	{	cout << "Could not open Resort.dat file.";
		return;
	}

	// Write the new record to the file.

	finout.write((char *)&resort2, sizeof(resort2));
	
	// Close the file.
	
	finout.close();
	return;
}



/* Function to add new booking details to Booking.dat file. */

void rec_book(void)
{

	// Declare objects of the booking class.

	booking booking1, booking2;
	
	fstream finout1;
	char check, book_type, rtype;
	int lastnum, i, j, hcd1, hcd2, vavail;
	
	system("cls");
	cout << "\nADD NEW BOOKING DETAILS\n";
	
	// Open Booking.dat file for reading.

	fstream finout("Booking.dat", ios::in);
	if(!finout)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -