-
Notifications
You must be signed in to change notification settings - Fork 3
/
Day 12; Inheritance.swift
51 lines (41 loc) · 1.38 KB
/
Day 12; Inheritance.swift
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
import Foundation
// Class Person
class Person {
private let firstName: String
private let lastName: String
private let id: Int
// Initializer
init(firstName: String, lastName: String, id: Int) {
self.firstName = firstName
self.lastName = lastName
self.id = id
}
// Print person data
func printPerson() {
print("Name: \(lastName), \(firstName)")
print("ID: \(id)")
}
} // End of class Person
// Class Student
class Student: Person {
var testScores: [Int]
init(firstName: String, lastName: String, id: Int, scores: [Int]) {
self.testScores = scores
super.init(firstName: firstName, lastName: lastName, id: id)
}
func calculate() -> Character {
let average = testScores.reduce(0, +) / testScores.count
if average >= 90 { return "O" }
else if average >= 80 { return "E" }
else if average >= 70 { return "A" }
else if average >= 55 { return "P" }
else if average >= 40 { return "D" }
else { return "T" }
}
} // End of class Student
let nameAndID = readLine()!.components(separatedBy: " ")
let _ = readLine()
let scores = readLine()!.components(separatedBy: " ").map{ Int($0)! }
let s = Student(firstName: nameAndID[0], lastName: nameAndID[1], id: Int(nameAndID[2])!, scores: scores)
s.printPerson()
print("Grade: \(s.calculate())")