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

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

Thời Gian Thi: 14h40 - 15h40

Muộn 1 phút trừ 0.5 điểm 

Liên kết rút gọn:

https://gokisoft.com/1564

Bình luận

avatar
Võ Như Việt [C2010L]
2021-10-28 13:29:22


#Contact.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BT1564_Exam_Csharp_
{
    class Contact
    {
        public string Name { get; set; }
        public string Phone { get; set; }

        public Contact()
        {
        }

        public Contact(string name, string phone)
        {
            Name = name;
            Phone = phone;
        }

        public void Input()
        {
            Console.WriteLine("Enter Name: ");
            Name = Console.ReadLine();
            Console.WriteLine("Enter PhoneNumber: ");
            Phone = Console.ReadLine();
        }

        public void Display()
        {
            Console.WriteLine("{0} \t {1}", Name, Phone);
        }
    }
}


#Program.cs


using System;
using System.Collections;

namespace BT1564_Exam_Csharp_
{
    class Program
    {
        static Hashtable ht = new Hashtable();
        delegate void SwitchCase();
        static void Main(string[] args)
        {
           
            SwitchCase[] options = { addContact, FindByName,Display,Exit };
            int choose;

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

                if(choose > 0 && choose <= options.Length)
                {
                    options[choose - 1]();
                }
                else
                {
                    Console.WriteLine("Choose Wrong!!!");
                }
            } while (choose != options.Length);
        }

        private static void addContact()
        {
            
            Console.WriteLine("Input n person:");
            int n = int.Parse(Console.ReadLine());
            for(int i = 0; i < n; i++) {
                Contact c = new Contact();
                c.Input();
                ht.Add(c.Name,c.Phone);
            }
        }

        private static void FindByName()
        {
            
            Console.WriteLine("Input name need search: ");
            string checkname = Console.ReadLine();
            foreach (DictionaryEntry item in ht)
            {
                if( checkname.Equals(item.Key))
                {
                    Console.WriteLine("");
                    Console.WriteLine("This search: Name is {0} \t Phone number:{1}", item.Key, item.Value);
                }
            }
        }

        private static void Display()
        {
            Console.WriteLine("\t Address Book \t");
            Console.WriteLine("Contact Name \t Phone number");
            foreach (DictionaryEntry item in ht)
            {
                Console.WriteLine("{0} \t {1}" ,item.Key,item.Value);
            }
        }

        private static void Exit()
        {
            Console.WriteLine("Exit program!!!");
        }

        static void ShowMenu()
        {
            Console.WriteLine("1. Add new contact");
            Console.WriteLine("2. Find a contact by name");
            Console.WriteLine("3. Display contacts");
            Console.WriteLine("4. Exit");

        }
    }
}


avatar
Đào Mạnh Dũng [C2010L]
2021-10-28 12:45:17


#Contact.cs


using System;

namespace _1564
{
    internal class Contact
    {
        public string Name { get; set; }
        public string Phone { get; set; }

        public Contact()
        {
        }

        public Contact(string name, string phone)
        {
            Name = name;
            Phone = phone;
        }

        public void Input()
        {
            Console.WriteLine("nhap name");
            Name = Console.ReadLine();
            Console.WriteLine("nhap phone");
            Phone = Console.ReadLine();
        }

        public void Display()
        {
            Console.WriteLine(this);
        }

        public void Display2()
        {
            Console.WriteLine(Name+"\t"+Phone);
        }

        public override string ToString()
        {
            return "Contact{" + "Name=" + Name + ", Phone=" + Phone + '}';
        }
    }
}


#ContactManager.cs


using lib;
using System;
using System.Collections.Generic;

namespace _1564
{
    internal class ContactManager
    {
        private static Dictionary<string, Contact> contactManager = new Dictionary<string, Contact>();

        private static void Main(string[] args)
        {
            SwitchCase[] menu = { ShowMenu, Add, Find, Display, Utility.Exist };
            Utility.Menu(menu);
        }

