付费自习室座位预约代码
时间: 2023-11-12 15:04:53 浏览: 41
以下是一个简单的付费自习室座位预约代码示例:
```python
class Seat:
def __init__(self, id):
self.id = id
self.is_available = True
def reserve(self):
self.is_available = False
class StudyRoom:
def __init__(self, id, seat_count, price_per_hour):
self.id = id
self.seats = [Seat(i) for i in range(1, seat_count+1)]
self.price_per_hour = price_per_hour
def reserve_seat(self, seat_id):
seat = self.seats[seat_id-1]
if seat.is_available:
seat.reserve()
print("座位 {} 预定成功!".format(seat_id))
return True
else:
print("座位 {} 已经被预定了!".format(seat_id))
return False
class Payment:
def __init__(self, amount):
self.amount = amount
def process_payment(self):
# 实现支付逻辑
print("支付成功!")
class Booking:
def __init__(self, study_room, seat_id, hours):
self.study_room = study_room
self.seat_id = seat_id
self.hours = hours
self.total_amount = self.hours * self.study_room.price_per_hour
def reserve_seat(self):
success = self.study_room.reserve_seat(self.seat_id)
if success:
self.process_payment()
def process_payment(self):
payment = Payment(self.total_amount)
payment.process_payment()
# 创建一个自习室实例
study_room = StudyRoom(1, 10, 5)
# 预定座位和支付
booking = Booking(study_room, 3, 2) # 预定自习室1的3号座位,2小时
booking.reserve_seat() # 预定座位并支付
```
以上代码仅为示例,实际应用中可能会有更复杂的逻辑和需求。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)