python
import json
import os
from datetime import datetime
class Customer:
def __init__(self, name, phone, email="", company="", notes=""):
self.id = datetime.now.strftime("%Y%m%d%H%M%S") 生成唯一ID
self.name = name
self.phone = phone
self.email = email
pany = company
self.notes = notes
self.created_at = datetime.now.isoformat
self.updated_at = self.created_at
def to_dict(self):
return self.__dict__
class CustomerManager:
def __init__(self, file_path="customers.json"):
self.file_path = file_path
self.customers = self._load_data
def _load_data(self):
if os.path.exists(self.file_path):
with open(self.file_path, 'r') as f:
data = json.load(f)
return [Customer(c) for c in data]
return []
def _save_data(self):
data = [c.to_dict for c in self.customers]
with open(self.file_path, 'w') as f:
json.dump(data, f, indent=2)
def add_customer(self):
print("
添加新客户")
name = input("姓名: ").strip
phone = input("电话: ").strip
email = input("邮箱(可选): ").strip
company = input("公司(可选): ").strip
notes = input("备注(可选): ").strip
if not name or not phone:
print("错误:姓名和电话为必填项")
return
new_customer = Customer(name, phone, email, company, notes)
self.customers.append(new_customer)
self._save_data
print("客户添加成功!")
def list_customers(self):
print("
客户列表")
print("-" 80)
print(f"{'ID':<15} {'姓名':<10} {'电话':<15} {'邮箱':<20} {'公司':<15}")
print("-" 80)
for customer in self.customers:
print(f"{customer.id:<15} {customer.name:<10} {customer.phone:<15}
f"{customer.email:<20} {pany:<15}")
print(f"
共 {len(self.customers)} 位客户")
def search_customers(self):
keyword = input("
请输入搜索关键字: ").lower.strip
results = []
for customer in self.customers:
if (keyword in customer.name.lower or
keyword in customer.phone or
keyword in customer.email.lower):
results.append(customer)
if results:
print(f"
找到 {len(results)} 条结果:")
for i, c in enumerate(results, 1):
print(f"{i}. {c.name} ({c.phone})
else:
print("未找到匹配的客户")
def edit_customer(self):
customer_id = input("
请输入要修改的客户ID: ").strip
customer = next((c for c in self.customers if c.id == customer_id), None)
if not customer:
print("未找到该客户")
return
print("
当前信息:")
print(f"1. 姓名: {customer.name}")
print(f"2. 电话: {customer.phone}")
print(f"3. 邮箱: {customer.email}")
print(f"4. 公司: {pany}")
print(f"5. 备注: {customer.notes}")
try:
field = int(input("请选择要修改的字段(1-5): "))
new_value = input("请输入新值: ").strip
if field == 1:
if not new_value:
raise ValueError("姓名不能为空")
customer.name = new_value
elif field == 2:
if not new_value:
raise ValueError("电话不能为空")
customer.phone = new_value
elif field == 3:
customer.email = new_value
elif field == 4:
pany = new_value
elif field == 5:
customer.notes = new_value
else:
print("无效的选项")
return
customer.updated_at = datetime.now.isoformat
self._save_data
print("修改成功!")
except ValueError as e:
print(f"错误: {e}")
def delete_customer(self):
customer_id = input("
请输入要删除的客户ID: ").strip
customer = next((c for c in self.customers if c.id == customer_id), None)
if customer:
confirm = input(f"确认删除 {customer.name} 吗?(y/n): ").lower
if confirm == 'y':
self.customers.remove(customer)
self._save_data
print("客户已删除")
else:
print("未找到该客户")
def main:
manager = CustomerManager
while True:
print("
管理系统")
print("1. 添加客户")
print("2. 显示客户列表")
print("3. 搜索客户")
print("4. 修改")
print("5. 删除客户")
print("0. 退出系统")
choice = input("请选择操作: ").strip
if choice == '1':
manager.add_customer
elif choice == '2':
manager.list_customers
elif choice == '3':
manager.search_customers
elif choice == '4':
manager.edit_customer
elif choice == '5':
manager.delete_customer
elif choice == '0':
print("感谢使用,再见!")
break
else:
print("无效的输入,请重新选择")
if __name__ == "__main__":
main
主要功能特点:
1. 数据持久化:使用JSON文件存储数据
2. 包含:
3. 主要功能:
4. 输入验证:
5. 用户界面:
使用方法:
1. 将代码保存为 `customer_system.py`
2. 通过命令行运行:`python customer_system.py`
3. 按照菜单提示进行操作
系统会自动创建 `customers.json` 文件来存储数据。所有操作都会实时保存到文件中,确保数据安全。
可以根据需要扩展以下功能:
添加数据备份功能
版权声明: 知妳网保留所有权利,部分内容为网络收集,如有侵权,请联系QQ793061840删除,添加请注明来意。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态