Created
November 20, 2018 23:52
-
-
Save darbio/af9bd0948d733489820469263ab01d75 to your computer and use it in GitHub Desktop.
Address class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace Darb.io.Core | |
{ | |
// Based upon Microsoft Address Class | |
// https://msdn.microsoft.com/en-us/library/cc966788.aspx | |
public class Address | |
{ | |
/// A string specifying the street line of an address. The AddressLine property is the most precise, | |
/// official line for an address relative to the postal agency servicing the area specified by the | |
/// Locality, PostalTown, or PostalCode properties. Typical use of this element would be to enclose | |
/// a street address, private bag, or any other similar official address. | |
public string AddressLine { get; } | |
/// A string specifying the populated place for the address. This commonly refers to a city, but may | |
/// refer to a suburb or neighborhood in certain countries. | |
public string Locality { get; } | |
/// A string specifying the postal city of an address. | |
public string PostalTown { get; } | |
/// A string specifying the subdivision name within the country or region for an address. This element | |
/// is also commonly treated as the first order administrative subdivision, but in some cases it is the | |
/// second, third, or fourth order subdivision within a country, dependency, or region. | |
public string AdminDistrict { get; } | |
/// A string specifying the higher level administrative subdivision used in some countries or regions. | |
public string District { get; } | |
/// A string specifying the post code, postal code, or ZIP Code of an address. | |
public string PostalCode { get; } | |
/// A string specifying the country | |
public string Country { get; } | |
public Address(string addressLine = null, string locality = null, | |
string postalTown = null, string adminDistrict = null, | |
string district = null, string postalCode = null, string country = null) | |
{ | |
if (string.IsNullOrEmpty(country)) | |
{ | |
throw new ArgumentException("The parameter must be a not null, not empty string", nameof(country)); | |
} | |
AddressLine = addressLine; | |
Locality = locality; | |
PostalTown = postalTown; | |
AdminDistrict = adminDistrict; | |
District = district; | |
PostalCode = postalCode; | |
Country = country; | |
} | |
public bool IsValid() | |
{ | |
// AddressLine must always be set | |
var addressLineValid = !string.IsNullOrEmpty(this.AddressLine); | |
// For an Address object to be valid, it must contain at least one context element | |
// such as PostalTown, PostalCode, AdminDistrict, District, or Locality. | |
var values = new List<string>() { | |
this.Locality, this.PostalTown, this.AdminDistrict, this.District, this.PostalCode | |
}; | |
var contextValid = values.Count(a => !string.IsNullOrEmpty(a)) > 0; | |
return addressLineValid && contextValid; | |
} | |
} | |
// Austria, Belgium, Denmark, Finland, France, Germany, Greece, Luxembourg, Norway, Sweden, Switzerland, Taiwan, and The Netherlands | |
public abstract class AddressType1 : Address | |
{ | |
protected AddressType1(string streetAddress, string city, string postCode, string country) | |
: base(addressLine: streetAddress, locality: city, postalCode: postCode, country: country) | |
{ | |
if (string.IsNullOrEmpty(streetAddress)) | |
{ | |
throw new ArgumentException("The parameter must be a not null, not empty string", nameof(streetAddress)); | |
} | |
if (string.IsNullOrEmpty(city)) | |
{ | |
throw new ArgumentException("The parameter must be a not null, not empty string", nameof(city)); | |
} | |
if (string.IsNullOrEmpty(postCode)) | |
{ | |
throw new ArgumentException("The parameter must be a not null, not empty string", nameof(postCode)); | |
} | |
} | |
} | |
// Australia and Brazil | |
public abstract class AddressType2 : Address | |
{ | |
protected AddressType2(string streetAddress, string city, string state, string postalCode, string country) | |
: base(addressLine: streetAddress, locality: city, district: state, postalCode: postalCode, country: country) | |
{ | |
if (string.IsNullOrEmpty(streetAddress)) | |
{ | |
throw new ArgumentException("The parameter must be a not null, not empty string", nameof(streetAddress)); | |
} | |
if (string.IsNullOrEmpty(city)) | |
{ | |
throw new ArgumentException("The parameter must be a not null, not empty string", nameof(city)); | |
} | |
if (string.IsNullOrEmpty(state)) | |
{ | |
throw new ArgumentException("The parameter must be a not null, not empty string", nameof(state)); | |
} | |
if (string.IsNullOrEmpty(postalCode)) | |
{ | |
throw new ArgumentException("The parameter must be a not null, not empty string", nameof(postalCode)); | |
} | |
} | |
} | |
// Canada | |
public abstract class AddressType3 : Address | |
{ | |
public AddressType3(string streetAddress, string city, string province, string postalCode, string country) | |
: base(addressLine: streetAddress, locality: city, adminDistrict: province, postalCode: postalCode, country: country) | |
{ | |
if (string.IsNullOrEmpty(streetAddress)) | |
{ | |
throw new ArgumentException("The parameter must be a not null, not empty string", nameof(streetAddress)); | |
} | |
if (string.IsNullOrEmpty(city)) | |
{ | |
throw new ArgumentException("The parameter must be a not null, not empty string", nameof(city)); | |
} | |
if (string.IsNullOrEmpty(province)) | |
{ | |
throw new ArgumentException("The parameter must be a not null, not empty string", nameof(province)); | |
} | |
if (string.IsNullOrEmpty(postalCode)) | |
{ | |
throw new ArgumentException("The parameter must be a not null, not empty string", nameof(postalCode)); | |
} | |
} | |
} | |
// Italy, Portugal, and Spain | |
public abstract class AddressType4 : Address | |
{ | |
protected AddressType4(string streetAddress, string city, string province, string postCode, string country) | |
: base(addressLine: streetAddress, locality: city, adminDistrict: province, country: country) | |
{ | |
if (string.IsNullOrEmpty(streetAddress)) | |
{ | |
throw new ArgumentException("The parameter must be a not null, not empty string", nameof(streetAddress)); | |
} | |
if (string.IsNullOrEmpty(city)) | |
{ | |
throw new ArgumentException("The parameter must be a not null, not empty string", nameof(city)); | |
} | |
if (string.IsNullOrEmpty(province)) | |
{ | |
throw new ArgumentException("The parameter must be a not null, not empty string", nameof(province)); | |
} | |
if (string.IsNullOrEmpty(postCode)) | |
{ | |
throw new ArgumentException("The parameter must be a not null, not empty string", nameof(postCode)); | |
} | |
} | |
} | |
// Hong Kong | |
public abstract class AddressType5 : Address | |
{ | |
protected AddressType5(string streetAddress, string city, string territory, string country) | |
: base(addressLine: streetAddress, locality: city, adminDistrict: territory, country: country) | |
{ | |
if (string.IsNullOrEmpty(streetAddress)) | |
{ | |
throw new ArgumentException("The parameter must be a not null, not empty string", nameof(streetAddress)); | |
} | |
if (string.IsNullOrEmpty(city)) | |
{ | |
throw new ArgumentException("The parameter must be a not null, not empty string", nameof(city)); | |
} | |
if (string.IsNullOrEmpty(territory)) | |
{ | |
throw new ArgumentException("The parameter must be a not null, not empty string", nameof(territory)); | |
} | |
} | |
} | |
// Puerto Rico | |
public abstract class AddressType6 : Address | |
{ | |
protected AddressType6(string streetAddress, string city, string postCode, string country) | |
: base(addressLine: streetAddress, locality: city, postalCode: postCode, country: country) | |
{ | |
if (string.IsNullOrEmpty(streetAddress)) | |
{ | |
throw new ArgumentException("The parameter must be a not null, not empty string", nameof(streetAddress)); | |
} | |
if (string.IsNullOrEmpty(city)) | |
{ | |
throw new ArgumentException("The parameter must be a not null, not empty string", nameof(city)); | |
} | |
if (string.IsNullOrEmpty(postCode)) | |
{ | |
throw new ArgumentException("The parameter must be a not null, not empty string", nameof(postCode)); | |
} | |
} | |
} | |
// Singapore and New Zealand | |
public abstract class AddressType7 : Address | |
{ | |
protected AddressType7(string streetAddress, string city, string postCode, string country) | |
: base(addressLine: streetAddress, locality: city, postalCode: postCode, country: country) | |
{ | |
if (string.IsNullOrEmpty(streetAddress)) | |
{ | |
throw new ArgumentException("The parameter must be a not null, not empty string", nameof(streetAddress)); | |
} | |
if (string.IsNullOrEmpty(city)) | |
{ | |
throw new ArgumentException("The parameter must be a not null, not empty string", nameof(city)); | |
} | |
if (string.IsNullOrEmpty(postCode)) | |
{ | |
throw new ArgumentException("The parameter must be a not null, not empty string", nameof(postCode)); | |
} | |
} | |
} | |
// United Kingdom | |
public abstract class AddressType8 : Address | |
{ | |
protected AddressType8(string addressLine1, string addressLine2, string postalCity, string district, string postCode, string country) | |
: base(addressLine: addressLine1, locality: addressLine2, postalTown: postalCity, district: district, postalCode: postCode, country: country) | |
{ | |
if (string.IsNullOrEmpty(addressLine1)) | |
{ | |
throw new ArgumentException("The parameter must be a not null, not empty string", nameof(addressLine1)); | |
} | |
if (string.IsNullOrEmpty(postalCity)) | |
{ | |
throw new ArgumentException("The parameter must be a not null, not empty string", nameof(postalCity)); | |
} | |
if (string.IsNullOrEmpty(district)) | |
{ | |
throw new ArgumentException("The parameter must be a not null, not empty string", nameof(district)); | |
} | |
if (string.IsNullOrEmpty(postCode)) | |
{ | |
throw new ArgumentException("The parameter must be a not null, not empty string", nameof(postCode)); | |
} | |
} | |
} | |
// United States | |
public abstract class AddressType9 : Address | |
{ | |
protected AddressType9(string streetAddress, string city, string city2, string state, string zipCode, string country) | |
: base(addressLine: streetAddress, locality: city, postalTown: city2, adminDistrict: state, postalCode: zipCode, country: country) | |
{ | |
if (string.IsNullOrEmpty(streetAddress)) | |
{ | |
throw new ArgumentException("The parameter must be a not null, not empty string", nameof(streetAddress)); | |
} | |
if (string.IsNullOrEmpty(city)) | |
{ | |
throw new ArgumentException("The parameter must be a not null, not empty string", nameof(city)); | |
} | |
if (string.IsNullOrEmpty(state)) | |
{ | |
throw new ArgumentException("The parameter must be a not null, not empty string", nameof(state)); | |
} | |
if (string.IsNullOrEmpty(zipCode)) | |
{ | |
throw new ArgumentException("The parameter must be a not null, not empty string", nameof(zipCode)); | |
} | |
} | |
} | |
public class AustraliaAddress : AddressType2 | |
{ | |
List<string> _validStates = new List<string>() { | |
"QLD", "NSW", "VIC", "TAS", "SA", "WA", "NT", "ACT" | |
}; | |
AustraliaAddress(string streetAddress, string city, string state, int postalCode) | |
: base(streetAddress, city, state.ToString(), $"{postalCode}", "Australia") | |
{ | |
if (!_validStates.Contains(state)) | |
{ | |
throw new ArgumentException("State must be valid, e.g. 'NSW'", nameof(state)); | |
} | |
} | |
} | |
public class AustriaAddress : AddressType1 | |
{ | |
public AustriaAddress(string streetAddress, string city, string postCode) | |
: base(streetAddress, city, postCode, "Austria") { } | |
} | |
public class BelgiumAddress : AddressType1 | |
{ | |
public BelgiumAddress(string streetAddress, string city, string postCode) | |
: base(streetAddress, city, postCode, "Belgium") { } | |
} | |
public class BrazilAddress : AddressType2 | |
{ | |
public BrazilAddress(string streetAddress, string city, string state, string postalCode) | |
: base(streetAddress, city, state, postalCode, "Brazil") { } | |
} | |
public class CanadaAddress : AddressType3 | |
{ | |
public CanadaAddress(string streetAddress, string city, string province, string postalCode) | |
: base(streetAddress, city, province, postalCode, "Canada") { } | |
} | |
public class DenmarkAddress : AddressType1 | |
{ | |
public DenmarkAddress(string streetAddress, string city, string postCode) | |
: base(streetAddress, city, postCode, "Denmark") { } | |
} | |
public class HongKongAddress : AddressType5 | |
{ | |
public HongKongAddress(string streetAddress, string city, string territory) : | |
base(streetAddress, city, territory, "Hong Kong") | |
{ } | |
} | |
public class FinlandAddress : AddressType1 | |
{ | |
public FinlandAddress(string streetAddress, string city, string postCode) | |
: base(streetAddress, city, postCode, "Finland") { } | |
} | |
public class FranceAddress : AddressType1 | |
{ | |
public FranceAddress(string streetAddress, string city, string postCode) | |
: base(streetAddress, city, postCode, "France") { } | |
} | |
public class GermanyAddress : AddressType1 | |
{ | |
public GermanyAddress(string streetAddress, string city, string postCode) | |
: base(streetAddress, city, postCode, "Germany") { } | |
} | |
public class GreeceAddress : AddressType1 | |
{ | |
public GreeceAddress(string streetAddress, string city, string postCode) | |
: base(streetAddress, city, postCode, "Greece") { } | |
} | |
public class ItalyAddress : AddressType4 | |
{ | |
public ItalyAddress(string streetAddress, string city, string province, string postCode) | |
: base(streetAddress, city, province, postCode, "Italy") { } | |
} | |
public class LuxembourgAddress : AddressType1 | |
{ | |
public LuxembourgAddress(string streetAddress, string city, string postCode) | |
: base(streetAddress, city, postCode, "Luxembourg") { } | |
} | |
public class NederlandsAddress : AddressType1 | |
{ | |
public NederlandsAddress(string streetAddress, string city, string postCode) | |
: base(streetAddress, city, postCode, "Nederlands") { } | |
} | |
public class NewZealandAddress : AddressType7 | |
{ | |
public NewZealandAddress(string streetAddress, string city, string postCode) | |
: base(streetAddress, city, postCode, "New Zealand") { } | |
} | |
public class NorwayAddress : AddressType1 | |
{ | |
public NorwayAddress(string streetAddress, string city, string postCode) | |
: base(streetAddress, city, postCode, "Norway") { } | |
} | |
public class PortugalAddress : AddressType4 | |
{ | |
public PortugalAddress(string streetAddress, string city, string province, string postCode) | |
: base(streetAddress, city, province, postCode, "Portugal") { } | |
} | |
public class PuertoRicoAddress : AddressType6 | |
{ | |
public PuertoRicoAddress(string streetAddress, string city, string postCode) | |
: base(streetAddress, city, postCode, "Puerto Rico") { } | |
} | |
public class SingaporeAddress : AddressType7 | |
{ | |
public SingaporeAddress(string streetAddress, string city, string postCode) | |
: base(streetAddress, city, postCode, "Singapore") { } | |
} | |
public class SpainAddress : AddressType4 | |
{ | |
public SpainAddress(string streetAddress, string city, string province, string postCode) | |
: base(streetAddress, city, province, postCode, "Spain") { } | |
} | |
public class SwedenAddress : AddressType1 | |
{ | |
public SwedenAddress(string streetAddress, string city, string postCode) | |
: base(streetAddress, city, postCode, "Sweden") { } | |
} | |
public class TaiwanAddress : AddressType1 | |
{ | |
public TaiwanAddress(string streetAddress, string city, string postCode) | |
: base(streetAddress, city, postCode, "Taiwan") { } | |
} | |
public class UnitedKingdomAddress : AddressType8 | |
{ | |
public UnitedKingdomAddress(string addressLine1, string addressLine2, string postalCity, string district, string postCode, string country) | |
: base(addressLine1, addressLine2, postalCity, district, postCode, "United Kingdom") { } | |
} | |
public class UnitedStatesAddress : AddressType9 | |
{ | |
public UnitedStatesAddress(string streetAddress, string city, string city2, string state, int zipCode, string country) | |
: base(streetAddress, city, city2, state, $"{zipCode}", "United States") { } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment