From 1b778dbb7b828e9e6b7fb8c1fb32cbbeaa9aed83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=94=90=E6=96=87=E9=9B=AF?= <3363402744@qq.com> Date: Wed, 29 May 2024 20:08:01 +0800 Subject: [PATCH] main --- 2.py | 36 ------------- main.py | 162 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 162 insertions(+), 36 deletions(-) delete mode 100644 2.py create mode 100644 main.py diff --git a/2.py b/2.py deleted file mode 100644 index f325d86..0000000 --- a/2.py +++ /dev/null @@ -1,36 +0,0 @@ -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 \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..337d130 --- /dev/null +++ b/main.py @@ -0,0 +1,162 @@ +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): + tk.Label(self, text="Hotel Management System", font=("Arial", 16)).pack(pady=10) + tk.Button(self, text="Check Availability", command=self.show_availability_window).pack(pady=5) + tk.Button(self, text="Make Reservation", command=self.show_reservation_window).pack(pady=5) + tk.Button(self, text="Cancel Reservation", command=self.show_cancel_window).pack(pady=5) + tk.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") + tk.Label(self.reservation_window, text="Check-in Date (YYYY-MM-DD)").pack() + self.checkin_entry = tk.Entry(self.reservation_window) + self.checkin_entry.pack() + tk.Label(self.reservation_window, text="Check-out Date (YYYY-MM-DD)").pack() + self.checkout_entry = tk.Entry(self.reservation_window) + self.checkout_entry.pack() + tk.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) + tk.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") + tk.Label(self.reservation_window, text="Room Number").pack() + self.room_number_entry = tk.Entry(self.reservation_window) + self.room_number_entry.pack() + tk.Label(self.reservation_window, text="Check-in Date (YYYY-MM-DD)").pack() + self.checkin_entry = tk.Entry(self.reservation_window) + self.checkin_entry.pack() + tk.Label(self.reservation_window, text="Check-out Date (YYYY-MM-DD)").pack() + self.checkout_entry = tk.Entry(self.reservation_window) + self.checkout_entry.pack() + tk.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") + tk.Label(self.cancel_window, text="Room Number").pack() + self.cancel_room_number_entry = tk.Entry(self.cancel_window) + self.cancel_room_number_entry.pack() + tk.Label(self.cancel_window, text="Check-in Date (YYYY-MM-DD)").pack() + self.cancel_checkin_entry = tk.Entry(self.cancel_window) + self.cancel_checkin_entry.pack() + tk.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() \ No newline at end of file