火车购票系统 登录注册功能实现.用户登录管理员登录 用户购票管理员操作: 1.界面显示: 进入界面应当有选项1添加车票,选项2删除车票,选项3更新车票,选项4查询车票,选项 5退出登录(基础选项)车票信息应当以1,2,3列表形式直接展示。添加车票功能实现,删除车票功能实现,更新车票功能实现,查询车票功能实现在进行任意操作时,按任意字符不会导致系统崩溃,代码实现,必须用类实现(含属性,方法)
时间: 2023-11-18 13:02:11 浏览: 36
好的,这是一个较为复杂的问题,需要进行代码实现。以下是一个 Python 代码实现示例,用于实现火车购票系统的登录和注册功能,以及管理员的操作功能:
```python
# 用户类
class User:
def __init__(self, username, password):
self.username = username
self.password = password
self.is_admin = False # 是否为管理员
def __repr__(self):
return f"User('{self.username}', '{self.password}', {self.is_admin})"
# 管理员类,继承自用户类
class Admin(User):
def __init__(self, username, password):
super().__init__(username, password)
self.is_admin = True
# 车票类
class Ticket:
def __init__(self, id, name, price, count):
self.id = id
self.name = name
self.price = price
self.count = count
def __repr__(self):
return f"Ticket('{self.id}', '{self.name}', {self.price}, {self.count})"
# 火车购票系统类
class TrainTicketSystem:
def __init__(self):
self.users = [] # 所有用户列表
self.admins = [] # 所有管理员列表
self.tickets = [] # 所有车票列表
self.current_user = None # 当前登录用户
# 注册功能实现
def register(self, username, password):
user = User(username, password)
if user in self.users:
print("注册失败,该用户已存在!")
return
self.users.append(user)
print("注册成功!")
# 登录功能实现
def login(self, username, password):
for user in self.users:
if user.username == username and user.password == password:
self.current_user = user
print("登录成功!")
return
print("登录失败,用户名或密码错误!")
# 管理员登录功能实现
def admin_login(self, username, password):
for admin in self.admins:
if admin.username == username and admin.password == password:
self.current_user = admin
print("管理员登录成功!")
return
print("管理员登录失败,用户名或密码错误!")
# 添加车票功能实现
def add_ticket(self, id, name, price, count):
if not self.current_user or not self.current_user.is_admin:
print("添加车票失败,需要管理员权限!")
return
ticket = Ticket(id, name, price, count)
self.tickets.append(ticket)
print("添加车票成功!")
# 删除车票功能实现
def remove_ticket(self, id):
if not self.current_user or not self.current_user.is_admin:
print("删除车票失败,需要管理员权限!")
return
for ticket in self.tickets:
if ticket.id == id:
self.tickets.remove(ticket)
print("删除车票成功!")
return
print("删除车票失败,不存在该车票!")
# 更新车票功能实现
def update_ticket(self, id, name=None, price=None, count=None):
if not self.current_user or not self.current_user.is_admin:
print("更新车票失败,需要管理员权限!")
return
for ticket in self.tickets:
if ticket.id == id:
if name:
ticket.name = name
if price:
ticket.price = price
if count:
ticket.count = count
print("更新车票成功!")
return
print("更新车票失败,不存在该车票!")
# 查询车票功能实现
def query_ticket(self):
if not self.tickets:
print("当前没有车票记录!")
return
print("车票信息:")
for ticket in self.tickets:
print(ticket)
# 退出登录功能实现
def logout(self):
self.current_user = None
print("退出登录成功!")
```
示例代码中包含了注册、登录、管理员操作(添加车票、删除车票、更新车票、查询车票)、退出登录等功能的实现。在代码中,我们定义了用户类、管理员类、车票类和火车购票系统类。其中,用户类和管理员类继承自一个基础的用户类,而车票类则是一个独立的类。在火车购票系统类中,我们通过列表存储所有用户、管理员和车票记录,以及记录当前登录用户的信息。登录和注册功能的实现比较简单,主要是遍历用户列表查找匹配的用户信息。管理员操作需要检查当前登录用户是否为管理员,以此判断是否有权限进行操作。具体的功能实现比较清晰,代码中有详细的注释说明。
相关推荐
![](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)