C#中GetNamePeoplesAndYears与GetNameOfPeoples方法关联问题
C# 方法关联实现方案
前置问题修复
现有代码本身存在多处语法、逻辑错误,需要先修正才能完成关联调用:
GetNamePeoplesAndYears方法内的namePeople、yearBirthday、birthdayElement变量均定义在foreach循环内部,作用域仅限循环体,循环外无法访问,且原逻辑默认只返回最后一条人员数据,不符合多人员遍历需求GetNameOfPeoples方法内未定义yearBirthday、bitrthdayElement、namePeople变量,也没有调用GetNamePeoplesAndYears的逻辑,同时年龄计算逻辑写反,正确的年龄计算应为当前年份减去出生年份ParseDate方法的日期格式符错误:小写mm代表分钟,月份需要用大写MM,否则解析带月份的日期会出错
关联调用实现
我们调整两个方法的职责:GetNamePeoplesAndYears 负责把二维数组格式的人员数据转成结构化的三元组集合,GetNameOfPeoples 负责判断单条人员数据是否符合「闰年出生+年龄≤20」的条件,示例代码如下:
using System; using System.Collections.Generic; using System.Globalization; using System.IO; namespace TimeTress { class Program { static void Main(string[] args) { var timelineCSV = GetListOfStringsFromTextFile("../../../../timeline.csv"); var peopleCSV = GetListOfStringsFromTextFile("../../../../people.csv"); var timeline = ReadData(timelineCSV); var people = ReadData(peopleCSV); // 关联调用示例:获取所有符合条件的人员姓名 var allPersonInfo = GetNamePeoplesAndYears(people); foreach (var person in allPersonInfo) { string validName = GetNameOfPeoples(person); if (!string.IsNullOrEmpty(validName)) { Console.WriteLine($"符合条件的人员:{validName}"); } } } // 调整参数为单条人员的三元组数据,判断是否符合条件 static string GetNameOfPeoples((string name, int birthYear, DateTime birthDate) person) { var nowDate = DateTime.Now; const int countYears = 20; // 计算年龄时考虑生日是否已过,避免年份减去年份的误差 int age = nowDate.Year - person.birthYear; if (nowDate < person.birthDate.AddYears(age)) { age--; } if (DateTime.IsLeapYear(person.birthYear) && age <= countYears) return person.name; return string.Empty; } // 修改为返回所有人员的结构化信息集合 static List<(string name, int birthYear, DateTime birthDate)> GetNamePeoplesAndYears(string[][] people) { var result = new List<(string, int, DateTime)>(); foreach (var elements in people) { DateTime birthdayElement = ParseDate(elements[2]); int yearBirthday = birthdayElement.Year; string namePeople = elements[1]; result.Add((namePeople, yearBirthday, birthdayElement)); } return result; } static string[] GetListOfStringsFromTextFile(string filePath) { return File.Exists(filePath) ? File.ReadAllLines(filePath) : Array.Empty<string>(); } static void OutputString(string[] getString) { foreach (var lines in getString) { Console.WriteLine(lines); } } static string[][] ReadData(string[] array) { string[][] splitData = new string[array.Length][]; for (var i = 0; i < array.Length; i++) { splitData[i] = array[i].Split(";"); } return splitData; } static DateTime ParseDate(string value) { DateTime date; // 修正格式符,月份用大写MM if (!DateTime.TryParseExact(value, "yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out date)) { if (!DateTime.TryParseExact(value, "yyyy-MM", CultureInfo.InvariantCulture, DateTimeStyles.None, out date)) { if (!DateTime.TryParseExact(value, "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out date)) { throw new Exception("WRONG FORMAT"); } } } return date; } } }
调用流程说明
- 从文件读取的原始二维数组
people传入GetNamePeoplesAndYears,转换为包含姓名、出生年份、出生日期的结构化集合 - 遍历结构化集合,将每条人员数据传入
GetNameOfPeoples做条件判断,符合要求的人员姓名会被返回,否则返回空字符串 - 最终可收集所有符合条件的人员姓名完成业务需求
内容的提问来源于stack exchange,提问作者Ilya Kvashnin
相关产品推荐
相关产品推荐

