程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

小白學python系列————【Day50】選課系統項目

編輯:Python

今日內容概要

  • 選課系統項目分析
  • 選課系統架構設計
  • 項目源碼展示

選課系統項目分析

1.選課系統項目需求
選課系統
角色:學校、學員、課程、講師
要求:
1. 創建北京、上海 2 所學校
2. 創建linux , python , go 3個課程 , linux\py 在北京開, go 在上海開
3. 課程包含,周期,價格,通過學校創建課程
4. 通過學校創建班級, 班級關聯課程、講師5. 創建學員時,選擇學校,關聯班級
5. 創建講師角色時要關聯學校,
6. 提供三個角色接口
6.1 學員視圖, 可以注冊, 交學費, 選擇班級,
6.2 講師視圖, 講師可管理自己的班級, 上課時選擇班級, 查看班級學員列表 , 修改所管理的學員的成績
6.3 管理視圖,創建講師, 創建班級,創建課程
7. 上面的操作產生的數據都通過pickle序列化保存到文件裡

2.選課系統功能分析
管理功能(最核心)

  • 注冊功能
  • 登錄功能
  • 創建講師
  • 創建學校
  • 創建課程

講師功能

  • 登錄功能
  • 查看教授課程
  • 選擇教授課程
  • 查看課程學生
  • 評判學生分數

學生功能

  • 注冊功能
  • 登錄功能
  • 選擇學校
  • 選擇課程
  • 查看分數

系統架構設計

1.三層架構
功能展示層

  • src.py
  • admin_view.py
  • teacher_view.py
  • student_view.py

核心邏輯層

  • admin_interface.py
  • teacher_interface.py
  • student_interface.py

數據處理層

  • db_hanlder.py
  • model.py

項目源碼展示

start.py文件

import sys
import os
base_dir = os.path.dirname(__file__)
sys.path.append(base_dir)
if __name__ == '__main__':
from core import src
src.run()

conf文件夾下settings.py文件

import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
DB_DIR = os.path.join(BASE_DIR,'db')

lib文件夾下common.py文件

# 三個部分都要調用該裝飾器,所以使用有參裝飾器
def login_auth(user_type):
def outer(func_name):
def inner(*args, **kwargs):
from core import admin_view, teacher_view, student_view
module_dict = {

'admin': admin_view,
'teacher': teacher_view,
'student': student_view
}
module_name = module_dict.get(user_type)
if module_name.login_state.get('name'):
res = func_name(*args, **kwargs)
return res
else:
print("%s暫無登錄,請先登錄" % user_type)
module_name.login()
return inner
return outer

core文件夾下src.py文件

from core import admin_view, teacher_view, student_view
module_dict = {

'1': admin_view,
'2': teacher_view,
'3': student_view
}
def run():
while True:
print(f""" {
'=' * 15}選課系統{
'=' * 15} 1.管理員視圖 2.講師視圖 3.學生視圖 {
'=' * 37} """)
choice = input('請輸入您想要執行的視圖編號>>>:').strip()
if choice in module_dict:
module_name = module_dict.get(choice)
module_name.run()
else:
print('暫無該視圖編號')

core文件夾下admin.view.py文件

