0% found this document useful (0 votes)
39 views9 pages

Hotel Management Py1

The document contains Python code for a hotel management system that establishes a connection to a MySQL database. It defines functions for creating tables to store room and guest details, displaying hotel services and amenities, adding available rooms, booking rooms, and calculating bills for rooms, restaurant, gaming, car rentals, and updating the total amount owed in the database.
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)
39 views9 pages

Hotel Management Py1

The document contains Python code for a hotel management system that establishes a connection to a MySQL database. It defines functions for creating tables to store room and guest details, displaying hotel services and amenities, adding available rooms, booking rooms, and calculating bills for rooms, restaurant, gaming, car rentals, and updating the total amount owed in the database.
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/ 9

import mysql.

connector
import random

# Establish a MySQL connection


try:
db = mysql.connector.connect(
host='localhost',
user='root',
password='your_password',
database='myhotel',
auth_plugin='mysql_native_password'
)

cursor = db.cursor()

# Create the Rooms table if it doesn't exist


create_rooms_table = """
CREATE TABLE IF NOT EXISTS RoomsHistory (
room_no VARCHAR(10),
guest_id VARCHAR(6) PRIMARY KEY,
phone_no VARCHAR(15),
check_out DATE,
total DECIMAL(10, 2)
)
"""

cursor.execute(create_rooms_table)

# Create the Hotel table if it doesn't exist


create_room_and_guests_table = """
CREATE TABLE IF NOT EXISTS Hotel (
room_no VARCHAR(10) PRIMARY KEY,
name VARCHAR(255),
address VARCHAR(255),
age INT,
nationality VARCHAR(255),
phone_no VARCHAR(15),
email_id VARCHAR(255),
check_in DATE,
check_out DATE,
night_staying INT,
status VARCHAR(20),
pay_total DECIMAL(10, 2),
guest_id VARCHAR(6)
)
"""

cursor.execute(create_room_and_guests_table)
db.commit()

except mysql.connector.Error as e:
print(f"Error: {e}")
exit(1)

def display_details():
try:
print("Here are some of the services and amenities we offer:")

print("\nRoom Types and Rates:")


print("A. Ultra Royal (Room no.s A001 to A100) ----> Rs. 10000 ")
print("B. Royal (Room no.s B001 to B100) —--------> Rs. 5000")
print("C. Elite (Room no.s C001 to C100) —----------> Rs. 3500")
print("D. Budget (Room no.s D001 to D100) —------> Rs. 2500")

print("\nRestaurant Menu:")
print("1. Vegetarian Combo -------> Rs. 300")
print("2. Non-Vegetarian Combo -> Rs. 800")
print("3. Snacks or desserts —----> Rs 300")

print("\nGaming Options:")
print("1. Table Tennis —--> 150 Rs./hr")
print("2. Bowling —--------> 100 Rs./hr")
print("3. Snooker —-------> 250 Rs./hr")
print("4. Billiards —--------> 400 Rs./hr")
print("5. Video Games —-> 300 Rs./hr")
print("6. WaterSports —---> 350 Rs./hr")

print("\nCars available for sightseeing per hour:")


print("1. Swift Dzire —------> Rs. 200")
print("2. Toyota Innova —--> Rs. 250")
print("3. Nissan Sunny —--> Rs. 310")
print("4. Toyota Fortuner —> Rs. 370")
print("5. Jaguar XF —-------> Rs. 1000")
print("6. Buses —-------------> Rs. 700")

except Exception as e:
print(f"Error: {e}")

def add_room(room_no):
try:
cursor.execute('INSERT INTO Hotel (room_no, status) VALUES (%s, %s)',
(room_no, "Available"))
db.commit()

except mysql.connector.Error as e:
print(f"Error: {e}")

def view_available_rooms():
try:
cursor.execute("SELECT room_no FROM Hotel WHERE status = 'Available'")
available_rooms = cursor.fetchall()

if available_rooms:
print("Available rooms:")
for room in available_rooms:
print(f"Room {room[0]}")
else:
print("Sorry, there are no available rooms.")

except mysql.connector.Error as e:
print(f"Error: {e}")

def view_booked_rooms():
try:
cursor.execute("SELECT room_no FROM Hotel WHERE status = 'Booked'")
booked_rooms = cursor.fetchall()

if booked_rooms:
print("Booked rooms:")
for room in booked_rooms:
print(f"Room {room[0]}")
else:
print("Sorry, there are no booked rooms.")

except mysql.connector.Error as e:
print(f"Error: {e}")

ns=0
def book_room():
try:
room_type = input("Enter room type(A/B/C/D): ")
room_no = room_type + input("Enter room number to book: ")
name = input("Enter guest name: ")
address = input("Enter guest address: ")
age = input("Enter guest age: ")
nationality = input("Enter guest nationality: ")
phone = int(input("Enter guest phone number: "))
email = input("Enter guest email ID: ")
check_in = input("Enter check-in date (YYYY-MM-DD): ")
check_out = input("Enter check-out date (YYYY-MM-DD): ")
guest_id = ''
for i in range(2):
num1 = random.randint(48, 57)
num2 = random.randint(65, 90)
num3 = random.randint(97, 122)
guest_id = guest_id + chr(num1) + chr(num2) + chr(num3)
print("Your Guest ID is:", guest_id)

