Extract Fixed-Width Records
Description:
Consider the following class:
public class Member
{
public string Surname { get; set; }
public string FirstName { get; set; }
public DateTime MembershipStartDate { get; set; }
public int NumberOfDependents { get; set; }
public decimal MonthlyPremiumRands { get; set; }
}
Write a method that accepts a list of Member
objects, and returns a string containing a list of records (one record per line) with fixed-width fields.
For interest's sake, the program that this method plugs into will write the resulting string to a plain-text file.
The field widths are as follows:
Surname: 15 characters
FirstName: 15 characters
MembershipStartDate: 8 characters (in the format YYYYMMDD)
NumberOfDependents: 2 characters (with a leading zero)
MonthlyPremiumRands: 8 characters (no leading zeroes, no cents)
MonthlyPremiumRands
must be right-aligned. All other fields must be left-aligned.
Surname
and FirstName
must be converted to uppercase before writing. Furthermore, if either of them are more than 15 characters in length, truncate them to 15 characters.
The file must include a single space after each field, except the last one, and each record must end with a carriage-return/line-feed combination.
In other words, the record terminator in your output will be \r\n
. If there are no records (i.e. no lines), they must not be terminated.
For example, the record:
new Member
{
Surname = "Downs",
FirstName = "Graham",
MembershipStartDate = new DateTime(1998, 1, 1),
NumberOfDependents = 1,
MonthlyPremiumRands = 5120
}
should be written as follows:
DOWNS GRAHAM 19980101 01 5120
You may assume that the list will never be null
, but it may be empty, in which case you should return an empty string.
Similar Kata:
Stats:
Created | May 17, 2017 |
Published | May 22, 2017 |
Warriors Trained | 77 |
Total Skips | 8 |
Total Code Submissions | 187 |
Total Times Completed | 16 |
C# Completions | 16 |
Total Stars | 3 |
% of votes with a positive feedback rating | 80% of 10 |
Total "Very Satisfied" Votes | 6 |
Total "Somewhat Satisfied" Votes | 4 |
Total "Not Satisfied" Votes | 0 |
Total Rank Assessments | 11 |
Average Assessed Rank | 6 kyu |
Highest Assessed Rank | 6 kyu |
Lowest Assessed Rank | 7 kyu |