Last active
June 21, 2018 16:10
-
-
Save leesc22/3af23956c54b8123fdd491afb22c0818 to your computer and use it in GitHub Desktop.
Android for Beginners : ReportCard Kotlin Class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ReportCard constructor(var name: String) { | |
var englishGrade: Char? = null | |
var mathGrade: String? = null | |
var historyGrade: Int? = null | |
var biologyGrade: Double? = null | |
constructor(name: String, englishGrade: Char?, mathGrade: String?, historyGrade: Int?, biologyGrade: Double?) : this(name) { | |
this.englishGrade = englishGrade | |
this.mathGrade = mathGrade | |
this.historyGrade = historyGrade | |
this.biologyGrade = biologyGrade | |
} | |
override fun toString(): String { | |
var reportMessage = "Name: $name;" | |
englishGrade?.let { reportMessage += " English grade: $englishGrade;" } | |
mathGrade?.let { reportMessage += " Math grade: $mathGrade;" } | |
historyGrade?.let { reportMessage += " History grade: $historyGrade;" } | |
biologyGrade?.let { reportMessage += " Biology grade: $biologyGrade;" } | |
return reportMessage | |
} | |
} | |
val reportCard = ReportCard("John Doe") | |
reportCard.englishGrade = 'A' | |
reportCard.mathGrade = "A-" | |
reportCard.historyGrade = 85 | |
reportCard.biologyGrade = 90.5 | |
println(reportCard) // Name: John Doe; English grade: A; Math grade: A-; History grade: 85; Biology grade: 90.5; | |
val reportCard2 = ReportCard("Second Doe", 'B', null, 70, 60.5) | |
println(reportCard2) // Name: Second Doe; English grade: B; History grade: 70; Biology grade: 60.5; | |
val students = arrayListOf<String>("Jane Doe", "Peter", "Jane") | |
val englishGrades = arrayListOf<Char>('B', 'C', 'D') | |
val mathGrades = arrayListOf<String?>("B+", "B", null) | |
val historyGrades = arrayListOf<Int?>(null, null, 80) | |
for ((index, student) in students.withIndex()) { | |
val reportCard = ReportCard(student) | |
reportCard.englishGrade = englishGrades.get(index) | |
reportCard.mathGrade = mathGrades.get(index) | |
reportCard.historyGrade = historyGrades.get(index) | |
println(reportCard) | |
} | |
// Name: Jane Doe; English grade: B; Math grade: B+; | |
// Name: Peter; English grade: C; Math grade: B; | |
// Name: Jane; English grade: D; History grade: 80; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment