Advertisement
adolphuZ

Untitled

Jun 5th, 2024
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.70 KB | None | 0 0
  1. import json
  2.  
  3. class Osoba:
  4.     def __init__(self, name, last_name, age):
  5.         self.name = name
  6.         self.last_name = last_name
  7.         self.age = age
  8.  
  9.     def get_name(self):
  10.         return self.name
  11.  
  12.     def get_last_name(self):
  13.         return self.last_name
  14.  
  15.     def get_age(self):
  16.         return self.age
  17.  
  18.     def to_dict(self):
  19.         return {
  20.             'name': self.name,
  21.             'last_name': self.last_name,
  22.             'age': self.age
  23.         }
  24.  
  25. class Note:
  26.     def __init__(self, grades):
  27.         self.grades = grades
  28.  
  29.     def get_grades(self):
  30.         return self.grades
  31.  
  32.     def get_grade(self, subject):
  33.         return self.grades.get(subject, None)
  34.  
  35.     def to_dict(self):
  36.         return self.grades
  37.  
  38. class Student(Osoba):
  39.     def __init__(self, name, last_name, age, index_nr, grades):
  40.         super().__init__(name, last_name, age)
  41.         self.index_nr = index_nr
  42.         self.notes = Note(grades)
  43.  
  44.     def get_index_nr(self):
  45.         return self.index_nr
  46.  
  47.     def get_notes(self):
  48.         return self.notes.get_grades()
  49.  
  50.     def to_dict(self):
  51.         data = super().to_dict()
  52.         data.update({
  53.             'index_nr': self.index_nr,
  54.             'grades': self.notes.to_dict()
  55.         })
  56.         return data
  57.  
  58. class Group:
  59.     def __init__(self):
  60.         self.students = []
  61.  
  62.     def add_student(self, student):
  63.         self.students.append(student)
  64.  
  65.     def get_students(self):
  66.         return self.students
  67.  
  68.     def display_students(self):
  69.         for student in self.students:
  70.             print(f"Student: {student.get_name()} {student.get_last_name()}, Age: {student.get_age()}, Index: {student.get_index_nr()}")
  71.             print(f"Grades: {student.get_notes()}")
  72.  
  73.     def save_to_file(self, filename):
  74.         try:
  75.             with open(filename, 'w') as file:
  76.                 json.dump([student.to_dict() for student in self.students], file, indent=4)
  77.             print(f"Data successfully saved to {filename}")
  78.         except IOError as e:
  79.             print(f"An error occurred while writing to the file: {e}")
  80.  
  81. # Przykładowa inicjalizacja studentów
  82. grades_student1 = {
  83.     "Mathematics": 5,
  84.     "Physics": 4,
  85.     "Chemistry": 3
  86. }
  87.  
  88. grades_student2 = {
  89.     "Mathematics": 4,
  90.     "Physics": 5,
  91.     "Chemistry": 4
  92. }
  93.  
  94. student1 = Student("Anna", "Nowak", 22, "2021001", grades_student1)
  95. student2 = Student("Piotr", "Kowalski", 23, "2021002", grades_student2)
  96.  
  97. # Przykładowa inicjalizacja grupy z dowolnymi studentami
  98. group = Group()
  99. group.add_student(student1)
  100. group.add_student(student2)
  101.  
  102. # Wyświetlenie studentów w grupie
  103. group.display_students()
  104.  
  105. # Zapis grupy studentów do pliku
  106. group.save_to_file("students_group.json")
  107.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement