public void Write(string fileName, BaiFile data) { var lines = new List <string>(); lines.Add(data.FileHeader); foreach (var group in data.Groups) { lines.Add(group.GroupHeader); lines.AddRange(group.GroupContinuation); foreach (var account in group.Accounts) { lines.Add(account.AccountIdentifier); lines.AddRange(account.AccountContinuation); foreach (var detail in account.Details) { lines.Add(detail.TransactionDetail); lines.AddRange(detail.DetailContinuation); } lines.Add(account.AccountTrailer); } lines.Add(group.GroupTrailer); } lines.Add(data.FileTrailer); File.WriteAllLines(fileName, lines.ToArray()); }
public TranslatedBaiFile(BaiFile data) { Groups = new List <Group>(); // Translate myself, and walk the file if (!data.FileHeader.Trim().EndsWith("/")) { throw new NotImplementedException("Bai file is not properly formatted, I don't know how to handle this"); } if (!data.FileTrailer.Trim().EndsWith("/")) { throw new NotImplementedException("Bai file is not properly formatted, I don't know how to handle this"); } var fields = data.FileHeader.Trim().Split(','); if (fields.Length != 9) { throw new NotImplementedException("Bai file does not have proper number of FileHeader elements, I don't know how to handle this"); } HeaderRecordCode = fields[0]; SenderIdentification = fields[1]; ReceiverIdentification = fields[2]; FileIdentificationNumber = fields[5]; PhysicalRecordLength = fields[6]; BlockSize = fields[7]; VersionNumber = fields[8].Replace("/", ""); if (VersionNumber != "2") { throw new NotImplementedException("Bai file not version 2 format, I don't know how to handle this!"); } // Handle date 3, 4 FileCreationDateTime = BaiFileHelpers.DateTimeFromFields(fields[3], fields[4]); // End of Header foreach (var group in data.Groups) { Groups.Add(new Group(group)); } // Beginning of Trailer fields = data.FileTrailer.Split(','); if (fields.Length != 4) { throw new NotImplementedException("Bai file does not have proper number of FileTrailer elements, I don't know how to handle this"); } TrailerRecordCode = fields[0]; FileControlTotal = fields[1]; NumberOfGroups = int.Parse(fields[2]); NumberOfRecords = int.Parse(fields[3].Replace("/", "")); }
public TranslatedBaiFile(BaiFile data) { Groups = new List<Group>(); // Translate myself, and walk the file if (!data.FileHeader.Trim().EndsWith("/")) throw new NotImplementedException("Bai file is not properly formatted, I don't know how to handle this"); if (!data.FileTrailer.Trim().EndsWith("/")) throw new NotImplementedException("Bai file is not properly formatted, I don't know how to handle this"); var fields = data.FileHeader.Trim().Split(','); if (fields.Length != 9) throw new NotImplementedException("Bai file does not have proper number of FileHeader elements, I don't know how to handle this"); HeaderRecordCode = fields[0]; SenderIdentification = fields[1]; ReceiverIdentification = fields[2]; FileIdentificationNumber = fields[5]; PhysicalRecordLength = fields[6]; BlockSize = fields[7]; VersionNumber = fields[8].Replace("/", ""); if (VersionNumber != "2") throw new NotImplementedException("Bai file not version 2 format, I don't know how to handle this!"); // Handle date 3, 4 FileCreationDateTime = BaiFileHelpers.DateTimeFromFields(fields[3], fields[4]); // End of Header foreach (var group in data.Groups) { Groups.Add(new Group(group)); } // Beginning of Trailer fields = data.FileTrailer.Split(','); if (fields.Length != 4) throw new NotImplementedException("Bai file does not have proper number of FileTrailer elements, I don't know how to handle this"); TrailerRecordCode = fields[0]; FileControlTotal = fields[1]; NumberOfGroups = int.Parse(fields[2]); NumberOfRecords = int.Parse(fields[3].Replace("/", "")); }
private BaiFile _Parse() { var bai = new BaiFile(); var group = new BaiGroup("--default--"); var account = new BaiAccount("--default--"); var detail = new BaiDetail("--default--"); var continuation = ContinuationType.Account; foreach (var data in _data.Select((value, index) => new { value, index })) { var line = data.value; if (data.index == 0 && line.StartsWith("01")) { bai.FileHeader = line; } else if (data.index == _data.Length - 1 && line.StartsWith("99")) { bai.FileTrailer = line; } else if (line.StartsWith("02")) { continuation = ContinuationType.Group; group = new BaiGroup(line); } else if (line.StartsWith("98")) { group.GroupTrailer = line; bai.Groups.Add(group); } else if (line.StartsWith("03")) { continuation = ContinuationType.Account; account = new BaiAccount(line); detail = new BaiDetail("--default--"); } else if (line.StartsWith("49")) { if (detail.TransactionDetail != "--default--") { account.Details.Add(detail); } account.AccountTrailer = line; group.Accounts.Add(account); } else if (line.StartsWith("16")) { if (detail.TransactionDetail != "--default--") { account.Details.Add(detail); } continuation = ContinuationType.Detail; detail = new BaiDetail(line); } else if (line.StartsWith("88")) { switch (continuation) { case ContinuationType.Group: group.GroupContinuation.Add(line); break; case ContinuationType.Account: account.AccountContinuation.Add(line); break; case ContinuationType.Detail: detail.DetailContinuation.Add(line); break; } } else { throw new NotImplementedException("I don't know what to do with this line: " + line); } } return(bai); }
public static TranslatedBaiFile Translate(BaiFile data) { return new TranslatedBaiFile(data); }
public static TranslatedBaiFile Translate(BaiFile data) { return(new TranslatedBaiFile(data)); }