0% found this document useful (0 votes)
12 views

School Management Code

Uploaded by

abhij7262
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

School Management Code

Uploaded by

abhij7262
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

import pickle

class SchoolManagementSystem:
def __init__(self):
self.students = {}
self.teachers = {}
self.classes = {}

def add_student(self, student_id, name, age, grade):


self.students[student_id] = {"name": name, "age": age, "grade": grade}

def add_teacher(self, teacher_id, name, subject):


self.teachers[teacher_id] = {"name": name, "subject": subject}

def add_class(self, class_id, teacher_id, subject, students):


if teacher_id in self.teachers:
self.classes[class_id] = {
"teacher": self.teachers[teacher_id],
"subject": subject,
"students": [self.students[student_id] for student_id in students if student_id in
self.students]
}

def get_student(self, student_id):


return self.students.get(student_id, "Student not found")

def get_teacher(self, teacher_id):


return self.teachers.get(teacher_id, "Teacher not found")

def get_class(self, class_id):


return self.classes.get(class_id, "Class not found")

def record_attendance(self, class_id, student_id, status):


if class_id in self.classes and any(student["name"] == self.students[student_id]
["name"] for student in self.classes[class_id]["students"]):
self.classes[class_id].setdefault("attendance", {})[student_id] = status
else:
return "Class or student not found"

def calculate_fees(self, student_id, base_fee):


student = self.get_student(student_id)
if isinstance(student, dict):
fee = base_fee
if student["grade"] > 5:
fee *= 1.2
return f"Fees for {student['name']}: ${fee}"
return student

def save_data(self, filename):


with open(filename, "wb") as file:
pickle.dump(self, file)

@staticmethod
def load_data(filename):
try:
with open(filename, "rb") as file:
return pickle.load(file)
except (FileNotFoundError, EOFError):
return SchoolManagementSystem()
def main():
filename = "school_data.pkl"
school = SchoolManagementSystem.load_data(filename)

while True:
print("\n--- School Management System ---")
print("1. Add Student")
print("2. Add Teacher")
print("3. Add Class")
print("4. View Student")
print("5. View Teacher")
print("6. View Class")
print("7. Record Attendance")
print("8. Calculate Fees")
print("9. Save and Exit")

choice = int(input("Enter your choice: "))

if choice == 1:
student_id = int(input("Enter student ID: "))
name = input("Enter student name: ")
age = int(input("Enter student age: "))
grade = int(input("Enter student grade: "))
school.add_student(student_id, name, age, grade)

elif choice == 2:
teacher_id = int(input("Enter teacher ID: "))
name = input("Enter teacher name: ")
subject = input("Enter subject: ")
school.add_teacher(teacher_id, name, subject)

elif choice == 3:
class_id = input("Enter class ID: ")
teacher_id = int(input("Enter teacher ID: "))
subject = input("Enter subject: ")
student_ids = list(map(int, input("Enter student IDs (comma separated):
").split(',')))
school.add_class(class_id, teacher_id, subject, student_ids)

elif choice == 4:
student_id = int(input("Enter student ID to view: "))
print(school.get_student(student_id))

elif choice == 5:
teacher_id = int(input("Enter teacher ID to view: "))
print(school.get_teacher(teacher_id))

elif choice == 6:
class_id = input("Enter class ID to view: ")
print(school.get_class(class_id))

elif choice == 7:
class_id = input("Enter class ID: ")
student_id = int(input("Enter student ID: "))
status = input("Enter attendance status (Present/Absent): ")
print(school.record_attendance(class_id, student_id, status))
elif choice == 8:
student_id = int(input("Enter student ID for fee calculation: "))
base_fee = float(input("Enter base fee amount: "))
print(school.calculate_fees(student_id, base_fee))

elif choice == 9:
school.save_data(filename)
print("Data saved. Exiting...")
break

else:
print("Invalid choice. Try again.")

if __name__ == "__main__":
main()

You might also like