        private static void Add()
        {
            Contact contact = new Contact();
            contact.Input();
            contactManager.Add(contact.Name, contact);
        }

        private static void Find()
        {
            Console.WriteLine("nhap name cua contact");
            string name = Console.ReadLine();
            if (contactManager.ContainsKey(name))
            {
                contactManager[name].Display();
            }
            else
            {
                Console.WriteLine("Not found");
            }
        }

        private static void Display()
        {
            Console.WriteLine("\tAddress Book");
            Console.WriteLine("Contact Name Phone number");
            foreach (var item in contactManager)
            {
                item.Value.Display2();
            }
        }

        private static void ShowMenu()
        {
            Console.WriteLine(
            "o 1. Add new contact\n" +
            "o 2. Find a contact by name\n" +
            "o 3. Display contacts\n" +
            "o 4. Exit");
        }
    }
}


#Utility.cs


using Newtonsoft.Json;
using System;
using System.IO;

namespace lib
{
    internal delegate void SwitchCase();

    internal class Utility
    {
        public static void Menu(SwitchCase[] menu)
        {
            int choose;
            while (true)
            {
                menu[0]();
                choose = Utility.ReadInt();
                if (choose <= menu.Length)
                {
                    menu[choose]();
                }
                else Console.WriteLine("Dau vao khong hop le! ");
            }
        }

        public static void Exist()
        {
            Environment.Exit(0);
        }

        public static int ReadInt()
        {
            int integer;
            while (true)
            {
                try
                {
                    integer = int.Parse(Console.ReadLine());
                    break;
                }
                catch (Exception)
                {
                    Console.Write("du lieu dau vao khong hop le, nhap lai : ");
                }
            }
            return integer;
        }

        public static float ReadFloat()
        {
            float _float;
            while (true)
            {
                try
                {
                    _float = float.Parse(System.Console.ReadLine());
                    break;
                }
                catch (Exception)
                {
                    Console.Write("du lieu dau vao khong hop le, nhap lai : ");
                }
            }
            return _float;
        }

        public static DateTime ReadDate()
        {
            DateTime date;
            while (true)
            {
                try
                {
                    date = ConvertDateTime(Console.ReadLine());
                    break;
                }
                catch (Exception)
                {
                    Console.Write("du lieu dau vao khong hop le, nhap lai : ");
                }
            }
            return date;
        }

        public static string ConvertDateTime(DateTime myDate)
        {
            return myDate.ToString("dd-MM-yyyy");
        }

        public static DateTime ConvertDateTime(string str)
        {
            return DateTime.ParseExact(str, "dd-MM-yyyy", System.Globalization.CultureInfo.InvariantCulture);
        }

        public static T ReadfileJson<T>(string path)
        {
            return JsonConvert.DeserializeObject<T>(System.IO.File.ReadAllText(path));
        }

        public static void SavefileJson(object obj, string path)
        {
            System.IO.File.WriteAllText(path, JsonConvert.SerializeObject(obj));
            Console.WriteLine("save file json is success!");
        }

        public static void SaveObject(object serialize, string path)
        {
            using (Stream stream = File.Open(path, FileMode.Create))
            {
                new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter().Serialize(stream, serialize);
                Console.WriteLine("save file object is success!");
            }
        }

        public static object ReadObject(string path)
        {
            using (Stream stream = File.Open(path, FileMode.Open))
            {
                return new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter().Deserialize(stream);
            }
        }

        public static void directoryCreate(string path)
        {
            string directoryPath = path;

            DirectoryInfo directory = new DirectoryInfo(directoryPath);

            if (!directory.Exists)
            {
                directory.Create();
            }

            Console.WriteLine("Create directory {0} is success!", directory.Name);
        }
    }
}


avatar
Hieu Ngo [community,C2009G]
2021-10-28 04:10:49


#Contact.cs


using System;
using System.Collections.Generic;
using System.Text;

namespace Test1
{
    class Contact
    {
        public string ContactName { get; set; }
        public string PhoneNumber { get; set; }
        public Contact () { }

