Reputation: 405
Here Customer
and BodyModel
are data classes. I need an inner function at User
, toCustomer()
, to convert User class
into Customer class
. I am stuck at changing Boolean type
to Customer.Type
Customer Class
data class Customer(
val id: String,
val bodyModel: BodyModel?,
val isDrinks : Type
){
enum class Type(val type: String, val value: Boolean) {
NO("No", false),
YES("Yes", true)
}
}
BodyModel class
data class BodyModel(
val height: Int?,
val weight: Int?
)
User class
data class User(
val id: String,
val height: Int?,
val isDrinks: Boolean?
){
@Ignore
fun toCustomer() = Customer(
id,
BodyModel(height?:-1, -1 ),
Customer.Type(?????????)
)
Upvotes: 0
Views: 1100
Reputation: 7604
If this is a toy example and you want something more general, try this:
enum class Type(val type: String, val value: Boolean) {
NO("No", false),
YES("Yes", true);
companion object {
val map: Map<Boolean, Type> = Type.values().associateBy{it.value}
}
}
and then do this
fun toCustomer() = Customer(
id,
BodyModel(height?:-1, -1 ),
Customer.Type.map[isDrinks]
)
Upvotes: 1
Reputation: 4702
You could do it like this:
@Ignore
fun toCustomer() = Customer(
id,
BodyModel(height?:-1, -1 ),
if (isDrinks == true) Customer.Type.YES else Customer.Type.NO
)
Upvotes: 1