Import Mysql (Exception Handling Included)
Import Mysql (Exception Handling Included)
connector
class HotelManagementSystem:
def __init__(self):
self.connection = self.create_connection()
self.cursor = self.connection.cursor()
def create_connection(self):
try:
connection = mysql.connector.connect(
host='localhost',
database='hotel_management'
if connection.is_connected():
except Error as e:
print(f"Error: {e}")
return None
try:
self.cursor.execute(query, values)
self.connection.commit()
except Error as e:
print(f"Error: {e}")
def view_rooms(self):
try:
query = "SELECT * FROM rooms"
self.cursor.execute(query)
rooms = self.cursor.fetchall()
if rooms:
else:
except Error as e:
print(f"Error: {e}")
def available_rooms(self):
try:
self.cursor.execute(query)
rooms = self.cursor.fetchall()
if rooms:
else:
except Error as e:
print(f"Error: {e}")
try:
room = self.cursor.fetchone()
self.connection.commit()
# Insert the booking into the bookings table
self.cursor.execute(query, values)
self.connection.commit()
else:
except Error as e:
print(f"Error: {e}")
def close_connection(self):
if self.connection.is_connected():
self.cursor.close()
self.connection.close()
print("Connection closed.")
# Command-line interface
def main():
hms = HotelManagementSystem()
while True:
print("5. Exit")
if choice == '1':
hms.add_room(room_type, price)
elif choice == '2':
hms.view_rooms()
print("\nAvailable Rooms:")
hms.available_rooms()
hms.close_connection()
break
else:
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.