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

Project: Contact Book – Part 1 (Setup + Core Structure)

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

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

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

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

ဒီ project မှာ Contacts (name, phone, email) တွေကို သိမ်းထား၊ ပြောင်းလဲနိုင်တဲ့ simple Contact Book console app တစ်ခု တည်ဆောက်သွားမှာပါ။ Basic chapter က class, object, property, constructor concepts တွေကို ပြန်အသုံးချပြီး, real-world data structure တစ်ခုကို model လုပ်နည်း လေ့လာရမှာပါ။ Part 1 မှာတော့ project ရဲ့ foundation ဖြစ်တဲ့ Contact class နဲ့ contacts တွေကို List ထဲမှာ သိမ်းမယ့် ContactManager class ကို ဦးဆုံး ရေးမှာပါ။ ဒီ structure ကောင်းကောင်း တည်ဆောက်ထားရင် နောက် part တွေမှာ feature တွေ ထပ်ဖြည့်ရတာ လွယ်ကူသွားပါမယ်။ Console.ReadLine() နဲ့ user input ကို လက်ခံပြီး Add Contact feature အခြေခံကို လုပ်ဆောင်ကြည့်ပါမယ်။

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

Console App project အသစ်တစ်ခု create လုပ်ပြီး Contact.cs ထဲမှာ Name, Phone, Email properties တွေပါတဲ့ Contact class ကို ရေးပါ။ ContactManager class ထဲမှာ List<Contact> field တစ်ခု ထားပြီး AddContact() method နဲ့ ShowAll() method နှစ်ခု ရေးပါ။ Main() method ထဲမှာ simple menu loop (while loop) ဆောက်ပြီး user ကနေ "1. Add Contact" ရွေးရင် Console.ReadLine() နဲ့ name/phone/email ကို ဖတ်ပြီး AddContact() ကို ခေါ်ပါ။ "2. Show All" ရွေးရင် ShowAll() ကို ခေါ်ပြီး contacts list ကို console မှာ ပြပါ။ "0. Exit" နဲ့ loop ကနေ ထွက်နိုင်အောင် condition ထည့်ပါ။

Code နမူနာ

csharp
class Contact
{
    public string Name { get; set; }
    public string Phone { get; set; }
    public string Email { get; set; }
}

class ContactManager
{
    private List<Contact> contacts = new List<Contact>();

    public void AddContact(Contact c)
    {
        contacts.Add(c);
        Console.WriteLine("Contact added: " + c.Name);
    }

    public void ShowAll()
    {
        Console.WriteLine("--- Contact List ---");
        foreach (Contact c in contacts)
        {
            Console.WriteLine($"{c.Name} | {c.Phone} | {c.Email}");
        }
    }
}

class Program
{
    static void Main(string[] args)
    {
        ContactManager manager = new ContactManager();
        bool running = true;

        while (running)
        {
            Console.WriteLine("\n1. Add Contact  2. Show All  0. Exit");
            string choice = Console.ReadLine();

            if (choice == "1")
            {
                Console.Write("Name: ");
                string name = Console.ReadLine();
                Console.Write("Phone: ");
                string phone = Console.ReadLine();
                Console.Write("Email: ");
                string email = Console.ReadLine();
                manager.AddContact(new Contact { Name = name, Phone = phone, Email = email });
            }
            else if (choice == "2")
            {
                manager.ShowAll();
            }
            else if (choice == "0")
            {
                running = false;
            }
        }
    }
}
You should see
Console မှာ menu ပေါ်ပြီး Contact တွေကို add လုပ်နိုင်၊ list အနေနဲ့ ပြန်ကြည့်နိုင်တဲ့ working console app ရရှိမှာ ဖြစ်ပါတယ်။

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

Contact class ထဲမှာ Category ("Family", "Work", "Friend") property တစ်ခု ထပ်ဖြည့်ပြီး, ShowAll() ထဲမှာ category ပါအောင် ပြပါ။

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

Menu loop ရေးတဲ့အခါ Exit condition ကို အမှန်ထားဖို့ သတိထားပါ၊ မဟုတ်ရင် infinite loop ဖြစ်သွားနိုင်ပါတယ်။

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

  • List<Contact> ကို Main() method ထဲမှာ local variable အနေနဲ့ ထားပြီး method တစ်ခုချင်းစီကို parameter အနေနဲ့ ပို့ရတာ ရှုပ်စေတတ်တာ — class field အနေနဲ့ ထားရင် manage လုပ်ရ ပိုလွယ်ပါတယ်
  • Console.ReadLine() က return value ကို variable မှာ မသိမ်းဘဲ ချန်ထားခဲ့တတ်တာ

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

Contact class ထဲမှာ Category ("Family", "Work", "Friend") property တစ်ခု ထပ်ဖြည့်ပြီး, ShowAll() ထဲမှာ category ပါအောင် ပြပါ။

You'll know it worked when: Console မှာ menu ပေါ်ပြီး Contact တွေကို add လုပ်နိုင်၊ list အနေနဲ့ ပြန်ကြည့်နိုင်တဲ့ working console app ရရှိမှာ ဖြစ်ပါတယ်။

Project: Contact Book – Part 1 (Setup + Core Structure) | Thuta Learning