-
-
Save 5AbhishekSaxena/3af12192d066c69e531425fb867e7f4d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Contact( | |
twitterHandle: String, | |
githubHandle: String, | |
phoneNumber: String, | |
email: String, | |
) { | |
val twitterHandle: String | |
val githubHandle: String | |
val phoneNumber: String | |
val email: String | |
init { | |
this.twitterHandle = twitterHandle.ifBlank { | |
throw IllegalArgumentException("Twitter handle must not be blank.") | |
} | |
this.githubHandle = githubHandle.ifBlank { | |
throw IllegalArgumentException("GitHub handle must not be blank.") | |
} | |
this.phoneNumber = phoneNumber.ifBlank { | |
throw IllegalArgumentException("Phone number must not be blank.") | |
} | |
this.email = email.ifBlank { | |
throw IllegalArgumentException("Email must not be blank.") | |
} | |
} | |
class Builder { | |
private var twitterHandle: String = "" | |
private var githubHandle: String = "" | |
private var phoneNumber: String = "" | |
private var email: String = "" | |
fun setTwitterHandle(twitterHandle: String) = apply { | |
this.twitterHandle = twitterHandle | |
} | |
fun setGithubHandle(githubHandle: String) = apply { | |
this.githubHandle = githubHandle | |
} | |
fun setPhoneNumber(phoneNumber: String) = apply { | |
this.phoneNumber = phoneNumber | |
} | |
fun setEmail(email: String) = apply { | |
this.email = email | |
} | |
fun build(): Contact { | |
return Contact( | |
twitterHandle = twitterHandle, | |
githubHandle = githubHandle, | |
phoneNumber = phoneNumber, | |
email = email | |
) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment