Hello! I would to share my solution for the second challenge. I would appreciate any feedback!
const val LIST_WIDTH = 45
val menuList = File("data/tavern-menu-items.txt")
.readText()
.split("\n")
.toMutableList()
fun main() {
val header = "Welcome to $TAVERN_NAME"
val headerLength = header.length
val indentForHeader = constructIndent("*", (LIST_WIDTH - headerLength - 2) / 2)
print("\n$indentForHeader $header $indentForHeader\n\n")
while (menuList.isNotEmpty()) {
val (category) = menuList[0].split(",")
val indentForCategory = constructIndent(" ", (LIST_WIDTH - category.length - 4) / 2)
println("$indentForCategory~[$category]~$indentForCategory")
for (i in (0 until menuList.size).reversed()) {
val (currentItemCategory, itemName, price) = menuList[i].split(",")
if (currentItemCategory == category) {
printItem(itemName, price)
menuList.removeAt(i)
}
}
}
/* patronList.forEachIndexed { index, patron ->
println("Good evening, $patron -- you are #${index + 1} in line.")
placeOrder(patron, menuList.shuffled().first())
}
(0..9).forEach {
val first = patronList.shuffled().first()
val last = lastName.shuffled().first()
val name = "$first $last"
uniquePatrons += name
}
println(uniquePatrons)
var orderCount = 0
while (orderCount <= 9) {
placeOrder(uniquePatrons.shuffled().first(), menuList.shuffled().first())
orderCount++
}*/
}
private fun constructIndent(symbol: String, rep: Int): String {
val result = StringBuilder()
repeat(rep) {
result.append(symbol)
}
return result.toString()
}
private fun printItem(itemName: String, price: String) {
val itemWords: MutableList<String> = itemName.split(" ").toMutableList()
itemWords.forEachIndexed { index, word ->
when (word.toLowerCase()) {
"of" -> {}
else -> itemWords[index] = word.capitalize()
}
}
val itemCapitalized = itemWords.joinToString(" ")
val space = constructIndent(".", LIST_WIDTH - itemName.length - price.length)
print("$itemCapitalized$space$price\n")
}