# Check if the room is available


cursor.execute("SELECT status FROM Hotel WHERE room_no = %s", (room_no,))
status = cursor.fetchone()
if status and status[0] == 'Available':
# Calculate the number of nights
cursor.execute("SELECT DATEDIFF(%s, %s)", (check_out, check_in))
night_staying = cursor.fetchone()[0]

# Book the room and insert guest details


cursor.execute("INSERT INTO RoomsHistory (room_no, guest_id, phone_no,
check_out) VALUES (%s, %s, %s)",
(room_no, guest_id, phone, check_out))
cursor.execute("INSERT INTO Hotel (name, address, age, nationality, phone_no,
email, check_in, check_out, room_type, guest_id, night_staying, status) VALUES (%s, %s,
%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) WHERE room_no = %s",
(name, address, age, nationality, phone, email, check_in, check_out,
room_type, guest_id, night_staying, "Booked",room_no))
db.commit()
print("Room booked successfully.")
else:
print("Room is not available for booking. Or the room doesn't exist. Or Invalid room
number.")

except mysql.connector.Error as e:
print(f"Error: {e}")

def calculate_room_rent(room_no, guest_id):


try:
if room_no.startswith('A'):
room_rent = 10000 * night_staying
elif room_no.startswith('B'):
room_rent = 5000 * night_staying
elif room_no.startswith('C'):
room_rent = 3500 * night_staying
elif room_no.startswith('D'):
room_rent = 2500 * night_staying
else:
print("Invalid room number.")
return
cursor.execute("UPDATE Hotel SET pay_total = pay_total + %s WHERE guest_id =
%s", (room_rent, guest_id))
cursor.execute("UPDATE RoomsHistory SET total = total + %s WHERE guest_id =
%s", (room_rent, guest_id))

db.commit()
print(f"Room Rent for Room {room_no} added to the total amount.")

except mysql.connector.Error as e:
print(f"Error: {e}")

def calculate_restaurant_bill(guest_id):
try:
r_bill = 0
while True:
print("Restaurant Menu:")
print("1. Vegetarian Combo -------> Rs. 300")
print("2. Non-Vegetarian Combo -> Rs. 800")
print("3. Snacks or desserts —----> Rs 300")

choice = int(input("Enter The Choice (1/2/3, 0 to finish): "))


if choice == 0:
break
elif choice == 1:
quantity = int(input("Enter The Quantity: "))
r_bill += (500 * quantity)
elif choice == 2:
quantity = int(input("Enter The Quantity: "))
r_bill += (800 * quantity)
elif choice == 3:
quantity = int(input("Enter The Quantity: "))
r_bill += (300 * quantity)
else:
print("Invalid choice. Please select from 1, 2, 3, or 0 to finish.")

cursor.execute("UPDATE Hotel SET pay_total = pay_total + %s WHERE guest_id =


%s", (r_bill, guest_id))
cursor.execute("UPDATE RoomsHistory SET total = total + %s WHERE guest_id =
%s", (r_bill, guest_id))

db.commit()
print("Restaurant bill added to the total amount.")

except mysql.connector.Error as e:
print(f"Error: {e}")

def calculate_gaming_bill(guest_id):
try:
g_bill = 0
while True:
print("\nGaming Options:")
print("1. Table Tennis —--> 150 Rs./hr")
print("2. Bowling —--------> 100 Rs./hr")
print("3. Snooker —-------> 250 Rs./hr")
print("4. Billiards —--------> 400 Rs./hr")
print("5. Video Games —-> 300 Rs./hr")
print("6. Water Sports —--> 350 Rs./hr")

choice = int(input("Enter The Choice (1/2/3/4/5/6, 0 to finish): "))


if choice == 0:
break
elif choice == 1:
hours = int(input("Enter The Number of Hours: "))
g_bill += (150 * hours)
elif choice == 2:
hours = int(input("Enter The Number of Hours: "))
g_bill += (100 * hours)
elif choice == 3:
hours = int(input("Enter The Number of Hours: "))
g_bill += (250 * hours)
elif choice == 4:
hours = int(input("Enter The Number of Hours: "))
g_bill += (400 * hours)
elif choice == 5:
hours = int(input("Enter The Number of Hours: "))
g_bill += (300 * hours)
elif choice == 6:
hours = int(input("Enter The Number of Hours: "))
g_bill += (350 * hours)
else:
print("Invalid choice. Please select from 1, 2, 3, 4, 5, 6, or 0 to finish.")

cursor.execute("UPDATE Hotel SET pay_total = pay_total + %s WHERE guest_id =


%s", (g_bill, guest_id))
cursor.execute("UPDATE RoomsHistory SET total = total + %s WHERE guest_id =
%s", (g_bill, guest_id))

db.commit()
print("Gaming bill added to the total amount.")

except mysql.connector.Error as e:
print(f"Error: {e}")

def book_cars_for_sightseeing(guest_id):
try:
print("\nCars available for sightseeing per hour:")
print("1. Swift Dzire —------> Rs. 200")
print("2. Toyota Innova —--> Rs. 250")
print("3. Nissan Sunny —--> Rs. 310")
print("4. Toyota Fortuner —> Rs. 370")
print("5. Jaguar XF —-------> Rs. 1000")
print("6. Buses —-------------> Rs. 700")

choice = int(input("Enter The Choice (1/2/3/4/5/6, 0 to finish): "))


if choice == 0:
return
elif choice in [1, 2, 3, 4, 5, 6]:
hours = int(input("Enter The Number of Hours: "))
car_rate = {1: 200, 2: 250, 3: 310, 4: 370, 5: 1000, 6: 700 }
car_cost = car_rate[choice] * hours

cursor.execute("UPDATE Hotel SET pay_total = pay_total + %s WHERE guest_id =


%s", (car_cost, guest_id))
cursor.execute("UPDATE RoomsHistory SET total = total + %s WHERE guest_id =
%s", (car_cost, guest_id))

db.commit()
print("Car booking cost added to the total amount")
else:
print("Invalid choice. Please select from 1, 2, 3, 4, 5, 6, or 0 to finish.")

except mysql.connector.Error as e:
print(f"Error: {e}")

# Function to handle final payment, update the main table, and clear guest data by room
number
def final_payment_gateway(guest_id):
try:
cursor.execute("SELECT room_no, pay_total FROM Hotel WHERE guest_id = %s",
(guest_id,))
guest_data = cursor.fetchone()
if guest_data:
total_payment = guest_data[1]
extra_charge = total_payment * 0.1
total_payment += extra_charge

print(f"Original Payment: {total_payment - extra_charge} Rs")


print(f"10% Extra Charge: {extra_charge} Rs")
print(f"Total Payment (including 10% extra): {total_payment} Rs")

while True:
payment = float(input("Enter the payment amount: "))
if payment >= total_payment:
change = payment - total_payment
print(f"Payment successful. Change: {change} Rs")

# Clear guest data for the room


cursor.execute("DELETE FROM Hotel WHERE guest_id = %s", (guest_id,))
db.commit()
add_room(guest_data[0])
db.commit()
print(f"Room {guest_data[0]} status set to 'Available'. Guest data cleared.")
break
else:
print("Payment amount is insufficient. Please enter a valid amount.")
else:
print("No guest data found with the ID.")

except mysql.connector.Error as e:
print(f"Error: {e}")

def queries():
try:
st = input("Enter your queries in proper format: ")
add = eval(input("Enter data for queries: "))
cursor.execute(st, add)
data = cursor.fetchall()
print(data)

except mysql.connector.Error as e:
print(f"Error: {e}")

def main():
while True:
try:
print("\nHotel Management System")
print("Welcome to Star Park Hotel!")
print("Main Menu:")
print("1. To get details")
print("2. Empty the hotel to make a fresh start and add new rooms")
print("3. View available rooms")
print("4. View booked rooms")
print("5. Book a room")
print("6. Calculate room rent")
print("7. Calculate restaurant bill")
print("8. Calculate gaming bill")
print("9. Book cars for sightseeing")
print("10. Final payment gateway and leave the room")
print("11. More queries")
print("0. Exit")

choice = input("Enter your choice: ")


if choice == "1":
display_details()
elif choice == "2":
cursor.execute("TRUNCATE TABLE Hotel")
for i in range(4):
l = ['A', 'B', 'C', 'D']
for j in range(1, 100):
room_no = l[i] + str(j)
add_room(room_no)
print("All rooms are added. All rooms are reset to empty.")
elif choice == "3":
view_available_rooms()
elif choice == "4":
view_booked_rooms()
elif choice == "5":
book_room()
elif choice == "6":
room_type = input("Enter room type: ")
guest_id = input("Enter Guest ID: ")
calculate_room_rent(room_type, guest_id)
elif choice == "7":
guest_id = input("Enter Guest ID: ")
calculate_restaurant_bill(guest_id)
elif choice == "8":
guest_id = input("Enter Guest ID: ")
calculate_gaming_bill(guest_id)
elif choice == "9":
guest_id = input("Enter Guest ID: ")
book_cars_for_sightseeing(guest_id)
elif choice == "10":
guest_id = input("Enter Guest ID: ")
final_payment_gateway(guest_id)
elif choice == "11":
queries()
elif choice == "0":
print("Thank you for using Star Park Hotel Management System. Goodbye!")
break
else:
print("Invalid choice. Please try again.")

except Exception as e:
print(f"Error: {e}")

if __name__ == "__main__":
main()

You might also like