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}")
- # 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)
- # Wyświetlenie studentów w grupie
- group.display_students()
- # Zapis grupy studentów do pliku
- group.save_to_file("students_group.json")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement