Skip to content

Commit

Permalink
fix day 5 kotlin
Browse files Browse the repository at this point in the history
  • Loading branch information
ythirion committed Jan 3, 2024
1 parent 0d38a94 commit b4bd87b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 35 deletions.
35 changes: 22 additions & 13 deletions exercise/kotlin/day05/src/test/kotlin/PopulationTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,29 @@ class PopulationTests : FunSpec({
Person("Glenn", "Quagmire")
)

fun formatPopulation(): String {
var response = ""

for (person in population) {
response += "${person.firstName} ${person.lastName}"

if (person.pets.isNotEmpty()) {
response += " who owns : "
}
for (pet in person.pets) {
response += "${pet.name} "
}
if (population.last() != person) {
response += lineSeparator()
}
}
return response
}

test("people with their pets") {
formatPopulation(population) shouldBe """Peter Griffin who owns : Tabby
val response = formatPopulation()

response shouldBe """Peter Griffin who owns : Tabby
|Stewie Griffin who owns : Dolly Brian
|Joe Swanson who owns : Spike
|Lois Griffin who owns : Serpy
Expand All @@ -36,15 +57,3 @@ class PopulationTests : FunSpec({
|Glenn Quagmire""".trimMargin()
}
})

fun formatPopulation(population: List<Person>): String =
population.joinToString(lineSeparator()) { formatPerson(it) }

fun Person.formatPets(): String {
return when {
pets.isNotEmpty() -> return pets.joinToString(" ", " who owns : ", " ") { it.name }
else -> ""
}
}

fun formatPerson(person: Person): String = "${person.firstName} ${person.lastName}" + person.formatPets()
35 changes: 13 additions & 22 deletions solution/kotlin/day05/src/test/kotlin/PopulationTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,8 @@ class PopulationTests : FunSpec({
Person("Glenn", "Quagmire")
)

fun formatPopulation(): String {
var response = ""

for (person in population) {
response += "${person.firstName} ${person.lastName}"

if (person.pets.isNotEmpty()) {
response += " who owns : "
}
for (pet in person.pets) {
response += "${pet.name} "
}
if (population.last() != person) {
response += lineSeparator()
}
}
return response
}

test("people with their pets") {
val response = formatPopulation()

response shouldBe """Peter Griffin who owns : Tabby
formatPopulation(population) shouldBe """Peter Griffin who owns : Tabby
|Stewie Griffin who owns : Dolly Brian
|Joe Swanson who owns : Spike
|Lois Griffin who owns : Serpy
Expand All @@ -57,3 +36,15 @@ class PopulationTests : FunSpec({
|Glenn Quagmire""".trimMargin()
}
})

fun formatPopulation(population: List<Person>): String =
population.joinToString(lineSeparator()) { formatPerson(it) }

fun Person.formatPets(): String {
return when {
pets.isNotEmpty() -> return pets.joinToString(" ", " who owns : ", " ") { it.name }
else -> ""
}
}

fun formatPerson(person: Person): String = "${person.firstName} ${person.lastName}" + person.formatPets()

0 comments on commit b4bd87b

Please sign in to comment.