0

sorry for my English, but I have a problem. I have a list of animals, and the "if else" construct should divide the animals into categories and output the correct category to the correct question.

Thank you in advance

fun main() {

    val animalList = mutableListOf(
        "whale",
        "cat",
        "dog",
        "dolphin",
        "parrot",
        "eagle",
        "fox",
        "bear",
        "lion",
        "tiger")

    println("What kind of animal would you like? Wild, predatory, home, can fly")

    val question: String? = "Wild animal"

    if (question == "Wild animal") {
        print("Your question: ")
        val question = readLine()
        println("Wild animals: whale, dolphin, eagle, fox, bear, lion, tiger")
    } else if (question == "Home pets") {
        println("Home pets: cat, dog, parrot")
    } else if (question == "Predator") {
        println("Predator: eagle, fox, bear, lion, tiger")
    } else if (question == "Can fly") {
        println("Can fly: eagle and parrot")
    } else {
        println("Error")
    }
}
Roar S.
  • 8,103
  • 1
  • 15
  • 37
Valik
  • 1

3 Answers3

1

I would probably have defined categories and animals as enums as shown below, and then created a mapping between category and animals (please see categoryToAnimals below).

As shown in main, you can replace if/else with when.

enum class AnimalCategory(val description: String) {
    Wild("Wild animals"),
    Predator("Predators"),
    Home("Home pets"),
    CanFly("Can fly");
}

enum class Animal {
    Bear,
    Cat,
    Dog,
    Dolphin,
    Eagle,
    Fox,
    Lion,
    Parrot,
    Tiger,
    Whale;
}

val categoryToAnimals = mapOf(
    AnimalCategory.Wild to setOf(
        Animal.Bear,
        Animal.Dolphin,
        Animal.Eagle,
        Animal.Fox,
        Animal.Lion,
        Animal.Tiger,
        Animal.Whale
    ),
    AnimalCategory.Home to setOf(Animal.Cat, Animal.Dog, Animal.Parrot),
    AnimalCategory.Predator to setOf(Animal.Bear, Animal.Eagle, Animal.Fox, Animal.Lion, Animal.Tiger),
    AnimalCategory.CanFly to setOf(Animal.Eagle, Animal.Parrot),
)

fun printAnimalList(animalCategory: AnimalCategory) {
    println("${animalCategory.description}: ${categoryToAnimals[animalCategory]!!.joinToString(", ")}")
}

fun main() {

    println("What kind of animal would you like? Wild, predatory, home, can fly")

    val question = readLine() ?: ""

    when (question.lowercase()) {
        "wild" -> printAnimalList(AnimalCategory.Wild)
        "home" -> printAnimalList(AnimalCategory.Home)
        "predatory" -> printAnimalList(AnimalCategory.Predator)
        "can fly" -> printAnimalList(AnimalCategory.CanFly)
        else -> println("Error")
    }
}
Roar S.
  • 8,103
  • 1
  • 15
  • 37
0

Even though the context and use case is not totally clear for me, I would try to answer it. Please try out this code

    fun main() {

val animalList = mutableListOf(
    "whale",
    "cat",
    "dog",
    "dolphin",
    "parrot",
    "eagle",
    "fox",
    "bear",
    "lion",
    "tiger")

println("What kind of animal would you like? Wild, predatory, home, can fly")

var question = readLine()


if (question == "Wild animal"){
    println("Wild animals: whale, dolphin, eagle, fox, bear, lion, tiger")

}else if (question == "Home pets"){
    println("Home pets: cat, dog, parrot")

}else if (question == "Predator"){
    println("Predator: eagle, fox, bear, lion, tiger")

}else if (question == "Can fly"){
    println("Can fly: eagle and parrot")

}else{
    println("Error")
}
}
  • Thank you very much. It helped me with my problem! – Valik Sep 03 '22 at 18:20
  • 1
    @Valik: If this is the answer that helped you the most, it is good conduct to accept this answer. People are spending time on answering questions, and accepting an answer is free. Accepting an answer also signals to others not to spend time on this question. BR – Roar S. Sep 04 '22 at 14:05
0

I'd solve this using an animal class, where it's categories are in a vararg enum property.

data class Animal(val name: String, vararg val categories: Animal.Category) {
    enum class Category {
        Wild("Wild animals"),
        Predator("Predators"),
        Home("Home pets"),
        CanFly("Can fly");
    }
}

Then you can create a list of these and filter it to get your response.

fun main() {
    val availableAnimals = listOf(
        Animal("whale", Animal.Category.Wild),
        Animal("cat", Animal.Category.Home),
        Animal("dog", Animal.Category.Home),
        Animal("dolphin", Animal.Category.Wild),
        Animal("parrot", Animal.Category.Home, Animal.Category.CanFly),
        Animal("eagle", Animal.Category.Wild, Animal.Category.CanFly, Animal.Category.Predator),
        Animal("fox", Animal.Category.Wild, Animal.Category.Predator),
        Animal("bear", Animal.Category.Wild, Animal.Category.Predator),
        Animal("lion", Animal.Category.Wild, Animal.Category.Predator),
        Animal("tiger", Animal.Category.Wild, Animal.Category.Predator)
    )

    println("What kind of animal would you like? ${Animal.Category.values().map(Animal.Category::name).joinToString()}")

    val response = readln()
    val responseCategory = Animal.Category.firstOrNull { it.name == response }
    if (responseCategory == null) {
        println("Error")
        return
    }
    val result = availableAnimals.filter { responseCategory in it.categories }
    println("${response}: ${result.joinToString(Animal::name)}")
}
Tenfour04
  • 83,111
  • 11
  • 94
  • 154