学生课程管理

学生类、课程类、教师类

学生类:学号、姓名、课程

课程类:课程号、课程名、课程备注、代课教师

教师类:教师编号、姓名

import sys


class Student:
    """保存学生的数据"""

    def __init__(self, no='', name='', cour=''):
        self.no = no
        self.name = name
        self.cour = cour

    def add(self):
        fs = open('student.txt')
        store = []
        msg = f'学号:{self.no}    姓名:{self.name} '
        with open('student.txt') as fss:
            s = fss.read()
            r1 = s.find(msg)
        if r1 != -1:
            for line in open('student.txt'):
                line = fs.readline()
                r = line.find(msg)
                if r != -1:
                    line = line.replace('\n', '') + self.cour + '   \n'
                store.append(line)
            with open('student.txt', 'w') as fs:
                for line in store:
                    fs.write(line)
        else:
            msg = f'学号:{self.no}    姓名:{self.name}    课程:{self.cour}   \n'
            with open('student.txt', 'a+') as fs:
                fs.write(msg)
        fs.close()

    def delete(self):
        print("1 按姓名或学号删除 \n2. 删除课程")
        a = input("请选择你要进行的操作:")
        b = ['1', '2']
        i = 0  # 判读文件中是否存在要删除的内容
        fs = open('student.txt')
        while a not in b:
            a = input("输入错误,请重新输入:")
        sth = input("选择要删除的内容:")
        if int(a) == 1:
            store = []
            for line in open('student.txt'):
                line = fs.readline()
                store.append(line)
            with open('student.txt', 'w') as fs:
                for line in store:
                    if sth not in line:
                        fs.write(line)
                    else:
                        fs.write()
                        i += 1
        elif int(a) == 2:
            fs = open('student.txt')
            store = []
            for line in open('student.txt'):
                line = fs.readline()
                r = line.find(sth)
                if r != -1:
                    line.replace(sth+"   ", '')
                    i += 1
                store.append(line)
        if i == 0:
            print("您所要删除的信息不存在")
        fs.close()

    def alter(self):
        i = 0
        no = input('输入学号:')
        name = input('输入姓名')
        msg = f'学号:{no}    姓名:{name}'
        no = input('输入修改后的学号:')
        name = input('输入修改后的姓名')
        cour = input('输入修改后的课程')
        msg2 = f'学号:{no}    姓名:{name}    课程:{cour}   \n'
        fs = open('student.txt')
        store = []
        for line in open('student.txt'):
            line = fs.readline()
            store.append(line)
        with open('student.txt', 'w') as fs:
            for line in store:
                r = line.find(msg)
                if r == -1:
                    fs.write(line)
                else:
                    fs.write(msg2)
                    i += 1
        if i == 0:
            print("您所要修改的信息不存在")
        fs.close()

    def search(self, sth):
        fs = open('student.txt')
        i = 0
        store = []
        for line in open('student.txt'):
            line = fs.readline()
            r = line.find(sth)
            if r != -1:
                print(line)
                i += 1
        if i == 0:
            print("您所要查找的信息不存在")
        fs.close()


