ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
နောက် lesson သုံးခုမှာ add၊ done၊ list လို text command တွေနဲ့ ထိန်းချုပ်ရတဲ့ Task Manager သေးသေးလေးတစ်ခု ဆောက်သွားပါမယ်။ Part 1 မှာ အခြေခံကိုပဲ ဆောက်ပါမယ်။ Task ဆိုတာ ဘာလဲ၊ task တွေကို ဘယ်မှာ သိမ်းမလဲ၊ တစ်ယောက်ယောက်က မှားတဲ့ input ပေးရင် ဘာဖြစ်မလဲ ဆိုတာတွေပါ။
ရုံးတစ်ခုက notice board ကို မြင်ကြည့်ပါ။ အလုပ်တစ်ခုချင်းစီအတွက် ပုံနှိပ်ထားတဲ့ form တစ်ရွက်စီ ရှိပါတယ်။ Form တိုင်းမှာ ထောင့်မှာ နံပါတ်၊ title ရေးဖို့ စာကြောင်းနဲ့ priority အတွက် box သုံးခု ပါပါတယ်။ ဝန်ထမ်းတွေက form ကို ဖြည့်လို့ရပေမယ့် နံပါတ်ကို ပုံနှိပ်ပြီးသွားရင် ဘယ်သူမှ ပြောင်းလို့မရပါဘူး၊ board ရဲ့ သော့ကိုလည်း ရုံးစာရေးတစ်ယောက်ပဲ ကိုင်ထားပါတယ်။ Kotlin မှာ form ကတော့ data class၊ priority box သုံးခုက enum၊ သော့ကိုင်တဲ့ စာရေးက TaskRepository class ပါ။ နောက်ပိုင်း ထည့်မယ့် feature တိုင်းက အဲဒီ စာရေးကနေပဲ ဖြတ်သွားရပါမယ်။
Problem → Why → Solution
ပြဿနာ: compile ဖြစ်ပေမယ့် မှားနေတဲ့ ပထမ draft
ပထမ draft တွေက များသောအားဖြင့် ဒီလိုပါပဲ။ Task မှာ var field တွေ၊ priority က ရိုးရိုး String၊ list က လွတ်လပ်တဲ့ variable၊ id ကို tasks.size + 1 နဲ့ ယူထားပါတယ်။ အားလုံး compile ဖြစ်ပြီး ပထမ demo လည်း အလုပ်လုပ်လို့ ပြီးပြီလို့ ထင်မိပါတယ်။
ပြီးတော့ ပြဿနာ စပါတယ်။ Task တစ်ခု ဖျက်ပြီး task အသစ်တစ်ခု ထည့်လိုက်ရင် task အသစ်က ရှိပြီးသား id ကို ရသွားပါတယ်။ "hihg" ဆိုတဲ့ priority က စာလုံးပေါင်းမှားတာ ဖြစ်ပေမယ့် String က လက်ခံလို့ နောက်ပိုင်း "high" နဲ့ filter လုပ်တိုင်း ဒီ task ကို တိတ်တိတ်လေး ကျော်သွားပါတယ်။ ဘာမှ throw မလုပ်ဘူး၊ ဘာမှလည်း print မထွက်ဘူး၊ program က မှားတဲ့ data ကို ဆက်ကိုင်ထားပါတယ်။
data class Task(var id: Int, var title: String, var priority: String)
fun main() {
val tasks = mutableListOf<Task>()
tasks.add(Task(tasks.size + 1, "Write report", "high"))
// "hihg" is a typo, but a String accepts it
tasks.add(Task(tasks.size + 1, "Call Ko Aung", "hihg"))
tasks.removeAt(0)
// only one task is left, so size + 1 is 2 again
tasks.add(Task(tasks.size + 1, "Send invoice", "low"))
println(tasks.map { it.id }) // [2, 2]
}ဘာကြောင့် compiler က မတားတာလဲ
စာကြောင်းတိုင်းက Kotlin ရဲ့ rule အတိုင်းပါပဲ။ String ထဲမှာ စာသားဘာမဆို ထည့်လို့ရတယ်၊ var ကို reassign လုပ်လို့ရတယ်၊ public list ကို ဘယ်သူမဆို ပြင်လို့ရတယ်၊ size + 1 ဆိုတာလည်း မှန်ကန်တဲ့ Int ပါ။ Compiler က type ကို စစ်တာ၊ ရည်ရွယ်ချက်ကို မစစ်ပါဘူး။ ဒီ bug တွေ မဖြစ်နိုင်အောင် လုပ်ချင်ရင် ကိုယ်ဆိုလိုချင်တာကို type ထဲမှာ ရေးရပါမယ်။ ပိတ်ထားတဲ့ ရွေးချယ်စရာအတွက် enum၊ မပြောင်းရတဲ့ field အတွက် val၊ list နဲ့ id counter ကို ပိုင်ဆိုင်တဲ့ class ပါ။
Core concept: rule တွေကို type အဖြစ် ရေးချထားပါ
ဒီလို project မျိုးက rule တွေ programmer ရဲ့ ခေါင်းထဲမှာပဲ ရှိနေရင် မှားတတ်ပါတယ်။ Part 1 မှာ အဲဒီ rule တွေကို type အဖြစ် ရေးချပြီး compiler စစ်ပေးနိုင်တာတွေကို compiler ကို စစ်ခိုင်းပါမယ်။ ဆုံးဖြတ်ချက် လေးခုရှိပြီး တစ်ခုချင်းစီက bug တစ်မျိုးစီကို ဖယ်ရှားပေးပါတယ်။
၁။ ပိတ်ထားတဲ့ ရွေးချယ်စရာကို enum ဖြစ်အောင် လုပ်ပါ
Priority မှာ မှန်တဲ့ value အတိအကျ သုံးခုပဲ ရှိလို့ enum class Priority { LOW, MEDIUM, HIGH } နဲ့ လွတ်လွတ်လပ်လပ် String ကို အစားထိုးပါတယ်။ HIHG လို typo က compile မဖြစ်တော့ပါဘူး၊ menu လိုတဲ့အခါ ဒါမှမဟုတ် နောက်မှာ user ရိုက်တဲ့ စာသားကို parse လုပ်ရတဲ့အခါ Priority.entries က စာရင်းအပြည့်အစုံ ပေးပါတယ်။ Entry တွေမှာ အစီအစဉ်လည်း ရှိပါတယ် (LOW က MEDIUM ရှေ့၊ MEDIUM က HIGH ရှေ့)၊ Part 3 မှာ sort လုပ်ဖို့ အဲဒါကို သုံးပါမယ်။
၂။ Record ကို ပြောင်းမရတဲ့ data class ဖြစ်အောင် လုပ်ပါ
data class Task က constructor property တွေကနေ toString()၊ equals()၊ hashCode() နဲ့ copy() ကို generate လုပ်ပေးပါတယ်။ Property တိုင်းကို val နဲ့ ကြေညာပါ။ Task ဆိုတာ အချက်အလက်တစ်ခုမို့ ပြောင်းချင်ရင် copy(isDone = true) နဲ့ ပြင်ထားတဲ့ copy အသစ်ကို ဆောက်ရပါတယ်။ ဒါကြောင့် id ကို မတော်တဆ ပြင်မိဖို့ မဖြစ်နိုင်တော့ဘူး၊ isDone = false လို default value ပေးထားရင် caller က အဲဒီ argument ကို ချန်ထားလို့ ရပါတယ်။
၃။ List ကို repository နောက်မှာ ထားပါ
TaskRepository က private mutable list နဲ့ private counter ကို ပိုင်ဆိုင်ထားလို့ caller တွေက add၊ findById၊ all ကိုပဲ သုံးလို့ရပါတယ်။ all() က toList() ဖြစ်တဲ့ snapshot ကို ပြန်ပေးလို့ ပြန်ရတဲ့ value နဲ့ တကယ့် list ကို ဘယ်သူမှ clear လုပ်လို့ မရပါဘူး။ nextId counter က တိုးတာချည်းပဲ ဖြစ်လို့ Part 2 မှာ task တွေ စဖျက်တဲ့အခါမှာတောင် id တွေ မထပ်ပါဘူး။
၄။ မှားတဲ့ input ကို အသံထွက်ပြီး ငြင်းပါ၊ မရှိတာကို null နဲ့ ဖော်ပြပါ
require(condition) { message } က caller က rule ကို ချိုးတဲ့ အချိန်မှာတင် IllegalArgumentException ကို ပစ်ပါတယ်။ အမှားကို ရှာရလွယ်တဲ့ entry point မှာပါ။ ရှာလို့ မတွေ့တာ ပုံမှန်ဖြစ်နိုင်တဲ့ lookup ကတော့ မတူပါဘူး။ findById က Task? ပြန်ပေးပြီး caller တိုင်းက null ဆိုတာ ဘာလဲ ဆုံးဖြတ်ရတာကို compiler က အတင်းခိုင်းပါတယ်၊ များသောအားဖြင့် ?. ဒါမှမဟုတ် ?: နဲ့ပါ။
| အခြေအနေ | Kotlin tool | ရလာတဲ့ အကျိုး |
|---|---|---|
| Value က ရွေးချယ်စရာ အနည်းငယ်ထဲက တစ်ခု ဖြစ်ရမယ် | enum class | Typo တွေ compile မဖြစ်ဘူး |
| Caller က rule ကို ချိုးတယ် | require() က exception ပစ်တယ် | အမှားက entry point မှာတင် ပေါ်တယ် |
| Lookup က မတွေ့နိုင်ဘူး | Return type Task? | Null ကို ကိုင်တွယ်ဖို့ compiler က တောင်းဆိုတယ် |
| Record ကို မပြောင်းရဘူး | val နဲ့ copy() | မတော်တဆ ပြင်မိတာ မရှိဘူး |
- Task ကို data class နဲ့ ဖော်ပြ
- ရွေးချယ်စရာတွေကို enum နဲ့ ပိတ်
- List ကို repository နောက်မှာ ဝှက်
- မှားတဲ့ input ကို entry point မှာ ငြင်း
Caller တွေက repository နဲ့ပဲ ပြောပါတယ်၊ list ကို repository ကပဲ ထိပါတယ်။
အခြေခံ ပုံစံ
enum class Level { FIRST, SECOND }
data class Item(val id: Int, val name: String, val level: Level)
class ItemStore {
private val items = mutableListOf<Item>()
private var counter = 1
fun add(name: String): Item { /* require(...), build, store, return */ }
fun find(id: Int): Item? = items.firstOrNull { it.id == id }
}ရွေးချယ်စရာတွေကို enum နဲ့ ပိတ်၊ record တစ်ခုကို ပြောင်းမရတဲ့ data class နဲ့ ဖော်ပြ၊ ပြီးရင် list နဲ့ id counter ကို store class တစ်ခုက ပိုင်ပြီး function သေးသေးလေးတွေပဲ ပေးပါ။
Part 1 code: model၊ repository နဲ့ ပထမဆုံး run
အောက်က file က Part 1 program အပြည့်အစုံ ဖြစ်ပြီး ဒီအတိုင်း run လို့ရပါတယ်။ Priority၊ Task နဲ့ TaskRepository ဆိုတဲ့ နာမည်တွေကို မှတ်ထားပါ။ Part 2 နဲ့ Part 3 မှာ ဒီနာမည်တွေကို မပြောင်းဘဲ ပြန်သုံးပြီး ထပ်တိုးသွားမှာမို့ပါ။ main function က task သုံးခု ထည့်ပါတယ် (တစ်ခုက title ပတ်လည်မှာ space တွေ ပါပါတယ်)၊ ပြီးတော့ blank title ကို စမ်းထည့်ပြီး ငြင်းတာကို ပြပါတယ်၊ task အားလုံးကို အလိုအလျောက်ရတဲ့ toString() နဲ့ print ထုတ်ပြီး နောက်ဆုံးမှာ ရှိတဲ့ id တစ်ခုနဲ့ မရှိတဲ့ id တစ်ခုကို lookup လုပ်ပါတယ်။
enum class Priority { LOW, MEDIUM, HIGH }
data class Task(
val id: Int,
val title: String,
val priority: Priority,
val isDone: Boolean = false,
)
class TaskRepository {
private val tasks = mutableListOf<Task>()
private var nextId = 1
fun add(title: String, priority: Priority = Priority.MEDIUM): Task {
require(title.isNotBlank()) { "Task title must not be blank" }
val task = Task(id = nextId++, title = title.trim(), priority = priority)
tasks.add(task)
return task
}
fun findById(id: Int): Task? = tasks.firstOrNull { it.id == id }
fun all(): List<Task> = tasks.toList()
}
fun main() {
val repo = TaskRepository()
repo.add("Review Kotlin syntax", Priority.HIGH)
repo.add(" Practice data classes ", Priority.MEDIUM)
repo.add("Read about null safety")
try {
repo.add(" ", Priority.LOW)
} catch (e: IllegalArgumentException) {
println("Rejected: ${e.message}")
}
repo.all().forEach { println(it) }
println("Task #2 -> ${repo.findById(2)?.title}")
println("Task #9 -> ${repo.findById(9)?.title ?: "not found"}")
}Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Closed choice
enum class Priority { LOW, MEDIUM, HIGH }Priority က value သုံးခုထဲက တစ်ခုပဲ ဖြစ်နိုင်ပါတယ်။ Priority.URGENT ကို compiler က ငြင်းပြီး entry တွေက LOW၊ MEDIUM၊ HIGH အစီအစဉ်အတိုင်း ရှိနေပါတယ်။
- Immutable record with a default
val isDone: Boolean = false,Property တိုင်းက val မို့ Task တစ်ခု ဆောက်ပြီးရင် မပြောင်းတော့ပါဘူး။ Default value ပေးထားလို့ add() က isDone ကို မပေးလည်း ရပါတယ်၊ ဒါကြောင့် task အသစ်တိုင်း isDone=false လို့ ပေါ်ပါတယ်။
- Unique ids that never shrink
val task = Task(id = nextId++, title = title.trim(), priority = priority)nextId++ က ဒီ task အတွက် လက်ရှိ value ကို သုံးပြီးမှ counter ကို တိုးပါတယ်၊ ဒါကြောင့် id 1၊ 2၊ 3 ကို တစ်ခါစီပဲ ပေးပါတယ်။ trim() က ဒုတိယ title ပတ်လည်က space တွေကို ဖယ်ပေးပါတယ်။
- Fail fast on bad input
require(title.isNotBlank()) { "Task title must not be blank" }isNotBlank() က empty string နဲ့ space ချည်းပဲ ပါတဲ့ string အတွက် false ပါ။ Condition က false ဖြစ်ရင် require က ကွင်းထဲက message နဲ့ IllegalArgumentException ကို ပစ်ပြီး main က ဖမ်းပြီး Rejected အဖြစ် print ထုတ်ပါတယ်။
- A lookup that may find nothing
fun findById(id: Int): Task? = tasks.firstOrNull { it.id == id }firstOrNull က ပထမဆုံး ကိုက်တာ ဒါမှမဟုတ် null ကို ပြန်ပေးလို့ return type က Task? ဖြစ်ရပါမယ်။ main က ?.title နဲ့ Elvis operator ?: နဲ့ ကိုင်တွယ်ပြီး id 9 အတွက် not found လို့ ပေါ်တာ ဒီလိုပါပဲ။
- A read-only snapshot
fun all(): List<Task> = tasks.toList()toList() က element တွေကို list အသစ်ထဲ copy ကူးပေးပါတယ်။ လက်ခံတဲ့ caller က ဖတ်လို့ရတယ်၊ သူ့ copy ကို sort လည်း လုပ်လို့ရတယ်၊ ဒါပေမယ့် repository ထဲက task တွေကို ထည့်/ဖျက် မလုပ်နိုင်ပါဘူး။
Rejected: Task title must not be blank
Task(id=1, title=Review Kotlin syntax, priority=HIGH, isDone=false)
Task(id=2, title=Practice data classes, priority=MEDIUM, isDone=false)
Task(id=3, title=Read about null safety, priority=MEDIUM, isDone=false)
Task #2 -> Practice data classes
Task #9 -> not foundData class က အလကား ပေးတာတွေ
class ရှေ့မှာ data လို့ ရေးလိုက်ရင် primary constructor ကနေ ပျင်းစရာ အပိုင်းတွေကို compiler က generate လုပ်ပေးပါတယ်။ ဖတ်လို့ရတဲ့ toString()၊ property value တွေကို နှိုင်းယှဉ်တဲ့ equals() နဲ့ hashCode()၊ ပြောင်းထားတဲ့ duplicate ဆောက်ဖို့ copy()၊ val (id, title) = task လို destructuring ကို အလုပ်လုပ်စေတဲ့ componentN() function တွေ ရပါတယ်။
enum class Priority { LOW, MEDIUM, HIGH }
data class Task(
val id: Int,
val title: String,
val priority: Priority,
val isDone: Boolean = false,
)
fun main() {
val a = Task(1, "Write report", Priority.HIGH)
val b = Task(1, "Write report", Priority.HIGH)
println(a == b)
println(a === b)
val done = a.copy(isDone = true)
println(done)
println(a.isDone)
val (id, title) = a
println("$id / $title")
println(Priority.entries.map { it.name })
println(Priority.HIGH > Priority.LOW)
}ပထမ == က true ထွက်တာက တန်ဖိုးတူတဲ့ သီးခြား object နှစ်ခုကို တူညီတယ်လို့ သတ်မှတ်လို့ပါ၊ === ကတော့ identity ကို စစ်လို့ false ထွက်ပါတယ်။ copy(isDone = true) က မူလ object ကို မထိဘဲ ထားခဲ့လို့ a.isDone က false ပဲ ပေါ်ပါတယ်။ Enum entry တွေကို > နဲ့ နှိုင်းယှဉ်လို့ရတာက entry တွေကို ကြေညာထားတဲ့ အစီအစဉ်အတိုင်း နှိုင်းယှဉ်လို့ပါ။
require() နဲ့ စစ်ဆေးခြင်း
require က standard library ထဲက precondition helper အနည်းငယ်ထဲက တစ်ခုပါ။ Caller ထိန်းချုပ်တဲ့ argument တွေအတွက် require ကိုသုံးပြီး object ရဲ့ ကိုယ်ပိုင် state အတွက် check ကို သုံးပါ။ အသီးသီး IllegalArgumentException နဲ့ IllegalStateException ကို ပစ်ပြီး နှစ်ခုလုံးက message ကို လိုအပ်မှ တည်ဆောက်ပါတယ်။ အောက်က snippet က isBlank() ဘာကြောင့် အရေးကြီးလဲ ကိုလည်း ပြပါတယ်။ Space သုံးလုံးပါတဲ့ string က empty မဟုတ်ပါဘူး။
fun main() {
val title = " "
println(title.isEmpty())
println(title.isBlank())
try {
require(title.isNotBlank()) { "Task title must not be blank" }
} catch (e: IllegalArgumentException) {
println(e::class.simpleName + ": " + e.message)
}
}String မှာ character သုံးလုံးရှိလို့ isEmpty() အတွက် false၊ isBlank() အတွက် true ထွက်ပြီး ပြီးရင် require ပစ်တဲ့ exception ရဲ့ message ကို print ထုတ်ပါတယ်။ ကွင်းထဲက message က condition မှား မှသာ run လို့ ပုံမှန်အခြေအနေမှာ message ကောင်းကောင်းရေးတာက ဘာကုန်ကျစရိတ်မှ မရှိပါဘူး။
Project က ဘယ်ကိုသွားမလဲ
Part 1 မှာ command တွေ မပါသေးတာ ရည်ရွယ်ချက်ရှိရှိပါ။ Part 2 မှာ လူတွေ တကယ်သုံးမယ့် feature တွေ ထည့်ပါမယ်။ Task ကို ပြီးအောင်လုပ်တာ၊ lambda နဲ့ ရှာတာနဲ့ when ပေါ်မှာ တည်ဆောက်တဲ့ command interpreter သေးသေးလေးပါ။ Part 3 မှာ report interface၊ sort နဲ့ group လုပ်တာနဲ့ task တွေကို file ထဲ သိမ်းတာတို့နဲ့ ပြီးပြတ်အောင် လုပ်ပါမယ်။ ဒီနေ့ ရေးခဲ့တဲ့ နာမည်သုံးခုက အတိအကျ ဆက်ရှိနေပါမယ်။
Task Manager roadmap
--------------------
Part 1: model Part 2: features Part 3: polish
----------------- -------------------- ----------------------
Priority enum complete(), search() Reportable interface
Task data class TaskManager commands summary and sorting
TaskRepository lambdas, when, ?. save and load a fileလက်တွေ့ project တွေမှာ
လက်တွေ့ project တွေမှာ
To-do list၊ ticketing system၊ order tracker အားလုံးလိုလိုက ဒီအစိတ်အပိုင်းတွေနဲ့ စပါတယ်။ id ပါတဲ့ record၊ status ဒါမှမဟုတ် priority လို ပိတ်ထားတဲ့ ရွေးချယ်စရာ အနည်းငယ်နဲ့ record တွေကို သိမ်းပြီး ရှာပေးတဲ့ နေရာတစ်ခုပါ။ Team တွေက အဲဒီနေရာကို repository လို့ ခေါ်ကြပါတယ်။ ကျန်တဲ့ program က သူ့ interface သေးသေးလေးနဲ့ပဲ ပြောလို့ ဒီမှာသုံးတဲ့ in-memory list ကို နောက်ပိုင်း SQLite ဒါမှမဟုတ် PostgreSQL လို database နဲ့ အစားထိုးရင်တောင် အသုံးပြုတဲ့ code ကို မထိရပါဘူး။
ဒီပုံစံက boundary တိုင်းမှာ ပေါ်ပါတယ်။ Web endpoint တစ်ခုက JSON body ကို data class အဖြစ် ပြောင်းပြီး ဘာမှ မသိမ်းခင် require လို စစ်ဆေးမှုတွေ လုပ်ပါတယ်၊ ရှာလို့ မတွေ့တဲ့ lookup ကို crash မဖြစ်စေဘဲ HTTP 404 အဖြစ် ပြောင်းပါတယ်။ ဒီမှာသုံးတဲ့ counter အခြေခံ id က အားလုံးကို memory ထဲမှာ ကိုင်ထားတဲ့ process တစ်ခုအတွက်တော့ ရပါတယ်၊ ဒါပေမယ့် တကယ့် database က id ကို sequence ဒါမှမဟုတ် UUID နဲ့ ကိုယ်တိုင်ပေးတာမို့ မျှဝေသုံးတဲ့ code က id တွေ ဆက်တိုက်ဖြစ်မယ်လို့ မယူဆရပါဘူး။
အမှားများ
ဖြစ်လေ့ရှိတဲ့ အမှားများ
Blank ကို isEmpty() နဲ့ စစ်တာ
isEmpty() က length သုညလားဆိုတာပဲ စစ်လို့ space ချည်းပဲ ပါတဲ့ title က စစ်ဆေးမှုကို ဖြတ်ပြီး မမြင်ရတဲ့ task အဖြစ် သိမ်းမိပါတယ်။ isBlank() ဒါမှမဟုတ် isNotBlank() ကို သုံးပြီး title.trim() ကို သိမ်းပါ။ ဒါမှ နောက်ပိုင်း နှိုင်းယှဉ်တာနဲ့ print ထုတ်တဲ့ list တွေ သပ်ရပ်နေပါမယ်။
Id ကို size + 1 နဲ့ ယူတာ
Task ကို ဖျက်လို့ရတာနဲ့ size + 1 က ရှိပြီးသား နံပါတ်ကို ပေးမိပါတယ်၊ အပေါ်က draft ထဲက [2, 2] ရလဒ်ပါပဲ။ ဒီမှာ nextId လုပ်သလို တိုးတာချည်းပဲ ဖြစ်တဲ့ သီးခြား counter တစ်ခု ထားပါ။
အတွင်းက list ကို ပြန်ပေးတာ
fun all() = tasks လို့ ရေးရင် private list က လက်တွေ့မှာ public ဖြစ်သွားပါတယ်။ Caller က တကယ့် object ကို ရသွားပြီး cast လုပ်ပြီး clear() ခေါ်လို့ရတယ်၊ ဒါမှမဟုတ် သူ့လက်ထဲက reference က မသိလိုက်ဘဲ ပြောင်းသွားတာမျိုး ဖြစ်နိုင်ပါတယ်။ tasks.toList() ကို ပြန်ပေးပါ၊ ဒါမှ caller တိုင်း ကိုယ်ပိုင် snapshot ကို ရပါမယ်။
Best Practices
Part 2 နဲ့ 3 အတွက် ဆက်ထားရမယ့် အကျင့်တွေ
- Priority၊ Task၊ TaskRepository ကို အခုနာမည်အတိုင်း ထားပါ၊ နောက်ပိုင်း part တွေက အစားထိုးတာ မဟုတ်ဘဲ ချဲ့တာမို့ပါ။
- အပြောင်းအလဲ သေးသေးလေးတိုင်းပြီးရင် program ကို run ပြီး print ထွက်တဲ့ စာကြောင်းတွေကို ခန့်မှန်းထားတာနဲ့ နှိုင်းယှဉ်ပါ။
- Signature တွေမှာ val နဲ့ read-only List ကို ဦးစားပေးပြီး var နဲ့ MutableList ကို data ပိုင်တဲ့ class ထဲမှာပဲ သုံးပါ။
- Rule အသစ်တစ်ခု ထည့်ရင် အဲဒီ rule ကို တမင်ချိုးတဲ့ စာကြောင်းတစ်ကြောင်းကို main မှာ ထည့်ပြီး ငြင်းမလားဆိုတာ စစ်ပါ။
Mini challenge
Id မပျက်ဘဲ remove နဲ့ rename လုပ်ပါ
TaskRepository မှာ remove(id) နဲ့ rename(id, newTitle) ကို ထပ်ထည့်ပါ။ ပြီးရင် main မှာ အချက်နှစ်ချက်ကို သက်သေပြပါ။ ဖျက်ပြီးနောက် id ကို ပြန်မသုံးဘူးဆိုတာနဲ့ blank title အသစ်ကို ငြင်းတယ်ဆိုတာပါ။
- remove(id: Int): Boolean က task တစ်ခု တကယ်ဖျက်မိမှ true ပြန်ရမယ်
- rename(id, newTitle) က update ဖြစ်တဲ့ Task ကို ပြန်ပေးရမယ်၊ id မရှိရင် null ပြန်ပြီး blank title ကို require နဲ့ ငြင်းရမယ်
- သိမ်းထားတဲ့ task ကို title အသစ်ပါတဲ့ copy နဲ့ အစားထိုးပါ၊ title ကို var မလုပ်ပါနဲ့
- Task သုံးခုထည့်ပြီး #2 ကို ဖျက်ပြီးနောက် နောက်ထပ် id က 3 မဟုတ်ဘဲ 4 ဖြစ်တာကို main မှာ ပြပါ
// Reuse Priority and Task from the lesson: every Task property is a val.
class TaskRepository {
private val tasks = mutableListOf<Task>()
private var nextId = 1
fun add(title: String, priority: Priority = Priority.MEDIUM): Task {
// TODO: validate, build the task with nextId++, store it, return it
}
fun remove(id: Int): Boolean {
// TODO: true only if a task with this id was removed
}
fun rename(id: Int, newTitle: String): Task? {
// TODO: reject a blank title, then replace the stored task with a copy
// return the new task, or null when the id is unknown
}
fun all(): List<Task> = tasks.toList()
}
fun main() {
// TODO: add three tasks, remove #2 twice, add a fourth and print every id
// TODO: rename #1 with extra spaces, rename #99, rename #3 with a blank title
}ရလဒ်: remove #2 က true နောက်ပြီး false ထွက်ပါမယ်၊ task အသစ်က id 4 ရပါမယ်၊ id တွေက [1, 3, 4] ဖြစ်ပါမယ်၊ rename က trim လုပ်ထားတဲ့ title ကို ပြန်ပေးပါမယ်၊ id 99 ကို rename လုပ်ရင် null ထွက်ပြီး blank rename က Task title must not be blank လို့ ထွက်ပါမယ်။
အရိပ်အမြွက် ကြည့်ရန်
tasks.removeAll { it.id == id } က တစ်ခုခု ဖျက်မိရင် true ပြန်ပေးပါတယ်။ rename အတွက် indexOfFirst နဲ့ နေရာကို ရှာ၊ copy(title = ...) နဲ့ ပြောင်းထားတဲ့ task ကို ဆောက်ပြီး tasks[index] = renamed နဲ့ ပြန်ရေးပါ။
အဖြေ ကြည့်ရန်
enum class Priority { LOW, MEDIUM, HIGH }
data class Task(
val id: Int,
val title: String,
val priority: Priority,
val isDone: Boolean = false,
)
class TaskRepository {
private val tasks = mutableListOf<Task>()
private var nextId = 1
fun add(title: String, priority: Priority = Priority.MEDIUM): Task {
require(title.isNotBlank()) { "Task title must not be blank" }
return Task(nextId++, title.trim(), priority).also { tasks.add(it) }
}
fun remove(id: Int): Boolean = tasks.removeAll { it.id == id }
fun rename(id: Int, newTitle: String): Task? {
require(newTitle.isNotBlank()) { "Task title must not be blank" }
val index = tasks.indexOfFirst { it.id == id }
if (index == -1) return null
val renamed = tasks[index].copy(title = newTitle.trim())
tasks[index] = renamed
return renamed
}
fun all(): List<Task> = tasks.toList()
}
fun main() {
val repo = TaskRepository()
repo.add("Write report", Priority.HIGH)
repo.add("Call Ko Aung")
repo.add("Send invoice", Priority.LOW)
println("remove #2: ${repo.remove(2)}")
println("remove #2 again: ${repo.remove(2)}")
println("new task id: ${repo.add("Plan sprint").id}")
println("ids: ${repo.all().map { it.id }}")
println("rename #1: ${repo.rename(1, " Write final report ")?.title}")
println("rename #99: ${repo.rename(99, "Ghost")}")
try {
repo.rename(3, " ")
} catch (e: IllegalArgumentException) {
println("blank rename: ${e.message}")
}
println("titles: ${repo.all().map { it.title }}")
}Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- Data ကို အရင် design လုပ်ပါ။ ပိတ်ထားတဲ့ ရွေးချယ်စရာအတွက် enum၊ record တစ်ခုစီအတွက် ပြောင်းမရတဲ့ data class
- Mutable list နဲ့ id counter ကို repository class တစ်ခုတည်းက ပိုင်ပြီး toList() snapshot ကို ပြန်ပေးပါ
- မှားတဲ့ input အတွက် require()၊ ရှာလို့မတွေ့နိုင်တဲ့ lookup အတွက် nullable return type သုံးပါ
- isBlank() က space ချည်းပဲ ပါတဲ့ title ကို ဖမ်းပေးပြီး size + 1 က id ဆောက်ဖို့ ဘေးကင်းတဲ့နည်း မဟုတ်ပါဘူး
Kotlin language guide — Kotlin