        public Contact(string contactName, string phoneNumber)
        {
            ContactName = contactName;
            PhoneNumber = phoneNumber;
        }
        public void Input()
        {
            Console.WriteLine("Enter Contact Name:");
            ContactName = Console.ReadLine();
            Console.WriteLine("Enter Phone Number:");
            PhoneNumber = Console.ReadLine();
        }

        public void Display()
        {
            Console.WriteLine("Contact Name: {0}, Phone Number: {1}", ContactName, PhoneNumber);
        }
    }
}


#Program.cs


using System;
using System.Collections.Generic;

namespace Test1
{
    class Program
    {
        delegate void SwitchCase();
        public static List<Contact> ContactList = new List<Contact>();
        static void Main(string[] args)
        {
           
            int choose;
            SwitchCase[] options = { AddNewContact, FindByName, Display, Exit };
            do
            {
                ShowMenu();
                choose = int.Parse(Console.ReadLine());
                if(choose >0 && choose <= options.Length)
                {
                    options[choose - 1]();
                }
            } while (choose != options.Length);
        }

        private static void Exit()
        {
            Console.WriteLine("Exit!!!");
        }

        private static void Display()
        {
            foreach(Contact contact in ContactList)
            {
                contact.Display();
            }
        }

        private static void FindByName()
        {
            Console.WriteLine("Enter Name Find:");
            string nameFind = Console.ReadLine();
            int count = 0;
            foreach(Contact contact in ContactList)
            {
                if(contact.ContactName == nameFind)
                {
                    count++;
                    contact.Display();
                }
            }
            if(count > 0)
            {
                Console.WriteLine("Tong so nguoi tim duoc {0}", count);
            } else
            {
                Console.WriteLine("{0} dose not exist", nameFind);
            }
        }

        private static void AddNewContact()
        {
            Console.WriteLine("Enter Number of Contact:");
            int n = int.Parse(Console.ReadLine());
            for(int i = 0; i < n; i++)
            {
                Contact contact = new Contact();
                contact.Input();
                ContactList.Add(contact);
            }
        }

        private static void ShowMenu()
        {
            Console.WriteLine("1. Add new contact");
            Console.WriteLine("2. Find a contact by name");
            Console.WriteLine("3. Display contacts");
            Console.WriteLine("4. Exit");
            Console.WriteLine("Choose:");
        }
    }
}


avatar
Nguyễn Hùng Anh [community,C2009G]
2021-10-28 04:09:47


#Contact.cs


using System;
using System.Collections.Generic;
using System.Text;

namespace Bt1564
{
    class Contact
    {
        public string Fullname { get; set; }
        public string Phone { get; set; }

        public Contact()
        {
        }

        public Contact(string fullname, string phone)
        {
            Fullname = fullname;
            Phone = phone;
        }

        public void Input()
        {
            Console.WriteLine("Enter name: ");
            Fullname = Console.ReadLine();

            Console.WriteLine("Enter phone number: ");
            Phone = Console.ReadLine();
        }

        public void Display()
        {
            Console.WriteLine("{0}\t{1}", Fullname, Phone);
        }

    }
}


#Program.cs


using System;
using System.Collections.Generic;

namespace Bt1564
{
    delegate void SwitchCase();
    class Program
    {
        static List<Contact> contactList;
        static void Main(string[] args)
        {
            contactList = new List<Contact>();
            SwitchCase[] options = { Input, FindByName, Display, ExitProgram };
            int c;
            
            do
            {
                ShowMenu();
                c = int.Parse(Console.ReadLine());

                if(c > 0 && c <= options.Length)
                {
                    options[c - 1]();
                } else
                {
                    Console.WriteLine("Nhap sai!!!");
                }
            } while (c != 4);
        }

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

            contactList.Add(contact);
        }

        private static void FindByName()
        {
            Console.WriteLine("Enter name: ");
            string name = Console.ReadLine();

            foreach(Contact contact in contactList)
            {
                if(contact.Fullname == name)
                {
                    Console.WriteLine("Contact Name \t Phone Number");
                    contact.Display();
                    break;
                }
            }
            Console.WriteLine("Not found!!!");
        }

