Microproject Code

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Microproject

Sub:- oop co-3i


Topic:-Hotel booking system

#include <iostream>
using namespace std;

const int MAX_ROOMS = 10; // Maximum number of rooms in the hotel

struct Room {
int roomNumber;
bool booked;
string guestName;
};

class Hotel {
private:
Room rooms[MAX_ROOMS];

public:
Hotel() {
for(int i = 0; i < MAX_ROOMS; i++) {
rooms[i].roomNumber = i + 1;
rooms[i].booked = false;
rooms[i].guestName = "";
}
}

void displayRooms() {
cout << "Room Number\tBooked\tGuest Name\n";
for(int i = 0; i < MAX_ROOMS; i++) {
cout << rooms[i].roomNumber << "\t\t" << (rooms[i].booked ? "Yes" :
"No") << "\t" << rooms[i].guestName << "\n";
}
}

void bookRoom(int roomNumber, string guestName) {


if(roomNumber < 1 || roomNumber > MAX_ROOMS) {
cout << "Invalid room number. Please choose between 1 and " <<
MAX_ROOMS << ".\n";
return;
}

if(rooms[roomNumber - 1].booked) {
cout << "Room " << roomNumber << " is already booked.\n";
} else {
rooms[roomNumber - 1].booked = true;
rooms[roomNumber - 1].guestName = guestName;
cout << "Room " << roomNumber << " booked for " << guestName << ".\n";
}
}
};

int main() {
Hotel hotel;

int choice;
do {
cout << "\nHotel Booking System\n";
cout << "1. Display Rooms\n";
cout << "2. Book Room\n";
cout << "3. Exit\n";
cout << "Enter your choice: ";
cin >> choice;

switch(choice) {
case 1:
hotel.displayRooms();
break;
case 2: {
int roomNumber;
string guestName;
cout << "Enter room number: ";
cin >> roomNumber;
cin.ignore(); // Clear the input buffer
cout << "Enter guest name: ";
getline(cin, guestName); // Read the entire line, including spaces
hotel.bookRoom(roomNumber, guestName);
break;
}
case 3:
cout << "Exiting program.\n";
break;
default:
cout << "Invalid choice. Please try again.\n";
}
} while(choice != 3);

return 0;
}
Output:-
Explanation:-

1. **Header and Constants**


- `#include <iostream>`: This includes the standard input-output library, which
provides functionality for input and output operations.
- `using namespace std;`: This brings the entire standard namespace into scope,
which means we don't have to explicitly write `std::` before standard library
functions.

2. **Constants and Structures**


- `const int MAX_ROOMS = 10;`: This defines a constant `MAX_ROOMS` to
specify the maximum number of rooms in the hotel.
- `struct Room { ... };`: This defines a structure called `Room` which holds
information about each room, including its number, booking status, and guest
name.

3. **Hotel Class**
- `class Hotel { ... };`: This is a class definition for the Hotel. It contains private
data members and public member functions.

- **Private Data Members**:


- `Room rooms[MAX_ROOMS];`: An array of `Room` structures to represent the
rooms in the hotel.

- **Public Member Functions**:


- `Hotel() { ... }`: Constructor method that initializes the rooms in the hotel with
default values (room number, booked status, and guest name).
- `void displayRooms() { ... }`: Method to display information about all the
rooms, including room number, booking status, and guest name.

- `void bookRoom(int roomNumber, string guestName) { ... }`: Method to book


a room. It takes a room number and guest name as input. It checks if the room is
valid and available, and if so, it updates the booking status and guest name for
that room.

4. **Main Function**
- `int main() { ... }`: The main function is where the program starts its execution.

- **Menu-Driven Interface**:
- The program presents a menu to the user using a `do-while` loop, where the
user can choose from three options:
1. Display Rooms
2. Book Room
3. Exit

- Depending on the user's choice, the program calls the corresponding member
function of the `Hotel` class.

- For booking a room, it prompts the user for a room number and guest name.
It uses `cin.ignore()` to clear the input buffer and `getline()` to read the entire line
for the guest's name, including spaces.

5. **Exiting the Program**


- When the user chooses to exit (option 3), the program displays a message and
the loop terminates, ending the program.

6. **Return Statement**
- `return 0;`: This indicates that the program has terminated successfully.

You might also like