帮我写一个火车售票系统代码
时间: 2023-08-28 14:22:27 浏览: 160
好的,下面是一个简单的火车售票系统的 Python 代码实现,供您参考:
``` python
class Train:
def __init__(self, train_num, start_station, end_station, departure_time, duration, price):
self.train_num = train_num
self.start_station = start_station
self.end_station = end_station
self.departure_time = departure_time
self.duration = duration
self.price = price
class Ticket:
def __init__(self, train_num, start_station, end_station, departure_time, duration, price, num):
self.train_num = train_num
self.start_station = start_station
self.end_station = end_station
self.departure_time = departure_time
self.duration = duration
self.price = price
self.num = num
class TicketOffice:
def __init__(self, train_list):
self.train_list = train_list
self.ticket_list = []
def search_train(self, start_station, end_station):
result_list = []
for train in self.train_list:
if train.start_station == start_station and train.end_station == end_station:
result_list.append(train)
return result_list
def buy_ticket(self, train_num, num):
for train in self.train_list:
if train.train_num == train_num:
ticket = Ticket(train.train_num, train.start_station, train.end_station, train.departure_time, train.duration, train.price, num)
self.ticket_list.append(ticket)
return ticket
return None
def show_ticket(self):
for ticket in self.ticket_list:
print("Train No.: %s, Start Station: %s, End Station: %s, Departure Time: %s, Duration: %s, Price: %s, Num: %s" % (ticket.train_num, ticket.start_station, ticket.end_station, ticket.departure_time, ticket.duration, ticket.price, ticket.num))
train_list = [Train("G1001", "Beijing", "Shanghai", "08:00", "5h", 500), Train("G1002", "Shanghai", "Beijing", "10:00", "5h", 500), Train("G1003", "Beijing", "Guangzhou", "12:00", "8h", 800)]
office = TicketOffice(train_list)
while True:
print("1. Search train")
print("2. Buy ticket")
print("3. Show ticket")
print("4. Exit")
choice = input("Please enter your choice: ")
if choice == "1":
start_station = input("Please enter start station: ")
end_station = input("Please enter end station: ")
result_list = office.search_train(start_station, end_station)
for train in result_list:
print("Train No.: %s, Start Station: %s, End Station: %s, Departure Time: %s, Duration: %s, Price: %s" % (train.train_num, train.start_station, train.end_station, train.departure_time, train.duration, train.price))
elif choice == "2":
train_num = input("Please enter train number: ")
num = input("Please enter number of tickets: ")
ticket = office.buy_ticket(train_num, num)
if ticket is not None:
print("Buy ticket successfully!")
else:
print("Sorry, the train is not available.")
elif choice == "3":
office.show_ticket()
elif choice == "4":
break
else:
print("Invalid choice, please try again.")
```
该系统包含三个类:Train、Ticket 和 TicketOffice。Train 类表示火车,包括火车号、起点站、终点站、出发时间、行车时间和票价等属性。Ticket 类表示车票,包括火车号、起点站、终点站、出发时间、行车时间、票价和购买数量等属性。TicketOffice 类表示售票处,包括火车列表和车票列表等属性,以及搜索火车、购买车票和展示车票等方法。
在程序运行时,用户可以选择进行搜索火车、购买车票和展示车票等操作。具体实现时,可以根据用户的选择调用不同的方法,从而实现相应的功能。
阅读全文