from interface import admin_interface
from lib import common
login_state = {

'name': None
}
def register():
username = input('username>>>:').strip()
password = input('password>>>:').strip()
real_pwd = input('real_pwd>>>:').strip()
if password != real_pwd:
return
flag, msg = admin_interface.register_interface(username, password)
print(msg)
def login():
username = input('username>>>:').strip()
password = input('password>>>:').strip()
flag, msg = admin_interface.login_interface(username, password)
if flag:
login_state['name'] = username
print(msg)
@common.login_auth('admin')
def creat_teacher():
# 獲取講師信息
teacher_name = input('teacher_name>>>:').strip()
# 調用接口
flag,msg = admin_interface.creat_teacher_interface(teacher_name,login_state.get('name'))
print(msg)
@common.login_auth('admin')
def creat_school():
# 獲取學校信息
school_name = input('school_name>>>:').strip()
school_addr = input('school_addr>>>:').strip()
# 調用接口創建
'''只要有管理員才可以創建學校,所以應該把管理員姓名形參傳進去,日志需要'''
flag,msg = admin_interface.creat_school_interface(school_name,school_addr,login_state.get('name'))
print(msg)
# @common.login_auth('admin')
# def creat_course():
# # 直接調用接口
# flag,msg = admin_interface.creat_course_interface(login_state.get('name'))
# print(msg)
@common.login_auth('admin')
def creat_course():
flag, msg_or_school_list = admin_interface.get_all_school_interface()
if not flag:
print(msg_or_school_list)
# 選擇學校
while True:
# 枚舉,循環打印學校名稱
for school_id, school_name in enumerate(msg_or_school_list, start=1):
print(f'學校編號{
school_id} 學校名稱{
school_name}')
# 選擇某個學校
target_id = input('請輸入創建課程的學校編號>>>:').strip()
if target_id == 'q':
break
if not target_id.isdigit():
print('編號必須是數字')
continue
target_id = int(target_id)
if target_id not in range(1, len(msg_or_school_list) + 1):
print('編號超出范圍')
continue
school_name = msg_or_school_list[target_id - 1]
# 獲取課程數據
course_name = input('請輸入課程名稱>>>:').strip()
course_price = input('請輸入課程價格>>>:').strip()
course_period = input('請輸入課程周期時間>>>:').strip()
flag, msg = admin_interface.creat_course_interface(school_name,course_name,course_price,course_period, login_state.get('name'))
print(msg)
func_dict = {

'1': register,
'2': login,
'3': creat_teacher,
'4': creat_school,
'5': creat_course
}
def run():
while True:
print(f""" {
'=' * 15}選課系統{
'=' * 15} 1.注冊功能 2.登錄功能 3.創建講師 4.創建學校 5.創建課程 {
'=' * 37} """)
choice = input('請輸入您想要執行的功能編號>>>:').strip()
if choice == 'q':
return
if choice in func_dict:
func_name = func_dict.get(choice)
func_name()
else:
print('暫無該編號')

core文件夾下student.view.py文件

from interface import admin_interface
from lib import common
login_state = {

'name': None
}
def register():
username = input('username>>>:').strip()
password = input('password>>>:').strip()
real_pwd = input('real_pwd>>>:').strip()
if password != real_pwd:
return
flag, msg = admin_interface.register_interface(username, password)
print(msg)
def login():
username = input('username>>>:').strip()
password = input('password>>>:').strip()
flag, msg = admin_interface.login_interface(username, password)
if flag:
login_state['name'] = username
print(msg)
@common.login_auth('admin')
def creat_teacher():
# 獲取講師信息
teacher_name = input('teacher_name>>>:').strip()
# 調用接口
flag,msg = admin_interface.creat_teacher_interface(teacher_name,login_state.get('name'))
print(msg)
@common.login_auth('admin')
def creat_school():
# 獲取學校信息
school_name = input('school_name>>>:').strip()
school_addr = input('school_addr>>>:').strip()
# 調用接口創建
'''只要有管理員才可以創建學校,所以應該把管理員姓名形參傳進去,日志需要'''
flag,msg = admin_interface.creat_school_interface(school_name,school_addr,login_state.get('name'))
print(msg)
# @common.login_auth('admin')
# def creat_course():
# # 直接調用接口
# flag,msg = admin_interface.creat_course_interface(login_state.get('name'))
# print(msg)
@common.login_auth('admin')
def creat_course():
flag, msg_or_school_list = admin_interface.get_all_school_interface()
if not flag:
print(msg_or_school_list)
# 選擇學校
while True:
# 枚舉,循環打印學校名稱
for school_id, school_name in enumerate(msg_or_school_list, start=1):
print(f'學校編號{
school_id} 學校名稱{
school_name}')
# 選擇某個學校
target_id = input('請輸入創建課程的學校編號>>>:').strip()
if target_id == 'q':
break
if not target_id.isdigit():
print('編號必須是數字')
continue
target_id = int(target_id)
if target_id not in range(1, len(msg_or_school_list) + 1):
print('編號超出范圍')
continue
school_name = msg_or_school_list[target_id - 1]
# 獲取課程數據
course_name = input('請輸入課程名稱>>>:').strip()
course_price = input('請輸入課程價格>>>:').strip()
course_period = input('請輸入課程周期時間>>>:').strip()
flag, msg = admin_interface.creat_course_interface(school_name,course_name,course_price,course_period, login_state.get('name'))
print(msg)
func_dict = {

'1': register,
'2': login,
'3': creat_teacher,
'4': creat_school,
'5': creat_course
}
def run():
while True:
print(f""" {
'=' * 15}選課系統{
'=' * 15} 1.注冊功能 2.登錄功能 3.創建講師 4.創建學校 5.創建課程 {
'=' * 37} """)
choice = input('請輸入您想要執行的功能編號>>>:').strip()
if choice == 'q':
return
if choice in func_dict:
func_name = func_dict.get(choice)
func_name()
else:
print('暫無該編號')

