假定某民航机场共有 N 个航班,每个航班都有航班号,起始站,终点站,飞 行时间(星期几,几点)和一定的成员定额。 设计一个飞机订票系统,能提供下列服务。 (1)航班信息录入功能(用文件保存) (2)航班信息浏览功能。 (3)查询功能:按航班号查询、按起点站查询、按终点站查询、按飞行时 间查询。 (4)功能选择用菜单实现(菜单可用字符串输出,或用 Tkinter 实现)编写Python代码
时间: 2024-02-11 10:07:59 浏览: 124
好的,我可以提供一份Python代码作为参考,实现上述的飞机订票系统。以下是代码:
```python
import csv
# 定义航班信息类
class Flight:
def __init__(self, flight_no, start, end, day, time, seat):
self.flight_no = flight_no
self.start = start
self.end = end
self.day = day
self.time = time
self.seat = seat
# 定义航班信息存储函数
def save_flight(flight_list):
with open('flight.csv', 'w', newline='') as file:
writer = csv.writer(file)
for flight in flight_list:
writer.writerow([flight.flight_no, flight.start, flight.end, flight.day, flight.time, flight.seat])
# 定义航班信息读取函数
def load_flight():
flight_list = []
with open('flight.csv', 'r') as file:
reader = csv.reader(file)
for row in reader:
flight = Flight(row[0], row[1], row[2], row[3], row[4], row[5])
flight_list.append(flight)
return flight_list
# 定义航班信息浏览函数
def view_flight(flight_list):
for flight in flight_list:
print("航班号:{},起始站:{},终点站:{},飞行时间:星期{},{}点,成员定额:{}".format(flight.flight_no, flight.start, flight.end, flight.day, flight.time, flight.seat))
# 定义按航班号查询函数
def search_flight_by_no(flight_list, flight_no):
result_list = []
for flight in flight_list:
if flight.flight_no == flight_no:
result_list.append(flight)
return result_list
# 定义按起点站查询函数
def search_flight_by_start(flight_list, start):
result_list = []
for flight in flight_list:
if flight.start == start:
result_list.append(flight)
return result_list
# 定义按终点站查询函数
def search_flight_by_end(flight_list, end):
result_list = []
for flight in flight_list:
if flight.end == end:
result_list.append(flight)
return result_list
# 定义按飞行时间查询函数
def search_flight_by_time(flight_list, day, time):
result_list = []
for flight in flight_list:
if flight.day == day and flight.time == time:
result_list.append(flight)
return result_list
# 定义菜单函数
def menu():
print("1. 录入航班信息")
print("2. 浏览航班信息")
print("3. 按航班号查询")
print("4. 按起点站查询")
print("5. 按终点站查询")
print("6. 按飞行时间查询")
print("0. 退出系统")
# 主程序
if __name__ == "__main__":
flight_list = [] # 航班信息列表
while True:
menu() # 显示菜单
choice = input("请输入功能编号:") # 读取用户输入
if choice == '1': # 录入航班信息
flight_no = input("请输入航班号:")
start = input("请输入起始站:")
end = input("请输入终点站:")
day = input("请输入飞行时间(星期几):")
time = input("请输入飞行时间(几点):")
seat = input("请输入成员定额:")
flight = Flight(flight_no, start, end, day, time, seat)
flight_list.append(flight)
save_flight(flight_list)
elif choice == '2': # 浏览航班信息
flight_list = load_flight()
view_flight(flight_list)
elif choice == '3': # 按航班号查询
flight_list = load_flight()
flight_no = input("请输入航班号:")
result_list = search_flight_by_no(flight_list, flight_no)
view_flight(result_list)
elif choice == '4': # 按起点站查询
flight_list = load_flight()
start = input("请输入起点站:")
result_list = search_flight_by_start(flight_list, start)
view_flight(result_list)
elif choice == '5': # 按终点站查询
flight_list = load_flight()
end = input("请输入终点站:")
result_list = search_flight_by_end(flight_list, end)
view_flight(result_list)
elif choice == '6': # 按飞行时间查询
flight_list = load_flight()
day = input("请输入飞行时间(星期几):")
time = input("请输入飞行时间(几点):")
result_list = search_flight_by_time(flight_list, day, time)
view_flight(result_list)
elif choice == '0': # 退出系统
print("感谢使用,再见!")
break
else:
print("输入错误,请重新输入!")
```
这份代码实现了航班信息的录入、浏览、查询和菜单等功能,使用了CSV文件进行航班信息的存储和读取。可以根据具体需求进行修改和扩展。
阅读全文