        private static void Display()
        {
            Console.WriteLine("\tAddress Book");
            Console.WriteLine("Contact Name \t Phone Number");
            foreach(Contact contact in contactList)
            {
                contact.Display();
            }
        }

        private static void ExitProgram()
        {
            Console.WriteLine("Exit program!!!");
        }

        static void ShowMenu()
        {
            Console.WriteLine("1. Add new contact");
            Console.WriteLine("2. Find a contact by name");
            Console.WriteLine("3. Display contacts");
            Console.WriteLine("4. Exit");
            Console.WriteLine("Choose: ");
        }
    }
}


avatar
Nguyen Trung Kien [C2010G]
2021-10-18 03:14:54


#Contact.cs


using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace Lession04
{
    class Contact
    {
        public Hashtable Add(Hashtable hashtable)
        {
            Contactmanager cont = new Contactmanager();

            Console.WriteLine("Enter FullName: ");
            cont.Fullname = Console.ReadLine();

            Console.WriteLine("Enter PhoneNumber: ");
            cont.Phonenumber = Console.ReadLine();

            hashtable.Add(cont.Fullname, cont.Phonenumber);

            return hashtable;
        }
        public void Display(Hashtable hashtable)
        {
            ICollection key = hashtable.Keys;
            Console.WriteLine("\t\tAddress Book\t");
            Console.WriteLine("\tContactName\t\tPhone");
            foreach (String s in key)
            {
                Console.WriteLine("\t" + s + "\t\t\t" + hashtable[s]);
            }
        }
        public Hashtable Seach(Hashtable hashtable)
        {
            Console.WriteLine("Enter Name Sach: ");
            String name = Console.ReadLine();
            Console.WriteLine("\n\tContatName\t\tPhone");
            int flag = 0;
            ICollection key = hashtable.Keys;
            foreach (string s in key)
            {
                if (s.Equals(name))
                {
                    Console.WriteLine("\t" + s + "\t\t\t" + hashtable[s]);
                    flag = 1;
                }
            }
            if (flag == 0)
            {
                Console.WriteLine("Name not found");
            }

            return hashtable;
        }

    }
}


#Contactmanager.cs


using System;
using System.Collections.Generic;
using System.Text;

namespace Lession04
{
    class Contactmanager
    {
        public String Fullname { get; set; }
        public String Phonenumber { get; set; }

        public Contactmanager()
        {

        }
        public Contactmanager(string fullname, string phonenumber)
        {
            Fullname = fullname;
            Phonenumber = phonenumber;
        }
        
    }

}


#Program.cs


using System;
using System.Collections;

namespace Lession04
{
    class Program
    {
        static void Main(string[] args)
        {
            Hashtable hashtable = new Hashtable();
            int choose;
            do
            {
                Menu();
                choose = int.Parse(Console.ReadLine());
                switch (choose)
                {
                    case 1:
                        Console.WriteLine("Enter number contact :");
                        int n = int.Parse(Console.ReadLine());
                        for (int i = 0; i < n; i++)
                        {
                            Console.WriteLine("Contact {0}", i+1);
                            new Contact().Add(hashtable);
                        }
                        break;
                    case 2:
                        new Contact().Seach(hashtable);
                        break;
                    case 3:
                        new Contact().Display(hashtable);
                        break;
                    case 4:
                        Console.WriteLine("Exit!!!!");
                        break;
                }
            } while (choose != 4);
        }
        static void Menu()
        {
            Console.WriteLine("1. Add new contact");
            Console.WriteLine("2. Find a contact by name");
            Console.WriteLine("3. Display contacts");
            Console.WriteLine("4. Exit");

        }
    }
}


avatar
PHẠM VĂN LIÊM [C2010G]
2021-10-18 02:58:01


#Contact.cs


using System;
using System.Collections.Generic;
using System.Text;

namespace Test02
{
    class Contact
    {
        public string name { get; set; }
        public string phone { get; set; }

