By GokiSoft.com| 09:41 01/11/2021|
C Sharp

[Video] Giải bài tập quản lý thông tin liên lạc sử dụng Hashtable C#


LINK VIDEO BÀI GIẢNG

[Examination] Thi thực hành môn C# - C Sharp


#Program.cs


using System;
using System.Collections;

namespace BT1564
{
    class ContactManager
    {
        static Hashtable hashtable;

        static void Main(string[] args)
        {
            //Key => value (Key: Name, Value: Object Contact)
            hashtable = new Hashtable();

            int choose;

            do
            {
                ShowMenu();
                choose = int.Parse(Console.ReadLine());

                switch(choose)
                {
                    case 1:
                        Input();
                        break;
                    case 2:
                        Search();
                        break;
                    case 3:
                        Display();
                        break;
                    case 4:
                        Console.WriteLine("Thoat!!!");
                        break;
                    default:
                        Console.WriteLine("Nhap sai!!!");
                        break;
                }
            } while (choose != 4);
        }

        private static void Display()
        {
            Console.WriteLine("=== Danh sach thong tin");
            foreach(Contact contact in hashtable.Values)
            {
                contact.Display();
            }
        }

        private static void Search()
        {
            Console.WriteLine("Nhap ten tim kiem: ");
            string name = Console.ReadLine();

            if(hashtable.ContainsKey(name))
            {
                Contact contact = (Contact)hashtable[name];
                contact.Display();
            }
        }

        private static void Input()
        {
            Contact contact = new Contact();
            contact.Input();

            hashtable.Add(contact.Name, contact);
        }

        static void ShowMenu()
        {
            Console.WriteLine("1. Nhap thong tin lien lac");
            Console.WriteLine("2. Tim kiem theo ten");
            Console.WriteLine("3. Hien thi thong tin");
            Console.WriteLine("4. Thoat");
            Console.WriteLine("Chon: ");
        }
    }
}


#Contact.cs


using System;
namespace BT1564
{
    public class Contact
    {
        public string Name { get; set; }
        public string Phone { get; set; }

        public Contact()
        {
        }

        public void Input()
        {
            Console.WriteLine("Nhap ten: ");
            Name = Console.ReadLine();

            Console.WriteLine("Nhap SDT: ");
            Phone = Console.ReadLine();
        }

        public void Display()
        {
            Console.WriteLine("Ten: {0}, SDT: {1}", Name, Phone);
        }
    }
}


Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)