using BillSplitter.Model;
using System.Collections.Generic;
public static class CampingTripStreamReader
private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
public static CampingTripParsingVO ReadDataFromFile(string fileName)
CampingTripParsingVO campingTripParsingVO = new CampingTripParsingVO();
int numberOfParticipants = 0;
if (!File.Exists(fileName))
log.ErrorFormat("Input file {0} does not exist", fileName);
campingTripParsingVO.Result = false;
using (StreamReader sr = new StreamReader(fileName))
string line = string.Empty;
while ((line = sr.ReadLine()) != null)
numberOfParticipants = ParseInt(line);
if (numberOfParticipants > 0)
currentTrip = new CampingTrip();
currentTrip.Participants = new List<CampingParticipant>(numberOfParticipants);
campingTripParsingVO.CampingTrips.Add(currentTrip);
while (numberOfParticipants > 0)
CampingParticipant campingParticipant = new CampingParticipant();
numberOfCharges = ParseInt(sr.ReadLine());
for (int i = 0; i < numberOfCharges; i++)
campingParticipant.TotalCharges += ParseMoney(sr.ReadLine());
currentTrip.Participants.Add(campingParticipant);
numberOfParticipants = numberOfParticipants - 1;
campingTripParsingVO.Result = true;
log.ErrorFormat("Error while processing file {0}. Inner Exception details {1}", fileName, ex.InnerException);
campingTripParsingVO.Result = false;
return campingTripParsingVO;
private static int ParseInt(string input)
int.TryParse(input, out value);
private static decimal ParseMoney(string input)
decimal.TryParse(input, out value);