📅  最后修改于: 2023-12-03 15:42:21.978000             🧑  作者: Mango
这是门门IT 2007年的第79题。本题要求实现一个简单的订单系统,支持添加商品,生成订单等基本操作。
class Product:
def __init__(self, id, name, price):
self.id = id
self.name = name
self.price = price
class Order:
def __init__(self, id, customer):
self.id = id
self.customer = customer
self.products = []
def add_product(self, product):
self.products.append(product)
class OrderManager:
def __init__(self):
self.orders = []
def add_product(self, order_id, product):
for order in self.orders:
if order.id == order_id:
order.add_product(product)
return
new_order = Order(order_id, product.customer)
new_order.add_product(product)
self.orders.append(new_order)
def generate_order_id(self):
order_ids = [order.id for order in self.orders]
new_id = max(order_ids) + 1 if order_ids else 1
return new_id
def generate_order(self, customer, products):
new_order_id = self.generate_order_id()
new_order = Order(new_order_id, customer)
for product in products:
new_order.add_product(product)
self.orders.append(new_order)
return new_order_id
def get_order_total_price(self, order_id):
for order in self.orders:
if order.id == order_id:
total_price = sum([product.price for product in order.products])
return total_price
return None
本题要求实现的是一个非常实用的订单管理系统,可以极大地简化商家的工作流程。通过学习本题,可以加深对面向对象编程的理解,熟悉类的设计和属性的使用。