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

코틀린스love다 - 이홍섭 #3

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

코틀린스love다 - 이홍섭 #3

hongxeob opened this issue Sep 9, 2024 · 0 comments

Comments

@hongxeob
Copy link
Member

hongxeob commented Sep 9, 2024

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

// UserManagementSystem
class UserManagementSystem {
    companion object {
        const val ADULT_AGE: Int = 18
    }

    private val users = mutableListOf<User>()

    fun addUser(
        name: String,
        age: Int,
    ) {
        users.add(User(name, age))
    }

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

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

    fun calculateAverageAge(): Double = users.map { it.age }.average()
}

// main
fun main() {
    val system = UserManagementSystem()

    // 사용자 추가
    val userList =
        listOf(
            User("옥순", 25),
            User("옥순", 25),
            User("상철", 17),
            User("영철", 30),
        )
    userList.forEach {
        system.addUser(it.name, it.age)
    }

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

    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