结合面向对象编程思想,开发“裹裹快投”快递服务控制台程序,可实现用户寄件、取件、查询订单、修改订单、取消订单等功能
时间: 2023-12-10 20:03:02 浏览: 260
好的,让我们一步一步来完成这个程序。
首先,我们需要定义一些类,包括用户类(User)、订单类(Order)、快递服务类(ExpressService)等。
User 类需要包括以下属性:
- 用户名(username)
- 密码(password)
- 姓名(name)
- 手机号码(phone)
还需要包括以下方法:
- 构造方法,用于初始化用户对象
- 获取用户名方法(getUsername)
- 获取密码方法(getPassword)
- 获取姓名方法(getName)
- 获取手机号码方法(getPhone)
代码如下:
```python
class User:
def __init__(self, username, password, name, phone):
self.username = username
self.password = password
self.name = name
self.phone = phone
def getUsername(self):
return self.username
def getPassword(self):
return self.password
def getName(self):
return self.name
def getPhone(self):
return self.phone
```
Order 类需要包括以下属性:
- 订单编号(order_id)
- 寄件人姓名(sender_name)
- 寄件人手机号码(sender_phone)
- 寄件人地址(sender_address)
- 收件人姓名(receiver_name)
- 收件人手机号码(receiver_phone)
- 收件人地址(receiver_address)
- 快递状态(status)
还需要包括以下方法:
- 构造方法,用于初始化订单对象
- 获取订单编号方法(getOrderId)
- 获取寄件人姓名方法(getSenderName)
- 获取寄件人手机号码方法(getSenderPhone)
- 获取寄件人地址方法(getSenderAddress)
- 获取收件人姓名方法(getReceiverName)
- 获取收件人手机号码方法(getReceiverPhone)
- 获取收件人地址方法(getReceiverAddress)
- 获取快递状态方法(getStatus)
- 修改快递状态方法(updateStatus)
代码如下:
```python
class Order:
def __init__(self, order_id, sender_name, sender_phone, sender_address, receiver_name, receiver_phone, receiver_address, status):
self.order_id = order_id
self.sender_name = sender_name
self.sender_phone = sender_phone
self.sender_address = sender_address
self.receiver_name = receiver_name
self.receiver_phone = receiver_phone
self.receiver_address = receiver_address
self.status = status
def getOrderId(self):
return self.order_id
def getSenderName(self):
return self.sender_name
def getSenderPhone(self):
return self.sender_phone
def getSenderAddress(self):
return self.sender_address
def getReceiverName(self):
return self.receiver_name
def getReceiverPhone(self):
return self.receiver_phone
def getReceiverAddress(self):
return self.receiver_address
def getStatus(self):
return self.status
def updateStatus(self, new_status):
self.status = new_status
```
ExpressService 类需要包括以下属性:
- 用户列表(user_list)
- 订单列表(order_list)
还需要包括以下方法:
- 构造方法,用于初始化快递服务对象
- 注册用户方法(registerUser)
- 登录方法(login)
- 寄件方法(send)
- 取件方法(receive)
- 查询订单方法(queryOrder)
- 修改订单方法(updateOrder)
- 取消订单方法(cancelOrder)
代码如下:
```python
class ExpressService:
def __init__(self):
self.user_list = []
self.order_list = []
def registerUser(self, username, password, name, phone):
user = User(username, password, name, phone)
self.user_list.append(user)
print("注册成功!")
def login(self, username, password):
for user in self.user_list:
if user.getUsername() == username and user.getPassword() == password:
print("登录成功!")
return True
print("登录失败,请检查用户名和密码!")
return False
def send(self, sender_name, sender_phone, sender_address, receiver_name, receiver_phone, receiver_address):
order_id = len(self.order_list) + 1
order = Order(order_id, sender_name, sender_phone, sender_address, receiver_name, receiver_phone, receiver_address, "已寄出")
self.order_list.append(order)
print("寄件成功!订单编号为:", order_id)
def receive(self, order_id):
for order in self.order_list:
if order.getOrderId() == order_id:
order.updateStatus("已取件")
print("取件成功!")
return
print("取件失败,请检查订单编号!")
def queryOrder(self, order_id):
for order in self.order_list:
if order.getOrderId() == order_id:
print("订单编号:", order.getOrderId())
print("寄件人姓名:", order.getSenderName())
print("寄件人手机号码:", order.getSenderPhone())
print("寄件人地址:", order.getSenderAddress())
print("收件人姓名:", order.getReceiverName())
print("收件人手机号码:", order.getReceiverPhone())
print("收件人地址:", order.getReceiverAddress())
print("快递状态:", order.getStatus())
return
print("查询失败,请检查订单编号!")
def updateOrder(self, order_id, new_status):
for order in self.order_list:
if order.getOrderId() == order_id:
order.updateStatus(new_status)
print("修改成功!")
return
print("修改失败,请检查订单编号!")
def cancelOrder(self, order_id):
for order in self.order_list:
if order.getOrderId() == order_id:
self.order_list.remove(order)
print("取消成功!")
return
print("取消失败,请检查订单编号!")
```
现在我们已经完成了所有的类的定义,下一步是编写控制台程序。我们可以使用菜单方式来实现用户与程序的交互。
代码如下:
```python
express_service = ExpressService()
while True:
print("裹裹快投快递服务控制台程序")
print("1. 注册用户")
print("2. 登录")
print("3. 寄件")
print("4. 取件")
print("5. 查询订单")
print("6. 修改订单")
print("7. 取消订单")
print("8. 退出")
choice = input("请选择功能:")
if choice == "1":
username = input("请输入用户名:")
password = input("请输入密码:")
name = input("请输入姓名:")
phone = input("请输入手机号码:")
express_service.registerUser(username, password, name, phone)
elif choice == "2":
username = input("请输入用户名:")
password = input("请输入密码:")
express_service.login(username, password)
elif choice == "3":
sender_name = input("请输入寄件人姓名:")
sender_phone = input("请输入寄件人手机号码:")
sender_address = input("请输入寄件人地址:")
receiver_name = input("请输入收件人姓名:")
receiver_phone = input("请输入收件人手机号码:")
receiver_address = input("请输入收件人地址:")
express_service.send(sender_name, sender_phone, sender_address, receiver_name, receiver_phone, receiver_address)
elif choice == "4":
order_id = input("请输入订单编号:")
express_service.receive(int(order_id))
elif choice == "5":
order_id = input("请输入订单编号:")
express_service.queryOrder(int(order_id))
elif choice == "6":
order_id = input("请输入订单编号:")
new_status = input("请输入新的快递状态:")
express_service.updateOrder(int(order_id), new_status)
elif choice == "7":
order_id = input("请输入订单编号:")
express_service.cancelOrder(int(order_id))
elif choice == "8":
print("谢谢使用,再见!")
break
else:
print("输入有误,请重新输入!")
```
到这里,我们就完成了这个控制台程序的开发。用户可以通过菜单选择需要的功能,进行快递服务相关的操作。
阅读全文