// Format: how many Credits is [Quantity] [Item] public bool Solve(string line, Context context, out string answer) { if (!line.StartsWith(Constants.StatementQuestion)) { answer = null; return(false); } string question = line.Substring(Constants.StatementQuestion.Length); string[] right = question.Split(new[] { Constants.Space }, StringSplitOptions.RemoveEmptyEntries); string item = right.Last(); int quantity = Roman.Parse(string.Join(Constants.Space, right.Take(right.Length - 1)), context.RomanMapping); if (!context.ItemPrice.ContainsKey(item)) { throw new ArgumentException(Constants.ItemNotFound, item); } decimal amount = quantity * context.ItemPrice[item]; answer = string.Format("{0}is {1} Credits", question, decimal.ToInt32(amount)); return(true); }
// Format: [Quantity] [Item] is [Amount] Credits public bool Parse(string line, Context context) { if (line.EndsWith(Constants.QuestionMark)) { return(false); } string[] fields = line.Split(new[] { Constants.ParserDelimiter }, StringSplitOptions.RemoveEmptyEntries); if (fields.Length != 2) { return(false); } string[] left = fields[0].Split(new[] { Constants.Space }, StringSplitOptions.RemoveEmptyEntries); if (left.Length < 2) { return(false); } string[] right = fields[1].Split(new[] { Constants.Space }, StringSplitOptions.RemoveEmptyEntries); if (right.Length != 2) { return(false); } string item = left.Last(); int quantity = Roman.Parse(string.Join(Constants.Space, left.Take(left.Length - 1)), context.RomanMapping); int amount = int.Parse(right[0]); decimal price = ((decimal)amount) / quantity; context.ItemPrice[item] = price; return(true); }
// Format: how much is [Quantity] public bool Solve(string line, Context context, out string answer) { if (!line.StartsWith(Constants.DefinitionQuestion)) { answer = null; return(false); } string right = line.Substring(Constants.DefinitionQuestion.Length); int quantity = Roman.Parse(right, context.RomanMapping); answer = string.Format("{0}is {1}", right, quantity); return(true); }
public void Parse(int expected, string letter) { Assert.AreEqual(expected, Roman.Parse(letter)); }