w3resource

Kotlin Class: Create Customer class and send welcome email

Kotlin Class: Exercise-12 with Solution

Write a Kotlin program that creates a class 'Customer' with properties for name, email, and address. Include a function to send a welcome email to the customer.

Sample Solution:

Kotlin Code:

class Customer(
    val name: String,
    val email: String,
    val address: String
) {
    fun sendWelcomeEmail() {
        println("Sending a welcome email to $name at $email...")
        // Write your code here to send the welcome email
        println("Email sent successfully!")
    }
}
fun main() {
    val customer = Customer("Ime Juan", "[email protected]", "100 ABC Street")
    customer.sendWelcomeEmail()
}

Sample Output:

Sending a welcome email to Ime Juan at [email protected]...
Email sent successfully!

Explanation:

In the above exercise -

  • First we define a class "Customer" with three properties: name, email, and address, representing the customer's name, email address, and their physical address.
  • The class includes a function "sendWelcomeEmail()" that simulates sending a welcome email to the customer. In this example, we simply print a message indicating that the email is being sent and another message to simulate the successful delivery of the email.
  • In the "main()" function, we create an instance of the Customer class named customer with sample values for the properties.
  • Next, we call the "sendWelcomeEmail()" function on the customer object to simulate sending a welcome email.

Kotlin Editor:


Previous: Create an Animal class and make animal sounds.
Next: MathUtils class with static functions.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/kotlin-exercises/class/kotlin-class-exercise-12.php