class Teacher:

    """课程的相关信息"""

    def __init__(self, no='', name='', memo=''):
        self.no = no
        self.name = name
        self.memo = memo

    def add(self):
        fc = open('course.txt')
        msg = f'课程名:{self.name}    课程号:{self.no}    课程备注:{self.memo}   \n'
        with open('course.txt', 'a+') as fc:
            fc.write(msg)
        fc.close()

    def delete(self):
        sth = input("请选择你要删除的课程名或课程号:")
        fc = open('course.txt')
        store = []
        for line in open('course.txt'):
            line = fc.readline()
            store.append(line)
        with open('course.txt', 'w') as fc:
            for line in store:
                if sth not in line:
                    fc.write(line)
                else:
                    fc.write('')
        fc.close()

    def alter(self):
        no = input('输入课程号:')
        name = input('输入课程名')
        msg = f'课程名:{name}    课程号:{no}'
        no = input('输入修改后的课程号:')
        name = input('输入修改后的课程名')
        cour = input('输入修改后的备注')
        msg2 = f'课程名:{name}    课程号:{no}    备注:{cour}   \n'
        fc = open('course.txt')
        store = []
        for line in open('course.txt'):
            line = fc.readline()
            store.append(line)
        with open('course.txt', 'w') as fc:
            for line in store:
                r = line.find(msg)
                if r == -1:
                    fc.write(line)
                else:
                    fc.write(msg2)
        fc.close()

    def search(self, sth):
        fc = open('course.txt')
        i = 0
        for line in open('course.txt'):
            line = fc.readline()
            r = line.find(sth)
            if r != -1:
                print(line)
                i += 1
        if i == 0:
            print("查找信息不存在")
        fc.close()


class Course(Teacher):
    """除继承 Teacher 类 对课程的增删改查功能外,可增删改查代课教师"""

    def __init__(self, teacher=''):
        self.teacher = teacher

    def add_teacher(self):
        fc = open('course.txt')
        store = []
        i = 0  # 判断文件中是否存在该课程
        j = input("请输入该教师代课名称")
        for line in open('course.txt'):
            line = fc.readline()
            r = line.find(j)
            if r != -1:
                line = line.replace('\n', '') + '代课教师:' + self.teacher + '   \n'
                i += 1
            store.append(line)
        if i == 0:
            print("文件中不存在该课程")
        with open('course.txt', 'w') as fc:
            for line in store:
                fc.write(line)
        fc.close()

    def delete_teacher(self):
        sth = input("输入要删除的代课老师")
        fc = open('course.txt')
        store = []
        i = 0
        for line in open('course.txt'):
            line = fc.readline()
            store.append(line)
        with open('course.txt', 'w+'):
            for line in store:
                if sth not in line:
                    fc.write(line)
                else:
                    fc.write(line.replace('代课教师:'+sth, ''))
                    i += 1
        if i == 0:
            print("该课程不存在")
        fc.close()

    def alter_teacher(self):
        sth = input("请输入原代课教师:")
        msg = input("请输入修改后的代课教师:")
        fc = open('course.txt')
        store = []
        i = 0
        for line in open('course.txt'):
            line = fc.readline()
            store.append(line)
        with open('course.txt', 'w+'):
            for line in store:
                if sth not in line:
                    fc.write(line)
                else:
                    fc.write(line.replace(sth, msg))
                    i += 1
        if i == 0:
            print("原代课老师不存在!")
        fc.close()

    def search_teacher(self, sth):
        fc = open('course.txt')
        i = 0
        for line in open('course.txt'):
            line = fc.readline()
            r = line.find(sth)
            if r != -1:
                print(line)
                i += 1
        if i == 0:
            print("查找信息不存在")
        fc.close()