        public Contact()
        {

        }

        public Contact(string name, string phone)
        {
            this.name = name;
            this.phone = phone;
        }

        public void Input()
        {
            Console.Write("Nhap ten nguoi lien he: ");
            name = Console.ReadLine();
            Console.WriteLine("Nhap so dien thoai nguoi lien he");
            phone = Console.ReadLine();
        }

        public void Display()
        {
            Console.WriteLine("Ten lien he: {0}, phone: {1}", name, phone);
        }
    }
}


#Program.cs


using System;
using System.Collections;

namespace Test02
{
    class Program
    {
        static Hashtable hash = new Hashtable();
        static void Main(string[] args)
        {
            int choose;
            do
            {
                ShowMenu();
                choose = ReadInt();
                switch(choose)
                {
                    case 1:
                        Input();
                        break;
                    case 2:
                        Search();
                        break;
                    case 3:
                        Display();
                        break;
                    case 4:
                        Console.WriteLine("See you again.");
                        break;
                    default:
                        Console.WriteLine("Wrong option!!!");
                        break;
                }
            } while (choose != 4);
        }

        private static void Display()
        {
            foreach(DictionaryEntry item in hash)
            {
                Console.WriteLine("Name: {0}, Telephone: {1}", item.Key, item.Value);
            }
        }

        private static void Search()
        {
            Console.WriteLine("Search phone number by name.");
            string name = Console.ReadLine();
           
            foreach(DictionaryEntry item in hash)
            {
                if(item.Key == name)
                {
                    
                    Console.WriteLine("Telephone found is " + item.Value);
                }
            }
            
        }

        private static void Input()
        {
            Console.WriteLine("How many new contacts do you want add?");
            int n = ReadInt();
            for(int i = 1; i <= n; i++)
            {
                Console.WriteLine("Input new contacts " + i);
                Contact contact = new Contact();
                contact.Input();
                hash.Add(contact.name, contact.phone);
            }
        }

        static void ShowMenu()
        {
            Console.WriteLine("1:Add new Contact.");
            Console.WriteLine("2:Find a contact by Name.");
            Console.WriteLine("3:Display contacts.");
            Console.WriteLine("4:Exit.");
        }

        static int ReadInt()
        {
            int result;
            while(true)
            {
                try
                {
                    result = int.Parse(Console.ReadLine());
                    return result;
                } catch
                {
                    Console.WriteLine("Wrong input!!!");
                }
            }
        }
    }
}


avatar
Nam20021608 [community,AAHN-C2009G]
2021-10-16 09:15:52


#Program.cs


using System;
using Examl2.Libary;
using Examl2.Model;
using System.Collections.Generic;
namespace Examl2
{
    class Program
    {
        static List<ContactManager> listContact = new List<ContactManager>();
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
        static void Test01(){
            int a;
            do
            {
                Console.WriteLine("1. Them lien lac");
                Console.WriteLine("2. Tim so dien thoai theo ten");
                Console.WriteLine("3. Hien thi toan bo thong tin lien he");
                Console.WriteLine("4. Thoat");
                Console.Write("Lua chon: ");
                a = Utility.ReadInt();
                switch(a){
                    case 1: 
                        Console.WriteLine("nhap so luong nguoi lien lac : ");
                        int c = Utility.ReadInt();
                        for(int i = 0; i < c ; i++){
                            ContactManager cm = new ContactManager();
                            cm.Input();
                            listContact.Add(cm);
                        }
                        break;
                    case 2: 
                        Console.WriteLine("Nhap ten thong tin lien he can tim : ");
                        String str = Console.ReadLine();
                        String d = "";
                        if(listContact.Count < 0){
                            Console.WriteLine("Vui long them lien he , danh sach chong !");
                        }
                        for(int i = 0 ; i < listContact.Count ; i++){
                            if(listContact[i].Name.Equals(str)){
                                listContact[i].Display();
                                d += listContact[i].Name;
                            }
                        }
                        if(d == null){
                            Console.WriteLine("Khong tim thay lien he nay!");
                        }
                        break;
                    case 3: 
                        foreach(ContactManager cs in listContact){
                            cs.Display();
                        }
                        break;
                    case 4:
                        Console.WriteLine("Dong thanh cong!");
                        break;
                    default:
                        Console.WriteLine("Nhap lai !");
                        break;
                }
            } while (a!=4);
        }
    }
}


