教务管理系统雏形

前言

上一篇博客里写了系统管理雏形,但是bug太多,于是推倒重写了一遍,虽然还是有很多可以精简的地方,但是比上一篇要好很多了,每一点改变都是一次提升,我们要让自己变得越来越强大
下面来看代码,有点长
#!user/bin/env python
# coding:utf-8
"""
file:       jiaowuguali
date:       2017 - 09 - 04 10:18
author:     fang
version:    1.0
desc:
"""
import time
import sys


class User_input_handle():  ##创建用户输入处理类
    def all_user_input(self, str):
        while True:
            usr_input = raw_input(str)
            if usr_input == "":
                continue
            else:
                return usr_input

    def is_num(self, str):
        while True:
            usr_input = raw_input(str)
            if usr_input.isdigit():
                return usr_input
                # return int(usr_input)
            else:
                print "please input numbers!"

    def is_lower(self, str):
        while True:
            usr_input = raw_input(str)
            if usr_input == "":
                continue
            else:
                return usr_input.lower()


uih = User_input_handle()  ##实例化用户输入处理,处理用户输入


class Course(object):  ##定义课程类
    def __init__(self, course_name, course_price, course_attribute, course_cycle_time, course_addr):
        self.course_name = course_name
        self.course_price = course_price
        self.course_attribute = course_attribute
        self.course_cycle_time = course_cycle_time
        self.course_addr = course_addr


class Person(object):  ##定义person类为父类
    def __init__(self, name, age, gender):
        self.name = name
        self.age = age
        self.gender = gender


class Teacher(Person):  ##定义teacher类,继承person类
    def __init__(self, teacher_name, teacher_age, teacher_gender, teacher_course, teacher_pay, teacher_passwd="123456"):
        self.teacher_name = teacher_name
        self.teacher_age = teacher_age
        self.teacher_gender = teacher_gender
        self.teacher_course = teacher_course
        self.teacher_pay = teacher_pay
        self.teacher_passwd = teacher_passwd

    def teacher_teaching(self, teacher_name):
        selection_of_course = school_course_dict.keys()
        teacher_choise_course = uih.all_user_input("chose your course %s:" % (selection_of_course))
        if teacher_choise_course in selection_of_course:
            print "%s is giving %s lessons to students" % (teacher_name, teacher_choise_course)
            time.sleep(3)
            print "class is over!!"
        else:
            print "wrong input,try again"

    def show_student_info(self):
        for student_name_show in student_dict.keys():
            student_dict[student_name_show].student_show_info(student_name_show)

    def show_teacher_info(self, name):
        print teacher_dict[name]
        print """
    =============%s info===============
    teacher_name        :\t%s
    teacher_age         :\t%s
    teacher_gender      :\t%s
    teacher_course      :\t%s
    teacher_pay         :\t%s
    teacher_passwd      :\t%s
    ===================================
                """ % (teacher_dict[name].teacher_name,
                       teacher_dict[name].teacher_name,
                       teacher_dict[name].teacher_age,
                       teacher_dict[name].teacher_gender,
                       teacher_dict[name].teacher_course,
                       teacher_dict[name].teacher_pay,
                       teacher_dict[name].teacher_passwd)


class Student(Person):  ##定义student类,继承person类
    def __init__(self, student_name, student_age, student_gender, student_class, student_id, student_pay_money="n",
                 student_passwd="123456", student_course="null"):
        self.student_name = student_name
        self.student_age = student_age
        self.student_gender = student_gender
        self.student_class = student_class
        self.student_id = student_id
        self.student_pay_money = student_pay_money
        self.student_passwd = student_passwd
        self.student_course = student_course

    def student_study(self, student_name):
        if student_name in student_dict.keys():
            print "%s is learning" % (student_name)
            time.sleep(3)
            print "%s finish learning" % (student_name)

    def student_pay(self, stu_name):
        if student_dict[stu_name].student_pay_money == "y":
            print "you have paid for tuition already"
        else:
            while True:
                selection_of_course = school_course_dict.keys()
                student_choise_course = uih.all_user_input("chose your course %s \n choose : " % (selection_of_course))
                if student_choise_course in selection_of_course:
                    student_course = school_course_dict[student_choise_course]
                    while True:
                        print """
    ==================================                
    your choise is %s
    ================%s===============
    course_name         :\t%s
    course_price        :\t%s
    course_attribute    :\t%s
    course_cycle_time   :\t%s
    course_addr         :\t%s
    ==================================
                        """ % (student_choise_course, student_choise_course,
                               student_course.course_name,
                               student_course.course_price,
                               student_course.course_attribute,
                               student_course.course_cycle_time,
                               student_course.course_addr,)
                        stu_choise = uih.is_lower(
                            "confirm to pay for tuition %s yuan? \n choose( Y/N ) :" % (student_course.course_price,))
                        if stu_choise == "y":
                            print "confirm , please wait"
                            time.sleep(3)
                            print "Success!!"
                            student_dict[stu_name].student_pay_money = "y"
                            student_dict[stu_name].student_course = student_choise_course
                            break
                        elif stu_choise == "n":
                            print "you havn't pay for your tuition!"
                            break
                        else:
                            print "wrong input,plese choose again"
                            continue
                else:
                    print "wrong input,plese choose again"
                    continue
                break

    def student_show_info(self, stu_name):
        print """
    =============%s info===============
    student_name        :\t%s
    student_age         :\t%s
    student_gender      :\t%s
    student_class       :\t%s
    student_id          :\t%s
    student_pay_money   :\t%s
    student_password    :\t%s
    student_course      :\t%s
    ====================================
        """ % (student_dict[stu_name].student_name,
               student_dict[stu_name].student_name,
               student_dict[stu_name].student_age,
               student_dict[stu_name].student_gender,
               student_dict[stu_name].student_class,
               student_dict[stu_name].student_id,
               student_dict[stu_name].student_pay_money,
               student_dict[stu_name].student_passwd,
               student_dict[stu_name].student_course)


