Advertisement
KingAesthetic

C# Example 2

Aug 13th, 2024 (edited)
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.88 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. // Kaizer here. I created a phonebook in C# displaying contact names, with the ability to search for existing contacts, delete contacts, e.t.c.
  6.  
  7. class Contact
  8. {
  9.     public string Name { get; }
  10.     public string PhoneNumber { get; }
  11.     public string Email { get; }
  12.  
  13.     public Contact(string name, string phoneNumber, string email)
  14.     {
  15.         Name = name;
  16.         PhoneNumber = phoneNumber;
  17.         Email = email;
  18.     }
  19.  
  20.     public void Display()
  21.     {
  22.         Console.WriteLine($"Name: {Name}, Phone: {PhoneNumber}, Email: {Email}");
  23.     }
  24. }
  25.  
  26. class Phonebook
  27. {
  28.     private List<Contact> contacts = new List<Contact>();
  29.  
  30.     public void AddContact(string name, string phoneNumber, string email)
  31.     {
  32.         contacts.Add(new Contact(name, phoneNumber, email));
  33.         Console.WriteLine($"Contact {name} added successfully.");
  34.     }
  35.  
  36.     public void DeleteContact(string name)
  37.     {
  38.         var contact = contacts.FirstOrDefault(c => c.Name == name);
  39.         if (contact != null)
  40.         {
  41.             contacts.Remove(contact);
  42.             Console.WriteLine($"Contact {name} deleted successfully.");
  43.         }
  44.         else
  45.         {
  46.             Console.WriteLine($"Contact {name} not found.");
  47.         }
  48.     }
  49.  
  50.     public void SearchContact(string name)
  51.     {
  52.         var contact = contacts.FirstOrDefault(c => c.Name == name);
  53.         if (contact != null)
  54.         {
  55.             Console.WriteLine("Contact found:");
  56.             contact.Display();
  57.         }
  58.         else
  59.         {
  60.             Console.WriteLine($"Contact {name} not found.");
  61.         }
  62.     }
  63.  
  64.     public void DisplayContacts()
  65.     {
  66.         if (contacts.Count == 0)
  67.         {
  68.             Console.WriteLine("No contacts in the phonebook.");
  69.         }
  70.         else
  71.         {
  72.             Console.WriteLine("Contacts in the phonebook:");
  73.             foreach (var contact in contacts)
  74.             {
  75.                 contact.Display();
  76.             }
  77.         }
  78.     }
  79. }
  80.  
  81. class Program
  82. {
  83.     static void Main(string[] args)
  84.     {
  85.         Phonebook phonebook = new Phonebook();
  86.         phonebook.AddContact("HiddenKesh", "357-323-112", "[email protected]");
  87.         phonebook.AddContact("KaizerTiger", "777-777-777", "[email protected]");
  88.         phonebook.AddContact("SoloCord", "212-478-098", "[email protected]");
  89.         phonebook.AddContact("YingYang", "676-874-679", "[email protected]");
  90.         phonebook.AddContact("PictureFolder", "345-100-094", "[email protected]");
  91.         phonebook.DisplayContacts();
  92.         phonebook.SearchContact("HiddenKesh");
  93.         phonebook.DeleteContact("SoloCord");
  94.         phonebook.DisplayContacts();
  95.         phonebook.SearchContact("YingYang");
  96.         phonebook.DeleteContact("PictureFolder");
  97.         phonebook.SearchContact("BartholomewJohnsonIII");
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement