`
斌强-朱
  • 浏览: 51460 次
社区版块
存档分类
最新评论

Python学生管理系统

 
阅读更多

class Student:
	stuID = ""
	name = ""
	sex = "M"
	classID = "NULL"
	
	#set ID 
	def setStuID(self,stuID):
		self.stuID = stuID
	
	def setName(self,name):
		self.name = name
	
	def setSex(self,sex):
		self.sex = sex
		
	def setClassID(self,classID):
		self.classID = classID
	
	def getStuId(self):
		return self.stuID
	
	def getName(self):
		return self.name
	
	def getSex(self):
		return self.sex
	
	def getClassID(self):
		return self.classID
		
主函数

import os
import re
import sys
import string
import student

#save file
global FILEPATH
FILEPATH = "student.db"
#temporary file  
global TEMPFILE
TEMPFILE = "temp.db"

#This is menu
def menu():
    while True :
        print "1.Add a student information"
        print "2.Query student information"
        print "3.Delete a student information"
        print "0.Quit"
        opt = raw_input("Select:")
        if opt == "1":
            while True:
                addStudent()
                opt2 = raw_input("Continue Add(Y/N)?:")
                if opt2 == "Y" or opt2 == "y" or opt2 == "":
                    continue
                else:
                    break
        elif opt == "2":
            while True:
                query()
                opt2 = raw_input("Continue Query(Y/N)?:")
                if opt2 == "Y" or opt2 == "y" or opt2 == "":
                    continue
                else:
                    break            
        elif opt == "3":
            while True:
                delMenu()
                opt2 = raw_input("Continue Delete(Y/N)?:")
                if opt2 == "Y" or opt2 == "y" or opt2 == "":
                    continue
                else:
                    break             
        elif opt == "0" :
            exitProgram()   
            break 
        else:
            print "Error input"
                
#Add a student
def addStudent() :
    stu = student.Student()
    while True:
        stuID = raw_input("ID(001-999):")
        #match ID 001-999
        p = re.match("^[0-9]{3}$", stuID)
        if p :
            if stuID == "000":
                print "ID must be  001-999"
                continue
            if isIDExist(stuID):
                print "ID = %s already exist!" % stuID
                continue
            else :    
                stu.setStuID(stuID)
            break
        else:
            print "ID must be 001-999"
    
    while True:        
        stuName = raw_input("Name(a-z,A-Z,5 char):")
        #match name a-z A-Z 5 char
        p = re.match("^[a-zA-Z]{1,5}$",stuName)
        if p :
            stu.setName(stuName)
            break
        else :
            print "Name format error must a-z,A-Z,within 5 char"
    
    while True:    
        stuSex = raw_input("Sex(default is M):")
        #default value
        if stuSex == "":
            stu.setSex("M")
            print "Sex:M"
            break
        if stuSex =="M" or stuSex == "m":
            stu.setSex(string.upper(stuSex))
            break             
        #if stuSex == "M" or stuSex == "m" || stuSex == "F" stuSex == "f":
        p = re.match("^M|m|F|f$",stuSex)
        if p :
            stu.setSex(string.upper(stuSex))
            break
        else :
            print "Sex(M/f)"
        
    while True:
        stuClass = raw_input("Class(01-99):")
        #default value
        if stuClass == "":
            stu.setClassID("NULL")
            print "CLASS:NULL"
            break
        #match 00-99
        p = re.match("^[0-9]{2}$",stuClass)
        if p :
            #get rid of 00
            if stuClass == "00":
                print "Class must 01-99"
                continue
            stu.setClassID(stuClass)
            break
        else:
            print "Class must 01-99"
    #save to file        
    file1 = open(FILEPATH,"a")
    print "ID\tNAME\tSEX\tCLASS"
    print stu.getStuId(),"\t",stu.getName(),"\t",stu.getSex(),"\t",stu.getClassID()
    file1.write(stu.getStuId()+"\t"+stu.getName()+"\t"+stu.getSex()+"\t"+stu.getClassID()+"\n")
    print "Add student success!"
    file1.close()

#Delete student menu    
def delMenu():
    print "1.Delete by ID"
    print "2.Delete contains ID"
    opt = raw_input("Select:")
    if opt == "1":
        delStudentByID()
    elif opt == "2":
        delStudentContainsID()
    else:
        print "Error input"    
        
#Delete contains id        
def delStudentContainsID():
    contID = raw_input("ID:")
    if getInfoContainsID(contID)==0 :
        print "Can't find ID contains \"%s\" student" % contID
        return
    opt = raw_input("Are you sure delete all(Y/N):")
    if not opt == "y" or opt == "Y":
        return     
    f = open(FILEPATH,"r")
    tmp = open(TEMPFILE,"a")
    i=0
    for eachLine in f:
        items = eachLine.split("\t")
#        if not re.match(contID, items[0]):
        if items[0].count(contID) ==0:
            tmp.write(eachLine)
        else: 
            i+=1     
    f.close()
    tmp.close()
    os.remove(FILEPATH)
    os.rename(TEMPFILE, FILEPATH)
    print "Deleted %d data" % i

#get contains ID information                 
def getInfoContainsID(stuID):
    f = open(FILEPATH)
    i=0
    for eachLine in f:
        items = eachLine.split("\t")
        
        if not items[0].count(stuID) ==0:
#        if re.match(stuID,items[0]):
            i+=1
            if i==1:
                print "ID\tNAME\tSEX\tCLASS"
            print eachLine,
    if i==0:
        return 0
    else :
        return i
    f.close()
        
#Delete student by ID    
def delStudentByID():
    delID = raw_input("Delete ID:")
    if not isIDExist(delID) :
        print "Can't find ID = %s student information" % delID
        return 
    
    getInfoByID(delID)
    opt = raw_input("Are you sure delete it(Y/N):")
    if not (opt == "Y" or opt == "y"):
        return
    
    f = open(FILEPATH,"r")
    tmp = open(TEMPFILE,"a")
    
    for eachLine in f:
        split = eachLine.split("\t")        
        if not delID == split[0]:
            tmp.write(eachLine)                      
    tmp.close()
    f.close()
    os.remove(FILEPATH)
    os.rename(TEMPFILE, FILEPATH)
    print "Delete success!"

#Query menu    
def query():
    print "1.Query student by ID"
    print "2.Query all students"
    opt = raw_input("Select:")
    if opt == "1":
        queryByID()
    elif opt == "2":
        queryAll()
    #default is query all    
    elif opt == "":
        queryAll()    
    else :
        print "Error Input!"    
        
#query ID exist
def isIDExist(ID):
    f = open(FILEPATH)
    flag = 0
    for eachline in f:
        temp = eachline.split("\t")
        if temp[0] == ID:
            flag+=1
    f.close()
    if flag == 0 :
        return False
    else:
        return True
    
#get information by ID for delete student     
def getInfoByID(stuID):
    f = open(FILEPATH)
    i=0
    for eachLine in f:
        items = eachLine.split("\t")
        if items[0] == stuID:
            i+=1
            if i==1:
                print "ID\tNAME\tSEX\tCLASS"
            print eachLine    
    f.close()         
    
#Query student by ID
def queryByID():
    queryID = raw_input("ID:")
    f = open(FILEPATH)
#    lines = f.readlines()
#    print lines[0].strip()
#    for line in lines:
#        a = line.split()
#        if queryID == a[1].strip():
#            print line.strip()
    flag = 0
    for eachline in f:
        #split by "\t" get as C array temp[4]
        temp = eachline.split("\t")
#        print temp[0] , temp[1] , temp[2] , temp[3]
        if temp[0] == queryID:
            flag+=1
            if flag == 1:
                print "ID\tNAME\tSEX\tCLASS"
            print eachline,
    if flag == 0 :
        print "Can't find ID = %s student information" % queryID       
    f.close()
#    f = open(FILEPATH)
#    readLines = f.readlines()
    
#    for eachLine in f:
#        eachLine
#                
#        if eachLine == queryID:
#            print eachLine
#    f.close()

#Query all students    
def queryAll():
    f = open(FILEPATH,"r")
    i=0
    for eachLine in f:
        i+=1
        if i==1:
            print "ID\tNAME\tSEX\tCLASS"
        print eachLine,
    f.close()
    if i==0:
        print "No data!"

def exitProgram():
    print "Thank you Bye!"
#    sys.exit()
    
def init():
    if os.path.exists(FILEPATH) :
        print "Load file successful"
    else:
        try:
            f = open(FILEPATH,"w")
        except Exception:
            print "Can't open file"
            sys.exit()
        finally:
            f.close()
    
if __name__ == '__main__':
    init()
    menu()













分享到:
评论

相关推荐

    python 学生管理系统

    Python学生管理系统是一款基于Python编程语言开发的软件,用于管理和组织教育机构中的学生信息。这个系统通常结合了数据库技术,如MySQL,以及用户界面库,如Thinger,来提供一个直观且功能丰富的操作环境。让我们...

    python学生管理系统tkinter版

    Python学生管理系统是一个初学者友好的项目,它使用了Python的GUI库Tkinter来构建一个图形用户界面,使得用户可以通过交互式的方式进行学生信息的管理。Tkinter是Python标准库的一部分,提供了一套易于使用的控件和...

    python学生管理系统源码文件

    python学生管理系统源码文件

    python学生管理系统的程序

    python学生管理系统的程序python学生管理系统的程序python学生管理系统的程序python学生管理系统的程序python学生管理系统的程序python学生管理系统的程序python学生管理系统的程序python学生管理系统的程序python...

    Python界面版学生系统,python学生管理系统界面版,Python

    【Python界面版学生系统】是一种基于Python编程语言开发的、具有图形用户界面(GUI)的学生管理系统。这个系统利用Python的数据库连接能力与MySQL数据库进行交互,实现了对学生信息的基本操作,如增加、删除、修改和...

    基于python学生管理系统.rar

    基于python学生管理系统.rar基于python学生管理系统.rar基于python学生管理系统.rar基于python学生管理系统.rar基于python学生管理系统.rar基于python学生管理系统.rar基于python学生管理系统.rar基于python学生管理...

    python学生管理系统-mysql_operation_pythontkinter_python_tkintermysql_T

    Python学生管理系统是一个基于MySQL数据库和Python的Tkinter图形用户界面(GUI)开发的简单应用,用于管理和操作学生信息。这个系统结合了Python编程语言、Tkinter库(用于创建GUI)、以及MySQL数据库操作,实现了对...

    基于Python学生管理系统的程序.zip

    基于python学生管理系统的程序.zip 基于python学生管理系统的程序.zip 基于python学生管理系统的程序.zip 基于python学生管理系统的程序.zip 基于python学生管理系统的程序.zip 基于python学生管理系统的程序.zip ...

    python学生管理系统

    【Python学生管理系统】是利用Python编程语言开发的一款基于图形用户界面(GUI)的管理软件,主要目标是方便教育工作者或管理员对学生信息进行高效、便捷的管理。在这个系统中,开发者运用了`tkinter`库来构建用户...

    Python学生管理系统(web网页版)

    Python学生管理系统(web网页版),前面发布了python的控制台版本的学生管理系统和使用tkinter界面版的学生管理系统,这次是使用Django开发基于学生的实体类的增删改查,里面包含项目演示录屏和完整的项目源码与...

    python学生管理系统.rar

    学生管理系统.rar学生管理系统.rar学生管理系统.rar学生管理系统.rar学生管理系统.rar学生管理系统.rar学生管理系统.rar学生管理系统.rar学生管理系统.rar学生管理系统.rar学生管理系统.rar学生管理系统.rar学生管理...

Global site tag (gtag.js) - Google Analytics