def student_info_input():
    student_name = uih.all_user_input("student name:")
    student_age = uih.is_num("student age:")
    student_gender = uih.all_user_input("student gender:")
    student_class = uih.all_user_input("student class:")
    student_id = uih.all_user_input("student id:")
    return (student_name, student_age, student_gender, student_class, student_id)


def teacher_info_input():
    teacher_name = uih.all_user_input("teacher name:")
    teacher_age = uih.is_num("teacher age:")
    teacher_gender = uih.all_user_input("teacher gender:")
    teacher_course = uih.all_user_input("teacher course:")
    teacher_pay = uih.all_user_input("teacher pay:")
    teacher_passwd = uih.all_user_input("teacher_passwd")
    return (teacher_name, teacher_age, teacher_gender, teacher_course, teacher_pay, teacher_passwd)


def coures_info_input():
    course_name = uih.all_user_input("course name:")
    course_price = uih.is_num("course price:")
    course_attribute = uih.all_user_input("course attribute:")
    course_cycle_time = uih.all_user_input("course cycle time:")
    course_addr = uih.all_user_input("course addr:")
    return (course_name, course_price, course_attribute, course_cycle_time, course_addr)


class School(object):  ##定义学校类,用来存放学校的属性和方法
    def __init__(self, school_name, school_addr, school_motto, school_passwd="123456"):  ##初始化,校名,校址,校训
        self.school_name = school_name
        self.school_addr = school_addr
        self.school_motto = school_motto
        self.school_passwd = school_passwd

    def recruit_students(self, student_name, student_age, student_gender, student_class, student_id,
                         student_pay_money="no", student_passwd="123456"):  ##定义招生的方法,学生用对象来存储
        global student_dict
        student_name = Student(student_name, student_age, student_gender, student_class, student_id,
                               student_pay_money, student_passwd)  ##实例化一个studentname对象

        student_dict.update({student_name.student_name: student_name})  ##字典存储学生实例

    def employ_teachers(self, teacher_name, teacher_age, teacher_gender, teacher_course, teacher_pay,
                        teacher_passwd="123456"):
        global teacher_dict
        teacher_name = Teacher(teacher_name, teacher_age, teacher_gender, teacher_course, teacher_pay, teacher_passwd)
        teacher_dict.update({teacher_name.teacher_name: teacher_name})  ##字典存储教师实例

    def creat_ourse(self, course_name, course_price, course_attribute, course_cycle_time, course_addr):
        global school_course_dict
        course_name = Course(course_name, course_price, course_attribute, course_cycle_time, course_addr)
        school_course_dict.update({course_name.course_name: course_name})


def main_function(print1, choice1, fun1=uih.is_num, print2="your choise:"):  # 定义功能菜单和用户输入
    print(print1)
    while True:
        function1_input = fun1(print2)
        if function1_input == "":
            continue
        elif function1_input in choice1:
            return function1_input
        else:
            print "wrong input,try again:"


def stu_log_in_function():
    user_name = uih.all_user_input("your name:")
    try_count = 0
    try:
        while True:
            if user_name in student_dict:
                user_passwd = uih.all_user_input("your passwd:")
                if student_dict[user_name].student_passwd == "null":
                    print "please register first"
                    break
                elif user_passwd == student_dict[user_name].student_passwd:
                    print "login ok!"
                    return user_name
                elif try_count >= 2:
                    print "you have try more than 3 times"
                    return "False"
                else:
                    print "wrong passwd,try again"
                    try_count += 1
                    continue
            else:
                print "your name is not in the student dictionary,try talk with school master!"
                return "False"
    except:
        print "your infomation is not exist"


