Thuta Learning
ProjectsProgrammingbeginner

Practice Project: Task Manager - Part 3

Relax. We'll talk through this in plain words — no textbook voice.

What you'll walk away with

  • Apply Practice Project: Task Manager - Part 3 in a hands-on project
  • Write and run the code yourself
  • Build a complete project step by step

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

kotlin
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 should see
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.

Easy traps

  • Forgetting to add the override keyword when overriding a function from an interface, causing a compile error
  • Not realizing tasks.count { it.isDone } * 100 / tasks.size is integer division — you only catch it once you need a decimal percentage and realize you need toDouble()

Now Try It Yourself

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 }.

You'll know it worked when: You'll see a report in the console showing task counts by priority, a task list sorted by priority order, and the completed percentage.

Practice Project: Task Manager - Part 3 | Thuta Learning