您的位置:首页 > 编程语言 > Python开发

使用python实现一个简单的学生信息管理系统

2015-09-08 17:35 2391 查看
最近公司搬办公室,杂七杂八的事情比较多,又碰上业务要上线了。。。很多事情堆到一起来做,导致最近没什么时间学习,写博客。前两天胜利日放假,把以前用java写的学生信息管理系统用python重新写了一遍,以便于帮助python的学习。

好了,废话不多说,首先进行需求分析,下面是我根据需求画的系统结构图:



纯手工制图。。。。。画的不好敬请谅解。从上图来看,整个系统分为main,add,delete,change,select,sort,io,print共八个模块,实现了对学生信息的增删改查排的功能,将结果储存到student.txt文件中去。
学生信息的数据结构我将其设计为一个学生的一条记录用一个列表来存储,这个列表包含的信息为:学号,姓名,年龄,成绩,地址这些字段。同时,所有学生的记录又结合成一个列表,这样,这个列表就存储了所有学生的信息。
下面是我的源代码以及对该源代码的分析,以供大家借鉴参考以及自己的记录。(PS:由于本人学习Python的时间比较短,代码难免有写的比较渣的地方,希望各位大神轻喷(^-^!!!))
cat main.py
#__author__ = 'huxianglin'
# -*- coding: utf-8 -*-
import add
import delete
import change
import select
import sort

file_path= 'student.txt'       #首先定义数据的存储路径,这里定义为当前程序锁在目录的根目录中

def main():                    #在main函数中使用while循环来获取用户的输入信息
while True:
print(u"welcome to student information management system!")
print(u"you can use input:add;delete;change;select;sort;quit")
keyword=raw_input("please input what you want to operation:")
if keyword=="quit":                      #由于python中没有类似于switch case的方式来实现多项选择,本来打算使用dict来实现这个功能的,但是按照网上的方式,死活无法达到想要的功能,于是,逼得没办法咬咬牙,用if elif来代替switch case的功能,请大神轻喷哈!
exit(0)
elif keyword=="add":
add.index(file_path)
elif keyword=="delete":
delete.index(file_path)
elif keyword=="change":
change.index(file_path)
elif keyword=="select":
select.index(file_path)
elif keyword=="sort":
sort.index(file_path)
else:
print(u"please input correct option!")
'''
else:                                    #这一段实现有问题。。。无法达到目标。
option={"add":add.index(file_path),
"delete":delete.index(file_path),
"change":change.index(file_path),
"select":select.index(file_path),
"sort":sort.index(file_path)}
option.get(keyword,"your input is wrong,please input again!")()
'''
if __name__ == '__main__':
main()
cat add.py
#__author__ = 'huxianglin'
# -*- coding: utf-8 -*-
import io

def index(file_path):                #这个函数是用来被main调用,实现添加记录的功能,通过这个函数来与用户交互,将获取到的数据通过io写入到文件中。
while True:
print(u"please input a student information like:number name age score address")
print(u"If you want to break,please input quit!")
keyword=raw_input(u"please input what you want:")
if keyword=="quit":
break
else:
addline=add()
addline.addinfo(keyword.split(" "),file_path)

class add:                           #这里定义了一个类,主要用来实现将获取到的一条记录传到io中,写入文件。
def __init__(self):
print(u"we will input this student's information")
def addinfo(self,student,file_path):
read=io.io()
read.add(student,file_path)
cat delete.py
#__author__ = 'huxianglin'
# -*- coding: utf-8 -*-
import io

def index(file_path):                 #同理,这里也是通过与用户交互,获取到用户想要删除的记录的学号,然后交给delete类来实现删除记录的功能。(ps:这里少了一个判断用户输入的信息是否被成功执行的功能,时间仓促,没有完善哈。)
while True:
print(u"please input that you want to delete student's number!")
print(u"If you want to break,please input quit!")
keyword=raw_input(u"please input what you want:")
if keyword=="quit":
break
else:
deleteline=delete()
deleteline.deleteinfo(keyword,file_path)

