private bool IsValid(Passport pass) { bool valid = true; if (Convert.ToInt32(pass.BirthYear) > 2002 || Convert.ToInt32(pass.BirthYear) < 1920) { valid = false; return(valid); } if (Convert.ToInt32(pass.IssueYear) > 2020 || Convert.ToInt32(pass.IssueYear) < 2010) { valid = false; return(valid); } if (Convert.ToInt32(pass.Expiration) < 2020 || Convert.ToInt32(pass.Expiration) > 2030) { valid = false; return(valid); } if (pass.Height.EndsWith("cm")) { var h = Convert.ToInt32(pass.Height.Split(Convert.ToChar("c"))[0]); if (h <= 150 && h >= 193) { valid = false; return(valid); } } else if (pass.Height.EndsWith("in")) { var h = Convert.ToInt32(pass.Height.Split(Convert.ToChar("i"))[0]); if (h <= 59 || h >= 76) { valid = false; return(valid); } } if (!Regex.Match(pass.HairColor, @"#[0-9a-f]{6}").Value.Equals(pass.HairColor)) { valid = false; return(false); } if (!Enum.TryParse <Color>(pass.EyeColor, out _)) { valid = false; return(false); } if (!Regex.IsMatch(pass.PassportID, @"\d{9}")) { valid = false; return(valid); } return(valid); }
private List <Passport> CreatePassportList(string path) { List <string> strings = new List <string>(); List <Passport> passports = new List <Passport>(); char[] delimts = { '-', ' ' }; var line = string.Empty; string str = string.Empty; StreamReader file = new StreamReader(path); while ((line = file.ReadLine()) != null) { if (line != string.Empty) { if (str != string.Empty) { str += " " + line; } else { str += line; } } else { strings.Add(str); str = string.Empty; } } strings.Add(str); foreach (string l in strings) { char[] d = { ':' }; Passport pass = new Passport(); string[] parts = l.Split(delimts); foreach (var t in parts) { if (t.Contains("byr")) { string[] pieces = t.Split(d); pass.BirthYear = pieces[1]; } if (t.Contains("iyr")) { string[] pieces = t.Split(d); pass.IssueYear = pieces[1]; } if (t.Contains("eyr")) { string[] pieces = t.Split(d); pass.Expiration = pieces[1]; } if (t.Contains("hgt")) { string[] pieces = t.Split(d); pass.Height = pieces[1]; } if (t.Contains("hcl")) { string[] pieces = t.Split(d); pass.HairColor = pieces[1]; } if (t.Contains("ecl")) { string[] pieces = t.Split(d); pass.EyeColor = pieces[1]; } if (t.Contains("pid")) { string[] pieces = t.Split(d); pass.PassportID = pieces[1]; } if (t.Contains("cid")) { string[] pieces = t.Split(d); pass.CountryID = pieces[1]; } } passports.Add(pass); } return(passports); }