w3resource

Kotlin Class: Create Customer class and send welcome email


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.


Pre-Knowledge (Before You Start!)

Before attempting this exercise, you should be familiar with the following concepts:

  • Classes in Kotlin: A class is a blueprint for creating objects with specific attributes and behaviors.
  • Primary Constructor: Used to initialize properties such as name, email, and address when an object is created.
  • Functions in Classes: Functions define behaviors for the class. Here, a function will simulate sending a welcome email.
  • Object Creation: Creating an instance of a class and calling its methods to perform actions.
  • Printing Output: Using the println() function to display messages to the console.

Hints (Try Before Looking at the Solution!)

Try to solve the problem using these hints:

  • Hint 1: Define a class named Customer with properties for name, email, and address.
  • Hint 2: Inside the class, create a function called sendWelcomeEmail() that prints a message simulating an email being sent.
  • Hint 3: In the main() function, create an instance of the Customer class with sample data.
  • Hint 4: Call the sendWelcomeEmail() function on the object to display the simulated email message.

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.

Go to:


PREV : Create an Animal class and make animal sounds.
NEXT : MathUtils class with static functions.

Kotlin Editor:


Improve this sample solution and post your code through Disqus

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.