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

1
  • Can you provide us with more detail about the Language & ReservationFrequency for recreatability? Commented Feb 7, 2023 at 8:29

1 Answer 1

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()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.