Advertisement
adolphuZ

Untitled

Jun 5th, 2024
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.96 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.     def load_from_file(self, filename):
  82.         try:
  83.             with open(filename, 'r') as file:
  84.                 data = json.load(file)
  85.                 self.students = []
  86.                 for student_data in data:
  87.                     student = Student(
  88.                         student_data['name'],
  89.                         student_data['last_name'],
  90.                         student_data['age'],
  91.                         student_data['index_nr'],
  92.                         student_data['grades']
  93.                     )
  94.                     self.add_student(student)
  95.             print(f"Data successfully loaded from {filename}")
  96.         except IOError as e:
  97.             print(f"An error occurred while reading the file: {e}")
  98.         except json.JSONDecodeError as e:
  99.             print(f"An error occurred while decoding JSON data: {e}")
  100.  
  101. # Przykładowa inicjalizacja studentów
  102. grades_student1 = {
  103.     "Mathematics": 5,
  104.     "Physics": 4,
  105.     "Chemistry": 3
  106. }
  107.  
  108. grades_student2 = {
  109.     "Mathematics": 4,
  110.     "Physics": 5,
  111.     "Chemistry": 4
  112. }
  113.  
  114. student1 = Student("Anna", "Nowak", 22, "2021001", grades_student1)
  115. student2 = Student("Piotr", "Kowalski", 23, "2021002", grades_student2)
  116.  
  117. # Przykładowa inicjalizacja grupy z dowolnymi studentami
  118. group = Group()
  119. group.add_student(student1)
  120. group.add_student(student2)
  121.  
  122. # Zapis grupy studentów do pliku
  123. group.save_to_file("students_group.json")
  124.  
  125. # Wyświetlenie studentów w grupie przed odczytem z pliku
  126. print("\nGroup before loading from file:")
  127. group.display_students()
  128.  
  129. # Symulacja błędu odczytu - zmiana nazwy pliku na nieistniejący
  130. print("\nSimulating file read error:")
  131. group.load_from_file("nonexistent_file.json")
  132.  
  133. # Odczyt grupy studentów z pliku
  134. group.load_from_file("students_group.json")
  135.  
  136. # Wyświetlenie studentów w grupie po odczycie z pliku
  137. print("\nGroup after loading from file:")
  138. group.display_students()
  139.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement