private List <Lender> ParseCsvFile() { var listOfLenders = new List <Lender>(); using (var parser = new TextFieldParser(_filePath)) { parser.TextFieldType = FieldType.Delimited; parser.SetDelimiters(","); var header = parser.ReadLine(); while (!parser.EndOfData) { var fields = parser.ReadFields(); if (fields == null || fields.Length != 3) { throw new ApplicationException("The csv file is possibly in an incorrect format"); } var lender = new Lender { Name = fields[0], Rate = TryConvertRate(fields[1]), Available = TryConvertAvailable(fields[2]) }; listOfLenders.Add(lender); } } return(listOfLenders); }
public List <Lender> Match(decimal principal, List <Lender> lenders) { if (principal <= 0) { throw new ArgumentException("The loan principal must be positive"); } if (lenders == null || lenders.Count <= 0) { throw new ArgumentException("No available lenders were available in the matching algorithm"); } decimal runningtotal = 0; var listOfMatches = new List <Lender>(); foreach (var lender in lenders) { if (runningtotal == principal) { break; } if ((runningtotal < principal) && (principal - runningtotal >= lender.Available)) { // Full match listOfMatches.Add(lender); runningtotal += lender.Available; continue; } if (principal - runningtotal < lender.Available) { // partial match var used = principal - runningtotal; var newLender = new Lender { Name = lender.Name, Rate = lender.Rate, Available = used }; listOfMatches.Add(newLender); break; } } return(listOfMatches); }