#ContactManager.cs


using System;
using Examl2.Libary;
namespace Examl2.Model
{
    class ContactManager
    {
        public String Name{get;set;}
        public int Contact{get;set;}

        public ContactManager(){}

        public ContactManager(String name , int contact){
            this.Name = name;
            this.Contact =contact;
        }

        public void Input(){
            Console.WriteLine("Nhap Ten nguoi lien lac: ");
            Name = Console.ReadLine();
            Contact = Utility.ReadContact();
        }
        public void Display(){
            Console.WriteLine("Ten : {0} , So dien thoai: {1}",Name , Contact);
        }
    }
}


#Utility.cs


using System;
namespace Examl2.Libary
{
    class Utility
    {
        public static int ReadContact()
        {
            Console.WriteLine("Nhap so dien thoai: ");
            String c = Console.ReadLine();
            while (true)
            {
                try
                {
                    if (c.Length != 10){
                        Console.WriteLine("Số điện thoại của bạn không hợp lệ , phải có 10 số");
                    }

                    if (c.Substring(0, 2).Equals("09") || c.Substring(0, 2).Equals("03")
                    || c.Substring(0, 2).Equals("08") || c.Substring(0, 2).Equals("07")
                    || c.Substring(0, 2).Equals("05")
                    ){
                        int phone = int.Parse(c);
                        return phone;
                    }else
                    {
                        Console.WriteLine("Dau so dien thoai bat dau : 09 , 03, 08, 05 ,07");
                    }
                    
                }catch (Exception){
                    Console.WriteLine("Vui long nhap so dien thoai , khong nhap ki tu !");
                }
            }
        }
        public static int ReadContact(String c)
        {
            while (true)
            {
                try
                {
                    if (c.Length != 10){
                        Console.WriteLine("Số điện thoại của bạn không hợp lệ , phải có 10 số");
                    }
                    
                    if (c.Substring(0, 2).Equals("09") || c.Substring(0, 2).Equals("03")
                    || c.Substring(0, 2).Equals("08") || c.Substring(0, 2).Equals("07")
                    || c.Substring(0, 2).Equals("05")
                    ){
                        int phone = int.Parse(c);
                        return phone;
                    }else
                    {
                        Console.WriteLine("Dau so dien thoai bat dau : 09 , 03, 08, 05 ,07");
                    }
                    
                }catch (Exception){
                    Console.WriteLine("Vui long nhap so dien thoai , khong nhap ki tu !");
                }
            }
        }
        public static int ReadInt(){
            int b;
            while (true)
            {
                 try
                 {
                      b= int.Parse(Console.ReadLine());
                      return b;
                 }
                 catch (System.Exception)
                 {
                     Console.WriteLine("Nhap lai !");
                 }
            }
        }
    }
}


avatar
Hoàng Thiện Thanh [community,AAHN-C2009G]
2021-10-16 09:11:15


#Program.cs


using Exam.Models;
using Exam.Utils;
using System;
using System.Collections.Generic;

