管理系统

 2025-06-14  阅读 1  评论 0

摘要:python
import json
import os
from datetime import datetime
class Customer:
def __init__(self, n

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})

  • {pany}")
  • 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. 包含:

  • 唯一ID(基于时间戳生成)
  • 姓名
  • 电话
  • 邮箱
  • 公司
  • 备注
  • 创建/更新时间
  • 3. 主要功能:

  • 添加客户
  • 显示客户列表
  • 搜索客户(支持姓名、电话、邮箱、公司等多字段搜索)
  • 修改
  • 删除客户
  • 数据自动保存
  • 4. 输入验证:

  • 必填字段检查
  • 数据类型验证
  • 5. 用户界面:

  • 清晰的命令行菜单
  • 表格化数据显示
  • 操作确认提示
  • 使用方法:

    1. 将代码保存为 `customer_system.py`

    2. 通过命令行运行:`python customer_system.py`

    3. 按照菜单提示进行操作

    系统会自动创建 `customers.json` 文件来存储数据。所有操作都会实时保存到文件中,确保数据安全。

    可以根据需要扩展以下功能:

  • 增加更多客户字段
  • 实现数据导入/导出
  • 添加用户权限管理
  • 增加统计报表功能
  • 实现多条件组合搜索
  • 添加数据备份功能

    版权声明: 知妳网保留所有权利,部分内容为网络收集,如有侵权,请联系QQ793061840删除,添加请注明来意。

    原文链接:https://www.6g9.cn/qwsh/ddfc4AD5TVVFVDw.html

    标签:管理系统

    发表评论:

    关于我们
    知妳网是一个专注于知识成长与生活品质的温暖社区,致力于提供情感共鸣、实用资讯与贴心服务。在这里,妳可以找到相关的知识、专业的建议,以及提升自我的优质内容。无论是职场困惑、情感心事,还是时尚美妆、健康生活,知妳网都能精准匹配妳的需求,陪伴妳的每一步成长。因为懂妳,所以更贴心——知妳网,做妳最知心的伙伴!
    联系方式
    电话:
    地址:广东省中山市
    Email:admin@qq.com

    Copyright © 2022 知妳网 Inc. 保留所有权利。 Powered by

    页面耗时0.1620秒, 内存占用1.7 MB, 访问数据库19次