python 手机店代码
时间: 2024-06-24 10:01:35 浏览: 7
在Python中,编写一个手机店的模拟代码可能涉及到创建类来表示不同的商品(如手机),库存管理,顾客购买流程等。这里是一个简化的示例,展示了如何使用类和对象来实现这个概念:
```python
class Phone:
def __init__(self, brand, model, price):
self.brand = brand
self.model = model
self.price = price
self.stock = 0
def display_info(self):
print(f"{self.brand} {self.model} costs {self.price}.")
def purchase(self, quantity, customer):
if quantity <= self.stock:
self.stock -= quantity
customer.purchase_history.append((self.brand, self.model, quantity))
print(f"Purchased! {quantity} {self.brand} {self.model}s from your inventory.")
else:
print("Insufficient stock.")
class Customer:
def __init__(self):
self.purchase_history = []
def show_purchase_history(self):
print("Purchase History:")
for item in self.purchase_history:
print(item)
# 创建实例
iphone_12 = Phone("Apple", "iPhone 12", 999)
customer = Customer()
# 模拟购买
iphone_12.stock = 5 # 假设库存为5台
iphone_12.purchase(2, customer)
# 显示购买历史
customer.show_purchase_history()
```
在这个例子中,`Phone`类代表手机,包含品牌、型号、价格和库存。`purchase`方法用于顾客购买手机,如果库存足够,则减少库存并更新顾客的购买历史。`Customer`类则用于跟踪顾客的购买历史。
你可以根据需要扩展这个代码,例如添加更多功能如库存补充、促销活动等。如果你有特定的问题或者想深入了解某个部分,请告诉我,我会提供更详细的解释。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)