示例#1
0
        public int CheckPassports(string data, IPassportValidator validator)
        {
            var passports = SplitPassports(data);

            return(passports.Select(x => PassportData.ParsePassport(x).IsValid(validator))
                   .Count(x => x == true));
        }
示例#2
0
        private static List <PassportData> ReadInputFile(string v)
        {
            List <PassportData> result          = new List <PassportData>();
            StringBuilder       dataAccumulator = new StringBuilder();

            foreach (string line in File.ReadLines(v))
            {
                if (string.IsNullOrEmpty(line))
                {
                    result.Add(PassportData.Parse(dataAccumulator));
                    dataAccumulator.Clear();
                }
                else
                {
                    dataAccumulator.Append($" {line}");
                }
            }

            if (dataAccumulator.Length > 0)
            {
                result.Add(PassportData.Parse(dataAccumulator));
            }

            return(result);
        }
示例#3
0
 public bool Validate(PassportData data)
 {
     return(!(
                string.IsNullOrWhiteSpace(data.BirthYear) ||
                string.IsNullOrWhiteSpace(data.IssueYear) ||
                string.IsNullOrWhiteSpace(data.ExpirationYear) ||
                string.IsNullOrWhiteSpace(data.Height) ||
                string.IsNullOrWhiteSpace(data.HairColor) ||
                string.IsNullOrWhiteSpace(data.EyeColor) ||
                string.IsNullOrWhiteSpace(data.PassportId)
                ));
 }
示例#4
0
        internal static PassportData Parse(StringBuilder dataAccumulator)
        {
            MatchCollection matches = regex.Matches(dataAccumulator.ToString());

            if (!matches.Any())
            {
                throw new ArgumentException("bad string data");
            }
            PassportData passportData = new PassportData();

            foreach (Match match in matches)
            {
                passportData.Fields.Add(match.Groups[1].Value, match.Groups[2].Value);
            }

            return(passportData);
        }
示例#5
0
            public static PassportData ParsePassport(string data)
            {
                var fields = data.Split(PassportDataSeparator, StringSplitOptions.RemoveEmptyEntries);

                var passport = new PassportData();

                foreach (var field in fields)
                {
                    var keyValue = field.Split(PassportDataValueSeparator);
                    if (keyValue.Length != 2)
                    {
                        continue;
                    }

                    if (!passport.KeyValueFunctions.TryGetValue(keyValue[0].Trim(), out var action))
                    {
                        continue;
                    }

                    action?.Invoke(keyValue[1].Trim());
                }

                return(passport);
            }