Untitled

mail@pastecode.io avatar
unknown
kotlin
a year ago
1.2 kB
3
Indexable
Never
import java.time.LocalDate
import java.time.Period

class User {
    var name: String = ""
    var email: String = ""
    var dateAdded: LocalDate = LocalDate.now()
}

class EmailSender {
    fun sendEmail(email: String, subject: String, body: String) {
        // Sending email logic here
    }
}

class UserManager {
    private val users: MutableList<User> = mutableListOf()
    private val emailSender: EmailSender = EmailSender()

    fun addUser(user: User) {
        val isPastMonthUser = !getUsersAddedLastMonth().any { it.email == user.email }
        users.add(user)

        if (isPastMonthUser) {
            emailSender.sendEmail(
                user.email,
                "Welcome!",
                "Welcome to our service, ${user.name}!"
            )
        } else {
            emailSender.sendEmail(
                user.email,
                "Profile updated",
                "Your profile has been updated, ${user.name}!"
            )
        }
    }

    fun getUsersAddedLastMonth(): List<User> {
        val monthAgo = LocalDate.now().minus(Period.ofMonths(1))
        return users.filter { it.dateAdded.isAfter(monthAgo) }
    }
}