class Menu:
    """主菜单"""

    def menu(self):
        print("1.学生管理\n2.教师管理\n3.课程管理\n4.学生课程查询\n5.教师课表查询\n6.退出")
        a = input("请选择你要进行的操作:")
        b = ['1', '2', '3', '4', '5', '6']
        while a not in b:
            a = input("输入错误,请重新输入:")
        if int(a) == 1:
            Menu.student(self)
        elif int(a) == 2:
            Menu.teacher(self)
        elif int(a) == 3:
            Menu.course(self)
        elif int(a) == 4:
            Menu.print_student(self)
        elif int(a) == 5:
            Menu.print_teacher(self)
        elif int(a) == 6:
            sys.exit()

    def student(self):
        print("1.增加信息\n2.删除信息\n3.修改信息\n4.查找信息\n5.返回主菜单\n6.退出")
        a = input("请选择你要进行的操作:")
        b = ['1', '2', '3', '4', '5', '6', '7']
        if int(a) == 1:
            no = input("请输入添加的学号:")
            name = input("请输入添加的姓名:")
            course = input("请输入添加的课程:")
            stu = Student(no, name, course)
            stu.add()
            Menu.menu(self)
        elif int(a) == 2:
            stu = Student()
            stu.delete()
            Menu.menu(self)
        elif int(a) == 3:
            stu = Student()
            stu.alter()
            Menu.menu(self)
        elif int(a) == 4:
            stu = Student()
            sth = input("输入你要查询的学号或姓名:")
            stu.search(sth)
            Menu.menu(self)
        elif int(a) == 5:
            Menu.menu(self)
        elif int(a) == 6:
            sys.exit()

    def course(self):
        print("1.增加信息\n2.删除信息\n3.修改信息\n4.查找信息\n5.返回主菜单\n6.退出")
        a = input("请选择你要进行的操作:")
        b = ['1', '2', '3', '4', '5', '6', '7']
        if int(a) == 1:
            no = input("请输入添加的代课教师:")
            cou = Course(no)
            cou.add_teacher()
            Menu.menu(self)
        elif int(a) == 2:
            cou = Course()
            cou.delete_teacher()
            Menu.menu(self)
        elif int(a) == 3:
            cou = Course()
            cou.alter_teacher()
            Menu.menu(self)
        elif int(a) == 4:
            cou = Course()
            sth = input("输入你要查询的代课教师姓名:")
            cou.search_teacher(sth)
            Menu.menu(self)
        elif int(a) == 5:
            Menu.menu(self)
        elif int(a) == 6:
            sys.exit()

    def teacher(self):
        print("1.增加信息\n2.删除信息\n3.修改信息\n4.查找信息\n5.返回主菜单\n6.退出")
        a = input("请选择你要进行的操作:")
        b = ['1', '2', '3', '4', '5', '6', '7']
        if int(a) == 1:
            no = input("请输入添加的课程号:")
            name = input("请输入添加的课程名:")
            memo = input("请输入添加的备注:")
            tea = Teacher(no, name, memo)
            tea.add()
            Menu.menu(self)
        elif int(a) == 2:
            tea = Teacher()
            tea.delete()
            Menu.menu(self)
        elif int(a) == 3:
            tea = Teacher()
            tea.alter()
            Menu.menu(self)
        elif int(a) == 4:
            tea = Teacher()
            sth = input("输入你要查询的课程号或课程名:")
            tea.search(sth)
            Menu.menu(self)
        elif int(a) == 5:
            Menu.menu(self)
        elif int(a) == 6:
            sys.exit()

    def print_student(self):
        fs = open('student.txt')
        sth = input("请输入学生姓名或学号:")
        i = 0
        for line in open('student.txt'):
            line = fs.readline()
            r = line.find(sth)
            if r != -1:
                msg = line
                i += 1
        if i == 0:
            print("您所要查找的信息不存在")
        fc = open('course.txt')
        for line in open('course.txt'):
            line = fc.readline()
            r1 = line.find("课程名:")
            r2 = line.find(" ")
            slice_course = line[r1 + 4:r2]
            if slice_course in msg:
                print(line.replace('\n', ''))
        fs.close()
        fc.close()
        Menu.menu(self)

    def print_teacher(self):
        sth = input("请输入教师姓名:")
        i = 0
        fc = open('course.txt')
        for line in open('course.txt'):
            if sth in line:
                print(line)
        Menu.menu(self)


a = Menu()
a.menu()

功能菜单

学生管理:增删改查学生基本信息(学号、姓名);学生课程管理(增删改查)

课程管理:增删改查课程基本信息(课程号、课程名、课程备注);课程代课教师管理(增删改查)

教师管理:增删改查课程基本信息

教师编号给忘了,没有写,不过大致和前面相同,第一个学生类完成后,其他类按照学生类改一改就行了。如果显示找不到文件,在当前python文件所在目录添加那两个文本文件即可。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

韶光换y

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值