代码一、车票(车次编号、起点站、终点站、发车时间、到达时间、价格、剩余票数)查询 1、站站查询 2、车次查询
时间: 2023-05-14 20:06:57 浏览: 145
ChatGPT: 您好,以下是车票查询的代码:
```
class Train:
def __init__(self, train_no, start_station, end_station, departure_time, arrival_time, price, remaining_tickets):
self.train_no = train_no
self.start_station = start_station
self.end_station = end_station
self.departure_time = departure_time
self.arrival_time = arrival_time
self.price = price
self.remaining_tickets = remaining_tickets
class TrainTicketSystem:
def __init__(self):
self.trains = []
def add_train(self, train):
self.trains.append(train)
def search_by_station(self, start_station, end_station):
result = []
for train in self.trains:
if train.start_station == start_station and train.end_station == end_station:
result.append(train)
return result
def search_by_train_no(self, train_no):
for train in self.trains:
if train.train_no == train_no:
return train
return None
```
您可以通过创建 Train 对象并将其添加到 TrainTicketSystem 中来添加车次信息。search_by_station 方法可以通过起点站和终点站查询车次信息,search_by_train_no 方法可以通过车次编号查询车次信息。
阅读全文