python实现预约系统
时间: 2023-12-04 15:01:12 浏览: 32
以下是一个简单的Python实现预约系统的示例代码:
```python
class Appointment:
def __init__(self, date, start_time, end_time, name):
self.date = date
self.start_time = start_time
self.end_time = end_time
self.name = name
class Schedule:
def __init__(self):
self.appointments = []
def add_appointment(self, appointment):
for a in self.appointments:
if appointment.date == a.date and \
appointment.start_time >= a.start_time and \
appointment.start_time < a.end_time:
return False
self.appointments.append(appointment)
return True
def remove_appointment(self, appointment):
if appointment in self.appointments:
self.appointments.remove(appointment)
return True
else:
return False
def get_appointments_by_date(self, date):
result = []
for a in self.appointments:
if a.date == date:
result.append(a)
return result
schedule = Schedule()
# 添加预约
a1 = Appointment("2021-08-01", "09:00", "10:00", "张三")
if schedule.add_appointment(a1):
print("添加预约成功")
else:
print("添加预约失败")
# 查询某一天的预约
date = "2021-08-01"
appointments = schedule.get_appointments_by_date(date)
if len(appointments) > 0:
print("以下是{}的预约列表:".format(date))
for a in appointments:
print("{} {}-{} {}".format(a.date, a.start_time, a.end_time, a.name))
else:
print("{}没有预约".format(date))
# 取消预约
if schedule.remove_appointment(a1):
print("取消预约成功")
else:
print("取消预约失败")
```
运行结果:
```
添加预约成功
以下是2021-08-01的预约列表:
2021-08-01 09:00-10:00 张三
取消预约成功
```