core文件夾下teacher.view.py文件

from lib import common
from db import models
from interface import teacher_interface
login_state = {

'name': None
}
def login():
username = input('username>>>:').strip()
password = input('password>>>:').strip()
flag, msg = teacher_interface.login_interface(username, password)
if flag:
login_state['name'] = username
print(msg)
@common.login_auth('teacher')
def check_teach_course():
flag,msg = teacher_interface.check_teach_course_interface(login_state.get('name'))
print(msg)
@common.login_auth('teacher')
def select_teach_course():
# 首先獲取所有學校供講師選擇
flag, msg_or_school_list = teacher_interface.get_all_school_interface()
if not flag:
print(msg_or_school_list)
return
# 選擇學校
while True:
# 枚舉,循環打印學校名稱
for school_id, school_name in enumerate(msg_or_school_list, start=1):
print(f'學校編號{
school_id} 學校名稱{
school_name}')
# 選擇某個學校
target_id = input('請輸入創建課程的學校編號>>>:').strip()
if target_id == 'q':
break
if not target_id.isdigit():
print('編號必須是數字')
continue
target_id = int(target_id)
if target_id not in range(1, len(msg_or_school_list) + 1):
print('編號超出范圍')
continue
school_name = msg_or_school_list[target_id - 1]
# 獲取某個學校下課程供老師選擇
flag, msg_or_course_list = teacher_interface.get_all_course_interface(school_name)
# 循環打印
while True:
for course_id,course_name in enumerate(msg_or_course_list,start=1):
print(f'課程編號{
course_id} 課程{
course_name}')
# 選擇某個學校
target_id = input('請輸入選擇教授的課程編號>>>:').strip()
if target_id == 'q':
break
if not target_id.isdigit():
print('編號必須是數字')
continue
target_id = int(target_id)
if target_id not in range(1, len(msg_or_course_list) + 1):
print('編號超出范圍')
continue
course_name = msg_or_course_list[target_id - 1]
flag,msg = teacher_interface.select_teach_course_interface(course_name,login_state.get('name'))
print(msg)
@common.login_auth('teacher')
def check_course_student():
# 直接展示所有課程,然後展示課程下的學生
flag,course_name_list = teacher_interface.get_all_course_name_interface()
if not flag:
print(course_name_list)
# 循環打印課程供講師選擇
while True:
for course_id,course_name in enumerate(course_name_list,start=1):
print(f'課程編號{
course_id} 課程{
course_name}')
# 選擇某個學校
target_id = input('請輸入選擇教授的課程編號>>>:').strip()
if target_id == 'q':
break
if not target_id.isdigit():
print('編號必須是數字')
continue
target_id = int(target_id)
if target_id not in range(1, len(course_name_list) + 1):
print('編號超出范圍')
continue
course_name = course_name_list[target_id - 1]
flag,msg = teacher_interface.check_course_student_interface(course_name)
print(msg)
@common.login_auth('teacher')
def update_course_score():
"""直接展示所有數據,然後展示課程下的學生,最後選擇學生,修改分數"""
flag,course_name_list = teacher_interface.get_all_course_name_interface()
if not flag:
print(course_name_list)
# 循環打印
while True:
# 循環打印所有的學校名稱供管理員選擇
for course_id, course_name in enumerate(course_name_list, start=1):
print("課程編號:%s 課程名稱:%s" % (course_id, course_name))
# 選擇學校
target_id = input('請輸入想要查看的課程編號>>>(q):').strip()
if target_id == 'q':
return
if not target_id.isdigit():
print('編號必須是純數字')
continue
target_id = int(target_id)
if target_id not in range(1, len(course_name_list) + 1):
print('編號超出了范圍')
continue
course_name = course_name_list[target_id - 1]
flag, student_name_list = teacher_interface.check_course_student_interface(course_name)
# 循環打印學生姓名
while True:
# 循環打印
for course_id, student_name in enumerate(student_name_list, start=1):
print("學生編號:%s 學生名稱:%s" % (course_id, student_name))
target_id = input('請輸入想要修改分數的學生編號>>>(q):').strip()
if target_id == 'q':
return
if not target_id.isdigit():
print('編號必須是純數字')
continue
target_id = int(target_id)
if target_id not in range(1, len(student_name_list) + 1):
print('編號超出了范圍')
continue
student_name = student_name_list[target_id - 1]
score = input('請輸入該學生的分數>>>:').strip()
flag,msg = teacher_interface.update_course_score_interface(student_name,course_name,score,login_state.get('name'))
print(msg)
func_dic = {

'1': login,
'2': check_teach_course,
'3': select_teach_course,
'4': check_course_student,
'5': update_course_score
}
def run():
while True:
print(f""" {
'=' * 15}選課系統{
'=' * 15} 1.登錄功能 2.查看教授課程 3.選擇教授課程 4.查看課程學生 5.評判學生分數 {
'=' * 37} """)
choice = input('請輸入您想要執行的功能編號>>>:').strip()
if choice == 'q':
return
if choice in func_dic:
func_name = func_dic.get(choice)
func_name()
else:
print('暫無該編號')

interface文件夾下admin_interface.py文件

from db import models
def register_interface(username, password):
# 1.驗證用戶名是否已存在
admin_obj = models.Admin.check_obj(username)
if admin_obj:
return False, f'{
username}已存在'
# 2.完成注冊
models.Admin(username, password)
# admin_obj.save_obj()
return True, f'{
username}注冊成功'
def login_interface(username, password):
admin_cls = models.Admin.check_obj(username) # <db.models.Admin object at 0x000001DCD60AD668>
print(admin_cls)
if not admin_cls:
return False, f'{
username}不存在'
if admin_cls.pwd == password:
return True, f'{
username}登錄成功'
return False, f'{
username}密碼錯誤'
def creat_teacher_interface(teacher_name, admin_name):
# 校驗老師是否存在
teacher_obj = models.Teacher.check_obj(teacher_name)
if teacher_obj:
return False, f'講師{
teacher_name}已存在'
# 獲取管理員功能
admin_obj = models.Admin.check_obj(admin_name)
admin_obj.creat_teacher(teacher_name)
return True, f'管理員:{
admin_name} 創建了老師賬戶:{
teacher_name}'
def creat_school_interface(school_name, school_addr, admin_name):
# 校驗學校是否存在
school_obj = models.School.check_obj(school_name)
if school_obj:
return False, f'學校{
school_name}已存在'
# 獲取管理員功能
admin_obj = models.Admin.check_obj(admin_name)
admin_obj.creat_school(school_name, school_addr)
return True, f"管理員{
admin_name} 創建了學校{
school_name} 地點在{
school_addr}"
def get_all_school_interface():
school_obj_list = models.School.get_all_obj() # [1,2,3]
if not school_obj_list:
return False, '當前沒有創建任何學校'
return True, school_obj_list
def creat_course_interface(school_name, course_name, course_price, course_period, admin_name):
# 1.查看當前課程是否存在
""" 如何確保課程名稱不沖突 不同學校可以有相同課程 提議:在課程名的前面拼接學校名稱 """
course_name = f'{
school_name}_{
course_name}'
is_course_obj = models.Course.check_obj(course_name)
if is_course_obj:
return False, f'當前課程{
course_name}已存在'
admin_obj = models.Admin.check_obj(admin_name)
admin_obj.creat_course(course_name, course_price, course_period,school_name)
school_obj = models.School.check_obj(school_name)
school_obj.course_list.append(course_name)
school_obj.save_obj()
return True, f'管理員:{
admin_name} 在學校:{
school_name} 創建了課程:{
course_name}'

interface文件夾下student_interface.py文件

from db import models
def register_interface(username, password):
# 1.驗證用戶名是否已存在
admin_obj = models.Student.check_obj(username)
if admin_obj:
return False, f'{
username}已存在'
# 2.完成注冊
models.Student(username, password)
return True, f'{
username}注冊成功'
def login_interface(username, password):
admin_cls = models.Student.check_obj(username) # <db.models.Admin object at 0x000001DCD60AD668>
print(admin_cls)
if not admin_cls:
return False, f'{
username}不存在'
if admin_cls.pwd == password:
return True, f'{
username}登錄成功'
return False, f'{
username}密碼錯誤'
def get_all_school_interface():
school_obj_list = models.School.get_all_obj() # [1,2,3]
if not school_obj_list:
return False, '當前沒有創建任何學校'
return True, school_obj_list
def choise_school_interface(student_name):
student_obj = models.Student.check_obj(student_name)
if not student_obj.school:
return True,f'學員{
student_name}還沒選擇學校'
return False,f'學院{
student_name}已選擇學校{
student_obj.school_name}'
def select_school_interface(school_name,student_name):
student_obj = models.Student.check_obj(student_name)
student_obj.school = school_name
student_obj.save_obj()
return True,f'學員{
student_name} 選擇去{
school_name}學習'
def get_all_course_interface():
course_obj_list = models.Course.get_all_obj() # [1,2,3]
if not course_obj_list:
return False, '當前沒有創建任何課程'
return True, course_obj_list
def choise_course_interface(student_name):
student_obj = models.Student.check_obj(student_name)
if not student_obj.course_list:
return True, f'學員{
student_name}還沒選擇課程'
return False, f'學員{
student_name}已選擇課程{
student_obj.course_list}'
def select_course_interface(course_name,student_name):
student_obj = models.Student.check_obj(student_name)
if course_name in student_obj.course_list:
return False,f'你已經選過該課程'
student_obj.course_list.append(course_name)
# 選課後分數默認值是0
student_obj.score[course_name] = None
student_obj.save_obj()
# 編輯課程類中課程列表對象
course_obj = models.Course.check_obj(course_name)
course_obj.course_student_list.append(student_name)
course_obj.save_obj() # ['上海中醫藥大學_chinese', '北京大學_math', '山西大學_english']
return True, f'學員:{
student_name},選擇了課程:{
course_name}'
def check_socore_interface(student_name):
student_obj = models.Student.check_obj(student_name)
print(student_obj.score)
return True,student_obj.score

interface文件夾下teacher_interface.py文件

from db import models
def login_interface(username, password):
admin_cls = models.Teacher.check_obj(username) # <db.models.Admin object at 0x000001DCD60AD668>
if not admin_cls:
return False, f'{
username}不存在'
# print(admin_cls.pwd) # 默認密碼為123
if admin_cls.pwd == int(password):
return True, f'{
username}登錄成功'
return False, f'{
username}密碼錯誤'
def check_teach_course_interface(teacher_name):
teacher_obj = models.Teacher.check_obj(teacher_name)
if not teacher_obj.teach_course:
return False,f'講師{
teacher_name}還未選擇教授任何一門課程!'
return True,teacher_obj.teach_course
def get_all_school_interface():
school_obj_list = models.School.get_all_obj() # [1,2,3]
if not school_obj_list:
return False, '當前沒有創建任何學校'
return True, school_obj_list
def get_all_course_interface(school_name):
school_obj = models.School.check_obj(school_name)
return True,school_obj.course_list
def select_teach_course_interface(course_name,teacher_name):
teacher_obj = models.Teacher.check_obj(teacher_name)
if course_name in teacher_obj.teach_course:
return False, f'您已經選擇了教授該課程:{
course_name}'
teacher_obj.teach_course.append(course_name)
teacher_obj.save_obj()
return True, f'您已經成功選擇教授課程:{
course_name}'
def get_all_course_name_interface():
course_obj_list = models.Course.get_all_obj() # [1,2,3]
if not course_obj_list:
return False, '當前沒有創建任何學校'
return True, course_obj_list
def check_course_student_interface(course_name):
course_obj = models.Course.check_obj(course_name)
return True,course_obj.course_student_list
def update_course_score_interface(student_name,course_name,score,teacher_name):
# 添加分數數據到學生對象中
student_obj = models.Student.check_obj(student_name)
student_obj.score[course_name] = score
student_obj.save_obj()
# 添加學生分數數據到課程對象中
course_obj = models.Course.check_obj(course_name)
course_obj.course_score_dict[student_name] = score
course_obj.save_obj()
return True,f'講師{
teacher_name} 將課程{
course_name}下的學生{
student_name}分數修改為{
score}'

db文件夾下db_handler.py文件

import os
from conf import settings
import pickle
def save_obj(obj):
# 由於db目錄下將來需要存各個角色的文件數據,為了便於管理
# 類名叫什麼文件夾的名字就叫什麼
admin_path = os.path.join(settings.DB_DIR, obj.__class__.__name__)
if not os.path.exists(admin_path):
os.mkdir(admin_path)
# 1.拼接對象文件路徑
obj_file_path = os.path.join(admin_path, obj.name)
# 2.pickle模塊寫入
with open(obj_file_path, 'wb')as f:
pickle.dump(obj, f)
def select_obj(cls, username):
admin_path = os.path.join(settings.DB_DIR, cls.__name__)
if not os.path.exists(admin_path):
os.mkdir(admin_path)
# 1.拼接對象文件路徑
obj_file_path = os.path.join(admin_path, username)
# 如果路徑存在,則獲取對象,增強方法兼容性
if os.path.exists(obj_file_path):
# 2.pickle模塊讀
with open(obj_file_path, 'rb')as f:
data = pickle.load(f)
return data
def select_all_obj(cls):
# 1.拼接類對象的文件夾路徑
dir_path = os.path.join(settings.DB_DIR, cls.__name__)
if not os.path.exists(dir_path):
os.mkdir(dir_path)
# 2.獲取文件夾路徑下所有的文件名稱
return os.listdir(dir_path)
if __name__ == '__main__':
class MyClass:
pass
print(os.listdir(r'D:\pythonProject\day35_ccs\db\School'))

db文件夾下models.py文件

from db import db_handler
# 定義一個父類(所有子類所共有的方法)
class BaseClass(object):
def save_obj(self):
db_handler.save_obj(self)
@classmethod
def check_obj(cls, username):
return db_handler.select_obj(cls, username)
@classmethod
def get_all_obj(cls):
return db_handler.select_all_obj(cls)
class Admin(BaseClass):
def __init__(self, name, pwd):
self.name = name
self.pwd = pwd
self.save_obj()
def creat_school(self, school_name, school_addr):
School(school_name, school_addr)
def creat_teacher(self, teacher_name,pwd=123):
Teacher(teacher_name,pwd)
def creat_course(self, course_name, course_price, course_period, school_name):
Course(course_name, course_price, course_period, school_name)
class Student(BaseClass):
def __init__(self,name,pwd):
self.name = name
self.pwd = pwd
self.school = None
self.score = {
} # {‘課程1’:100,‘課程2’:90}
self.course_list = []
self.save_obj()
class School(BaseClass):
def __init__(self, name, addr):
self.name = name # 此處name不可以取值school_name,原因是在db_handler中用到了obj.name
self.addr = addr
self.course_list = []
self.save_obj()
class Teacher(BaseClass):
def __init__(self, name, pwd):
self.name = name
self.pwd = pwd
self.teach_course = []
self.save_obj()
class Course(BaseClass):
def __init__(self, course_name, course_price, course_period, school_name):
self.name = course_name
self.course_price = course_price
self.course_period = course_period
self.school = school_name
self.course_student_list = []
self.course_score_dict = {
} # {'學生1':分數1, '學生2':分數2}
self.save_obj()

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved