You are on page 1of 4

#Importing mysql.

connector to Python

import mysql.connector

# Connecting MySQL Database to Python

db = mysql.connector.connect(
host="localhost",
user="root",
password="mysql",
database="hotel_management_system"
)
cur = db.cursor()

#Creating Table

cur.execute("""CREATE TABLE IF NOT EXISTS hotel(


id bigint ,
guest_name VARCHAR(40),
contact bigint,
room_number int(3) PRIMARY KEY,
room_type VARCHAR(10),
fee_per_day int(4),
room_status VARCHAR(10),
check_in_date DATE,
check_out_date DATE)""")

#Function to add a new room in the hotel

def addRoom():
print(" --- ENTER ROOM DETAILS --- ")
rno= int(input("Enter Room No. : "))
status= "Vacant"
cur.execute("INSERT INTO hotel(room_number,room_status)
VALUES({},'{}')".format(rno,status))
db.commit()
print(" --- ROOM ADDED SUCCESSFULLY --- ")

#Function to show all rooms and their details

def showRooms():
cur.execute(" SELECT * FROM hotel ")
details= cur.fetchall()
if details:
for room in details:
print(room)
else:
print("SORRY WE HAVE NO ROOMS")

#Function to show vacant rooms and their details

def showVacantRooms():
cur.execute(" SELECT * FROM hotel WHERE room_status='Vacant' ")
details= cur.fetchall()
if details:
for Vroom in details:
print(Vroom)
else:
print("SORRY WE HAVE NO VACANT ROOMS AT THE MOMENT")

#Function to show occupied rooms and their details

def showOccupiedRooms():
cur.execute(" SELECT * FROM hotel WHERE room_status='Occupied' ")
details= cur.fetchall()
if details:
for Oroom in details:
print(Oroom)
else:
print("WE HAVE NO OCCUPIED ROOMS")

#Function to show types of rooms we offer

def roomType():
print("We have 3 types of Rooms in our hotel :")
print(" Simple Room (Cost = Rs.2000) ")
print(" Deluxe Room (Cost = Rs.4000) ")
print(" Premium Room (Cost = Rs.6000) ")

#Function to decide cost of room booked per day as per the type of room

t='Simple'
u='Deluxe'
v='Premium'
def cost(type):
global f
if type== t :
print("You chose a Simple Room. Your fee for the day is Rs.2000")
cur.execute(" UPDATE hotel SET room_type='Simple' WHERE room_number=%s",
(r,))
f=2000
return f
elif type== u:
print("You chose a Deluxe Room. Your fee for the day is Rs.4000")
cur.execute(" UPDATE hotel SET room_type='Deluxe' WHERE room_number=%s",
(r,))
f=4000
return f
elif type== v :
print("You chose a Premium Room. Your fee for the day is Rs.6000")
cur.execute(" UPDATE hotel SET room_type='Premium' WHERE room_number=%s",
(r,))
f=6000
return f
else:
print("Invalid choice")
#Function to book a room

def bookRoom():
global r
name= eval(input("Enter Your Name : "))
uid= int(input("Enter Your AADHAR ID PROOF (12 DIGITS) : "))
mobile= int(input("Enter Your Contact Number : +91 "))
r= int(input("Enter the Room Number you wish to book: "))
ty= eval(input("Choose the type of Room you want (Simple/Deluxe/Premium) : "))
cost(ty)
checkInDate= eval(input('Enter the Check In Date ("YYYY-MM-DD") : '))
checkOutDate= eval(input('Enter the Check Out Date ("YYYY-MM-DD") : '))

#Checking availability of entered room and assigning of room

cur.execute(" SELECT * FROM hotel WHERE room_number= %s",(r,))


room = cur.fetchone()
if room and room[6]== "Vacant":
cur.execute("UPDATE hotel SET room_status= 'Occupied' WHERE room_number=
%s", (r,))
cur.execute(" UPDATE hotel SET id= %s, guest_name= %s, contact= %s,
fee_per_day= %s, check_in_date= %s, check_out_date= %s WHERE room_number= %s",
(uid,name,mobile,f,checkInDate,checkOutDate,r))
db.commit()
print(" --- ROOM BOOKED SUCCESSFULLY --- ")
elif room and room[6]== "Occupied":
print("This room is already booked")
else:
print("Room Number not found")

#Function to check out a room

def check_out():
rno= int(input("Enter Your Room Number: "))
cur.execute("SELECT * FROM hotel WHERE room_number = %s" %(rno,))
room = cur.fetchone()
if room and room[6] == 'Occupied':
cur.execute("UPDATE hotel SET room_status = 'Vacant', id=NULL,
room_type=NULL, guest_name=NULL, contact=NULL, fee_per_day=NULL,
check_in_date=NULL, check_out_date=NULL WHERE room_number = %s", (rno,))
db.commit()
print("CHECKED OUT OF ROOM SUCCESSFULLY!")
print("THANK YOU FOR CHOOSING PYTHON HOTELS \n WE HOPE YOU VISIT US AGAIN")

elif room and room[6] == 'Vacant':


print("THIS ROOM WAS NOT BOOKED.")
else:
print("WE HAVE NO SUCH ROOM.")

#Function for client to make choices

def showmenu():
while True:
print("@" * 39)
print("---- WELCOME TO HOTEL PYTHON ----")
print("@" * 39)
print("Press 1 - Add a New Room")
print("Press 2 - Show All Rooms")
print("Press 3 - Show All Vacant Rooms")
print("Press 4 - Show All Occupied Rooms")
print("Press 5 - Show the types of Rooms we offer")
print("Press 6 - Book a Room")
print("Press 7 - Check Out Room")
print("Press 8 - Exit")

choice = int(input("Enter your choice : "))


if choice == 1:
addRoom()
elif choice == 2:
showRooms()
elif choice == 3:
showVacantRooms()
elif choice == 4:
showOccupiedRooms()
elif choice == 5:
roomType()
elif choice == 6:
bookRoom()
elif choice == 7:
check_out()
elif choice == 8:
break
showmenu()

db.close()

You might also like