phone book in c#
I am a beginner in programming and I should prepare a phone book in console application as my project. I have written a code for some part of it, however I can't write a method for searching, editing, and deleting the information in the array list. Here is the code and I appreciate if anyone help me on writing a code for bold parts which includes the method for search, edit, and delete. thanks
class Program
{
static ArrayList tel_book_arr = new ArrayList();
static void Main(string[] args)
{
int sel=0;
while (sel != 6)
{
Console.Clear();
Console.WriteLine("1 : enter information");
Console.WriteLine("2 : display information");
Console.WriteLine("3 : search information");
Console.WriteLine("4 : edit information");
Console.WriteLine("5 : delete information");
Console.WriteLine("6 : exit");
Console.Write("\nenter your choose : ");
sel = Convert.ToInt32(Console.ReadLine());
switch (sel)
{
case 1:
enter_info();
break;
case 2:
show_info();
break;
case 3:
search_ifo();
开发者_StackOverflow break;
case 4:
edit_info();
break;
case 5:
delet_ifo();
break;
}
}
}
static void enter_info()
{
Console.Clear();
telephone t = new telephone();
Console.Write("enter name : ");
t.name = Console.ReadLine();
Console.Write("enter family : ");
t.family = Console.ReadLine();
Console.Write("enter tel : ");
t.tel = Console.ReadLine();
tel_book_arr.Add(t);
}
static void show_info()
{
Console.Clear();
foreach (telephone temp in tel_book_arr)
{
Console.WriteLine("name : " + temp.name);
Console.WriteLine("family : " + temp.family);
Console.WriteLine("tel : " + temp.tel);
Console.ReadKey();
}
}
static void search_ifo()
{
Console.Clear();
object name = Console.Read("please enter the number: ");
object family = Console.Read("please enter the family: ");
}
static void edit_info()
{
Console.Clear();
search_ifo();
}
static void delet_ifo()
{
Console.Clear();
}
}
class telephone
{
public string name, family, tel;
}
Don't use an ArrayList to store your data, use a different collection like a List<telephone>
or a simple Dictionary
(see
http://msdn.microsoft.com/en-us/library/xfhwa508.aspx for more information)
This will give you practical help http://www.dotnetperls.com/dictionary
Have a look at this example
Dictionary<string, string> phonebook = new Dictionary<string, string>();
phonebook.Add("Fred", "555-5555");
phonebook.Add("Harry", "555-5556");
// See if Dictionary contains this string
if (phonebook.ContainsKey("Fred")) // True
{
string number = phonebook["Fred"];
Console.WriteLine(number);
}
// See if Dictionary contains this string
if (phonebook.ContainsKey("Jim"))
{
Console.WriteLine("Not in phonebook"); // Nope
}
精彩评论