using System; using System.Collections; using System.Collections.Generic; using ShareeBike.Model.Stations.StationNS; namespace ShareeBike.Model.Stations { public class StationDictionary : IEnumerable { /// Holds the list of stations. private readonly IDictionary m_oStationDictionary; /// /// Gets a station by key. /// /// Key to get station. /// Station for given key. public IStation this[string key] => ContainsKey(key) ? m_oStationDictionary[key] : new NullStation(); /// Count of stations. public int Count { get { return m_oStationDictionary.Count; } } public Version CopriVersion { get; } /// Constructs a station dictionary object. /// Version of copri- service. public StationDictionary(Version version = null, IDictionary stations = null) { m_oStationDictionary = stations ?? new Dictionary(); CopriVersion = version != null ? new Version(version.Major, version.Minor, version.Revision, version.Build) : new Version(0, 0, 0, 0); } public IEnumerator GetEnumerator() { return m_oStationDictionary.Values.GetEnumerator(); } /// /// Determines whether a station by given key exists. /// /// Key to check. /// True if station exists. public bool ContainsKey(string key) { return m_oStationDictionary.ContainsKey(key); } /// /// Remove a station by station id. /// /// public void RemoveById(string id) { if (!m_oStationDictionary.ContainsKey(id)) { // Nothing to do if there is no station with given name. return; } m_oStationDictionary.Remove(id); } /// /// Remove a station by station name. /// /// public IStation GetById(string id) { if (!m_oStationDictionary.ContainsKey(id)) { // Nothing to do if there is no station with given name. return null; } return m_oStationDictionary[id]; } /// /// Adds a station to dictionary of stations. /// /// public void Add(Station p_oStation) { if (p_oStation == null) { throw new ArgumentException("Can not add empty station to collection of stations."); } if (m_oStationDictionary.ContainsKey(p_oStation.Id)) { throw new ArgumentException(string.Format("Can not add station {0} to collection of stations. A station with given name already exists.", p_oStation.Id)); } m_oStationDictionary.Add(p_oStation.Id, p_oStation); } IEnumerator IEnumerable.GetEnumerator() { return m_oStationDictionary.Values.GetEnumerator(); } } }