import random import sys import datetime class StudentManagementSystem: def __init__(self): self.students = [] self.courses = [] self.passed_courses = [] def add_student(self): first_name = input("Enter the first name of the student: ") last_name = input("Enter the last name of the student: ") major = input("Select student's major (CE, EE, ET, ME, SE): ") if not self.is_valid_name(first_name) or not self.is_valid_name(last_name): print("Names should contain only letters and start with capital letters.") return current_year = datetime.datetime.now().year student_id = random.randint(10000, 99999) email = f"{first_name.lower()}.{last_name.lower()}@lut.fi" student_data = (student_id, first_name, last_name, current_year, major, email) self.students.append(student_data) with open("students.txt", "a") as f: f.write(','.join(map(str, student_data)) + 'n') print("Student added successfully!") def search_items(self, file_path, search_term, item_type): if len(search_term) < 3: print("Search term should contain at least 3 characters.") return with open(file_path, "r") as f: items = f.readlines() matching_items = [item for item in items if search_term in item] if matching_items: print(f"Matching {item_type}s:") for item in matching_items: item_info = item.strip().split(",") print(f"ID: {item_info[0]}, {item_type.capitalize()}: {item_info[1]}") else: print(f"No matching {item_type}s found.") def search_student(self): search_term = input("Give at least 3 characters of the student's first or last name: ") self.search_items("students.txt", search_term, "student") def search_course(self): search_term = input("Give at least 3 characters of the course name or teacher's name: ") self.search_items("courses.txt", search_term, "course") def is_valid_name(self, name): return name.isalpha() and name.istitle() def add_course_completion(self): course_id = input("Give the course ID: ") student_id = input("Give the student ID: ") grade = input("Give the grade (1-5): ") if not grade.isdigit() or not (1 <= int(grade) <= 5): print("Grade is not a correct grade.") return date_str = input("Enter a date (DD/MM/YYYY): ") try: date = datetime.datetime.strptime(date_str, "%d/%m/%Y") today = datetime.datetime.now() if date > today: print("Input date is later than today. Try again!") return if (today - date).days > 30: print("Input date is older than 30 days. Contact 'opinto'.") return except ValueError: print("Invalid date format. Use DD/MM/YYYY. Try again!") return with open("passed.txt", "r") as f: passed_courses = f.readlines() for i, line in enumerate(passed_courses): if line.startswith(course_id + "," + student_id): old_grade = int(line.strip().split(",")[-1]) if int(grade) <= old_grade: print(f"Student has passed this course earlier with grade {old_grade}") return passed_courses[i] = f"{course_id},{student_id},{date_str},{grade}n" with open("passed.txt", "w") as f: f.writelines(passed_courses) print("Record updated!") return with open("passed.txt", "a") as f: f.write(f"{course_id},{student_id},{date_str},{grade}n") print("Record added!") def show_student_record(self): student_id = input("Enter the student ID: ") with open("students.txt", "r") as f: students = f.readlines() student_info = [s.strip().split(",") for s in students if student_id in s][0] with open("passed.txt", "r") as f: passed_courses = f.readlines() with open("courses.txt", "r") as f: courses_info = f.readlines() student_courses = [] total_credits = 0 total_grade = 0 for line in passed_courses: if line.startswith(student_id): course_id, date_str, grade = line.strip().split(",")[:3] total_grade += int(grade) date = datetime.datetime.strptime(date_str, "%d/%m/%Y") course_info = [c.strip().split(",") for c in courses_info if course_id in c][0] course_name, credits, teachers = course_info[1], course_info[2], course_info[3] student_courses.append((course_id, course_name, credits, date_str, teachers, grade)) total_credits += int(credits) if student_info: print("Student ID:", student_id) print("Name:", student_info[1], student_info[2]) print("Starting year:", student_info[3]) print("Major:", student_info[4]) print("Email:", student_info[5]) if student_courses: print("Passed courses:") for course in student_courses: course_id, course_name, credits, date_str, teachers, grade = course print(f"Course ID: {course_id}, Name: {course_name}, Credits: {credits}, Date: {date_str}, Teacher(s): {teachers}, Grade: {grade}") average_grade = total_grade / len(student_courses) print(f"Total credits: {total_credits}, Average grade: {average_grade:.1f}") else: print("No passed courses.") else: print("Student not found.") def main_menu(self): print("You may select one of the following:n1) Add studentn2) Search studentn3) Search coursen4) Add course completionn5) Show student's recordn0) Exit") choice = input("What is your selection? ") if choice == "1": self.add_student() elif choice == "2": self.search_student() elif choice == "3": self.search_course() elif choice == "4": self.add_course_completion() elif choice == "5": self.show_student_record() elif choice == "0": print("Exiting the program.") sys.exit() else: print("Invalid input. Please select a valid option.") if __name__ == "__main__": system = StudentManagementSystem() while 1: system.main_menu()