main
唐文雯 6 months ago
parent e342fd2c19
commit 0d1d20fb6b

@ -1,62 +0,0 @@
from sqlalchemy import Column, Integer, String, ForeignKey, DateTime
from sqlalchemy.orm import relationship
from datetime import datetime, timezone
from .room_management import Room, Base # 假设room_management.py在同一包内或根据实际情况调整导入路径
# 预订模型
class Booking(Base):
__tablename__ = 'bookings' # 注意双下划线的正确使用
id = Column(Integer, primary_key=True)
room_id = Column(Integer, ForeignKey('rooms.id'), nullable=False)
guest_name = Column(String, nullable=False)
start_date = Column(DateTime, nullable=False, default=lambda: datetime.now(timezone.utc)) # 使用时区感知的默认值
end_date = Column(DateTime, nullable=False)
room = relationship("Room", back_populates="bookings")
def __repr__(self):
return f"Booking(id={self.id}, room={self.room.room_number}, guest='{self.guest_name}', start_date={self.start_date}, end_date={self.end_date})"
# 初始化Room模型与Booking之间的关系
Room.bookings = relationship("Booking", order_by=Booking.id, back_populates="room")
def make_booking(room_number, guest_name, start_date_str, end_date_str):
"""创建预订"""
from dateutil.parser import parse
try:
start_date = parse(start_date_str).replace(tzinfo=None)
end_date = parse(end_date_str).replace(tzinfo=None)
room = session.query(Room).filter_by(room_number=room_number, status='available').first()
if not room:
return "该房间不可用或不存在。"
new_booking = Booking(guest_name=guest_name, start_date=start_date, end_date=end_date, room=room)
room.status = 'occupied'
session.add(new_booking)
session.commit()
return f"{guest_name} 的预订成功,房间号:{room_number},时间:{start_date}{end_date}"
except Exception as e:
return str(e)
def cancel_booking(booking_id):
"""取消预订"""
booking = session.query(Booking).get(booking_id)
if not booking:
return "预订记录不存在。"
room = booking.room
room.status = 'available'
session.delete(booking)
session.commit()
return f"预订ID为{booking_id}的预订已取消。"
def get_bookings_by_guest(guest_name=None):
"""按客人姓名获取预订记录"""
query = session.query(Booking)
if guest_name:
query = query.filter_by(guest_name=guest_name)
return query.all()
def get_bookings_by_room(room_number):
"""按房间号获取预订记录"""
room = session.query(Room).filter_by(room_number=room_number).first()
if not room:
return []
return room.bookings

@ -1,162 +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
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()

@ -0,0 +1,15 @@
#!/usr/bin/env python
import os
import sys
if __name__ == '__main__':
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'hotel.settings')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)

163
tww.py

@ -1,163 +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
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()
Loading…
Cancel
Save