python机票订购系统
时间: 2023-11-15 22:46:33 浏览: 30
好的,一个简单的机票订购系统可以包含以下功能:
1. 用户注册和登录功能
2. 显示机票信息,包括航班号、航班日期、起飞时间、到达时间、出发地、目的地和票价等信息
3. 用户选择航班、座位类型和数量
4. 用户填写个人信息和联系方式
5. 用户确认订单并完成支付
下面是一个简单的示例代码,你可以参考一下:
```python
import random
class Flight:
def __init__(self, number, date, departure, arrival, departure_time, arrival_time, price):
self.number = number
self.date = date
self.departure = departure
self.arrival = arrival
self.departure_time = departure_time
self.arrival_time = arrival_time
self.price = price
self.seats = {'Economy': 100, 'Business': 20, 'First Class': 5}
def display_info(self):
print('Flight Number:', self.number)
print('Date:', self.date)
print('Departure:', self.departure)
print('Arrival:', self.arrival)
print('Departure Time:', self.departure_time)
print('Arrival Time:', self.arrival_time)
print('Price:', self.price)
print('Available Seats:')
for seat_type, num_seats in self.seats.items():
print(seat_type, ':', num_seats)
def book_ticket(self, seat_type, num_tickets):
if seat_type not in self.seats:
print('Invalid seat type.')
return False
if self.seats[seat_type] < num_tickets:
print('Not enough seats available.')
return False
self.seats[seat_type] -= num_tickets
return True
class User:
def __init__(self, name, email, phone):
self.name = name
self.email = email
self.phone = phone
def display_info(self):
print('Name:', self.name)
print('Email:', self.email)
print('Phone:', self.phone)
class BookingSystem:
def __init__(self):
self.flights = []
self.users = []
self.bookings = {}
def add_flight(self, flight):
self.flights.append(flight)
def display_flights(self):
for flight in self.flights:
flight.display_info()
def register_user(self, name, email, phone):
user = User(name, email, phone)
self.users.append(user)
return user
def login(self, email):
for user in self.users:
if user.email == email:
return user
return None
def book_ticket(self, user, flight, seat_type, num_tickets):
if flight.book_ticket(seat_type, num_tickets):
booking_id = random.randint(1000, 9999)
while booking_id in self.bookings:
booking_id = random.randint(1000, 9999)
self.bookings[booking_id] = {'user': user, 'flight': flight, 'seat_type': seat_type, 'num_tickets': num_tickets}
return booking_id
else:
return None
def display_booking_info(self, booking_id):
if booking_id in self.bookings:
booking = self.bookings[booking_id]
booking['user'].display_info()
booking['flight'].display_info()
print('Seat Type:', booking['seat_type'])
print('Number of Tickets:', booking['num_tickets'])
else:
print('Invalid Booking ID.')
```
这个代码中包含了三个类:Flight、User 和 BookingSystem。Flight 类存储航班信息和座位信息,User 类存储用户信息,BookingSystem 类处理用户操作,包括添加航班、注册用户、登录、预订机票和显示订单信息等。你可以根据实际需求对代码进行修改和扩展。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)