用Python写一个简单的,查询车票信息,包括时间,张数,车次等信息,购票时输入车次,张数,显示购买成功,更新车票张数信息,改签退票也要显示修改后的车票信息等的一个程序
时间: 2024-04-16 13:27:51 浏览: 92
以下是一个简单的示例程序,用于查询、购买、改签和退票车票信息。请注意,这只是一个基础的示例,实际应用中可能需要更复杂的逻辑和数据存储方式。
```python
class Ticket:
def __init__(self, train_number, date, available_tickets):
self.train_number = train_number
self.date = date
self.available_tickets = available_tickets
class TicketSystem:
def __init__(self):
self.tickets = []
def add_ticket(self, train_number, date, available_tickets):
ticket = Ticket(train_number, date, available_tickets)
self.tickets.append(ticket)
def find_ticket(self, train_number, date):
for ticket in self.tickets:
if ticket.train_number == train_number and ticket.date == date:
return ticket
return None
def book_ticket(self, train_number, date, num_tickets):
ticket = self.find_ticket(train_number, date)
if ticket is None:
print("该车次不存在或日期错误")
return
if num_tickets > ticket.available_tickets:
print("余票不足")
return
ticket.available_tickets -= num_tickets
print("购买成功")
def modify_ticket(self, train_number, date, num_tickets):
ticket = self.find_ticket(train_number, date)
if ticket is None:
print("该车次不存在或日期错误")
return
ticket.available_tickets += num_tickets
print("修改成功")
def refund_ticket(self, train_number, date, num_tickets):
ticket = self.find_ticket(train_number, date)
if ticket is None:
print("该车次不存在或日期错误")
return
ticket.available_tickets += num_tickets
print("退票成功")
# 示例用法
ticket_system = TicketSystem()
ticket_system.add_ticket("G1234", "2022-01-01", 100)
ticket_system.book_ticket("G1234", "2022-01-01", 2)
ticket_system.modify_ticket("G1234", "2022-01-01", 1)
ticket_system.refund_ticket("G1234", "2022-01-01", 1)
```
这个程序使用了两个类:`Ticket` 表示车票信息,包括车次、日期和可用张数;`TicketSystem` 是一个车票管理系统,负责添加、查询、购买、改签和退票等操作。
你可以根据实际需求,进一步完善和扩展这个程序。例如,可以引入用户系统、持久化数据存储等功能。
阅读全文