Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import json
- class Osoba:
- def __init__(self, name, last_name, age):
- self.name = name
- self.last_name = last_name
- self.age = age
- def get_name(self):
- return self.name
- def get_last_name(self):
- return self.last_name
- def get_age(self):
- return self.age
- def to_dict(self):
- return {
- 'name': self.name,
- 'last_name': self.last_name,
- 'age': self.age
- }
- class Note:
- def __init__(self, grades):
- self.grades = grades
- def get_grades(self):
- return self.grades
- def get_grade(self, subject):
- return self.grades.get(subject, None)
- def to_dict(self):
- return self.grades
- class Student(Osoba):
- def __init__(self, name, last_name, age, index_nr, grades):
- super().__init__(name, last_name, age)
- self.index_nr = index_nr
- self.notes = Note(grades)
- def get_index_nr(self):
- return self.index_nr
- def get_notes(self):
- return self.notes.get_grades()
- def to_dict(self):
- data = super().to_dict()
- data.update({
- 'index_nr': self.index_nr,
- 'grades': self.notes.to_dict()
- })
- return data
- class Group:
- def __init__(self):
- self.students = []
- def add_student(self, student):
- self.students.append(student)
- def get_students(self):
- return self.students
- def display_students(self):
- for student in self.students:
- print(f"Student: {student.get_name()} {student.get_last_name()}, Age: {student.get_age()}, Index: {student.get_index_nr()}")
- print(f"Grades: {student.get_notes()}")
- def save_to_file(self, filename):
- try:
- with open(filename, 'w') as file:
- json.dump([student.to_dict() for student in self.students], file, indent=4)
- print(f"Data successfully saved to {filename}")
- except IOError as e:
- print(f"An error occurred while writing to the file: {e}")
- def load_from_file(self, filename):
- try:
- with open(filename, 'r') as file:
- data = json.load(file)
- self.students = []
- for student_data in data:
- student = Student(
- student_data['name'],
- student_data['last_name'],
- student_data['age'],
- student_data['index_nr'],
- student_data['grades']
- )
- self.add_student(student)
- print(f"Data successfully loaded from {filename}")
- except IOError as e:
- print(f"An error occurred while reading the file: {e}")
- except json.JSONDecodeError as e:
- print(f"An error occurred while decoding JSON data: {e}")
- # Przykładowa inicjalizacja studentów
- grades_student1 = {
- "Mathematics": 5,
- "Physics": 4,
- "Chemistry": 3
- }
- grades_student2 = {
- "Mathematics": 4,
- "Physics": 5,
- "Chemistry": 4
- }
- student1 = Student("Anna", "Nowak", 22, "2021001", grades_student1)
- student2 = Student("Piotr", "Kowalski", 23, "2021002", grades_student2)
- # Przykładowa inicjalizacja grupy z dowolnymi studentami
- group = Group()
- group.add_student(student1)
- group.add_student(student2)
- # Zapis grupy studentów do pliku
- group.save_to_file("students_group.json")
- # Wyświetlenie studentów w grupie przed odczytem z pliku
- print("\nGroup before loading from file:")
- group.display_students()
- # Symulacja błędu odczytu - zmiana nazwy pliku na nieistniejący
- print("\nSimulating file read error:")
- group.load_from_file("nonexistent_file.json")
- # Odczyt grupy studentów z pliku
- group.load_from_file("students_group.json")
- # Wyświetlenie studentów w grupie po odczycie z pliku
- print("\nGroup after loading from file:")
- group.display_students()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement