Please review my code for printing a Christmas Tree. The idea was to merge some functional programming ideas (immutability, streams) and OOP (every thing is a small object).
I'm looking for a review focused on readability of the code.
Here is a output from the code:
*
***
*****
*******
*********
And the code:
fun main(a: Array<String>) {
print(Tree(height = 5))
}
class Tree(val height: Int) {
override fun toString(): String {
return (0..height - 1)
.map { i ->
Stage(i, height)
}
.joinToString(
separator = "\n",
postfix = "\n"
)
}
}
class Stage(val idx: Int, val height: Int) {
override fun toString(): String {
val trunkWidth = TrunkWidth(idx)
val margin = Margin(MarginWidth(TreeWidth(height), trunkWidth).value())
return listOf(
margin,
Trunk(trunkWidth.value()),
margin
).joinToString(separator = "")
}
}
class MarginWidth(val tree: TreeWidth, val trunk: TrunkWidth) {
fun value(): Int {
return (tree.value() - trunk.value()) / 2
}
}
class TreeWidth(val height: Int) {
fun value(): Int {
return height * 2 - 1;
}
}
class TrunkWidth(val idx: Int) {
fun value(): Int {
return 1 + 2 * idx
}
}
class Trunk(val size: Int) {
override fun toString(): String {
return UniformString(size, "*").toString()
}
}
class Margin(val size: Int) {
override fun toString(): String {
return UniformString(size, " ").toString()
}
}
class UniformString(val size: Int, val char: String) {
override fun toString(): String {
return (0..size - 1)
.map { char }
.joinToString(separator = "")
}
}