def student_register():
    student_info_in = student_info_input()
    student_passwd = uih.all_user_input("passwd:")
    student_name_tmp = student_info_in[0]
    global student_dict
    print student_info_in[0]
    student_name_tmp = Student(student_info_in[0], student_info_in[1], student_info_in[2], student_info_in[3],
                               student_info_in[4], "n", student_passwd)
    student_dict.update({student_name_tmp.student_name: student_name_tmp})


def student_change_passwd(user_name):
    old_passwd = uih.all_user_input("please input your old passwd:")
    try:
        if old_passwd == student_dict[user_name].student_passwd:
            new_passwd1 = uih.all_user_input("please input your new passwd:")
            new_passwd2 = uih.all_user_input("again:")
            if new_passwd1 == new_passwd2:
                student_dict[user_name].student_passwd = new_passwd2
                print "update sucess"
            else:
                print "The two input results are inconsistent, please try again"
        else:
            print "wrong passwd"
    except:
        print "you haven't register"
        student_choose = uih.is_lower("register now?(Y/N):")
        if student_choose == "y":
            student_register()


def teac_log_in_function():
    user_name = uih.all_user_input("your name:")
    try_count = 0
    try:
        while True:
            if user_name in teacher_dict:
                user_passwd = uih.all_user_input("your passwd:")
                if user_passwd == teacher_dict[user_name].teacher_passwd:
                    print "login ok!"
                    return user_name
                elif try_count >= 2:
                    print "you have try more than 3 times"
                    return "False"
                else:
                    print "wrong passwd,try again"
                    try_count += 1
                    continue
            else:
                print "your name is not in the teacher dictionary,try talk with school master!"
                return "False"
    except:
        print "your infomation is not exist"


def teacher_change_passwd(user_name):
    old_passwd = uih.all_user_input("please input your old passwd:")
    try:
        if old_passwd == teacher_dict[user_name].teacher_passwd:
            new_passwd1 = uih.all_user_input("please input your new passwd:")
            new_passwd2 = uih.all_user_input("again:")
            if new_passwd1 == new_passwd2:
                teacher_dict[user_name].teacher_passwd = new_passwd2
                print "update sucess"
            else:
                print "The two input results are inconsistent, please try again"
        else:
            print "wrong passwd"
    except:
        print "you haven't register"


def mana_log_in_function():
    user_name = uih.all_user_input("your name:")
    try_count = 0
    try:
        while True:
            if user_name in manager_dict:
                user_passwd = uih.all_user_input("your passwd:")
                if user_passwd == manager_dict[user_name].school_passwd:
                    print "login ok!"
                    return user_name
                elif try_count >= 2:
                    print "you have try more than 3 times"
                    return "False"
                else:
                    print "wrong passwd,try again"
                    try_count += 1
                    continue
            else:
                print "your name is not in the teacher dictionary,try talk with school master!"
                return "False"
    except:
        print "your infomation is not exist"


def mana_change_passwd(user_name):
    old_passwd = uih.all_user_input("please input your old passwd:")
    try:
        if old_passwd == manager_dict[user_name].school_passwd:
            new_passwd1 = uih.all_user_input("please input your new passwd:")
            new_passwd2 = uih.all_user_input("again:")
            if new_passwd1 == new_passwd2:
                manager_dict[user_name].school_passwd = new_passwd2
                print "update sucess"
            else:
                print "The two input results are inconsistent, please try again"
        else:
            print "wrong passwd"
    except:
        print "you haven't register"


def main_menu():
    print1 = """
    ---------Welcome to the education management system---------
    ============================================================
                        Please select school
            1.%s
            2.%s
            3.quit
    ============================================================
    """ % (school_1.school_name, school_2.school_name)
    choice1 = "123"
    while True:
        main_function_choice = main_function(print1, choice1)
        if main_function_choice == "3":
            sys.exit()
        elif main_function_choice == "1":
            school_1_menu()
        elif main_function_choice == "2":
            school_1_menu()


def school_1_menu():
    print2 = """
    ---------------------------------------------
        1.student
        2.teacher
        3.mananger
        4.student_register
        5.return
        6.quit
    ---------------------------------------------"""
    choice3 = "123456"
    while True:
        school_function_choice = main_function(print2, choice3)
        if school_function_choice == "5":
            break
        elif school_function_choice == "6":
            sys.exit()
        elif school_function_choice == "1":
            student_menu()
        elif school_function_choice == "2":
            teacher_menu()
        elif school_function_choice == "3":
            manager_menu()
        elif school_function_choice == "4":
            student_register()


