0% found this document useful (0 votes)
4 views

Import Mysql (Exception Handling Included)

Uploaded by

kgvk275
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)
4 views

Import Mysql (Exception Handling Included)

Uploaded by

kgvk275
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/ 12

import mysql.

connector

from mysql.connector import Error

class HotelManagementSystem:

def __init__(self):

self.connection = self.create_connection()

self.cursor = self.connection.cursor()

def create_connection(self):

"""Create connection to MySQL Database"""

try:

connection = mysql.connector.connect(

host='localhost',

user='root', # Change this to your MySQL username

password='your_password', # Change this to your MySQL


password

database='hotel_management'

if connection.is_connected():

print("Successfully connected to the database")


return connection

except Error as e:

print(f"Error: {e}")

return None

def add_room(self, room_type, price):

"""Add a new room to the database"""

try:

query = "INSERT INTO rooms (room_type, price, status) VALUES


(%s, %s, %s)"

values = (room_type, price, 'available')

self.cursor.execute(query, values)

self.connection.commit()

print("Room added successfully.")

except Error as e:

print(f"Error: {e}")

def view_rooms(self):

"""View all rooms in the hotel"""

try:
query = "SELECT * FROM rooms"

self.cursor.execute(query)

rooms = self.cursor.fetchall()

if rooms:

for room in rooms:

print(f"Room ID: {room[0]}, Type: {room[1]}, Price:


{room[2]}, Status: {room[3]}")

else:

print("No rooms found.")

except Error as e:

print(f"Error: {e}")

def available_rooms(self):

"""View only available rooms"""

try:

query = "SELECT * FROM rooms WHERE status = 'available'"

self.cursor.execute(query)

rooms = self.cursor.fetchall()

if rooms:

for room in rooms:


print(f"Room ID: {room[0]}, Type: {room[1]}, Price:
{room[2]}")

else:

print("No available rooms at the moment.")

except Error as e:

print(f"Error: {e}")

def book_room(self, customer_name, room_id, check_in,


check_out):

"""Book a room if available"""

try:

# Check if the room is available

self.cursor.execute("SELECT status FROM rooms WHERE room_id


= %s", (room_id,))

room = self.cursor.fetchone()

if room and room[0] == 'available':

# Update room status to 'booked'

self.cursor.execute("UPDATE rooms SET status = 'booked'


WHERE room_id = %s", (room_id,))

self.connection.commit()
# Insert the booking into the bookings table

query = "INSERT INTO bookings (customer_name, room_id,


check_in, check_out) VALUES (%s, %s, %s, %s)"

values = (customer_name, room_id, check_in, check_out)

self.cursor.execute(query, values)

self.connection.commit()

print(f"Room {room_id} successfully booked for


{customer_name} from {check_in} to {check_out}.")

else:

print(f"Room {room_id} is not available.")

except Error as e:

print(f"Error: {e}")

def close_connection(self):

"""Close the connection to the database"""

if self.connection.is_connected():

self.cursor.close()

self.connection.close()

print("Connection closed.")
# Command-line interface

def main():

hms = HotelManagementSystem()

while True:

print("\nHotel Management System")

print("1. Add Room")

print("2. View All Rooms")

print("3. View Available Rooms")

print("4. Book Room")

print("5. Exit")

choice = input("Enter your choice: ")

if choice == '1':

room_type = input("Enter Room Type (e.g., Single, Double,


Suite): ")

price = float(input("Enter Room Price: "))

hms.add_room(room_type, price)
elif choice == '2':

print("\nAll Rooms in the Hotel:")

hms.view_rooms()

elif choice == '3':

print("\nAvailable Rooms:")

hms.available_rooms()

elif choice == '4':

customer_name = input("Enter Customer Name: ")

room_id = int(input("Enter Room ID: "))

check_in = input("Enter Check-in Date (YYYY-MM-DD): ")

check_out = input("Enter Check-out Date (YYYY-MM-DD): ")

hms.book_room(customer_name, room_id, check_in,


check_out)

elif choice == '5':

hms.close_connection()

print("Exiting the system.")

break
else:

print("Invalid choice, please try again.")

if __name__ == '__main__':

main()
Output of the following code
Hotel Management System
1. Add Room
2. View All Rooms
3. View Available Rooms
4. Book Room
5. Exit
Enter your choice: 1
Enter Room Type (e.g., Single, Double, Suite):
Double
Enter Room Price: 150.0
Room added successfully.

Hotel Management System


1. Add Room
2. View All Rooms
3. View Available Rooms
4. Book Room
5. Exit
Enter your choice: 2

All Rooms in the Hotel:


Room ID: 1, Type: Double, Price: 150.0, Status:
available

Hotel Management System


1. Add Room
2. View All Rooms
3. View Available Rooms
4. Book Room
5. Exit
Enter your choice: 3
Available Rooms:
Room ID: 1, Type: Double, Price: 150.0

Hotel Management System


1. Add Room
2. View All Rooms
3. View Available Rooms
4. Book Room
5. Exit
Enter your choice: 4
Enter Customer Name: John Doe
Enter Room ID: 1
Enter Check-in Date (YYYY-MM-DD): 2024-11-16
Enter Check-out Date (YYYY-MM-DD): 2024-11-20
Room 1 successfully booked for John Doe from
2024-11-16 to 2024-11-20.

Hotel Management System


1. Add Room
2. View All Rooms
3. View Available Rooms
4. Book Room
5. Exit
Enter your choice: 5
Connection closed.
Exiting the system.

You might also like