class delete:                        #这个类通过io获取到所有学生信息,然后将用户想要删除的学生学号进行匹配,假如匹配到,则删除该条记录。
def deleteinfo(self,number,file_path):
read=io.io()
information=[]
information=read.read(information,file_path)
for i in information:
if i[0] == number:
information.remove(i)
read.write(information,file_path)
cat change.py
#__author__ = 'huxianglin'
# -*- coding: utf-8 -*-
import io

def index(file_path):                #这里通过交互获取到用户想要修改的学生的学号,然后传递给change类。
while True:
print(u"please input that you want to change student's number!")
print(u"If you want to break,please input quit!")
keyword=raw_input(u"please input what you want:")
if keyword=="quit":
break
else:
changeline=change()
changeline.changeinfo(keyword,file_path)

class change:                        #这里通过从io中获取学生信息,存储到list中,然后遍历list,获取到匹配学号的记录,然后获取到用户想要修改后的数据,将其写入到list中,最后再将这个新的List存入到文件中(PS:这里卡了我好久,因为文件中的学生记录是一条记录存储一行,需要在记录的最后面添加上换行符,但是修改后的数据是没有换行符的,为了将换行符添加进去,我想了很多办法,最后才想到直接将输入的字符串和换行符进行链接,然后再存储到list中去。。。。。。表示真想狠狠抽自己嘴巴子。。。)
def changeinfo(self,number,file_path):
read=io.io()
information=[]
information=read.read(information,file_path)
for i in information:
if i[0] == number:
print(u"please input the student's information that you want to change like:number name age score address")
keyword=raw_input(u"please input what you want:")
keyword=keyword+'\n'
information[information.index(i)]=keyword.split(" ")
read.write(information,file_path)
cat select.py
#__author__ = 'huxianglin'
# -*- coding: utf-8 -*-
import io
import printstudent

def index(file_path):                #通过交互,获取到用户想要查询的学生的学号,然后传入select类中进行查找。
while True:
print(u"please input that you want to select student's number!")
print(u"If you want to break,please input quit!")
keyword=raw_input(u"please input what you want:")
if keyword=="quit":
break
else:
selectline=select()
selectline.selectinfo(keyword,file_path)

class select:                        #通过获取到用户想要查找的记录的学号,同时获取到学生信息列表,然后将其传入到printstudent块中进行处理。
def selectinfo(self,number,file_path):
read=io.io()
information=[]
information=read.read(information,file_path)
printstudent.selectstudent(number,information)
cat sort.py
#__author__ = 'huxianglin'
# -*- coding: utf-8 -*-
import io
import printstudent

def index(file_path):                #同样的,通过交互获取到用户想要通过什么方式进行排序,可选择的有学号,年龄,成绩。
while True:
print(u"please input what keyword that you want to sort,you can choose 'number'or'age'or'score'those keywords")
print(u"If you want to break,please input quit!")
keyword=raw_input(u"please input what you want:")
if keyword=="quit":
break
elif keyword=="number":
numbersort=sortstudent()
numbersort.numbersort(file_path)
elif keyword=="age":
agesort=sortstudent()
agesort.agesort(file_path)
elif keyword=="score":
scoresort=sortstudent()
scoresort.scoresort(file_path)
else:
print(u"please input currect keyword")

class sortstudent:                #通过index函数调用类中不同的排序关键字对应的方法,可以对学生信息通过关键字进行排序。(PS:我觉得这部分写的比较繁琐,可能可以进行更简化的操作,提高代码重用率,可是目前我没想到更好的办法。。。抱歉哈。。。)这里提到了sorted函数,这个函数可以对传入进去的列表进行排序,生成一个新的列表副本,同时里面还可以使用lambda对应列表嵌套进行排序,这个功能是一个很实用的功能,只想说好顶赞!!!

