using System.Collections.Generic;
namespace Contact_Management
public static void Main(string[] args)
ContactBook cb = new ContactBook();
Console.WriteLine("--------Contact Management--------");
ContactBook.PrintOptions();
var input = Console.ReadLine();
public string Name { get; set; }
public string MobileNo { get; set; }
public string City { get; set; }
public Person(string name, string mobileNo, string city)
this.MobileNo = mobileNo;
private List<Person> contactBookList = new List<Person>();
public List<Person> ContactBookList
get { return contactBookList; }
set { this.contactBookList = value; }
ContactBookList = new List<Person>();
public static void PrintOptions()
Console.WriteLine("\n Choose One from following Options: \n");
Console.WriteLine(" #1 Add Contact");
Console.WriteLine(" #2 Update Contact ");
Console.WriteLine(" #3 Delete Contact");
Console.WriteLine(" #4 Show All Contacts");
Console.WriteLine(" #5 Search Records");
Console.WriteLine(" #6 Save");
Console.WriteLine(" #7 Get All Records From File");
Console.WriteLine(" #8 Exit \n");
public void CreateContact()
startInsert: Console.WriteLine("\n Enter Name :");
var name = Console.ReadLine();
Console.WriteLine("Enter Mobile Number :");
var moNo = Console.ReadLine();
Console.WriteLine("Enter City Name :");
var city = Console.ReadLine();
var contactWithSameName = ContactBookList.Where(p => p.Name == name && p.MobileNo == moNo).SingleOrDefault();
if (contactWithSameName == null)
Person person = new Person(name, moNo, city);
person.Id = AutoIncrementId();
AddContactToList(person);
Console.WriteLine("\n !!!!!! Contact is already available with this Name, Please Enter Different Name Or Mobile Number :");
private int AutoIncrementId()
private void AddContactToList(Person person)
ContactBookList.Add(person);
public void ShowAllContacts()
Console.WriteLine("\n----------------------------CONTACT-LIST----------------------------");
Console.WriteLine("\n\nId \t\t Name \t\t\t Mobile No \t\t City");
Console.WriteLine("------------------------------------------------------------------------");
if (ContactBookList.Count == 0)
Console.WriteLine("\t\t\t No Data Found ");
foreach (var person in ContactBookList)
Console.Write(" {0,-10}", person.Id);
Console.Write("\t {0,-20}", person.Name);
Console.Write("{0,-20}", person.MobileNo);
Console.Write("{0,-20} \n", person.City);
public void DeleteContact()
if (ContactBookList.Count != 0)
startDelete: Console.WriteLine("\n Enter name you want to delete :");
var name = Console.ReadLine();
var contactWithSameName = ContactBookList.Where(person => person.Name == name);
if (contactWithSameName.Any())
ContactBookList.RemoveAll(x => x.Name == name);
Console.WriteLine("\n !!!!!! WRONG CHOICE.... Contact is not available with this Name \n");
Console.WriteLine("1. Please Enter Correct Name :");
Console.WriteLine("2. See Contact List :");
Console.WriteLine("3. Go Back to Main Menu : \n");
var ch = Console.ReadLine();
Console.WriteLine("\n!!!!! There is no data available to delete \n ");
public void WriteToFile()
string json = JsonConvert.SerializeObject(ContactBookList.ToArray());
public void ReadFromFile()
public void UpdateContact()
if (ContactBookList.Count != 0)
startUpdate: Console.WriteLine("\n Enter existing name you want to update");
var oldName = Console.ReadLine();
var contactWithSameName = ContactBookList.Where(person => person.Name == oldName);
if (contactWithSameName.Any())
updateOptions: Console.WriteLine("\n What you want to update, Choose from following :");
Console.WriteLine(" 1. Name");
Console.WriteLine(" 2. Mobile No.");
Console.WriteLine(" 3. City \n");
var updateInput = Console.ReadLine();
Console.WriteLine("\n Enter new Name :");
var newName = Console.ReadLine();
foreach (var person in contactWithSameName)
Console.WriteLine("\n Enter new Mobile No :");
var newMobileNo = Console.ReadLine();
foreach (var person in contactWithSameName)
person.MobileNo = newMobileNo;
Console.WriteLine("\n Enter new City :");
var newCity = Console.ReadLine();
foreach (var person in contactWithSameName)
Console.WriteLine("\n\t !!!!! WRONG CHOICE : ");
Console.WriteLine("\t 1. Do you want to Continue? ");
Console.WriteLine("\t 2. Go Back to Main Menu : \n");
var ch = Console.ReadLine();
Console.WriteLine("\n >>>>> Please choose correct options from following:");
Console.WriteLine("\n !!!!!! WRONG CHOICE.... Contact is not available with this Name.");
Console.WriteLine("1. Please Enter Correct Name :");
Console.WriteLine("2. See Contact List : ");
Console.WriteLine("3. Go Back to Main Menu : \n");
var ch = Console.ReadLine();
Console.WriteLine("\n!!!!! There is no data available to Update \n ");
public void SearchRecord()
if(ContactBookList.Count != 0)
Console.WriteLine("\n Enter Contact Person's Name you want to see : ");
var name = Console.ReadLine();
var matchedRecords = ContactBookList.Where(p => p.Name == name);
Console.WriteLine("\n----------------------------CONTACT-LIST----------------------------");
Console.WriteLine("\n\nId \t\t Name \t\t\t Mobile No \t\t City");
Console.WriteLine("------------------------------------------------------------------------");
if (matchedRecords.Count() == 0)
Console.WriteLine("\t\t\t No Data Found ");
foreach(var person in matchedRecords)
Console.Write(" {0,-10}", person.Id);
Console.Write("\t {0,-20}", person.Name);
Console.Write("{0,-20}", person.MobileNo);
Console.Write("{0,-20} \n", person.City);
Console.WriteLine("\n !!!!! There is no data available to search .... Please Add Some data first \n");