1

Is it any Kotlin collection/data class able to store the table data as shown below? I can only figure out the way to store the item and value as below:

var WorkA = mapOf{"Name" to "AA", "Gender" to "Female"}...

but I can't figure out how to link the "verify result".

Please give me some advice as I'm a newbie. Thanks.

enter image description here

scr
  • 134
  • 2
  • 7

1 Answers1

1

I would create a Worker data class with the fields listed in Item column as properties and some boolean properties representing validity of those fields.

data class Worker(val name:String, val gender :String, val age :Int, val tel:String,val email:String){

    var isNameValid : Boolean = false
        get(){
            return name.isNotEmpty()
        }

    var isGenderValid : Boolean = false
        get(){
            return gender.equals("Male",true) || gender.equals("Female",true)
        }

    var isAgeValid : Boolean = false
        get(){
            return age >= 18 && age<=60
        }
    var isTelValid : Boolean = false
        get(){
            //TODO : validate telephone number
            return true
        }
    var isEmailValid: Boolean = false
        get(){
            //TODO: validate email.
            return true
        }
}

You can create Worker objects and use them as elements of an Worker array/list.

val workerA = Worker("A", "Male",  18, "123", "a@mail.com")
val workerB = Worker("B", "Female",  20, "122", "b@mail.com")

val workers: List<Worker> = listOf(workerA, workerB)
println(workers[0].isNameValid)

As your question does not provide the validation rules for fields and the table provided does not make it obvious, I have left implementation of getters up to you. Check the questions linked below.

How should I validate an e-mail address?

Email and phone Number Validation in android

Omkar76
  • 1,317
  • 1
  • 8
  • 22