Beta

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.

Strings
Lists
Algorithms

More By Author:

Check out these other kata created by GrahamD

Stats:

CreatedMay 17, 2017
PublishedMay 22, 2017
Warriors Trained77
Total Skips8
Total Code Submissions187
Total Times Completed16
C# Completions16
Total Stars3
% of votes with a positive feedback rating80% of 10
Total "Very Satisfied" Votes6
Total "Somewhat Satisfied" Votes4
Total "Not Satisfied" Votes0
Total Rank Assessments11
Average Assessed Rank
6 kyu
Highest Assessed Rank
6 kyu
Lowest Assessed Rank
7 kyu
Ad
Contributors
  • GrahamD Avatar
  • hobovsky Avatar
Ad