using System; /// /// Represents a phone number for a contact. Contains methods to determine if two phone numbers are the same. /// namespace Contacts { public class Phone : IEquatable { public string number { get; } public PhoneType? Type { get; } /// /// Accepts an input string in the format "1234567890" to create a new Phone object for a Contacts. /// /// /// If null, defaults to "Other" /// Thrown if input has an invalid format. public Phone(string number, PhoneType? type) { if (number.Length != 10) throw new ArgumentException("Input phone number should be in the format 1234567890."); this.number = number; this.Type = type; } /// /// Determines if two phone numbers are equal, to avoid potential duplication. /// /// Phone number to compare to. If null, returns false. /// Whether or not the two phone numbers are the same. public bool Equals(Phone? numberToCompare) { if (numberToCompare == null) return false; return this.ToString().Equals(numberToCompare.ToString()); } /// /// Returns the phone number without any formatting, for use in saving contacts to a file. /// /// Unformatted phone number public string DisplayString() { return number; } /// /// Formats the phone number to "(123) 456 - 7890" /// /// Formatted phone number public override string ToString() { return "(" + number.Substring(0, 3) + ") " + number.Substring(3, 3) + " - " + number.Substring(6); } } }