使用python實現一個簡單的學生信息管理系統

最近公司搬辦公室,雜七雜八的事情比較多,又碰上業務要上線了。。。很多事情堆到一起來做,導致最近沒什麼時間學習,寫博客。前兩天勝利日放假,把以前用java寫的學生信息管理系統用python重新寫了一遍,以便於幫助python的學習。

好了,廢話不多說,首先進行需求分析,下面是我根據需求畫的系統結構圖:

wKioL1XuoPLj9myoAAMYbJgXM2I990.jpg

純手工製圖。。。。。畫的不好敬請諒解。從上圖來看,整個系統分爲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學下去的信心!!!學習的路上總會碰到溝溝坎坎,堅持下去,終能走到終點!加油,各位共勉之!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章