Home > Mobile >  I have list in kotlin and if else must should distribute them into categories
I have list in kotlin and if else must should distribute them into categories

Time:09-04

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")
    }
}

CodePudding user response:

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")
    }
}

CodePudding user response:

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")
}
}

CodePudding user response:

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)}")
}
  • Related