public void Execute(string input) { //Remove question mark input = input.Substring(0, input.Length - 1); string[] parts = input.Split(new string[] { " is " }, StringSplitOptions.RemoveEmptyEntries); string[] preIsWords = parts[0].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); string[] postIsWords = parts[1].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); string sourceCommodity = postIsWords.Skip(postIsWords.Length - 1).ToString(); string destinationCommodity = preIsWords[2]; string[] aliases = postIsWords.Take(postIsWords.Length - 1).ToArray(); StringBuilder sb = new StringBuilder(); //Create Roman Numeral from aliases for (int i = 0; i < aliases.Length - 1; i++) { sb.Append(aliasMap.GetValueForAlias(aliases[i])); } double sourceCommodityPrice = commodityIndex.GetPriceByCommodity(sourceCommodity); double destinationCommodityPrice = commodityIndex.GetPriceByCommodity(destinationCommodity); //Convert Roman to Decimal double?totalUnits = converter.ToDecimal(sb.ToString()); if (totalUnits.HasValue) { double totalSourceCommodity = sourceCommodityPrice * totalUnits.Value; Console.WriteLine(String.Format("{0} is {1} {2}", parts[1], (totalSourceCommodity / destinationCommodityPrice), destinationCommodity)); } }
public void UnitExpressionTest() { AliasMapper aliasMap = new AliasMapper(); RomanConverter converter = new RomanConverter(); CommodityIndex commodityIndex = new CommodityIndex(); aliasMap.AddAlias("glob", "C"); aliasMap.AddAlias("pish", "X"); ExpressionValidationHelper helper = new ExpressionValidationHelper(aliasMap, commodityIndex); UnitExpression expression = new UnitExpression(commodityIndex, aliasMap, converter, helper); expression.Execute("pish glob Iron is 7020 Credits"); Assert.IsTrue(commodityIndex.Exists("Iron")); Assert.AreEqual <double>(commodityIndex.GetPriceByCommodity("Iron"), 78); expression.Execute("pish glob Iron is 6300 Credits"); Assert.AreEqual <double>(commodityIndex.GetPriceByCommodity("Iron"), 70); }
public void UnitExpressionTest() { AliasMapper aliasMap = new AliasMapper(); RomanConverter converter = new RomanConverter(); CommodityIndex commodityIndex = new CommodityIndex(); aliasMap.AddAlias("glob", "I"); aliasMap.AddAlias("prok", "V"); aliasMap.AddAlias("pish", "X"); aliasMap.AddAlias("tegj", "L"); ExpressionValidationHelper helper = new ExpressionValidationHelper(aliasMap, commodityIndex); UnitExpression expression = new UnitExpression(commodityIndex, aliasMap, converter, helper); expression.Execute("glob glob silver is 34 Credits"); Assert.True(commodityIndex.Exists("silver")); Assert.Equal <double>(17, commodityIndex.GetPriceByCommodity("silver")); expression.Execute("glob prok iron is 400 Credits"); Assert.Equal <double>(100, commodityIndex.GetPriceByCommodity("iron")); }
public void Execute(string input) { //Remove question mark input = input.Substring(0, input.Length - 1); string[] parts = input.Split(new string[] { " is " }, StringSplitOptions.RemoveEmptyEntries); string[] words = parts[1].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); string commodity = words[words.Length - 1]; StringBuilder sb = new StringBuilder(); //Create Roman Numeral from aliases for (int i = 0; i < words.Length - 1; i++) { sb.Append(aliasMap.GetValueForAlias(words[i])); } //Convert Roman to Decimal double?totalUnits = converter.ToDecimal(sb.ToString()); if (totalUnits.HasValue) { Console.WriteLine(String.Format("{0} is {1}", parts[1], totalUnits.Value * commodityIndex.GetPriceByCommodity(commodity))); } }