def student_menu():
    while True:
        stu_login_flag = stu_log_in_function()
        if stu_login_flag != "False":
            print_stu = """
    -----------------welcome----------------
    1.change password
    2.show my info
    3.Select courses and pay tuition fee
    4.return
    5.quit
    ----------------------------------------
            """
            choice_stu = "12345"
            while True:
                stu_function_choice = main_function(print_stu, choice_stu)
                if stu_function_choice == "4":
                    break
                elif stu_function_choice == "5":
                    sys.exit()
                elif stu_function_choice == "1":
                    student_change_passwd(stu_login_flag)
                elif stu_function_choice == "2":
                    student_dict[stu_login_flag].student_show_info(stu_login_flag)
                elif stu_function_choice == "3":
                    student_dict[stu_login_flag].student_pay(stu_login_flag)
            break
        else:
            break


def teacher_menu():
    while True:
        teac_login_flag = teac_log_in_function()
        if teac_login_flag != "False":
            print_teac = """
    -----------------welcome----------------
        1.have a class
        2.view student info
        3.change password
        4.show my info
        5.return
        6.quit
    ----------------------------------------
            """
            choice_teac = "123456"
            while True:
                teac_function_choice = main_function(print_teac, choice_teac)
                if teac_function_choice == "5":
                    break
                elif teac_function_choice == "6":
                    sys.exit()
                elif teac_function_choice == "1":
                    teacher_dict[teac_login_flag].teacher_teaching(teac_login_flag)
                elif teac_function_choice == "2":
                    teacher_dict[teac_login_flag].show_student_info()
                elif teac_function_choice == "3":
                    teacher_change_passwd(teac_login_flag)
                elif teac_function_choice == "4":
                    teacher_dict[teac_login_flag].show_teacher_info(teac_login_flag)
            break
        else:
            break


def manager_menu():
    while True:
        man_login_flag = mana_log_in_function()
        if man_login_flag != "False":
            print_man = """
    -----------------welcome----------------
    1.recruit_students
    2.employ_teachers
    3.creat_ourse
    4.change password
    5.return
    6.quit
    ----------------------------------------
            """
            choice_man = "123456"
            while True:
                stu_function_choice = main_function(print_man, choice_man)
                if stu_function_choice == "5":
                    break
                elif stu_function_choice == "6":
                    sys.exit()
                elif stu_function_choice == "1":
                    student_info_in = student_info_input()
                    student_passwd = uih.all_user_input("Please set the initial password for the student:")
                    student_pay = uih.is_lower("Paid for the tuition ? ( Y/N ):")

                    school_1.recruit_students(student_info_in[0], student_info_in[1], student_info_in[2],
                                              student_info_in[3], student_info_in[4], student_pay, student_passwd)
                elif stu_function_choice == "2":
                    teacher_info_in = teacher_info_input()
                    school_1.employ_teachers(teacher_info_in[0], teacher_info_in[1], teacher_info_in[2],
                                             teacher_info_in[3], teacher_info_in[4], teacher_info_in[5])
                elif stu_function_choice == "3":
                    cou_info_input = coures_info_input()
                    school_1.creat_ourse(cou_info_input[0], cou_info_input[1], cou_info_input[2],
                                         cou_info_input[3], cou_info_input[4])
                elif stu_function_choice == "4":
                    mana_change_passwd(man_login_flag)
            break
        break


def main():
    main_menu()


if __name__ == '__main__':
    teacher_dict = {}
    student_dict = {}
    manager_dict = {}
    school_course_dict = {}
    school_1 = School("xiyou", "shannxi_xi'an", "good good study,day day up")
    school_2 = School("xili", "shannxi_xi'an", "Motherland, honor, responsibility ")
    manager_dict.update({"xiyou": school_1,"xili":school_2})

    school_1.employ_teachers("laoli", "18", "male", "Linux", "260000")
    school_1.recruit_students("xiaoli", "16", "male", "basic_class", "00001", "y")
    school_1.recruit_students("xiaohua", "15", "female", "basic_class", "00002", "n")
    school_1.creat_ourse("C", "11800", "technology", "one year", "shannxi_xian")
    school_1.creat_ourse("Java", "13000", "technology", "7 month", "shannxi_xian")
    school_1.creat_ourse("RHCE", "16000", "technology", "4 month", "shannxi_xian")
    school_1.creat_ourse("Linux", "12300", "technology", "4 month", "shannxi_xian")

    main()
图片附太多也没有意义,只附一张表示运行通过吧。

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值