0

I have an enum class which maps language locale to a list.

How can I fix the function getReservationFrequencies inside the companion object to return the value of the map based on the locale (key)?

enum class ReservationFrequencies(val frequencies: Map<Language, List<ReservationFrequency>>)  {
    APPLICATION(mapOf(
            Language.en to listOf(
                    ReservationFrequency(0,"once",0),
                    ReservationFrequency(1,"Monthly",2),
                    ReservationFrequency(2,"Quarterly",5),
                    ReservationFrequency(3,"Semi-annually",5),
                    ReservationFrequency(4,"Annually",5)
            ),
            Language.ar to listOf(
                    ReservationFrequency(0,"مرة واحده",0),
                    ReservationFrequency(1,"شهرياً",2),
                    ReservationFrequency(2,"ربع سنوياً",5),
                    ReservationFrequency(3,"نصف سنوياً",5),
                    ReservationFrequency(4," سنوياً",5)
            )
    ));
}

I tried creating a companion object that includes a function that returns the list

companion object {
    fun getReservationFrequencies(locale: String?) : List<ReservationFrequency> {
        val reservationFrequencyDtos = mutableListOf<ReservationFrequency>()
        reservationFrequencies.values()
            .filter { locale!!.contains(it.name)}
            .forEach() {
                ReservationFrequency.add(ReservationFrequency(code = it.frequencies))
            }
    }
}

I'm expecting a List<ReservationFrequency> based on the locale

Z-100
  • 518
  • 3
  • 19
  • Can you provide us with more detail about the Language & ReservationFrequency for recreatability? – Z-100 Feb 07 '23 at 08:29

1 Answers1

0

Probably just this

fun getReservationFrequencies(locale: String?) : List<ReservationFrequency> =
    ReservationFrequencies.APPLICATION.frequencies.filter { 
        it.key.name == locale // I don't know the specifics of how to match the String locale arg against Language
    }.map {
        it.value
    }.first()
AndrewL
  • 2,034
  • 18
  • 18