siavash parsa
siavash parsa

Reputation: 11

how to access value of data class object getting from api kotlin

this is one of my response from api:an object of number of chargers

NumberOfChargers(BusACGBTtype2=0, BusACType2=0, CarACType2=11, BusDCCCS2=0, CarACGBT=1, CarACType1=1, CarDCCCS1=1, CarDCCCS2=0, CarDCCHAdeMO=8, CarDCChaoji=0, CarDCGBT=1, MotorcycleACDefault=0, MotorcycleACType1=0, MotorcycleACType2=0)

I want to code something like this:

if the value of each item was !=0 then print the name of each item

for example if BusACGBTtype2 value !=0 then print BusACGBTtype2

how can I do this?? shall I use Iterating? or store it in a map?

Edit: added the data class definition:

data class NumberOfChargers(
  @SerializedName("Bus-AC-GB/Ttype2")
  val BusACGBTtype2:Int,
  @SerializedName("Bus-AC-Type2")
  val BusACType2:Int ,
  @SerializedName("Car-AC-Type2")
  var CarACType2: Int,
  @SerializedName("Bus-DC-CCS2")
  val BusDCCCS2: Int,
  @SerializedName("Car-AC-GB/T")
  val CarACGBT: Int,
  @SerializedName("Car-AC-Type1")
  val CarACType1: Int,
  @SerializedName("Car-DC-CCS1")
  val CarDCCCS1: Int,
  @SerializedName("Car-DC-CCS2")
  val CarDCCCS2: Int,
  @SerializedName("Car-DC-CHAdeMO")
  val CarDCCHAdeMO: Int,
  @SerializedName("Car-DC-Chaoji")
  val CarDCChaoji: Int,
  @SerializedName("Car-DC-GB/T")
  val CarDCGBT: Int,
  @SerializedName("Motorcycle-AC-Default")
  val MotorcycleACDefault: Int,
  @SerializedName("Motorcycle-AC-Type1")
  val MotorcycleACType1: Int,
  @SerializedName("Motorcycle-AC-Type2")
  val MotorcycleACType2: Int
)

Upvotes: 1

Views: 335

Answers (1)

Mohmmaed-Amleh
Mohmmaed-Amleh

Reputation: 438

class NumberOfChargers(
    @SerializedName("Bus-AC-GB/Ttype2")
    val BusACGBTtype2: Int,
    @SerializedName("Bus-AC-Type2")
    val BusACType2: Int,
    @SerializedName("Car-AC-Type2")
    val CarACType2: Int,
    @SerializedName("Bus-DC-CCS2")
    val BusDCCCS2: Int,
    @SerializedName("Car-AC-GB/T")
    val CarACGBT: Int,
    @SerializedName("Car-AC-Type1")
    val CarACType1: Int,
    @SerializedName("Car-DC-CCS1")
    val CarDCCCS1: Int,
    @SerializedName("Car-DC-CCS2")
    val CarDCCCS2: Int,
    @SerializedName("Car-DC-CHAdeMO")
    val CarDCCHAdeMO: Int,
    @SerializedName("Car-DC-Chaoji")
    val CarDCChaoji: Int,
    @SerializedName("Car-DC-GB/T")
    val CarDCGBT: Int,
    @SerializedName("Motorcycle-AC-Default")
    val MotorcycleACDefault: Int,
    @SerializedName("Motorcycle-AC-Type1")
    val MotorcycleACType1: Int,
    @SerializedName("Motorcycle-AC-Type2")
    val MotorcycleACType2: Int
) {
    private val vehiclesNameValueMap: Map<Vehicles, Int> = mutableMapOf(
        Vehicles.BusACGBTtype2 to BusACGBTtype2,
        Vehicles.BusACType2 to BusACType2,
        Vehicles.CarACType2 to CarACType2,
        //and so on
    )

    fun getVehiclesWhenNot(number: Int) = vehiclesNameValueMap.filter { (_, value) ->
        value != number
    }.keys
}

enum class Vehicles(name: String) {
    BusACGBTtype2("BusACGB"),
    BusACType2("BusAC"),
    CarACType2("CarAC"),
    BusDCCCS2("provide name"),
    CarACGBT("provide name"),
    CarACType1("provide name"),
    CarDCCCS1("provide name"),
    CarDCCCS2("provide name"),
    CarDCCHAdeMO("provide name"),
    CarDCChaoji("provide name"),
    CarDCGBT("provide name"),
    MotorcycleACDefault("provide name"),
    MotorcycleACType1("provide name"),
    MotorcycleACType2("provide name")
}

then you can use it like this ->

yourResponse.getVehiclesWhenNot(number = 0).map {
    println(it.name)
}

Upvotes: 0

Related Questions