From 338b7bcf6aeb18a5d25cb3d4820110ce3ee23b88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=94=90=E6=96=87=E9=9B=AF?= <3363402744@qq.com> Date: Tue, 28 May 2024 23:34:52 +0800 Subject: [PATCH] first commit --- 2.py | 36 +++++++++++++++ tww.py | 135 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 168 insertions(+), 3 deletions(-) create mode 100644 2.py diff --git a/2.py b/2.py new file mode 100644 index 0000000..f325d86 --- /dev/null +++ b/2.py @@ -0,0 +1,36 @@ +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/tww.py b/tww.py index 1a4d515..350f27b 100644 --- a/tww.py +++ b/tww.py @@ -1,4 +1,7 @@ -import datetime +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 @@ -11,10 +14,10 @@ class Reservation: self.room = room self.start_date = start_date self.end_date = end_date - self.total_cost = self.calculate_tatal_cost() + self.total_cost = self.calculate_total_cost() def calculate_total_cost(self): - days = (self.end_date - self.start_date).days + days = (self.end_date - self.start_date).days + 1 return days * self.room.rate class Hotel: @@ -32,3 +35,129 @@ class Hotel: 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() \ No newline at end of file