Hotel Management system
To manage hotel room bookings, guest details, and bill generation using Python and CSV file
handling.
1. Add a new guest booking.
2. View all guest bookings.
3. Search for a booking by guest ID.
4. Update booking details.
5. Delete a booking record.
6. Generate a bill for a guest.
SOLUTION:
# File name for storing hotel records
FILE_NAME = "hotel_bookings.csv"
# Function to initialize the file (create file if not exists)
def initialize_file():
try:
with open(FILE_NAME, "x") as file:
file.write("GuestID,GuestName,RoomType,CheckInDate,CheckOutDate,AmountDue\n") #
Header row
except FileExistsError:
pass
# Function to add a new guest booking
def add_booking(guest_id, guest_name, room_type, check_in_date, check_out_date,
amount_due):
with open(FILE_NAME, "a") as file:
file.write(f"{guest_id},{guest_name},{room_type},{check_in_date},{check_out_date},{amo
unt_due}\n")
print("Booking added successfully!")
# Function to view all guest bookings
def view_bookings():
try:
with open(FILE_NAME, "r") as file:
data = file.readlines()
if len(data) == 1: # Only header exists
print("No bookings found!")
else:
print("\nGuest Bookings:")
print(data[0].strip()) # Print header
for line in data[1:]:
print(line.strip())
except FileNotFoundError:
print("No records found! Initialize the system first.")
# Function to search for a booking by guest ID
def search_booking(guest_id):
try:
with open(FILE_NAME, "r") as file:
for line in file:
if line.startswith(guest_id + ","):
print("Booking Found:", line.strip())
return
print("Booking not found!")
except FileNotFoundError:
print("No records found! Initialize the system first.")
# Function to update booking details
def update_booking(guest_id, guest_name=None, room_type=None, check_in_date=None,
check_out_date=None, amount_due=None):
try:
with open(FILE_NAME, "r") as file:
lines = file.readlines()
updated = False
with open(FILE_NAME, "w") as file:
for line in lines:
parts = line.strip().split(",")
if parts[0] == guest_id:
parts[1] = guest_name if guest_name else parts[1]
parts[2] = room_type if room_type else parts[2]
parts[3] = check_in_date if check_in_date else parts[3]
parts[4] = check_out_date if check_out_date else parts[4]
parts[5] = amount_due if amount_due else parts[5]
updated = True
file.write(",".join(parts) + "\n")
if updated:
print("Booking details updated successfully!")
else:
print("Booking not found!")
except FileNotFoundError:
print("No records found! Initialize the system first.")
# Function to delete a booking record
def delete_booking(guest_id):
try:
with open(FILE_NAME, "r") as file:
lines = file.readlines()
deleted = False
with open(FILE_NAME, "w") as file:
for line in lines:
if not line.startswith(guest_id + ","):
file.write(line)
else:
deleted = True
if deleted:
print("Booking record deleted successfully!")
else:
print("Booking not found!")
except FileNotFoundError:
print("No records found! Initialize the system first.")
# Function to generate a bill for a guest
def generate_bill(guest_id, amount):
try:
with open(FILE_NAME, "r") as file:
lines = file.readlines()
updated = False
with open(FILE_NAME, "w") as file:
for line in lines:
parts = line.strip().split(",")
if parts[0] == guest_id:
parts[5] = str(float(parts[5]) + float(amount)) # Update the AmountDue
updated = True
file.write(",".join(parts) + "\n")
if updated:
print(f"Bill of {amount} added successfully for Guest ID {guest_id}!")
else:
print("Booking not found!")
except FileNotFoundError:
print("No records found! Initialize the system first.")
# Main program
if __name__ == "__main__":
initialize_file()
while True:
print("\nHotel Management System")
print("1. Add Guest Booking")
print("2. View All Bookings")
print("3. Search Booking")
print("4. Update Booking")
print("5. Delete Booking")
print("6. Generate Bill for Guest")
print("7. Exit")
choice = input("Enter your choice: ")
if choice == "1":
guest_id = input("Enter Guest ID: ")
guest_name = input("Enter Guest Name: ")
room_type = input("Enter Room Type (e.g., Single, Double, Suite): ")
check_in_date = input("Enter Check-In Date (YYYY-MM-DD): ")
check_out_date = input("Enter Check-Out Date (YYYY-MM-DD): ")
amount_due = input("Enter Amount Due: ")
add_booking(guest_id, guest_name, room_type, check_in_date, check_out_date,
amount_due)
elif choice == "2":
view_bookings()
elif choice == "3":
guest_id = input("Enter Guest ID to search: ")
search_booking(guest_id)
elif choice == "4":
guest_id = input("Enter Guest ID to update: ")
guest_name = input("Enter new Guest Name (leave blank to skip): ")
room_type = input("Enter new Room Type (leave blank to skip): ")
check_in_date = input("Enter new Check-In Date (leave blank to skip): ")
check_out_date = input("Enter new Check-Out Date (leave blank to skip): ")
amount_due = input("Enter new Amount Due (leave blank to skip): ")
update_booking(guest_id, guest_name, room_type, check_in_date, check_out_date,
amount_due)
elif choice == "5":
guest_id = input("Enter Guest ID to delete: ")
delete_booking(guest_id)
elif choice == "6":
guest_id = input("Enter Guest ID for bill generation: ")
amount = input("Enter Bill Amount: ")
generate_bill(guest_id, amount)
elif choice == "7":
print("Exiting...")
break
else:
print("Invalid choice! Please try again.")