Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- // Kaizer here. I created a phonebook in C# displaying contact names, with the ability to search for existing contacts, delete contacts, e.t.c.
- class Contact
- {
- public string Name { get; }
- public string PhoneNumber { get; }
- public string Email { get; }
- public Contact(string name, string phoneNumber, string email)
- {
- Name = name;
- PhoneNumber = phoneNumber;
- Email = email;
- }
- public void Display()
- {
- Console.WriteLine($"Name: {Name}, Phone: {PhoneNumber}, Email: {Email}");
- }
- }
- class Phonebook
- {
- private List<Contact> contacts = new List<Contact>();
- public void AddContact(string name, string phoneNumber, string email)
- {
- contacts.Add(new Contact(name, phoneNumber, email));
- Console.WriteLine($"Contact {name} added successfully.");
- }
- public void DeleteContact(string name)
- {
- var contact = contacts.FirstOrDefault(c => c.Name == name);
- if (contact != null)
- {
- contacts.Remove(contact);
- Console.WriteLine($"Contact {name} deleted successfully.");
- }
- else
- {
- Console.WriteLine($"Contact {name} not found.");
- }
- }
- public void SearchContact(string name)
- {
- var contact = contacts.FirstOrDefault(c => c.Name == name);
- if (contact != null)
- {
- Console.WriteLine("Contact found:");
- contact.Display();
- }
- else
- {
- Console.WriteLine($"Contact {name} not found.");
- }
- }
- public void DisplayContacts()
- {
- if (contacts.Count == 0)
- {
- Console.WriteLine("No contacts in the phonebook.");
- }
- else
- {
- Console.WriteLine("Contacts in the phonebook:");
- foreach (var contact in contacts)
- {
- contact.Display();
- }
- }
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- Phonebook phonebook = new Phonebook();
- phonebook.DisplayContacts();
- phonebook.SearchContact("HiddenKesh");
- phonebook.DeleteContact("SoloCord");
- phonebook.DisplayContacts();
- phonebook.SearchContact("YingYang");
- phonebook.DeleteContact("PictureFolder");
- phonebook.SearchContact("BartholomewJohnsonIII");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement