/// <summary> /// Finds a commodity that has the sspecified name /// </summary> /// <param name="name">A commodity name to look for</param> /// <param name="comparisonOptions">Options used to compare the commodity names</param> /// <returns>The <see cref="Commodity"/> that was found, or null if the provided name does not match any commodity.</returns> /// <exception cref="ArgumentNullException">The provided name is null.</exception> public Commodity FindCommodityByName(string name, StringComparison comparisonOptions = StringComparison.InvariantCultureIgnoreCase) { if (name == null) { throw new ArgumentNullException("name", "A commodity name cannot be null"); } return(CommoditiesInternal.Where(c => c.Name.Equals(name, comparisonOptions)).FirstOrDefault()); }
/// <summary> /// Creates a new <see cref="Commodity"/> instance and adds it into this environment. /// If this commodity name already exists, the existing commodity is returned. /// </summary> /// <param name="name">The name of the new commodity</param> /// <param name="category">An opptionnal category name</param> /// <returns>The newly created <see cref="Commodity"/> instance.</returns> /// <exception cref="ArgumentNullException">The provided name is null.</exception> public Commodity CreateCommodity(string name, string category = null) { if (name == null) { throw new ArgumentNullException("name", "The commodity name cannot be null"); } Commodity existing = FindCommodityByName(name); if (existing != null) { return(existing); } Commodity result = new Commodity(name, category); CommoditiesInternal.Add(result); return(result); }