Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace ConsoleApp14
- {
- class Program
- {
- static void Main(string[] args)
- {
- MobilePhone mobilePhone = new MobilePhone();
- mobilePhone.Menu();
- }
- }
- class MobilePhone
- {
- List<Contatcs> ListOfContants = new List<Contatcs>();
- public static void PrintMenu()
- {
- Console.ForegroundColor = ConsoleColor.Green;
- Console.WriteLine();
- Console.WriteLine("Choose Option:");
- Console.WriteLine("0 - Quit");
- Console.WriteLine("1 - Print List Of Contatcs");
- Console.WriteLine("2 - Add new Contact");
- Console.WriteLine("3 - Update Existing Contact");
- Console.WriteLine("4 - Remove Exisitng Object");
- Console.WriteLine("5 - Find Contact");
- Console.WriteLine();
- Console.ResetColor();
- }
- public void Menu()
- {
- PrintMenu();
- bool MenuOption = true;
- while (MenuOption)
- {
- int number = int.Parse(Console.ReadLine());
- switch (number)
- {
- case 0:
- MenuOption = false;
- break;
- case 1:
- PrintListOfContacts();
- break;
- case 2:
- addNewContact();
- break;
- case 3:
- UpdateExistingContact();
- break;
- case 4:
- RemoveExistingContact();
- break;
- case 5:
- FindContact();
- break;
- }
- PrintMenu();
- }
- }
- private void PrintListOfContacts()
- {
- foreach (var item in ListOfContants)
- {
- Console.WriteLine(item.ToString());
- }
- }
- private void addNewContact()
- {
- Console.WriteLine("Enter name:");
- string name = Console.ReadLine();
- Console.WriteLine("Enter telephone number");
- string telephone = Console.ReadLine();
- if (FindContact(name) == false)
- {
- ListOfContants.Add(new Contatcs(name, telephone));
- }
- else
- {
- Console.WriteLine("Contact already Exit.");
- }
- }
- bool FindContact()
- {
- Console.WriteLine("Enter Contact Name:");
- string name = Console.ReadLine();
- Console.WriteLine(FindContact(name));
- return false;
- }
- bool FindContact(string name)
- {
- for (int i = 0; i < ListOfContants.Count; i++)
- {
- if (ListOfContants[i].Name == name)
- {
- Console.WriteLine("Conctact: " + name + " Found on position : " + i);
- return true;
- }
- }
- return false;
- }
- bool UpdateExistingContact()
- {
- Console.WriteLine("Enter name of Contact to Update:");
- string name = Console.ReadLine();
- if (FindContact(name) == true)
- {
- for (int i = 0; i < ListOfContants.Count; i++)
- {
- if (ListOfContants[i].Name == name)
- {
- Console.WriteLine("Enter new Name:");
- string NewName = Console.ReadLine();
- ListOfContants[i].Name = NewName;
- }
- }
- return true;
- }
- return false;
- }
- bool RemoveExistingContact()
- {
- Console.WriteLine("Enter Contact do remove:");
- string name = Console.ReadLine();
- if (FindContact(name) == true)
- {
- foreach (Contatcs contact in ListOfContants)
- {
- if (contact.Name.Equals(name))
- {
- ListOfContants.Remove(contact);
- return true;
- }
- }
- }
- return false;
- }
- }
- class Contatcs
- {
- public Contatcs(string Name, string TelephoneNumber)
- {
- this.Name = Name;
- this.TelephoneNumber = TelephoneNumber;
- }
- public string Name { get; set; }
- public string TelephoneNumber { get; set; }
- public override string ToString()
- {
- return "Name:" + Name + "Telephone number:" + TelephoneNumber;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment