【大模型2609·Python基础·15】综合项目实战:基于面向对象三层架构的客户管理系统

专栏导读:知识融会贯通的阶段性大作。本篇将 Python 基础语法、容器管理、面向对象封装与异常容错应用于实战,按照工业级三层解耦架构(Customer 实体模型层、CustomerService 核心业务逻辑层、CustomerView 用户交互控制台层),手把手实现功能完备的客户信息管理系统。


第15章 客户管理系统

15.1 需求说明

请看《综合案例—客户管理系统.docx》

15.2 代码实现

15.2.1 客户类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
"""
客户类
"""
import re


class Customer:
"""客户类"""

def __init__(self, id, name, age, phone, email):
"""
初始化客户信息
:param id: 客户编号
:param name: 客户姓名
:param age: 客户年龄
:param phone: 客户手机号
:param email: 客户邮箱
"""
self.id = id
self.name = name
self.age = age
self.phone = phone
self.email = email

@staticmethod
def check_id(c_id):
"""
检查编号是否为正整数
:param id: 客户编号
:return: True/False
"""
return c_id.isdigit() and int(c_id) > 0

@staticmethod
def check_name(c_name):
"""
检查姓名是否为字符
:param name: 客户姓名
:return: True/False
"""
return c_name.isalpha()

@staticmethod
def check_age(c_age):
"""
检查年龄是否为合法年龄值
:param age: 客户年龄
:return: True/False
"""
return c_age.isdigit() and 0 < int(c_age) < 120

@staticmethod
def check_phone(c_phone):
"""
检查手机号是否合法
:param phone: 客户手机号
:return: True/False
"""
return re.match(r"^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$", c_phone) is not None

@staticmethod
def check_email(c_email):
"""
检查邮箱是否合法
:param email: 客户邮箱
:return: True/False
"""
return re.match(r"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$", c_email) is not None

def __str__(self):
"""打印客户信息"""
return (f"Id: {self.id:<5}, Name: {self.name:<10}, Age: {self.age:<5}, Phone: {self.phone:<15}"
f", Email: {self.email:<25}")

16.2.2 客户管理类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
"""
客户管理类
"""
from Customer import Customer


class CustomerManager:
"""客户管理类"""
def __init__(self):
"""初始化客户管理系统"""
self.customer_id_dict = {} # 客户id字典
self.customer_name_dict = {} # 客户姓名字典

def menu(self):
"""显示菜单"""
print("-" * 50)
print("欢迎使用客户管理系统")
print("1. 添加客户")
print("2. 显示所有客户")
print("3. 查询客户")
print("4. 删除客户")
print("5. 修改客户")
print("6. 退出系统")
print("-" * 50)

def start(self):
"""启动系统"""
try:
while True:
self.menu()
choice = input("请选择操作:")
if choice == "1":
self.add_customer()
elif choice == "2":
self.list_all_customers()
elif choice == "3":
self.search_customer()
elif choice == "4":
self.delete_customer()
elif choice == "5":
self.update_customer()
elif choice == "6":
print("感谢使用客户管理系统")
break
else:
print("输入错误,请重新输入")
continue
except (EOFError, KeyboardInterrupt, ValueError):
print(f"退出客户管理系统")

def input_customer_id(self):
"""
输入客户编号
:return: 客户编号
"""
for i in range(3):
c_id = input("请输入客户编号:")
if Customer.check_id(c_id):
if c_id in self.customer_id_dict:
print(f"编号已存在,请重新输入,你还有{2-i}次机会")
else:
return c_id
else:
print(f"编号格式错误,请重新输入,你还有{2-i}次机会")
raise ValueError("输入错误次数过多")

def input_customer_name(self):
"""
输入客户姓名
:return: 客户姓名
"""
for i in range(3):
c_name = input("请输入客户姓名:")
if Customer.check_name(c_name):
if c_name in self.customer_name_dict:
print(f"姓名已存在,请重新输入,你还有{2-i}次机会")
else:
return c_name
else:
print(f"姓名格式错误,请重新输入,你还有{2-i}次机会")
raise ValueError("输入错误次数过多")

def input_customer_age(self):
"""
输入客户年龄
:return: 客户年龄
"""
for i in range(3):
c_age = input("请输入客户年龄:")
if Customer.check_age(c_age):
return c_age
else:
print(f"年龄格式错误,请重新输入,你还有{2-i}次机会")
raise ValueError("年龄输入错误次数过多")

def input_customer_phone(self):
"""
输入客户电话
:return: 客户电话
"""
for i in range(3):
c_phone = input("请输入客户手机号:")
if Customer.check_phone(c_phone):
return c_phone
else:
print(f"手机号格式错误,请重新输入,你还有{2-i}次机会")
raise ValueError("手机号输入错误次数过多")

