Course Builder

Report a typo

Implement the buildCourse function to provide a student list for a Course according to the CourseBuilder class.

Sample Input 1:

Kotlin Alice Bob

Sample 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)
}
___

Create a free account to access the full topic