Implement the buildCourse function to provide a student list for a Course according to the CourseBuilder class.
Lambda with receiver
Course Builder
Report a typo
Sample Input 1:
Kotlin Alice BobSample Output 1:
Course(name=Kotlin, students=[Student(name=Alice), Student(name=Bob), Student(name=Anonymous #1), Student(name=Anonymous #2)])Write a program in Kotlin
data class Course(val name: String, val students: List<Student>)
data class Student(val name: String)
class CourseBuilder(val name: String) {
val students = mutableListOf<Student>()
fun student(name: String) = students.add(Student(name))
fun build() = Course(name, students)
}
fun buildCourse(name: String, init: CourseBuilder.() -> Unit): Course {
// write your code here
}
fun main() {
val (course, a, b) = readLine()!!.split(' ')
val myCourse = buildCourse(course) {
student(a)
student(b)
for (i in 1..2) {
student("Anonymous #$i")
}
}
println(myCourse)
}
___
By continuing, you agree to the JetBrains Academy Terms of Service as well as Hyperskill Terms of Service and Privacy Policy.
Create a free account to access the full topic
By continuing, you agree to the JetBrains Academy Terms of Service as well as Hyperskill Terms of Service and Privacy Policy.