Facebook
From sdwqed, 1 Week ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 92
  1. //plik: student-list.ts
  2.  
  3. import { Student } from "./student"
  4.  
  5. export class StudentList {
  6.     private subject: string
  7.     private students: Student[]
  8.  
  9.     constructor(subject: string) {
  10.         this.subject = subject
  11.         this.students = []
  12.     }
  13.  
  14.     public addStudent(student: Student) {
  15.         this.students.push(student)
  16.     }
  17.  
  18.     public removeStudent(name: string) {
  19.         this.students = this.students.filter(student => student.getName() != name)
  20.     }
  21.  
  22.     public toString(): string {
  23.         return this.subject + 'nStudenci: ' + this.students.map(s => s.toString()).join('n---n')
  24.     }
  25. }
  26.  
  27.  
  28. //plik: student.ts
  29.  
  30. export class Student {
  31.     private name: string
  32.     private age: number
  33.     private grades: Map<string, number[]> //np fizyka, [5, 4, 2, 4.5]
  34.  
  35.     constructor(name: string, age: number) {
  36.         this.name = name
  37.         this.age = age
  38.         this.grades = new Map()
  39.     }
  40.  
  41.     public getName(): string {
  42.         return this.name
  43.     }
  44.  
  45.     public addGrade(subject: string, grade: number) {
  46.         if (this.grades.has(subject)) { //jeśli sa juz jakies oceny z przedmiotu 'subject'
  47.             const currentGrades = this.grades.get(subject) //pobieramy wszystkie oceny z przedmiotu 'subject'
  48.             currentGrades?.push(grade) //dodajemy nowa ocene
  49.             this.grades.set(subject, currentGrades!) //zamieniamy dotychczasowa pare: przedmiot-oceny, na nową, z dodaną oceną
  50.         } else { //jezeli wstawiamy pierwsza ocene z przedmiotu 'subject
  51.             this.grades.set(subject, [grade])
  52.         }
  53.     }
  54.  
  55.     public average(subject: string): number {
  56.  
  57.         const currentGrades = this.grades.get(subject)
  58.         let sum = 0
  59.         currentGrades?.forEach(grade => sum += grade)
  60.  
  61.         if (!this.grades.has(subject)) {
  62.             return 0
  63.         } else {
  64.             return sum / currentGrades!.length
  65.         }
  66.     }
  67.  
  68.     public toString(): string {
  69.         const subjects = this.grades.keys()
  70.  
  71.         let grades = ''
  72.         for (let subject of subjects) {
  73.             grades += subject + ': ' + this.grades.get(subject) + 'n'
  74.         }
  75.  
  76.         return `Nazwisko: ${this.name}nWiek: ${this.age}nOceny: ${grades}`
  77.     }
  78. }
  79.  
  80.  
  81.  
  82.  
  83. // W klasie student zmień pola ocena1 i ocena2 na
  84. // oceny (typ: mapa składająca się z par: przedmiot, lista ocen)
  85. // zmień metodę średniaOcen; metoda ta bierze jako parametr nazwę przedmiotu
  86. // zmień też odpowiednio metodę toString
  87.  
  88. // W pliku app.ts utwórz zmienną bestStudents, jest to mapa składająca
  89. // się z par: przedmiot-student, gdzie studentem jest student o
  90. // najwyższej średniej z danego przedmiotu
  91.  
  92. import { Student } from "./student"
  93. import { StudentList } from "./student-list"
  94.  
  95.  
  96. const student = new Student('Nowak', 20)
  97.  
  98. student.addGrade('fizyka', 4)
  99. student.addGrade('fizyka', 5)
  100. student.addGrade('biologia', 3)
  101. student.addGrade('matematyka', 2)
  102. student.addGrade('fizyka', 1)
  103. student.addGrade('biologia', 4)
  104.  
  105. console.log(student.average('fizyka'))