Let's think about it this way
This part is the final stage of the project — we'll define an interface called Reportable and have the TaskManager class implement it. We use an interface to establish the summary report logic as a contract, so that another class later on (say, ProjectManager) can reuse it too. We'll use groupBy to summarize task counts by priority, and sortedBy to sort the task list by priority order. Finally, when we combine the complete code for the whole project and run it, you'll see the full Task Manager app working end to end.
Let's build it
Write an interface called Reportable and declare a function fun printSummary(). Have the TaskManager class inherit it with : Reportable, and inside printSummary(), use tasks.groupBy { it.priority } to output how many tasks exist for each priority. Then use sortedByDescending { it.priority } to list tasks from highest to lowest priority. Also calculate and print the completed task percentage using the formula (tasks.count { it.isDone } * 100 / tasks.size). Combine the full TaskManager class with all the functions from Part 1 and Part 2, add about 5 tasks in main(), and call printSummary().
Code Example
interface Reportable {
fun printSummary()
}
class TaskManager : Reportable {
private val tasks = mutableListOf<Task>()
fun addTask(title: String, priority: Priority) {
if (title.isBlank()) return
tasks.add(Task(id = tasks.size + 1, title = title, priority = priority))
}
fun completeTask(id: Int) {
tasks.find { it.id == id }?.isDone = true
}
override fun printSummary() {
println("=== Task Summary ===")
val grouped = tasks.groupBy { it.priority }
grouped.forEach { (priority, list) -> println("$priority: ${list.size} task(s)") }
val sorted = tasks.sortedByDescending { it.priority }
println("--- Priority order ---")
sorted.forEach { println("${it.title} (${it.priority})") }
if (tasks.isNotEmpty()) {
val percent = tasks.count { it.isDone } * 100 / tasks.size
println("Completed: $percent%")
}
}
}
fun main() {
val manager = TaskManager()
manager.addTask("Kotlin syntax ပြန်ကြည့်ရန်", Priority.HIGH)
manager.addTask("Data class ကျင့်ရန်", Priority.MEDIUM)
manager.addTask("Lambda ကျင့်ရန်", Priority.HIGH)
manager.addTask("Interface လေ့လာရန်", Priority.LOW)
manager.addTask("Project ပြီးအောင်လုပ်ရန်", Priority.HIGH)
manager.completeTask(1)
manager.completeTask(3)
manager.printSummary()
}You'll see a report in the console showing task counts by priority, a task list sorted by priority order, and the completed percentage.5-Minute Challenge
Change the Priority enum class to the form enum class Priority(val weight: Int) { LOW(1), MEDIUM(2), HIGH(3) }, then update the sort logic to be more precise using sortedByDescending { it.priority.weight }.
A Quick Warning
Keep in mind that groupBy's output is a Map<Priority, List<Task>>, and the key order only follows insertion order — so when you want priority order, you'll need to separately apply something like sortedByDescending.