public FormatterResult <int> GetInt(string input) { FormatterResult <int> res = new FormatterResult <int>(); FormatterError formatterError = new FormatterError(); if (input == null) { res.Errors.Add(formatterError.Create("String is null")); return(res); } if (input.Length == 0) { res.Errors.Add(formatterError.Create("String is empty")); return(res); } int val = 0; if (!int.TryParse(input, out val)) { res.Errors.Add(formatterError.Create("Cant transform the string to int number")); return(res); } if (val <= 0) { res.Errors.Add(formatterError.Create("Can be only positive number")); return(res); } res.Value = val; return(res); }
public FormatterResult <string> GetString(string input) { FormatterResult <string> res = new FormatterResult <string>(); FormatterError formatterError = new FormatterError(); if (input == null) { res.Errors.Add(formatterError.Create("String is null")); return(res); } if (input.Length == 0) { res.Errors.Add(formatterError.Create("String is empty")); return(res); } Regex regex = new Regex("[a-zA-Z]"); if (!regex.IsMatch(input)) { res.Errors.Add(formatterError.Create("String hasn't any word")); return(res); } res.Value = input; return(res); }
public FormatterResult <DateTime> GetDateTime(string input) { FormatterResult <DateTime> res = new FormatterResult <DateTime>(); FormatterError formatterError = new FormatterError(); if (input == null) { res.Errors.Add(formatterError.Create("String is null")); return(res); } if (input.Length == 0) { res.Errors.Add(formatterError.Create("String is empty")); return(res); } DateTime val; if (!DateTime.TryParse(input, out val)) { res.Errors.Add(formatterError.Create("Cant transform the string to datetime format")); return(res); } res.Value = val; return(res); }
private static DateTime GetDateTime() { FormatterResult <DateTime> res = null; StringFormatter stringFormatter = new StringFormatter(); do { string input = Console.ReadLine(); res = stringFormatter.GetDateTime(input); if (res.IsValid) { Console.WriteLine("All right"); } else { Console.WriteLine("Errors:"); string s = string.Join("\n", res.Errors.Select(str => str.ErrorMessage)); Console.WriteLine(s); } }while (!res.IsValid); return(res.Value); }