namespace Exam
{
    public class Program
    {
        public static List<Contact> ContactList = new List<Contact>();
        static void ShowMenu()
        {
            Console.WriteLine("1. Add new Contact\n" +
                "2. Find a contact\n" +
                "3. Display contacts\n" +
                "4. Exit");
        }
        static void AddContact()
        {
            Contact c = new Contact();
            c.Input();
            ContactList.Add(c);
        }
        static void FindContact()
        {
            if (ContactList.Count == 0)
            {
                Console.WriteLine("No available contacts");
                return;
            }
            Console.WriteLine("Enter Name of contact:");
            string Name = Console.ReadLine();
            foreach (Contact c in ContactList) { 
                if (Name == c.Name)
                {
                    c.Display();
                }
            }
        }
        static void DisplayContact()
        {
            int count = 0;
            foreach(Contact c in ContactList)
            {
                count++;
                Console.WriteLine("Contact["+ count +"]");
                c.Display();
            }
        }
        static void Main(string[] args)
        {
            int choose;
            do
            {
                ShowMenu();
                choose = Utility.ReadInt();
                switch (choose)
                {
                    case 1:
                        AddContact();
                        break;
                    case 2:
                        FindContact();
                        break;
                    case 3:
                        DisplayContact();
                        break;
                    case 4:
                        Console.WriteLine("Program Exited!");
                        break;
                    default:
                        Console.WriteLine("Wrong input");
                        break;
                }
            } while (choose != 4);
        }
    }
}


#Contact.cs


using Exam.Utils;
using System;
using System.Collections.Generic;
using System.Text;

namespace Exam.Models
{
    public class Contact
    {
        public string Name { get; set; }
        private string phone;
        public string Phone { get { return phone; } set { if (value.Length != 10) {
                    Console.WriteLine("Phone number must have 10 number digits. Enter:");
                    value = Utility.ReadNum();
                }
                this.phone = value;
            } }
        public Contact()
        {

        }
        public Contact(string Name, string Phone)
        {
            this.Name = Name;
            this.Phone = Phone;
        }
        public void Input() {
            Console.WriteLine("Input Name:");
            Name = Console.ReadLine();
            Console.WriteLine("Input Phone:");
            Phone = Utility.ReadNum();
        }
        public void Display()
        {
            Console.WriteLine("Contact Name: {0}\n" +
                "Phone Num: {1}\n", Name, Phone);
        }
    }
}


#Utility.cs


using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace Exam.Utils
{
    public class Utility
    {
        public static string ReadNum()
        {
            string str = Console.ReadLine();
            while (!Regex.Match(str, "^[0-9]*$").Success)
            {
                Console.WriteLine("Only accept numbers");
                str = Console.ReadLine();
            }
            return str;
        }
        public static int ReadInt()
        {
            string str = Console.ReadLine();
            while (!check())
            {
                Console.WriteLine("Input cannot be read as numeric value");
                str = Console.ReadLine();
            }
            bool check()
            {
                try { int.Parse(str); } catch (Exception) { return false; }
                return true;
            }
            return int.Parse(str);
        }
    }
}


avatar
Le Khanh An [community,AAHN-C2009G]
2021-10-16 09:06:18


#Contact.cs


using System;
using System.Collections.Generic;
using System.Text;

namespace Contact_Manager
{
    class Contact
    {
        public string Name { get; set; }
        public string Phone { get; set; }

        public Contact()
        {
        }

        public void Input()
        {
            Console.Write("Enter name: ");
            Name = Console.ReadLine();
            Console.Write("Enter phone number: ");
            Phone = Console.ReadLine();
        }

        public void Display()
        {
            Console.WriteLine(Name + "\t" + Phone);
        }
    }
}


#HashTable.cs


using System;
using System.Collections.Generic;

namespace Contact_Manager
{
    class HashTable
    {
        static List<Contact> contacts = new List<Contact>();

        static void Main(string[] args)
        {
            int c;
            do
            {
                showMenu();
                c = int.Parse(Console.ReadLine());
                switch(c)
                {
                    case 1:
                        Input();
                        break;
                    case 2:
                        SearchByName();
                        break;
                    case 3:
                        Display();
                        break;
                    case 4:
                        Environment.Exit(0);
                        break;
                }
            } while (c != 4);

            Console.ReadLine();
        }

        static void Display()
        {
            Console.WriteLine("Contact Name      Phone number");
            foreach (Contact contact in contacts)
            {
                 contact.Display();
            }
        }

        static void SearchByName()
        {
            Console.Write("Enter name: ");
            string name = Console.ReadLine();

            int c = 0;
            foreach(Contact contact in contacts)
            {
                if(contact.Name == name)
                {
                    c++;
                    contact.Display();
                }
            }

            if (c == 0) Console.WriteLine("Not found");
        }

