Python写一个高铁购票系统,有一等座,二等座,有车次,有登录注册系统,有余票,可以改签和退票,有订单详情,
时间: 2024-03-10 07:46:32 浏览: 66
好的,这是一个比较复杂的系统,需要分多个模块实现。下面是一个大致的代码框架,你可以在此基础上进行开发。
## 1. 登录注册系统
首先需要实现一个登录注册系统,用于用户身份验证和管理。可以使用MySQL等数据库进行存储和查询用户信息。
```python
import mysql.connector
# 连接数据库
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)
# 注册功能
def register(username, password):
mycursor = mydb.cursor()
sql = "INSERT INTO users (username, password) VALUES (%s, %s)"
val = (username, password)
mycursor.execute(sql, val)
mydb.commit()
# 登录功能
def login(username, password):
mycursor = mydb.cursor()
sql = "SELECT * FROM users WHERE username = %s AND password = %s"
val = (username, password)
mycursor.execute(sql, val)
user = mycursor.fetchone()
if user is not None:
return True
else:
return False
```
## 2. 购票系统
实现购票系统需要考虑到多个因素,如余票、车次等。可以将车次信息存储在数据库中,每次购票时更新余票数量。
```python
# 连接数据库
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)
# 获取车次信息
def get_train_info(train_no):
mycursor = mydb.cursor()
sql = "SELECT * FROM trains WHERE train_no = %s"
val = (train_no,)
mycursor.execute(sql, val)
train_info = mycursor.fetchone()
return train_info
# 购票功能
def buy_ticket(train_no, seat_type, user_id):
train_info = get_train_info(train_no)
if train_info is None:
return "该车次不存在"
if seat_type == "1st":
if train_info[2] <= 0:
return "一等座已售完"
else:
sql = "UPDATE trains SET 1st_seat = %s WHERE train_no = %s"
val = (train_info[2]-1, train_no)
else:
if train_info[3] <= 0:
return "二等座已售完"
else:
sql = "UPDATE trains SET 2nd_seat = %s WHERE train_no = %s"
val = (train_info[3]-1, train_no)
mycursor = mydb.cursor()
mycursor.execute(sql, val)
mydb.commit()
# 保存订单信息
sql = "INSERT INTO orders (train_no, seat_type, user_id) VALUES (%s, %s, %s)"
val = (train_no, seat_type, user_id)
mycursor.execute(sql, val)
mydb.commit()
return "购票成功"
```
## 3. 改签和退票
改签和退票功能与购票类似,只需更新余票数量或删除订单即可。
```python
# 改签功能
def change_ticket(order_id, train_no, seat_type):
train_info = get_train_info(train_no)
if train_info is None:
return "该车次不存在"
if seat_type == "1st":
if train_info[2] <= 0:
return "一等座已售完"
else:
sql = "UPDATE trains SET 1st_seat = %s WHERE train_no = %s"
val = (train_info[2]-1, train_no)
else:
if train_info[3] <= 0:
return "二等座已售完"
else:
sql = "UPDATE trains SET 2nd_seat = %s WHERE train_no = %s"
val = (train_info[3]-1, train_no)
mycursor = mydb.cursor()
mycursor.execute(sql, val)
mydb.commit()
# 更新订单信息
sql = "UPDATE orders SET train_no = %s, seat_type = %s WHERE id = %s"
val = (train_no, seat_type, order_id)
mycursor.execute(sql, val)
mydb.commit()
return "改签成功"
# 退票功能
def refund_ticket(order_id):
mycursor = mydb.cursor()
# 获取订单信息
sql = "SELECT * FROM orders WHERE id = %s"
val = (order_id,)
mycursor.execute(sql, val)
order_info = mycursor.fetchone()
if order_info is None:
return "该订单不存在"
train_no = order_info[1]
seat_type = order_info[2]
# 更新余票数量
train_info = get_train_info(train_no)
if seat_type == "1st":
sql = "UPDATE trains SET 1st_seat = %s WHERE train_no = %s"
val = (train_info[2]+1, train_no)
else:
sql = "UPDATE trains SET 2nd_seat = %s WHERE train_no = %s"
val = (train_info[3]+1, train_no)
mycursor.execute(sql, val)
mydb.commit()
# 删除订单
sql = "DELETE FROM orders WHERE id = %s"
val = (order_id,)
mycursor.execute(sql, val)
mydb.commit()
return "退票成功"
```
## 4. 订单详情
订单详情可以查询用户的订单信息,并显示相应的车次和座位类型。
```python
# 订单详情功能
def get_order_info(user_id):
mycursor = mydb.cursor()
sql = "SELECT orders.id, trains.train_no, orders.seat_type FROM orders INNER JOIN trains ON orders.train_no = trains.train_no WHERE user_id = %s"
val = (user_id,)
mycursor.execute(sql, val)
orders = mycursor.fetchall()
for order in orders:
print("订单号:", order[0])
print("车次:", order[1])
print("座位类型:", order[2])
```
以上是一个简单的高铁购票系统的代码框架,你可以根据自己的需求进行修改和完善。
阅读全文