------------------main.py--------------------
from managerSystem import *
if __name__ == '__main__':
student_manager = StudentManager()
student_manager.run
-----------------------------------managerSystem.py----------------------------
from student import *
class StudentManager(object):
def __init__(self):
self.student_list=[]
def run(self):
self.load_student()
while true:
self.show_menu()
menu_num=int(input('please input the number of you choice'))
if menu_num==1:
self.add_student()
elif menu_num==2:
self.del_student()
elif menu_num==3:
self.modify_student()
elif menu_num==4:
self.search_student()
elif menu_num==5:
self.show_student()
elif menu_num==6:
self.save_student()
elif menu_num==7:
break
@staticmethod
def show_menu():
print('please make a choice:')
print('1.add')
print('2.delete')
print('3.modify')
print('4.search')
print('5.show')
print('6.save')
print('7.quit')
def add_student(self):
# 1.input
name = input('please inmput name')
gender = input('please inmput gender')
tel = input('please inmput tel')
#2.create object
student=Student(name,gender,tel)
#
self.student_list.append(student)
print(self.student_list)
print(student)
def del_student(self):
del_name=input('please input the name to be deleted')
for i in self.student_list:
if i.name==del_name:
self.student_list.remove(i)
break
else:
print('not find the student')
print(self.student_list)
def modify_student(self):
modify_name = input('please input the name to be modified')
for i in self.student_list:
if i.name == modify_name:
i.name=input('please input new name')
i.gender = input('please input new gender')
i.tel = input('please input new tel')
print(f'modify successed,new name:{i.name},new gender:{i.gender},new tel:{i.tel}')
break
else:
print('not find the student')
print(self.student_list)
def search_student(self):
search_name = input('please input the name to be searched')
for i in self.student_list:
if i.name == search_name:
print(f'search successed, name:{i.name},gender:{i.gender}, tel:{i.tel}')
break
else:
print('not find the student')
print(self.student_list)
def show_student(self):
print('name\tgender\tcellphone')
for i in self.student_list:
print(f'{i.name}\t{i.gender}\t{i.tel}')
def save_student(self):
# 1.open file
f=open('student.data','w')
# 2.return back the dict of object
new_list=[i.__dict__ for i in self.student_list]
print(new_list)
f.write(str(new_list))
f.close()
def load_student(self):
try:
f=open('student.data','r')
except:
f = open('student.data', 'w')
else:
data=f.read()
# 字符串还原为对象
new_list=eval(data)
self.student_list=[Student(i['name'],i['gender'],i['tel'])for i in new_list]
finally:
f.close()
-------------------student.py---------------------------
class Student(object):
def __init__(self,name,gender,tel):
# def __init__(self, name, gender, tel):
# name,gender,tel
self.name = name
self.gender = gender
self.tel = tel
def __str__(self):
return f'your name is {self.name},your gender is {self.gender},your tel is {self.tel}'
from managerSystem import *
if __name__ == '__main__':
student_manager = StudentManager()
student_manager.run
-----------------------------------managerSystem.py----------------------------
from student import *
class StudentManager(object):
def __init__(self):
self.student_list=[]
def run(self):
self.load_student()
while true:
self.show_menu()
menu_num=int(input('please input the number of you choice'))
if menu_num==1:
self.add_student()
elif menu_num==2:
self.del_student()
elif menu_num==3:
self.modify_student()
elif menu_num==4:
self.search_student()
elif menu_num==5:
self.show_student()
elif menu_num==6:
self.save_student()
elif menu_num==7:
break
@staticmethod
def show_menu():
print('please make a choice:')
print('1.add')
print('2.delete')
print('3.modify')
print('4.search')
print('5.show')
print('6.save')
print('7.quit')
def add_student(self):
# 1.input
name = input('please inmput name')
gender = input('please inmput gender')
tel = input('please inmput tel')
#2.create object
student=Student(name,gender,tel)
#
self.student_list.append(student)
print(self.student_list)
print(student)
def del_student(self):
del_name=input('please input the name to be deleted')
for i in self.student_list:
if i.name==del_name:
self.student_list.remove(i)
break
else:
print('not find the student')
print(self.student_list)
def modify_student(self):
modify_name = input('please input the name to be modified')
for i in self.student_list:
if i.name == modify_name:
i.name=input('please input new name')
i.gender = input('please input new gender')
i.tel = input('please input new tel')
print(f'modify successed,new name:{i.name},new gender:{i.gender},new tel:{i.tel}')
break
else:
print('not find the student')
print(self.student_list)
def search_student(self):
search_name = input('please input the name to be searched')
for i in self.student_list:
if i.name == search_name:
print(f'search successed, name:{i.name},gender:{i.gender}, tel:{i.tel}')
break
else:
print('not find the student')
print(self.student_list)
def show_student(self):
print('name\tgender\tcellphone')
for i in self.student_list:
print(f'{i.name}\t{i.gender}\t{i.tel}')
def save_student(self):
# 1.open file
f=open('student.data','w')
# 2.return back the dict of object
new_list=[i.__dict__ for i in self.student_list]
print(new_list)
f.write(str(new_list))
f.close()
def load_student(self):
try:
f=open('student.data','r')
except:
f = open('student.data', 'w')
else:
data=f.read()
# 字符串还原为对象
new_list=eval(data)
self.student_list=[Student(i['name'],i['gender'],i['tel'])for i in new_list]
finally:
f.close()
-------------------student.py---------------------------
class Student(object):
def __init__(self,name,gender,tel):
# def __init__(self, name, gender, tel):
# name,gender,tel
self.name = name
self.gender = gender
self.tel = tel
def __str__(self):
return f'your name is {self.name},your gender is {self.gender},your tel is {self.tel}'