        static void Input()
        {
            Console.Write("Enter n contacts: ");
            int n = int.Parse(Console.ReadLine());

            for(int i = 0; i < n; i++)
            {
                Contact contact = new Contact();
                contact.Input();
                contacts.Add(contact);
            }
        }

        public static void showMenu()
        {
            Console.Write("1. Add new contact\n2. Find a contact by name\n3. Display contacts\n4. Exit\nSelect: ");
        }
    }
}


avatar
Nguyễn Việt Hoàng [community,AAHN-C2009G]
2021-10-16 08:53:27


#HashTable.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ss12
{
    class HashTable
    {
        public string FullName { get; set; }
        public string PhoneNumBer { get; set; }
        public HashTable()
        {

        }
        public HashTable(string fullname, string phonenumber)
        {
            this.FullName = fullname;
            this.PhoneNumBer = phonenumber;
        }
        public void Input()
        {
            Console.WriteLine("Enter FullName :");
            FullName = Console.ReadLine();
            while(FullName.Length == 0)
            {
                Console.WriteLine("FullName is not Empty");
                Console.WriteLine("Enter FullName Again :");
                FullName = Console.ReadLine();
            }
            Console.WriteLine("Enter Phone NumBer :");
            PhoneNumBer = Console.ReadLine();
            while(PhoneNumBer.Length == 0)
            {
                Console.WriteLine("Phone NumBer is not Empty");
                Console.WriteLine("Enter Phone Number Again :");
                PhoneNumBer = Console.ReadLine();
            }
        }
        public void Display()
        {
            Console.WriteLine("FullName :{0}, PhoneNumBer :{1}", FullName, PhoneNumBer);
        }
    }
}


#Program.cs


using System;
using System.Collections.Generic;

namespace Ss12
{
    class Program
    {
        public static List<HashTable> HastList = new List<HashTable>();
        public static void ShowMenu()
        {
            Console.WriteLine("1.Add new Contact");
            Console.WriteLine("2.Find a Contact by name ");
            Console.WriteLine("3.Display contacts");
            Console.WriteLine("4.Exits");
        }
        public static void ListMenu()
        {
            int choose;
            do
            {
                ShowMenu();
                Console.WriteLine("Enter choose value of Menu");
                choose = int.Parse(Console.ReadLine());
                switch (choose)
                {
                    case 1:
                        Add();
                        break;
                    case 2:
                        FindByName();
                        break;
                    case 3:
                        Display();
                        break;
                    case 4:
                        Console.WriteLine("Exits");
                        return;
                    default:
                        Console.WriteLine("Value must be 1->4");
                        break;
                }
            } while (choose != 4);
        }

        private static void Display()
        {
            if(HastList.Count == 0)
            {
                Console.WriteLine("ConTact does not exists !");
                return;
            }
            Console.WriteLine("Display Contact :");
           foreach(HashTable h in HastList)
            {
                h.Display();
            }
        }

        private static void FindByName()
        {
            if (HastList.Count == 0)
            {
                Console.WriteLine("ConTact does not exists !");
                return;
            }
            int count = 0;
            Console.WriteLine("Enter Name You Need Find :");
            string find = Console.ReadLine();
            foreach(HashTable h in HastList)
            {
                if (h.FullName.Equals(find))
                {
                    h.Display();
                    count++;
                }
            }
            if(count == 0)
            {
                Console.WriteLine("Cannot Find Name ");
            }
        }

        private static void Add()
        {
            Console.WriteLine("Enter Sum Of Contact :");
            int sum = int.Parse(Console.ReadLine());
            for (int i = 0; i < sum; i++){
                Console.WriteLine("Contact[" + (i + 1) + "]");
                HashTable h = new HashTable();
                h.Input();
                HastList.Add(h);
            }
            Console.WriteLine("Add Contact Success");
        }

        static void Main(string[] args)
        {
            ListMenu();
        }
    }
}