-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStudent.php
69 lines (62 loc) · 1.48 KB
/
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
/**
* Created by PhpStorm.
* User: Dalton
* Date: 2017-01-11
* Time: 10:09 AM
*/
/**
* Describes a student by their
* surname, first name, email, and grade (in courses)
*
* @author Dalton Danis
*/
class Student {
/*
* Default constructor for creating a student
*/
function __construct()
{
$this->surname = '';
$this->first_name = '';
$this->emails = array();
$this->grades = array();
}
/*
* Adds an email to the email list
*/
function add_email($which, $address) {
$this->emails[$which] = $address;
}
/*
* Adds a grade to the student (course grade)
*/
function add_grade($grade) {
$this->grades[] = $grade;
}
/*
* Calculates the student's grade average
* @return grade average
*/
function average() {
$total = 0;
foreach ($this->grades as $value) {
$total += $value;
}
return $total / count($this->grades);
}
/*
* Concatenates the student's information into a string
* stored within the variable result.
* @return concatenated student's information
*/
function toString() {
$result = $this->first_name . ' ' . $this->surname;
$result .= ' (' . $this->average() . ")\n";
foreach ($this->emails as $which => $what) {
$result .= $which . ': ' . $what . "\n";
}
$result .= "\n";
return '<pre>' . $result . '</pre>';
}
}