Thuta Learning
ရှာဖွေရန်
ProjectsProgrammingbeginner

Practice Project: Task Manager - Part 3

စိတ်လျှော့ပါ။ ဒီခန်းကို စာအုပ်လိုမဟုတ်ဘဲ စကားပြောသလိုပဲ၊ နားလည်လွယ်အောင် ရှင်းပါမယ်။

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Practice Project: Task Manager - Part 3 ကို လက်တွေ့ project ထဲမှာ အသုံးချတတ်မယ်
  • ကိုယ်တိုင် code ရေးပြီး run ကြည့်တတ်မယ်
  • Project တစ်ခုလုံးကို အဆင့်လိုက် ဆောက်တတ်မယ်

ခဏလေး ဒီလိုပဲ စဉ်းစားကြည့်

ဒီ part က project ရဲ့ နောက်ဆုံးအဆင့်ဖြစ်ပြီး , Reportable ဆိုတဲ့ interface တစ်ခုကို define လုပ်ပြီး TaskManager class ကို implement လုပ်ခိုင်းပါမယ်။ Interface ကို သုံးတာက summary report logic ကို contract အနေနဲ့ သတ်မှတ်ဖို့နဲ့ နောက်ပိုင်း class အခြားတစ်ခု (ဥပမာ ProjectManager) ကလည်း reuse လုပ်နိုင်ဖို့ ရည်ရွယ်ပါတယ်။ groupBy နဲ့ priority အလိုက် task count ကို summarize လုပ်ပြီး sortedBy ကို သုံးပြီး task list ကို priority order အလိုက် စီပေးပါမယ်။ နောက်ဆုံးမှာ project တစ်ခုလုံးရဲ့ complete code ကို ပေါင်းစည်းပြီး run ကြည့်ရင် Task Manager app အပြည့်အစုံ အလုပ်လုပ်တာကို တွေ့ရပါလိမ့်မယ်။

လက်တွေ့ ဆောက်ကြည့်မယ်

Reportable ဆိုတဲ့ interface တစ်ခုရေးပြီး fun printSummary() ဆိုတဲ့ function တစ်ခု declare ပါ။ TaskManager class ကို : Reportable နဲ့ inherit ခိုင်းပြီး printSummary() ထဲမှာ tasks.groupBy { it.priority } ကို သုံးပြီး priority တစ်ခုချင်းစီမှာ task ဘယ်နှစ်ခုရှိလဲ count ထုတ်ပါ။ ပြီးရင် sortedByDescending { it.priority } နဲ့ task list ကို priority အမြင့်ဆုံးကနေ စီပြပါ။ Completed task ရာခိုင်နှုန်းကိုလည်း (tasks.count { it.isDone } * 100 / tasks.size) ဆိုတဲ့ formula နဲ့ တွက်ပြီး print ပါ။ Part 1 နဲ့ Part 2 က function တွေအားလုံးပါတဲ့ full TaskManager class ကို ပေါင်းစပ်ပြီး main() ထဲမှာ task 5 ခုလောက် add လုပ်ကြပြီး printSummary() ခေါ်ကြည့်ပါ။

Code နမူနာ

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
Priority အလိုက် task count, priority order အတိုင်း စီထားတဲ့ task list, ပြီးတော့ completed percentage တို့ကို console မှာ report အနေနဲ့ တွေ့ရပါလိမ့်မယ်။

၅ မိနစ် စမ်းကြည့်

Enum class Priority ကို enum class Priority(val weight: Int) { LOW(1), MEDIUM(2), HIGH(3) } ပုံစံပြောင်းပြီး sortedByDescending { it.priority.weight } နဲ့ sort logic ကို ပိုတိကျအောင် ပြင်ကြည့်ပါ။

သတိလေးတစ်ချက်

groupBy ရဲ့ output ဟာ Map<Priority, List<Task>> ဖြစ်ပြီး key order က insertion order အလိုက်သာ ရှိတတ်လို့ priority order ချင်တဲ့အခါ sortedByDescending လိုမျိုး ခွဲသုံးရမှာကို သတိထားပါ။

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • Interface ထဲက function ကို override လုပ်တဲ့နေရာမှာ override keyword ထည့်ဖို့ မေ့တတ်ပြီး compile error တက်တတ်ပါတယ်
  • tasks.count { it.isDone } * 100 / tasks.size ကို Int division ဖြစ်နေတာမသိဘဲ decimal ပါတဲ့ percentage လိုချင်ရင် toDouble() ခွဲစဥ်းစားမိမှ ပြင်ရတတ်ပါတယ်

အခု ကိုယ်တိုင် စမ်းကြည့်

Enum class Priority ကို enum class Priority(val weight: Int) { LOW(1), MEDIUM(2), HIGH(3) } ပုံစံပြောင်းပြီး sortedByDescending { it.priority.weight } နဲ့ sort logic ကို ပိုတိကျအောင် ပြင်ကြည့်ပါ။

You'll know it worked when: Priority အလိုက် task count, priority order အတိုင်း စီထားတဲ့ task list, ပြီးတော့ completed percentage တို့ကို console မှာ report အနေနဲ့ တွေ့ရပါလိမ့်မယ်။

Practice Project: Task Manager - Part 3 | Thuta Learning