-
Notifications
You must be signed in to change notification settings - Fork 0
/
Student.php
42 lines (34 loc) · 792 Bytes
/
Student.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php
class Student {
protected string $name;
protected $courses;
public function __construct(string $name) {
$this->name = $name;
$this->courses = [];
}
public function name() {
return $this->name;
}
public function addGrade($course, $grade) {
$c = null;
foreach ($this->courses as $object) {
if ($object->name() === $course) {
$c = $object;
}
}
if ($c == null) {
$c = new Course(name: $course);
array_push($this->courses, $c);
}
$c->addGrade($grade);
}
public function totals() {
$count = count($this->courses);
$keys = array_keys($this->courses);
echo "Grades for ".$this->name;
for($i = 0; $i < $count; $i++) {
$this->courses[$keys[$i]]->total();
}
}
}
?>