0% found this document useful (0 votes)
9 views24 pages

Ohhh

This document contains a C++ program for a hotel reservation system that allows users to book, view, change, and cancel hotel bookings. It includes structures for guest and booking information, functions for managing bookings, and a main menu for user interaction. The program also validates user input and calculates prices based on room types.

Uploaded by

ryomensukunaa200
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views24 pages

Ohhh

This document contains a C++ program for a hotel reservation system that allows users to book, view, change, and cancel hotel bookings. It includes structures for guest and booking information, functions for managing bookings, and a main menu for user interaction. The program also validates user input and calculates prices based on room types.

Uploaded by

ryomensukunaa200
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 24

#include <iostream>

#include <string>

#include <fstream>

#include <vector>

#include <stack>

#include <list>

#include <algorithm>

#include <sstream>

#include <cctype>

#include <limits>

using namespace std;

#define RESET "\033[0m"

#define RED "\033[31m"

#define GREEN "\033[32m"

#define YELLOW "\033[33m"

#define CYAN "\033[36m"

#define BOLD "\033[1m"

// Structure to hold guest information

struct Guest {

string name;

int age;

string gender;

string contactNumber;

};
// Structure to hold hotel booking information

struct HotelBooking {

Guest guest;

string hotelLocation;

string roomType;

string roomNumber;

string checkInDate;

string checkOutDate;

bool operator==(const HotelBooking &other) const {

return guest.name == other.guest.name &&

guest.age == other.guest.age &&

guest.gender == other.guest.gender &&

guest.contactNumber == other.guest.contactNumber &&

hotelLocation == other.hotelLocation &&

roomType == other.roomType &&

roomNumber == other.roomNumber &&

checkInDate == other.checkInDate &&

checkOutDate == other.checkOutDate;

};

// Global data structures

stack<HotelBooking> bookingHistory;

list<HotelBooking> bookingsList;
// Constants

const int ROOMS = 40;

string availableRooms[ROOMS] = {

"R101", "R102", "R103", "R104", "R105",

"R106", "R107", "R108", "R109", "R110",

"R111", "R112", "R113", "R114","R115",

"R116", "R117", "R118", "R119", "R120",

"R121", "R122", "R123", "R124", "R125",

"R126", "R127", "R128", "R129", "R130",

"R131", "R132", "R133", "R134","R135",

"R136", "R137", "R138", "R139", "R140"

};

string roomTypes[3] = {"Single", "Double", "Suite"};

const double basePrices[3] =

{3000.00, 5000.00, 7000.00};

void showMainMenu();

void bookHotel();

void viewBookings();

void changeBooking();

void cancelBooking();

void generateRoomNumber(HotelBooking& booking);

void assignRoomSchedule(HotelBooking& booking);


double calculatePrice(const string &roomType, int hotelIndex);

bool isValidContactNumber(const string &contactNumber);

bool isValidName(const string& name);

void saveBookingsToFile();

void loadBookingsFromFile();

void processPayment(double totalPrice);

int main() {

loadBookingsFromFile();

showMainMenu();

return 0;

void showMainMenu() {

int choice;

do {

system("cls");

cout << BOLD << CYAN;

cout << "|================ Hotel Reservation System


=================|" << RESET << endl;

cout << "| 1. Book a Hotel Room |" << endl;

cout << "| 2. View My Bookings |" << endl;

cout << "| 3. Change My Booking |" << endl;

cout << "| 4. Cancel My Booking |" << endl;

cout << "| 5. Exit |" << endl;

cout << "|


==============================================
=============|" << endl;
cout << "Please enter your choice (1-5): ";

cin >> choice;

switch (choice) {

case 1:

bookHotel();

break;

case 2:

viewBookings();

break;

case 3:

changeBooking();

break;

case 4:

cancelBooking();

break;

case 5:

cout << "Exiting the system. Thank you!\n";

break;

default:

cout << RED << "Invalid choice. Please try again.\n" << RESET;

if (choice != 5) {

cout << YELLOW << "Press Enter to continue..." << RESET;

cin.ignore();
cin.get();

} while (choice != 5);

// Function to book a hotel room

void bookHotel() {

system("cls");

HotelBooking booking;

cout << CYAN << "|================ Adding Hotel Reservation


================|" << RESET << endl;

// Input guest name

while (true) {

cout << GREEN << "Enter your name (First Name Last Name): " <<
RESET;

cin.ignore(); // Ignore leftover newline

getline(cin, booking.guest.name);

if (isValidName(booking.guest.name)) {

break;

} else {

cout << RED << "Invalid input. Please enter a valid name.\n" <<
RESET << endl;

}
// Input age

cout << GREEN << "Enter your age: " << RESET;

while (true) {

cin >> booking.guest.age;

if (cin.fail() || booking.guest.age < 18) {

cin.clear();

cin.ignore(numeric_limits<streamsize>::max(), '\n');

cout << RED << "Please enter a valid age (18 or older): " << RESET;

} else {

cin.ignore(numeric_limits<streamsize>::max(), '\n');

break;

// Input gender

cout << GREEN << "Enter your gender (Male 'M', Female 'F'): " <<
RESET;

while (true) {

cin >> booking.guest.gender;

cin.ignore(numeric_limits<streamsize>::max(), '\n');

if (booking.guest.gender == "M" || booking.guest.gender == "F" ||

booking.guest.gender == "m" || booking.guest.gender == "f") {

// Convert to uppercase

booking.guest.gender = toupper(booking.guest.gender[0]);

break;

} else {
cout << RED << "Please enter 'M' for Male or 'F' for Female: " <<
RESET;

// Input contact number

cout << GREEN << "Enter your contact number (11 digits starting with
'09'): " << RESET;

while (true) {

cin >> booking.guest.contactNumber;

if (isValidContactNumber(booking.guest.contactNumber)) {

break;

} else {

cout << RED << "Must be exactly 11 digits and start with '09': " <<
RESET;

cout << GREEN << "\nAvailable Rooms:\n" << RESET;

int roomCount = 0;

for (int i = 0; i < ROOMS; i++) {

cout << YELLOW << ". " << availableRooms[i] << "\t";

roomCount++;

if (roomCount == 5) {

cout << endl;

roomCount = 0;
}

cout << RESET << endl;

cout<<YELLOW<<"|Single = 3000 | Double = 5000| Suite = 7000


|"<<RESET<<endl;

cout << GREEN << "Enter room type (Single/Double/Suite): " << RESET;

while (true) {

cin >> booking.roomType;

// Convert to proper case

for (auto &c : booking.roomType) c = tolower(c);

if (booking.roomType == "single" || booking.roomType == "double" ||


booking.roomType == "suite") {

// Capitalize first letter

booking.roomType[0] = toupper(booking.roomType[0]);

break;

} else {

cout << RED << "Invalid room type. Please enter 'Single', 'Double',
or 'Suite': " << RESET;

generateRoomNumber(booking);

assignRoomSchedule(booking);

double totalPrice = calculatePrice(booking.roomType, 0); // Using 0 as


hotel index
cout << YELLOW << "\nThe total price for your stay (" <<
booking.roomType

<< " room) is: " << fixed << setprecision(2) << totalPrice << " PHP."
<< RESET << endl;

bookingsList.push_back(booking);

bookingHistory.push(booking);

cout << GREEN << "Booking successful! Please proceed with payment.\n"
<< RESET << endl;

processPayment(totalPrice);

system("cls"); // Clear the console

showMainMenu();

// Function to generate a unique room number

void generateRoomNumber(HotelBooking& booking) {

static int roomCounter = 100; // Starting room number

roomCounter++;

booking.roomNumber = "R" + to_string(roomCounter);

cout << "\tYour room number is: " << booking.roomNumber << endl;

// Function to assign check-in and check-out dates with basic validation

void assignRoomSchedule(HotelBooking& booking) {

while (true) {
cout << "\tEnter check-in date (DD/MM/YYYY): ";

cin >> booking.checkInDate;

if (booking.checkInDate.length() == 10 && booking.checkInDate[2] ==


'/' && booking.checkInDate[5] == '/') {

break;

} else {

cout << RED << "\tInvalid date format! Please use DD/MM/YYYY.\n"
<< RESET;

while (true) {

cout << "\tEnter check-out date (DD/MM/YYYY): ";

cin >> booking.checkOutDate;

if (booking.checkOutDate.length() == 10 && booking.checkOutDate[2]


== '/' && booking.checkOutDate[5] == '/') {

if (booking.checkOutDate > booking.checkInDate) {

break;

} else {

cout << RED << "\tCheck-out date must be after check-in date.\n"
<< RESET;

} else {

cout << RED << "\tInvalid date format! Please use DD/MM/YYYY.\n"
<< RESET;

}
}

// Function to calculate the price based on room type

double calculatePrice(const string &roomType, int hotelIndex) {

double price = 0.0;

// Assign base price based on the room type

if (roomType == "Single") {

price = basePrices[0]; // Price for single room

} else if (roomType == "Double") {

price = basePrices[1]; // Price for double room

} else if (roomType == "Suite") {

price = basePrices[2]; // Price for suite room

return price;

void viewBookings() {

loadBookingsFromFile();

system("cls");

if (bookingsList.empty()) {

cout << RED << "No bookings found.\n" << RESET << endl;

} else {

cout << CYAN << "=== Your Bookings ===\n" << RESET;

for (const auto& booking : bookingsList) {


cout << GREEN << "Guest: " << booking.guest.name << RESET <<
endl;

cout << "Location: " <<"Nueva Ecija" << endl;

cout << "Room Type: " << booking.roomType << endl;

cout << "Room Number: " << booking.roomNumber << endl;

cout << "Check-in Date: " << booking.checkInDate << endl;

cout << "Check-out Date: " << booking.checkOutDate << endl;

cout << "-----------------------------\n";

cout << YELLOW << "\nPress Enter to return to the main menu..." <<
RESET;

cin.ignore();

cin.get();

system("cls"); // Clear the console

showMainMenu();

// Function to change an existing booking

void changeBooking() {

system("cls"); // Clear the console

if (bookingsList.empty()) {

cout << RED << "No bookings found to change.\n" << RESET << endl;

cout << YELLOW << "\nPress Enter to return to the main menu..." <<
RESET;

cin.ignore();

cin.get();
system("cls"); // Clear the console

showMainMenu();

return;

} else {

int index = 0;

cout << CYAN << "=== Your Bookings ===\n" << RESET;

for (const auto& booking : bookingsList) {

cout << GREEN << "INDEX: " << index++ << RESET << endl;

cout << "Guest: " << booking.guest.name << endl;

cout << "Room Type: " << booking.roomType << endl;

cout << "Room Number: " << booking.roomNumber << endl;

cout << "Check-in Date: " << booking.checkInDate << endl;

cout << "Check-out Date: " << booking.checkOutDate << endl;

cout << "-----------------------------\n";

int bookingIndex;

cout << "\tEnter the index of the booking you want to change: ";

cin >> bookingIndex;

if (cin.fail() || bookingIndex < 0 || bookingIndex >= bookingsList.size())


{

cin.clear();

cin.ignore(numeric_limits<streamsize>::max(), '\n');

cout << RED << "Invalid index. Returning to main menu.\n" <<
RESET << endl;

cout << YELLOW << "\nPress Enter to return to the main menu..."
<< RESET;
cin.ignore();

cin.get();

system("cls"); // Clear the console

showMainMenu();

return;

auto it = bookingsList.begin();

advance(it, bookingIndex);

HotelBooking& booking = *it;

// Change guest name

char changeName;

cout << "\tCurrent Guest Name: " << booking.guest.name << endl;

cout << "\tDo you want to change the guest name? (y/n): ";

cin >> changeName;

if (changeName == 'y' || changeName == 'Y') {

cout << "\tEnter new name: ";

cin.ignore();

getline(cin, booking.guest.name);

if (!isValidName(booking.guest.name)) {

cout << RED << "\tInvalid name. Name not changed.\n" <<
RESET << endl;

}
// Change room type

char changeRoomType;

cout << "\tCurrent Room Type: " << booking.roomType << endl;

cout << "\tDo you want to change the room type? (y/n): ";

cin >> changeRoomType;

if (changeRoomType == 'y' || changeRoomType == 'Y') {

cout << "\tEnter new room type (Single/Double/Suite): ";

cin >> booking.roomType;

// Validate room type

for (auto &c : booking.roomType) c = tolower(c);

if (booking.roomType == "single" || booking.roomType == "double" ||


booking.roomType == "suite") {

// Capitalize first letter

booking.roomType[0] = toupper(booking.roomType[0]);

} else {

cout << RED << "\tInvalid room type. Room type not changed.\n"
<< RESET << endl;

// Change check-in and check-out dates

char changeDates;

cout << "\tCurrent Check-in Date: " << booking.checkInDate << endl;

cout << "\tCurrent Check-out Date: " << booking.checkOutDate <<


endl;

cout << "\tDo you want to change the check-in and check-out dates?
(y/n): ";

cin >> changeDates;


if (changeDates == 'y' || changeDates == 'Y') {

assignRoomSchedule(booking);

cout << GREEN << "\tBooking updated successfully!\n" << RESET <<
endl;

cout << YELLOW << "\nPress Enter to return to the main menu..." <<
RESET;

cin.ignore();

cin.get();

system("cls"); // Clear the console

showMainMenu();

// Function to cancel a booking

void cancelBooking() {

system("cls"); // Clear the console

if (bookingsList.empty()) {

cout << RED << "No bookings found to cancel.\n" << RESET << endl;

} else {

int index = 0;

cout << CYAN << "=== Your Bookings ===\n" << RESET;

for (const auto& booking : bookingsList) {

cout << GREEN << "INDEX: " << index++ << RESET << endl;

cout << "Guest: " << booking.guest.name << endl;


cout << "Room Type: " << booking.roomType << endl;

cout << "Room Number: " << booking.roomNumber << endl;

cout << "Check-in Date: " << booking.checkInDate << endl;

cout << "Check-out Date: " << booking.checkOutDate << endl;

cout << "-----------------------------\n";

int bookingIndex;

cout << "\tEnter the index of the booking you want to cancel: ";

cin >> bookingIndex;

if (cin.fail() || bookingIndex < 0 || bookingIndex >= bookingsList.size())


{

cin.clear();

cin.ignore(numeric_limits<streamsize>::max(), '\n');

cout << RED << "Invalid index. Returning to main menu.\n" <<
RESET << endl;

cout << YELLOW << "\nPress Enter to return to the main menu..."
<< RESET;

cin.ignore();

cin.get();

system("cls"); // Clear the console

showMainMenu();

return;

auto it = bookingsList.begin();

advance(it, bookingIndex);
char confirm;

cout << "\tAre you sure you want to cancel the booking for " << it-
>guest.name<< "? (y/n): ";

cin >> confirm;

if (confirm == 'y' || confirm == 'Y') {

bookingsList.erase(it);

cout << GREEN << "\tBooking cancelled successfully!\n" << RESET


<< endl;

} else {

cout << YELLOW << "\tCancellation aborted. Returning to main


menu.\n" << RESET << endl;

cout << YELLOW << "\nPress Enter to return to the main menu..." <<
RESET;

cin.ignore();

cin.get();

system("cls"); // Clear the console

showMainMenu();

bool bookingsLoaded = false; //Mark para malaman kung na-load na ang


bookings
void loadBookingsFromFile() {

if (bookingsLoaded) {

// Hindi na magloload ulit kapag naload na

return;

ifstream file("bookings.txt");

HotelBooking booking;

if (file.is_open()) {

while (file >> ws && getline(file, booking.guest.name, ' ') &&

file >> booking.guest.age >> booking.guest.gender >>


booking.guest.contactNumber >>

booking.hotelLocation >> booking.roomType >>

booking.roomNumber >> booking.checkInDate >>


booking.checkOutDate) {

bookingsList.push_back(booking);

file.close();

bookingsLoaded = true; // mark as loadad

cout << GREEN << "Bookings loaded successfully.\n" << RESET <<
endl;

} else {

cout << YELLOW << "Unable to open bookings file. Starting with empty
bookings list.\n" << RESET << endl;

}
void processPayment(double totalPrice) {

int paymentOption;

cout << "|================ Payment Options


========================|" << endl;

cout << "| 1. GCash |" << endl;

cout << "| 2. PayMaya |" << endl;

cout << "| 3. PayPal |" << endl;

cout << "| 4. Pay at the Counter |" << endl;

cout << "|


==============================================
============|" << endl;

cout << "Total Amount Due: " << fixed << setprecision(2) << totalPrice
<< " PHP\n";

cout << "Please select your payment method (1-4): ";

cin >> paymentOption;

switch (paymentOption) {

case 1:

cout<<" "<<endl;

cout << "You selected GCash. Processing payment...\n";

break;

case 2:

cout<<" "<<endl;

cout << "You selected PayMaya. Processing payment...\n";

break;

case 3:

cout<<" "<<endl;
cout << "You selected PayPal. Processing payment...\n";

break;

case 4:

cout<<" "<<endl;

cout << "You selected Pay at the Counter. Proceeding with


payment...\n";

break;

default:

cout<<" "<<endl;

cout << RED << "Invalid choice. Returning to main menu.\n" <<
RESET;

return;

// Simulate successful payment

cout << GREEN << "Payment successful! Your booking has been
confirmed.\n" << RESET;

cout << YELLOW << "\nPress Enter to return to the main menu..." <<
RESET;

cin.ignore();

cin.get();

system("cls"); // Clear the console

showMainMenu();

// Function to validate contact number

bool isValidContactNumber(const string &contactNumber) {

if (contactNumber.length() != 11) return false;


if (contactNumber.substr(0, 2) != "09") return false;

for (char c : contactNumber) {

if (!isdigit(c)) return false;

return true;

// Function to validate name

bool isValidName(const string& name) {

if (name.empty()) return false; // Check for empty string

bool hasValidCharacter = false; // Track if there are any valid characters

size_t lastNameStart = 0; // Track the start of the last name

size_t spaceCount = 0; // Track spaces to ensure no consecutive


spaces

bool initialFound = false; // Track if an initial has been found

bool firstNameValid = false; // Track if a valid first name is found

// Check for leading spaces

if (name[0] == ' ') return false;

for (size_t i = 0; i < name.length(); ++i) {

char ch = name[i];

if (isalpha(ch) || ch == '-' || ch == '\'') {

hasValidCharacter = true; // Found a valid character

lastNameStart = i; // Update the last name start position

spaceCount = 0; // Reset space count


initialFound = false; // Reset initial state

// Mark first name as valid if it's the first non-space character

if (spaceCount == 0 && !firstNameValid) {

firstNameValid = true;

// Allow spaces

else if (ch == ' ') {

spaceCount++;

// Ensure no consecutive spaces

if (spaceCount > 1) {

return false; // Invalid if there are consecutive spaces

} else {

// If we encounter an invalid character, return false

return false;

// Return false if no valid characters were found

return hasValidCharacter;

You might also like