Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Linq;
- namespace ClassesII_Demo.Models
- {
- public static class Phonebook
- {
- private static readonly List<Contact> contacts = new List<Contact>();
- public static void AddContact(Contact contact)
- {
- foreach (var phoneContact in contacts)
- {
- if (phoneContact.Equals(contact))
- {
- throw new ArgumentException("Contact already exists!");
- }
- }
- contacts.Add(contact);
- }
- public static string ListContactsForSave(string orderBy)
- {
- // remove unused variable
- var builder = new StringBuilder();
- builder.AppendLine("[");
- for (int i=0;i<contacts.Count;i++)
- {
- var contact = contacts[i];
- builder.AppendLine(contact.GetContactInfoForSaving());
- if (i != contacts.Count - 1)
- {
- builder.Append(",");
- }
- }
- builder.AppendLine("]");
- return builder.ToString();
- }
- public static string ListContacts(string orderBy)
- {
- if (orderBy.Equals("0"))
- {
- var builder = new StringBuilder();
- foreach (var contact in contacts)
- {
- builder.AppendLine(contact.GetContactInfo());
- }
- return builder.ToString();
- }
- else
- if (orderBy.Equals("name"))
- {
- List<Contact> sortedByName = contacts.OrderBy(x => x.Name).ToList();
- var builder = new StringBuilder();
- foreach (var contact in sortedByName)
- {
- builder.AppendLine(contact.GetContactInfo());
- }
- return builder.ToString();
- }
- else if (orderBy.Equals("time"))
- {
- List<Contact> sortedByTime = contacts.OrderBy(x => x.CreatedOn).ToList();
- var builder = new StringBuilder();
- foreach (var contact in sortedByTime)
- {
- builder.AppendLine(contact.GetContactInfo());
- }
- return builder.ToString();
- }
- return "Phonebook is empty.Please enter a contact first!";
- }
- internal static string RemoveContact(string v)
- {
- if (v == null)
- {
- throw new ArgumentException("Name is null");
- }
- // var contact
- foreach (var PhoneContacts in contacts)
- {
- var currentContact = (Contact)PhoneContacts;
- if (currentContact.Name == v)
- {
- contacts.Remove(currentContact);
- return "Contact removed from phonebook";
- }
- }
- return "Contact to remove not found";
- }
- // todo string name
- internal static string UpdateContact(string v, string newNumber)
- {
- if (v == null)
- {
- throw new NotImplementedException("Contact to update is null");
- }
- // todo var contact
- foreach (var PhoneContact in contacts)
- {
- if (PhoneContact.Name == v)
- {
- PhoneContact.PhoneNumber = newNumber;
- PhoneContact.CreatedOn = DateTime.Now;
- return $"Phonenumber of {v} has been update to {newNumber}!";
- }
- }
- return $"The contact {v} has not been found!";
- }
- //todo string name
- internal static string SearchContacts(string v)
- {
- if (v == null)
- {
- throw new NotImplementedException();
- }
- StringBuilder totalInfo = new StringBuilder();
- //todo var contact
- foreach (var PhoneContact in contacts)
- {
- //for (int i = 0; i < v.Length; i++)
- //{
- //}
- v = v.ToLower();
- string currentname = PhoneContact.Name.ToLower();
- if (currentname.Contains(v))
- {
- totalInfo.AppendLine(PhoneContact.GetContactInfo());
- }
- }
- if (totalInfo.Length == 0)
- {
- return $"Name containing {v} was not found";
- }
- else
- {
- return totalInfo.ToString();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement