Facebook
From Saif, 9 Months ago, written in Plain Text.
This paste is a reply to Code from Saif - view diff
Embed
Download Paste or View Raw
Hits: 178
  1. import random
  2. import datetime
  3. import sys
  4.  
  5. class StudentManagementSystem:
  6.     def __init__(self):
  7.         self.students = []
  8.         self.courses = []
  9.         self.passed_courses = []
  10.  
  11.     def add_student(self):
  12.         first_name = input("Enter the first name of the student: ")
  13.         last_name = input("Enter the last name of the student: ")
  14.         major = input("Select student's major (CE, EE, ET, ME, SE): ")
  15.  
  16.         if not first_name.isalpha() or not last_name.isalpha() or not first_name.istitle() or not last_name.istitle():
  17.             print("Names should contain only letters and start with capital letters.")
  18.             return
  19.  
  20.         current_year = datetime.datetime.now().year
  21.         student_id = random.randint(10000, 99999)
  22.  
  23.         email = f"{first_name.lower()}.{last_name.lower()}@lut.fi"
  24.         student_data = (student_id, first_name, last_name, current_year, major, email)
  25.         self.students.append(student_data)
  26.  
  27.         with open("students.txt", "a") as file:
  28.             file.write(','.join(map(str, student_data)) + 'n')
  29.  
  30.         print("Student added successfully!")
  31.  
  32.     def search_items(self, file_path, search_term, item_type):
  33.         if len(search_term) < 3:
  34.             print("Search term should contain at least 3 characters.")
  35.             return
  36.  
  37.         with open(file_path, "r") as file:
  38.             items = file.readlines()
  39.  
  40.         matching_items = [item for item in items if search_term in item]
  41.         if matching_items:
  42.             print(f"Matching {item_type}s:")
  43.             for item in matching_items:
  44.                 item_info = item.strip().split(",")
  45.                 print(f"ID: {item_info[0]}, {item_type.capitalize()}: {item_info[1]}")
  46.         else:
  47.             print(f"No matching {item_type}s found.")
  48.  
  49.     def search_student(self):
  50.         search_term = input("Give at least 3 characters of the student's first or last name: ")
  51.         self.search_items("students.txt", search_term, "student")
  52.  
  53.     def search_course(self):
  54.         search_term = input("Give at least 3 characters of the course name or teacher's name: ")
  55.         self.search_items("courses.txt", search_term, "course")
  56.  
  57.     def add_course_completion(self):
  58.         # Functionality for adding course completion
  59.         pass
  60.  
  61.     def show_student_record(self):
  62.         # Functionality for displaying student records
  63.         pass
  64.  
  65.     def main_menu(self):
  66.         print("You may select one of the following:")
  67.         print("1) Add student")
  68.         print("2) Search student")
  69.         print("3) Search course")
  70.         print("4) Add course completion")
  71.         print("5) Show student's record")
  72.         print("0) Exit")
  73.         choice = input("What is your selection? ")
  74.  
  75.         if choice == "1":
  76.             self.add_student()
  77.         elif choice == "2":
  78.             self.search_student()
  79.         elif choice == "3":
  80.             self.search_course()
  81.         elif choice == "4":
  82.             self.add_course_completion()
  83.         elif choice == "5":
  84.             self.show_student_record()
  85.         elif choice == "0":
  86.             print("Exiting the program.")
  87.             sys.exit()
  88.         else:
  89.             print("Invalid input. Please select a valid option.")
  90.  
  91. if __name__ == "__main__":
  92.     system = StudentManagementSystem&#40;&#41;
  93.     while True:
  94.         system.main_menu()
  95.