def numbersort(self,file_path):
read=io.io()
information=[]
information=read.read(information,file_path)
sorted_information=sorted(information,key=lambda student:student[0])
printstudent.printstudent(sorted_information)

def agesort(self,file_path):
read=io.io()
information=[]
information=read.read(information,file_path)
sorted_information=sorted(information,key=lambda student:student[2])
printstudent.printstudent(sorted_information)

def scoresort(self,file_path):
read=io.io()
information=[]
information=read.read(information,file_path)
sorted_information=sorted(information,key=lambda student:student[3])
printstudent.printstudent(sorted_information)
cat printstudent.py
#__author__ = 'Administrator'
# -*- coding: utf-8 -*-

def printstudent(information):                #这个没什么好说的,对查询和排序传过来的列表进行排序,其中,主要需要注意格式化输出。这里面使用了%+20s,其中的+20是占位20的宽度,数据向右对齐。由于排序和查询输出结构不一样,于是写了两个输出函数来实现(个人感觉这里也可以进行精简)。。。
for i in information:
print("number: %+20s" %str(i[0]))
print("name:   %+20s" %str(i[1]))
print("age:    %+20s" %str(i[2]))
print("score:  %+20s" %str(i[3]))
print("address:%+20s" %str(i[4]))

def selectstudent(num,information):
for i in information:
if i[0]==num:
print("number: %+20s" %str(i[0]))
print("name:   %+20s" %str(i[1]))
print("age:    %+20s" %str(i[2]))
print("score:  %+20s" %str(i[3]))
print("address:%+20s" %str(i[4]))
cat io.py
#__author__ = 'huxianglin'
# -*- coding: utf-8 -*-

class io:                #这部分是写的最纠结的,最开始由于对文件操作不熟悉,总是无法实现正确的换行写入操作。。。。。最开始选择逃避的办法,一条一条插入数据,后面发现写不下去了,还是需要写出写入文件的正确办法,在网上找了好久的资料终于在最后想到了在每个列表的末尾添加换行符来解决操作,于是,就有了上面那个修改数据后怎么添加换行符的操作。。。(PS:总觉得这种方式是拆东墙补西墙的感觉。。。碰到一个问题,解决一个,然后因为改动,又出现了新的问题。。。)这里一条一条插入数据使用了print的重定向功能,这样就自动在每条数据的最后面添加了换行符。

def read(self,information,file_path):
with open(file_path,'r') as f:
for line in f:
information.append(line.split(' '))
return  information

def write(self,information,file_path):
f=open(file_path,'w')
for line in information:
f.write(' '.join(line))
f.close()

def add(self,information,file_path):
f=open(file_path,'a')
print>>f,' '.join(information)
f.close()
cat student.txt
127 guojianlin 23 98 shenzhen
123 huxianglin 21 99 shenzhen
126 huxianglin 25 95 shenzhen
124 huxianglin 26 97 shenzhen
125 huxianglin 24 96 shenzhen
129 huxianglin 28 92 shenzhen
128 huxianglin 27 91 shenzhen
122 huxianglin 25 93 shenzhen
131 huxianglin 21 95 shenzhen
上面是文件里面存储的信息,随便刷的。。。总结来看这次小的程序设计做的磕磕绊绊主要是由于对python不熟悉的原因,有很多想法就是不知道该如何实现。。。QAQ果然我还是练得太少了!!!
但是在这次重写过程中,我发现python确实开发速度比起java来要快不少,特别是处理文件类的操作,list,dict简直就是python的大杀器,以前我实现这个功能用java写的,差不多有500行代码才实现,用python实现这个功能才用了200行左右的代码就搞定了,这还是在我对python不怎么熟悉的基础上,由以上对比可以看出,pyhthon开发确实比java等这些语言开发速度快多了。这也给了我继续将python学下去的信心!!!学习的路上总会碰到沟沟坎坎,坚持下去,终能走到终点!加油,各位共勉之!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  管理系统 python