Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

코틀린스럽다 - 정현진 #5

Open
currenjin opened this issue Sep 9, 2024 · 0 comments
Open

코틀린스럽다 - 정현진 #5

currenjin opened this issue Sep 9, 2024 · 0 comments

Comments

@currenjin
Copy link
Member

data class User(val name: String, val age: Int)

class UserManagementSystem {
    private val users = mutableListOf<User>()

    fun addUser(name: String, age: Int) = this.users.add(User(name, age))

    fun getAdultUsers(): List<User> = this.users.filter { it.age >= 18 }

    fun printUserNames() = this.users.forEach { println(it.name) }

    fun calculateAverageAge(): Double = this.users.sumOf { it.age } / users.size.toDouble()

}

fun main() {
    val system = UserManagementSystem()

    system.addUser("옥순", 25)
    system.addUser("상철", 17)
    system.addUser("영철", 30)

    println("모든 사용자 이름:")
    system.printUserNames()

    println("\n성인 사용자:")
    system.getAdultUsers().forEach { println("${it.name} (${it.age}세)") }

    println("\n평균 나이: ${system.calculateAverageAge()}")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant