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

Project: Task Manager App - Part 1 (Setup + Core Structure)

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

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

  • Project: Task Manager App - Part 1 (Setup + Core Structure) ကို လက်တွေ့ project ထဲမှာ အသုံးချတတ်မယ်
  • ကိုယ်တိုင် code ရေးပြီး run ကြည့်တတ်မယ်
  • Project တစ်ခုလုံးကို အဆင့်လိုက် ဆောက်တတ်မယ်

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

ဒီ mini project မှာ to-do list လို task list ကို manage လုပ်ပေးမယ့် Task Manager app ကို build ကြမှာပါ။ Task Manager ကို 3 part ခွဲပြီး build မှာဖြစ်ပြီး Part 1 မှာတော့ data model နဲ့ core structure ကို အရင်ချမယ်။ Task တစ်ခုချင်းစီအတွက် title, isDone, priority ပါဝင်တဲ့ Task struct ကို value type အနေနဲ့ သတ်မှတ်မယ်၊ priority level တွေအတွက် Priority enum ကို သုံးမယ်။ Task list တွေကို hold ထားပြီး add/print operation တွေ handle ရမှာဖြစ်လို့ TaskManager ကိုတော့ reference type ဖြစ်တဲ့ class နဲ့ ရေးမယ်။ Struct vs Class, enum topic တွေကို directly application လုပ်တဲ့ practice ဖြစ်ပါတယ်။

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

Priority ဆိုတဲ့ enum ကို low, medium, high case သုံးခု နဲ့ ဆောက်ပါ။ Task struct မှာ id (Int), title (String), priority (Priority), isDone (Bool, default false) field လေးခု ထည့်ပါ။ TaskManager class ကို ဆောက်ပြီး private var tasks: [Task] = [] array တစ်ခု ထားပါ၊ addTask(title:priority:) method ကို ရေးပြီး id ကို tasks.count + 1 နဲ့ auto-generate လုပ်ပါ။ printAllTasks() method ရေးပြီး task list တစ်ခုချင်းစီကို id, title, priority, status ပါအောင် format လုပ်ပြီး print ထုတ်ပါ။ Part 2 မှာ ဒီ TaskManager ကို filter/sort feature တွေ ထပ်ထည့်ပြီး ဆက်တည်ဆောက်သွားမှာဖြစ်ပါတယ်။

Code နမူနာ

swift
enum Priority: String {
    case low = "Low"
    case medium = "Medium"
    case high = "High"
}

struct Task {
    let id: Int
    var title: String
    var priority: Priority
    var isDone: Bool = false
}

class TaskManager {
    private var tasks: [Task] = []

    func addTask(title: String, priority: Priority) {
        let newTask = Task(id: tasks.count + 1, title: title, priority: priority)
        tasks.append(newTask)
    }

    func printAllTasks() {
        print("----- Task List -----")
        for task in tasks {
            let status = task.isDone ? "Done" : "Pending"
            print("#\(task.id) [\(task.priority.rawValue)] \(task.title) - \(status)")
        }
    }
}

let manager = TaskManager()
manager.addTask(title: "Swift optionals ပြန်ကျက်မယ်", priority: .high)
manager.addTask(title: "Struct vs Class notes ရေးမယ်", priority: .medium)
manager.printAllTasks()
You should see
Console မှာ task list header အောက်ကနေ id, priority, title, status ပါတဲ့ line နှစ်ကြောင်း format ကျကျ print ထွက်လာပါမယ်။

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

Priority ကို urgent case ထပ်ထည့်ပြီး task သုံးခုလောက် addTask နဲ့ ထည့်ကြည့်ပါ၊ printAllTasks() ခေါ်ပြီး output ပုံစံ မှန်မမှန် check ကြည့်ပါ- 5 minute လောက် ကြိုးစားကြည့်ပါ။

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

Task struct ကို value type အဖြစ်ပဲ ဆက်ထားပါ - array ထဲကို append လုပ်တဲ့အခါ copy ဖြစ်တာကို နားလည်ထားရင် Part 2 မှာ update logic ရေးရလွယ်ပါလိမ့်မယ်။

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

  • Task struct ကို class လို mutable reference အဖြစ် သုံးမယ်ထင်ပြီး var manager: Task ဆိုပြီး copy semantics ကို လွဲမှားနားလည်တတ်ကြတယ်
  • id ကို manual assign လုပ်ရင်း duplicate id ဖြစ်တာ သတိမထားမိတတ်ကြပါ (Part 3 မှာ validation ဖြင့် ဖြေရှင်းပေးပါမယ်)

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

Priority ကို urgent case ထပ်ထည့်ပြီး task သုံးခုလောက် addTask နဲ့ ထည့်ကြည့်ပါ၊ printAllTasks() ခေါ်ပြီး output ပုံစံ မှန်မမှန် check ကြည့်ပါ- 5 minute လောက် ကြိုးစားကြည့်ပါ။

You'll know it worked when: Console မှာ task list header အောက်ကနေ id, priority, title, status ပါတဲ့ line နှစ်ကြောင်း format ကျကျ print ထွက်လာပါမယ်။

Project: Task Manager App - Part 1 (Setup + Core Structure) | Thuta Learning