Ray Rahke
03/04/2024, 1:23 AMJoffrey
03/04/2024, 1:45 AMRay Rahke
03/04/2024, 1:54 AMRay Rahke
03/04/2024, 1:56 AMRay Rahke
03/04/2024, 1:57 AMRay Rahke
03/04/2024, 1:57 AMJoffrey
03/04/2024, 2:11 AMRay Rahke
03/04/2024, 7:10 AMRay Rahke
03/04/2024, 7:11 AMabstract class CommandHandler (
val parts: String
) {
companion object {
abstract fun match(str: String): Boolean
}
private fun format(str: String): List<String> {
return str.split(" ")
}
abstract fun execute()
}
class HelpCommand : CommandHandler() {
companion object {
override fun match(str: String): Boolean {
return str.startsWith("help")
}
}
override fun execute() {
// print help stuff ...
}
}
class Main {
val handlers: List<KFunction<CommandHandler>> = listOf(::HelpCommand)
val input = readln()
for (command. in handlers) {
if (command.match(input)) {
command(input).execute()
}
}
}
Ray Rahke
03/04/2024, 7:32 AMRay Rahke
03/04/2024, 7:32 AMJoffrey
03/04/2024, 7:33 AMRay Rahke
03/04/2024, 7:33 AMRay Rahke
03/04/2024, 7:33 AMJoffrey
03/04/2024, 7:34 AMRay Rahke
03/04/2024, 7:35 AMJoffrey
03/04/2024, 7:35 AMRay Rahke
03/04/2024, 7:35 AMRay Rahke
03/04/2024, 7:35 AMJoffrey
03/04/2024, 7:36 AMRay Rahke
03/04/2024, 7:37 AMRay Rahke
03/04/2024, 7:38 AMRay Rahke
03/04/2024, 7:38 AMRay Rahke
03/04/2024, 7:38 AMRay Rahke
03/04/2024, 7:38 AMJoffrey
03/04/2024, 7:39 AMinterface CommandType<T : CommandHandler> {
fun matches(str: String): Boolean
fun create(command: String): T
}
abstract class CommandHandler (
val parts: String
) {
private fun format(str: String): List<String> {
return str.split(" ")
}
abstract fun execute()
}
class HelpCommand(parts: String) : CommandHandler(parts) {
companion object : CommandType<HelpCommand> {
override fun matches(str: String): Boolean = str.startsWith("help")
override fun create(parts: String) = HelpCommand(parts)
}
override fun execute() {
// print help stuff ...
}
}
fun main() {
val handlers: List<CommandType<*>> = listOf(HelpCommand)
val input = readln()
for (commandType in handlers) {
if (commandType.matches(input)) {
commandType.create(input).execute()
}
}
}
Ray Rahke
03/04/2024, 7:39 AMRay Rahke
03/04/2024, 7:40 AMJoffrey
03/04/2024, 7:41 AMWout Werkman
03/04/2024, 7:41 AMRay Rahke
03/04/2024, 7:41 AMRay Rahke
03/04/2024, 7:42 AMRay Rahke
03/04/2024, 7:42 AMWout Werkman
03/04/2024, 8:37 AM