import tkinter as tk from tkinter import messagebox from datetime import datetime class Room: def __init__(self, room_number, room_type, rate, availability=True): self.room_number = room_number self.room_type = room_type self.rate = rate self.availability = availability class Reservation: def __init__(self, room, start_date, end_date): self.room = room self.start_date = start_date self.end_date = end_date self.total_cost = self.calculate_total_cost() def calculate_total_cost(self): days = (self.end_date - self.start_date).days + 1 return days * self.room.rate class Hotel: def __init__(self, name): self.name = name self.rooms = [] self.reservations = [] def add_room(self, room): self.rooms.append(room) def check_availability(self, start_date, end_date): available_rooms = [r for r in self.rooms if r.availability and not any((start_date <= res.start_date <= end_date or start_date <= res.end_date <= end_date) for res in self.reservations if res.room == r)] return available_rooms def make_reservation(self, room, start_date, end_date): reservation = Reservation(room, start_date, end_date) self.reservations.append(reservation) room.availability = False return reservation def cancel_reservation(self, reservation): self.reservations.remove(reservation) reservation.room.availability = True class HotelApp(tk.Tk): def __init__(self): super().__init__() self.hotel = Hotel("Python Inn") self.title("Hotel Management System") self.geometry("400x400") self.reservation_window = None self.cancel_window = None self.rooms = [ Room(101, "single", 100), Room(102, "double", 150), Room(103, "suite", 200) ] for room in self.rooms: self.hotel.add_room(room) self.create_widgets() def create_widgets(self): Label(self, text="Hotel Management System", font=("Arial", 16)).pack(pady=10) Button(self, text="Check Availability", command=self.show_availability_window).pack(pady=5) Button(self, text="Make Reservation", command=self.show_reservation_window).pack(pady=5) Button(self, text="Cancel Reservation", command=self.show_cancel_window).pack(pady=5) Button(self, text="Exit", command=self.quit).pack(pady=5) def show_availability_window(self): self.reservation_window = tk.Toplevel(self) self.reservation_window.title("Check Availability") Label(self.reservation_window, text="Check-in Date (YYYY-MM-DD)").pack() self.checkin_entry = Entry(self.reservation_window) self.checkin_entry.pack() Label(self.reservation_window, text="Check-out Date (YYYY-MM-DD)").pack() self.checkout_entry = Entry(self.reservation_window) self.checkout_entry.pack() Button(self.reservation_window, text="Check", command=self.check_and_display).pack() def check_and_display(self): try: start_date = datetime.strptime(self.checkin_entry.get(), "%Y-%m-%d").date() end_date = datetime.strptime(self.checkout_entry.get(), "%Y-%m-%d").date() available_rooms = self.hotel.check_availability(start_date, end_date) self.display_rooms(available_rooms) except ValueError: messagebox.showerror("Error", "Invalid date format. Please use YYYY-MM-DD.") def display_rooms(self, rooms): if rooms: result_text = "\n".join([f"Room {room.room_number} - {room.room_type} - ${room.rate}/night" for room in rooms]) else: result_text = "No rooms available for selected dates." self.result_window = tk.Toplevel(self) Label(self.result_window, text=result_text).pack() def show_reservation_window(self): self.reservation_window = tk.Toplevel(self) self.reservation_window.title("Make Reservation") Label(self.reservation_window, text="Room Number").pack() self.room_number_entry = Entry(self.reservation_window) self.room_number_entry.pack() Label(self.reservation_window, text="Check-in Date (YYYY-MM-DD)").pack() self.checkin_entry = Entry(self.reservation_window) self.checkin_entry.pack() Label(self.reservation_window, text="Check-out Date (YYYY-MM-DD)").pack() self.checkout_entry = Entry(self.reservation_window) self.checkout_entry.pack() Button(self.reservation_window, text="Reserve", command=self.make_reservation).pack() def make_reservation(self): try: room_number = int(self.room_number_entry.get()) room = next((r for r in self.rooms if r.room_number == room_number), None) if not room: messagebox.showerror("Error", "Room not found.") return start_date = datetime.strptime(self.checkin_entry.get(), "%Y-%m-%d").date() end_date = datetime.strptime(self.checkout_entry.get(), "%Y-%m-%d").date() if self.hotel.make_reservation(room, start_date, end_date): messagebox.showinfo("Success", "Reservation made successfully.") else: messagebox.showerror("Error", "Reservation failed. Room not available.") except Exception as e: messagebox.showerror("Error", str(e)) def show_cancel_window(self): self.cancel_window = tk.Toplevel(self) self.cancel_window.title("Cancel Reservation") Label(self.cancel_window, text="Room Number").pack() self.cancel_room_number_entry = Entry(self.cancel_window) self.cancel_room_number_entry.pack() Label(self.cancel_window, text="Check-in Date (YYYY-MM-DD)").pack() self.cancel_checkin_entry = Entry(self.cancel_window) self.cancel_checkin_entry.pack() Button(self.cancel_window, text="Cancel", command=self.cancel_selected_reservation).pack() def cancel_selected_reservation(self): try: room_number = int(self.cancel_room_number_entry.get()) start_date_str = self.cancel_checkin_entry.get() if start_date_str: start_date = datetime.strptime(start_date_str, "%Y-%m-%d").date() matching_reservations = [res for res in self.hotel.reservations if res.room.room_number == room_number and res.start_date == start_date] if matching_reservations: self.hotel.cancel_reservation(matching_reservations[0]) messagebox.showinfo("Success", "Reservation cancelled.") else: messagebox.showerror("Error", "Reservation not found.") else: messagebox.showerror("Error", "Please provide a check-in date.") except Exception as e: messagebox.showerror("Error", str(e)) if __name__ == "__main__": app = HotelApp() app.mainloop()