diff --git a/.vscode/launch.json b/.vscode/launch.json index bee5bd9..3558e0d 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -198,7 +198,7 @@ "type": "coreclr", "request": "launch", "preLaunchTask": "build", - "program": "${workspaceFolder}/Person/bin/Debug/net7.0/win-x64/Person.dll", + "program": "${workspaceFolder}/Person/bin/Debug/net6.0/Person.dll", "args": [ "s" ], diff --git a/Person/Person.cs b/Person/Person.cs index 5fc041b..fe6ee3d 100644 --- a/Person/Person.cs +++ b/Person/Person.cs @@ -79,11 +79,13 @@ public class Person ConsoleKey sex; long personKey; string? lastName; + string[] segments; string middleName; string? firstName; DateTime? dateTime; PersonName personName; string checkDirectory; + DateTime parseDateTime; ConsoleKey? consoleKey; string? approximateYears; string personKeyFormatted; @@ -96,6 +98,8 @@ public class Person log.Information(". . ."); if (consoleKey is not ConsoleKey.Y and not ConsoleKey.N) break; + else if (consoleKey is ConsoleKey.N) + break; firstName = null; for (int f = 0; f < 5; f++) { @@ -112,20 +116,32 @@ public class Person lastName = null; for (int f = 0; f < 5; f++) { - log.Information("Enter persons last name (minimum length of two characters)"); - line = System.Console.ReadLine(); - log.Information(". . ."); - if (string.IsNullOrEmpty(line) || line.Length < 2) - continue; - lastName = line.Trim(); + segments = firstName.Split(' '); + if (segments.Length > 1) + lastName = segments[^1]; + else + { + log.Information("Enter persons last name (minimum length of two characters)"); + line = System.Console.ReadLine(); + log.Information(". . ."); + if (string.IsNullOrEmpty(line) || line.Length < 2) + continue; + lastName = line.Trim(); + } break; } if (lastName is null) continue; - log.Information("Enter persons middle name (press enter if they don't have a middle name)"); - line = System.Console.ReadLine(); - log.Information(". . ."); - middleName = string.IsNullOrEmpty(line) ? string.Empty : line; + segments = firstName.Split(' '); + if (segments.Length > 2) + middleName = segments[1]; + else + { + log.Information("Enter persons middle name (press enter if they don't have a middle name)"); + line = System.Console.ReadLine(); + log.Information(". . ."); + middleName = string.IsNullOrEmpty(line) ? string.Empty : line; + } log.Information("Enter persons alias (press enter if they don't have a alias)"); line = System.Console.ReadLine(); log.Information(". . ."); @@ -143,8 +159,6 @@ public class Person log.Information(". . ."); if (consoleKey is not ConsoleKey.Y and not ConsoleKey.N) continue; - else if (consoleKey is ConsoleKey.N) - break; deceased = consoleKey == ConsoleKey.Y; dateTime = null; approximateYears = null; @@ -169,27 +183,18 @@ public class Person else { month = line.Trim(); - log.Information("Enter persons birthday day (press enter if not known)"); - line = System.Console.ReadLine(); - log.Information(". . ."); - if (string.IsNullOrEmpty(line)) + if (DateTime.TryParse(month, out parseDateTime) && parseDateTime != DateTime.MinValue) { - log.Information("Enter persons approximate age"); - line = System.Console.ReadLine(); - log.Information(". . ."); - if (string.IsNullOrEmpty(line) || !int.TryParse(line, out _)) - continue; - approximateYears = line.Trim(); + month = parseDateTime.Month.ToString(); + day = parseDateTime.Day.ToString(); + year = parseDateTime.Year.ToString(); } else { - day = line.Trim(); - log.Information("Enter persons birthday year (press enter if not known)"); + log.Information("Enter persons birthday day (press enter if not known)"); line = System.Console.ReadLine(); log.Information(". . ."); - if (!string.IsNullOrEmpty(line)) - year = line.Trim(); - else + if (string.IsNullOrEmpty(line)) { log.Information("Enter persons approximate age"); line = System.Console.ReadLine(); @@ -198,6 +203,24 @@ public class Person continue; approximateYears = line.Trim(); } + else + { + day = line.Trim(); + log.Information("Enter persons birthday year (press enter if not known)"); + line = System.Console.ReadLine(); + log.Information(". . ."); + if (!string.IsNullOrEmpty(line)) + year = line.Trim(); + else + { + log.Information("Enter persons approximate age"); + line = System.Console.ReadLine(); + log.Information(". . ."); + if (string.IsNullOrEmpty(line) || !int.TryParse(line, out _)) + continue; + approximateYears = line.Trim(); + } + } } } if (month is null || day is null || year is null)