實現(xiàn)從面向過程到面向?qū)ο蟮倪^度,通過更改前面的學(xué)生管理系統(tǒng)實現(xiàn)面向?qū)ο?,也證明了面向過程可以完美過度到面向?qū)ο?,從而為以后的程序進步做了進一步優(yōu)化,方便以后的程序更改。
import os
class StudentT(object):
def __init__(self):
self.student_list=[]
self.student_dict={}
#學(xué)生管理系統(tǒng)界面
@staticmethod
def jiemian():
print("---------------------------")
print(" 學(xué)生管理系統(tǒng) V1.0")
print(" ")
print(" 1:添加學(xué)生" )
print(" 2:刪除學(xué)生" )
print(" 3:修改學(xué)生" )
print(" 4:查詢學(xué)生" )
print(" 5:顯示所有學(xué)生" )
print(" 6:保存數(shù)據(jù)" )
print(" 7:退出系統(tǒng)" )
print(" ")
print("---------------------------")
#添加學(xué)生
def add(self):
name=input("請輸入錄入學(xué)生姓名:")
cls=input("請輸入學(xué)生班級:")
age=input("請輸入錄入學(xué)生年齡:")
phone=input("請輸入錄入學(xué)生手機號:")
addr=input("請輸入錄入學(xué)生家庭住址:")
self.student_dict={"name":name,"class":cls,"age":age,"phone":phone,"address":addr}
self.student_list.append(self.student_dict)
print()
print("-----添加學(xué)生信息界面-----")
print()
print("姓名\t\t","班級\t\t","年齡\t\t","電話號\t\t","家庭住址\t\t")
for student_dict_1 in self.student_list:
print("%s\t\t%s\t\t%s\t\t%s\t\t%s" %(student_dict_1["name"],
student_dict_1["class"],
student_dict_1["age"],
student_dict_1["phone"],
student_dict_1["address"]))
print()
print("錄入成功!")
print()
#刪除學(xué)生
def dele(self):
name_del=input("請輸入想要刪除的學(xué)生姓名:")
for student_dict_1 in self.student_list:
if name_del in student_dict_1["name"]:
self.student_list.remove(student_dict_1)
print()
print("刪除%s信息成功!" % name_del)
print()
break
else:
print()
print("您輸入的學(xué)生姓名錯誤,請重新輸入")
print()
#修改學(xué)生
def xiugai(self):
name_xiugai=input("請輸入想要修改的學(xué)生姓名:")
for student_dict_1 in self.student_list:
if name_xiugai == student_dict_1["name"]:
print()
print("-----修改界面-----")
print()
print("姓名\t\t", "班級\t\t", "年齡\t\t", "電話號\t\t", "家庭住址\t\t")
print("%s\t\t%s\t\t%s\t\t%s\t\t%s" %(student_dict_1["name"],
student_dict_1["class"],
student_dict_1["age"],
student_dict_1["phone"],
student_dict_1["address"]))
#回車不修改
student_dict_1["name"]=self.new_input(student_dict_1["name"],"請輸入修改后的學(xué)生姓名[回車不修改]:")
student_dict_1["class"]=self.new_input(student_dict_1["class"],"請輸入修改后的學(xué)生班級[回車不修改]:")
student_dict_1["age"]=self.new_input(student_dict_1["age"],"請輸入修改后的學(xué)生年齡[回車不修改]:")
student_dict_1["phone"]=self.new_input(student_dict_1["phone"],"請輸入修改后的學(xué)生手機號[回車不修改]:")
student_dict_1["address"]=self.new_input(student_dict_1["address"],"請輸入修改后的學(xué)生家庭地址[回車不修改]:")
print()
print("修改成功!")
print()
break
else:
print()
print("您輸入的學(xué)生姓名錯誤,請重新輸入")
print()
#查找學(xué)生
def find(self):
find_name=input("請輸入需要查找的學(xué)生姓名:")
for student_dict_1 in self.student_list:
if find_name == student_dict_1["name"]:
print()
print("-----查詢結(jié)果界面-----")
print()
print("姓名\t\t", "班級\t\t", "年齡\t\t", "電話號\t\t", "家庭住址\t\t")
print("%s\t\t%s\t\t%s\t\t%s\t\t%s" % (student_dict_1["name"],
student_dict_1["class"],
student_dict_1["age"],
student_dict_1["phone"],
student_dict_1["address"]))
else:
print()
print("-----查詢結(jié)果界面-----")
print()
print("無此學(xué)生信息")
#顯示所有學(xué)生信息
def showall(self):
if len(self.student_list)>0:
print()
print("-----顯示所有學(xué)生信息-----")
print()
print("姓名\t\t", "班級\t\t", "年齡\t\t", "電話號\t\t", "家庭住址\t\t")
for student_dict_1 in self.student_list:
print("%s\t\t%s\t\t%s\t\t%s\t\t%s" % (student_dict_1["name"],
student_dict_1["class"],
student_dict_1["age"],
student_dict_1["phone"],
student_dict_1["address"]))
else:
print()
print("暫無數(shù)據(jù)!")
print()
#設(shè)置用戶不輸入內(nèi)容返回原值,輸入內(nèi)容返回新內(nèi)容
def new_input(self,yuanzhi,message):
self.input_str=input(message)
if len(self.input_str)>0:
return self.input_str
else:
return yuanzhi
#保存數(shù)據(jù)至文件中
def save_file(self):
f = open("student2.txt", 'w', encoding='utf-8')
f.write(str(self.student_list))
f.close()
print("數(shù)據(jù)保存至student1.txt文件成功!")
#將數(shù)據(jù)讀取至變量中
def read_file(self):
if os.path.exists('student2.txt'):
f = open('student2.txt', 'r', encoding='utf-8')
ret = f.read()
self.student_list=eval(ret)
f.close()
print("數(shù)據(jù)讀取成功!")