Hello! I would to share the solution for the first challenge. Maybe someone wants to provide some sort of feedback:
const val LIST_WIDTH = 45
val menuList = File("data/tavern-menu-items.txt")
.readText()
.split("\n")
fun main() {
val greeting = "Welcome to $TAVERN_NAME"
val greetingLength = greeting.length
val indent = constructString("*", (LIST_WIDTH - greetingLength - 2) / 2)
print("\n$indent $greeting $indent\n\n")
menuList.forEach {
var (_, item, price) = it.split(",")
val itemWords: MutableList<String> = item.split(" ").toMutableList()
itemWords.forEachIndexed { index, word ->
when (word.toLowerCase()) {
"of" -> {}
else -> itemWords[index] = word.capitalize()
}
}
item = itemWords.joinToString(" ")
val space = constructString(".", LIST_WIDTH - item.length - price.length)
print("${item.capitalize()}$space$price\n")
}
}
private fun constructString(symbol: String, rep: Int): String {
val string = StringBuilder()
repeat(rep) {
string.append(symbol)
}
return string.toString()
}