Skip to content

Instantly share code, notes, and snippets.

@5AbhishekSaxena
Created June 5, 2023 19:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 5AbhishekSaxena/3af12192d066c69e531425fb867e7f4d to your computer and use it in GitHub Desktop.
Save 5AbhishekSaxena/3af12192d066c69e531425fb867e7f4d to your computer and use it in GitHub Desktop.
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