def input_customer_email(self):
"""
输入客户邮箱
:return: 客户邮箱
"""
for i in range(3):
c_email = input("请输入客户邮箱:")
if Customer.check_email(c_email):
return c_email
else:
print(f"邮箱格式错误,请重新输入,你还有{2-i}次机会")
raise ValueError("邮箱输入错误次数过多")

def add_customer(self):
"""
添加客户
:return:客户对象
"""
try:
c_id=self.input_customer_id()
c_name=self.input_customer_name()
c_age=self.input_customer_age()
c_phone=self.input_customer_phone()
c_email=self.input_customer_email()
cus= Customer(c_id,c_name,c_age,c_phone,c_email)
self.customer_id_dict[c_id]=cus
self.customer_name_dict[c_name]=cus
except ValueError as e:
print(e)
return None

def list_all_customers(self):
"""
列出所有客户信息
:return: None
"""
for cus in self.customer_id_dict.values():
print(cus)
print()
def search_customer(self):
"""
根据编号查询客户信息
:return: None
"""
c_id = input("请输入要删除的客户编号:")
cus = self.customer_id_dict.get(c_id)
if cus is not None:
print(cus)
else:
print("未找到该客户")

def delete_customer(self):
"""
删除客户信息
:return: None
"""
c_id = input("请输入要删除的客户编号:")
cus = self.customer_id_dict.get(c_id)
if cus is not None:
del self.customer_id_dict[c_id]
del self.customer_name_dict[cus.name]
print(f"删除客户信息成功:{cus}")
else:
print("未找到该客户")

def update_customer_id(self,c_id):
"""
输入客户的新编号
:return: 客户的新编号
"""
for i in range(3):
new_c_id = input(f"请输入客户的新编号({c_id}):")
if new_c_id == "":
return c_id
if Customer.check_id(new_c_id):
if new_c_id in self.customer_id_dict and new_c_id != c_id:
print(f"编号已存在,请重新输入,你还有{2-i}次机会")
else:
return new_c_id
else:
print(f"编号格式错误,请重新输入,你还有{2-i}次机会")
raise ValueError("输入错误次数过多")

def update_customer_name(self,c_name):
"""
输入客户的新姓名
:return: 客户的新姓名
"""
for i in range(3):
new_c_name = input(f"请输入客户的新姓名({c_name}):")
if new_c_name == "":
return c_name
if Customer.check_id(new_c_name):
if new_c_name in self.customer_name_dict and new_c_name != c_name:
print(f"编号已存在,请重新输入,你还有{2-i}次机会")
else:
return new_c_name
else:
print(f"编号格式错误,请重新输入,你还有{2-i}次机会")
raise ValueError("输入错误次数过多")

def update_customer_age(self,c_age):
"""
输入客户年龄
:return: 客户年龄
"""
for i in range(3):
new_c_age = input(f"请输入客户的新年龄({c_age}):")
if new_c_age == "":
return c_age
if Customer.check_age(new_c_age):
return new_c_age
else:
print(f"年龄格式错误,请重新输入,你还有{2-i}次机会")
raise ValueError("年龄输入错误次数过多")

def update_customer_phone(self,c_phone):
"""
输入客户电话
:return: 客户电话
"""
for i in range(3):
new_c_phone = input(f"请输入客户的新手机号({c_phone}):")
if new_c_phone == "":
return c_phone
if Customer.check_phone(new_c_phone):
return new_c_phone
else:
print(f"手机号格式错误,请重新输入,你还有{2-i}次机会")
raise ValueError("手机号输入错误次数过多")

def update_customer_email(self,c_email):
"""
输入客户邮箱
:return: 客户邮箱
"""
for i in range(3):
new_c_email = input(f"请输入客户的新邮箱({c_email}):")
if new_c_email == "":
return c_email
if Customer.check_email(new_c_email):
return new_c_email
else:
print(f"邮箱格式错误,请重新输入,你还有{2-i}次机会")
raise ValueError("邮箱输入错误次数过多")

def update_customer(self):
"""
修改客户信息
:return: None
"""
for i in range(3):
c_id_name = input("请输入要修改的客户编号或姓名:")
cus = self.customer_id_dict.get(c_id_name)
if cus is None:
cus = self.customer_name_dict.get(c_id_name)
print(cus)

if cus is not None:
c_id = cus.id
c_name = cus.name
print("新输入客户的新信息,直接回车表示不修改")
try:
c_id = self.update_customer_id(c_id)
c_name = self.update_customer_name(c_name)
c_age = self.update_customer_age(cus.age)
c_phone = self.update_customer_phone(cus.phone)
c_email = self.update_customer_email(cus.email)
cus = Customer(c_id, c_name, c_age, c_phone, c_email)
self.customer_id_dict[c_id] = cus
self.customer_name_dict[c_name] = cus
print("更新成功!")
return None
except ValueError as e:
print(e)
return None
else:
print(f"客户不存在,请重新输入,你还有{2-i}次输入机会")
raise ValueError("输入错误次数过多")

if __name__ == "__main__":
cms = CustomerManager()
cms.start()