using System; namespace Contacts { /// /// Represents an address for a contact. Contains methods to determine if two addresses are equal. /// public class Address : IEquatable
{ private readonly string _abbr; private readonly string _city; private readonly string _street; private readonly string _zip; public AddressType Type { get; } /// /// Accepts input string in format "123 Example St,City,ST,12345". /// /// Full address /// If null, defaults to "Other" public Address(string address, AddressType? type) { string[] parsedAddress = address.Split(","); if (parsedAddress.Length != 4) throw new ArgumentException("Address must be in the format of \"123 Example St,City,ST,12345\""); _street = parsedAddress[0]; _city = parsedAddress[1]; _abbr = parsedAddress[2]; _zip = parsedAddress[3]; this.Type = type ?? AddressType.Other; } /// /// Accepts input strings for each part of an address to create a new Address object. /// /// /// /// /// /// If null, defaults to "Other" public Address(string street, string city, string abbr, string zip, AddressType? type) { this._street = street; this._city = city; this._abbr = abbr; this._zip = zip; this.Type = type ?? AddressType.Other; } /// /// Determines if two addresses are equal, to avoid potential duplication. /// /// Address to compare to. If null, returns false. /// Whether or not the two addresses are the same. public bool Equals(Address? otherAddr) { if (otherAddr == null) return false; return this.ToString().Equals(otherAddr.ToString()); } /// /// Formats address object to "123 Example St, City, ST 12345". /// /// /// public override string ToString() { return _street + ", " + _city + ", " + _abbr + " " + _zip; } } }