Facebook
From Saif, 1 Year ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 181
  1. import random
  2. import sys
  3. import datetime
  4.  
  5.  
  6. class StudentManagementSystem:
  7.     def __init__(self):
  8.         self.students = []
  9.         self.courses = []
  10.         self.passed_courses = []
  11.  
  12.     def add_student(self):
  13.         first_name = input("Enter the first name of the student: ")
  14.         last_name = input("Enter the last name of the student: ")
  15.         major = input("Select student's major (CE, EE, ET, ME, SE): ")
  16.  
  17.         if not self.is_valid_name(first_name) or not self.is_valid_name(last_name):
  18.             print("Names should contain only letters and start with capital letters.")
  19.             return
  20.  
  21.         current_year = datetime.datetime.now().year
  22.         student_id = random.randint(10000, 99999)
  23.  
  24.         email = f"{first_name.lower()}.{last_name.lower()}@lut.fi"
  25.         student_data = (student_id, first_name, last_name, current_year, major, email)
  26.         self.students.append(student_data)
  27.  
  28.         with open("students.txt", "a") as f:
  29.             f.write(','.join(map(str, student_data)) + 'n')
  30.  
  31.         print("Student added successfully!")
  32.  
  33.     def search_items(self, file_path, search_term, item_type):
  34.         if len(search_term) < 3:
  35.             print("Search term should contain at least 3 characters.")
  36.             return
  37.  
  38.         with open(file_path, "r") as f:
  39.             items = f.readlines()
  40.  
  41.         matching_items = [item for item in items if search_term in item]
  42.         if matching_items:
  43.             print(f"Matching {item_type}s:")
  44.             for item in matching_items:
  45.                 item_info = item.strip().split(",")
  46.                 print(f"ID: {item_info[0]}, {item_type.capitalize()}: {item_info[1]}")
  47.         else:
  48.             print(f"No matching {item_type}s found.")
  49.  
  50.     def search_student(self):
  51.         search_term = input("Give at least 3 characters of the student's first or last name: ")
  52.         self.search_items("students.txt", search_term, "student")
  53.  
  54.     def search_course(self):
  55.         search_term = input("Give at least 3 characters of the course name or teacher's name: ")
  56.         self.search_items("courses.txt", search_term, "course")
  57.  
  58.     def is_valid_name(self, name):
  59.         return name.isalpha() and name.istitle()
  60.  
  61.     def add_course_completion(self):
  62.         course_id = input("Give the course ID: ")
  63.         student_id = input("Give the student ID: ")
  64.  
  65.         grade = input("Give the grade (1-5): ")
  66.         if not grade.isdigit() or not (1 <= int(grade) <= 5):
  67.             print("Grade is not a correct grade.")
  68.             return
  69.  
  70.         date_str = input("Enter a date (DD/MM/YYYY): ")
  71.         try:
  72.             date = datetime.datetime.strptime(date_str, "%d/%m/%Y")
  73.             today = datetime.datetime.now()
  74.  
  75.             if date > today:
  76.                 print("Input date is later than today. Try again!")
  77.                 return
  78.  
  79.             if (today - date).days > 30:
  80.                 print("Input date is older than 30 days. Contact 'opinto'.")
  81.                 return
  82.         except ValueError:
  83.             print("Invalid date format. Use DD/MM/YYYY. Try again!")
  84.             return
  85.  
  86.         with open("passed.txt", "r") as f:
  87.             passed_courses = f.readlines()
  88.  
  89.         for i, line in enumerate(passed_courses):
  90.             if line.startswith(course_id + "," + student_id):
  91.                 old_grade = int(line.strip().split(",")[-1])
  92.                 if int(grade) <= old_grade:
  93.                     print(f"Student has passed this course earlier with grade {old_grade}")
  94.                     return
  95.                 passed_courses[i] = f"{course_id},{student_id},{date_str},{grade}n"
  96.                 with open("passed.txt", "w") as f:
  97.                     f.writelines(passed_courses)
  98.                 print("Record updated!")
  99.                 return
  100.  
  101.         with open("passed.txt", "a") as f:
  102.             f.write(f"{course_id},{student_id},{date_str},{grade}n")
  103.         print("Record added!")
  104.  
  105.     def show_student_record(self):
  106.         student_id = input("Enter the student ID: ")
  107.  
  108.         with open("students.txt", "r") as f:
  109.             students = f.readlines()
  110.  
  111.         student_info = [s.strip().split(",") for s in students if student_id in s][0]
  112.  
  113.         with open("passed.txt", "r") as f:
  114.             passed_courses = f.readlines()
  115.  
  116.         with open("courses.txt", "r") as f:
  117.             courses_info = f.readlines()
  118.  
  119.         student_courses = []
  120.         total_credits = 0
  121.         total_grade = 0
  122.  
  123.         for line in passed_courses:
  124.             if line.startswith(student_id):
  125.                 course_id, date_str, grade = line.strip().split(",")[:3]
  126.                 total_grade += int(grade)
  127.                 date = datetime.datetime.strptime(date_str, "%d/%m/%Y")
  128.                 course_info = [c.strip().split(",") for c in courses_info if course_id in c][0]
  129.                 course_name, credits, teachers = course_info[1], course_info[2], course_info[3]
  130.                 student_courses.append((course_id, course_name, credits, date_str, teachers, grade))
  131.                 total_credits += int(credits)
  132.  
  133.         if student_info:
  134.             print("Student ID:", student_id)
  135.             print("Name:", student_info[1], student_info[2])
  136.             print("Starting year:", student_info[3])
  137.             print("Major:", student_info[4])
  138.             print("Email:", student_info[5])
  139.  
  140.             if student_courses:
  141.                 print("Passed courses:")
  142.                 for course in student_courses:
  143.                     course_id, course_name, credits, date_str, teachers, grade = course
  144.                     print(f"Course ID: {course_id}, Name: {course_name}, Credits: {credits}, Date: {date_str}, Teacher(s): {teachers}, Grade: {grade}")
  145.  
  146.                 average_grade = total_grade / len(student_courses)
  147.                 print(f"Total credits: {total_credits}, Average grade: {average_grade:.1f}")
  148.             else:
  149.                 print("No passed courses.")
  150.         else:
  151.             print("Student not found.")
  152.  
  153.     def main_menu(self):
  154.             print("You may select one of the following:n1) Add studentn2) Search studentn3) Search coursen4) Add course completionn5) Show student's recordn0) Exit")
  155.             choice = input("What is your selection? ")
  156.  
  157.             if choice == "1":
  158.                 self.add_student()
  159.             elif choice == "2":
  160.                 self.search_student()
  161.             elif choice == "3":
  162.                 self.search_course()
  163.             elif choice == "4":
  164.                 self.add_course_completion()
  165.             elif choice == "5":
  166.                 self.show_student_record()
  167.             elif choice == "0":
  168.                 print("Exiting the program.")
  169.                 sys.exit()
  170.             else:
  171.                 print("Invalid input. Please select a valid option.")
  172.  
  173.  
  174. if __name__ == "__main__":
  175.     system = StudentManagementSystem&#40;&#41;
  176.     while 1:
  177.         system.main_menu()