private static string PrintRecord(FileCabinetRecord record, string[] fields, string horizontalBorder) { StringBuilder builder = new StringBuilder(); builder.Append(wall); string[] recordsLength = horizontalBorder.Split("+", StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < fields.Length; i++) { var value = record.GetType().GetProperty(fields[i])?.GetValue(record, null); if (value is null) { continue; } builder.Append(" "); string stringValue = value.ToString(); if (value is DateTime time) { stringValue = time.ToString("yyyy-MMMM-dd", CultureInfo.InvariantCulture); } int horizontalBorderLength = recordsLength[i].Length - stringValue.Length - 2; if (value is string || value is char) { builder.Append(PrintStringValues(stringValue, horizontalBorderLength)); } else if (value is DateTime) { builder.Append(PrintDateTimeValue(stringValue, horizontalBorderLength)); } else { builder.Append(PrintDigitValues(value, horizontalBorderLength)); } builder.Append(" "); builder.Append(wall); } return(builder.ToString()); }
/// <summary> /// Prints single record. /// </summary> /// <param name="record">Record to print.</param> private void PrintRecord(FileCabinetRecord record) { if (record == null) { throw new ArgumentNullException($"Record is null."); } string output = "#"; PropertyInfo[] properties = record.GetType().GetProperties(); for (int i = 0; i < properties.Length; i++) { if (properties[i].PropertyType == typeof(DateTime)) { DateTime date = (DateTime)properties[i].GetValue(record); output += "Date of birth" + ": "; output += date.ToString("yyyy-MMM-d", CultureInfo.InvariantCulture); } else { if (properties[i].Name != "Id") { output += properties[i].Name + ": "; } output += properties[i].GetValue(record); } if (i != properties.Length - 1) { output += ", "; } } Console.WriteLine(output); }