/// <summary> /// Parse input file into separate trip expenses and generate filename of output /// </summary> /// <param name="inputPath"></param> private void ParseInputFile(String inputPath) { OuputFilePath = GetOutputFileName(inputPath); string[] lines = System.IO.File.ReadAllLines(inputPath); int cur = 0; int numberOfPurchasingRecordsInOneReceipt = 0; // headCount is the number of participants in each trip. int headCount = Int32.Parse(lines[cur++]); TripExpense tripExpense = new TripExpense(); while (headCount > 0) { numberOfPurchasingRecordsInOneReceipt = Int32.Parse(lines[cur++]); Receipt receipt = new Receipt(); while (numberOfPurchasingRecordsInOneReceipt > 0) { numberOfPurchasingRecordsInOneReceipt--; receipt.AddNewExpense(double.Parse(lines[cur++], System.Globalization.CultureInfo.InvariantCulture)); } // Add total amount paid by this participant into trip's ledger. // Then, move to the next participant's receipt. headCount--; tripExpense.AddNewExpense(receipt.GetSum()); if (headCount == 0) { // All receipts of a particular trip have been consolidated, so add this trip's expense into the main bill's ledger. // Then, move to the next trip's expense. headCount = Int32.Parse(lines[cur++]); allTripBills.Add(tripExpense); tripExpense = new TripExpense(); } } }
/// <summary> /// Add a new trip expense into bills ledger. A trip expense consists of amounts owed by each participant. /// </summary> /// <param name="tripExp"></param> private void AddNewTripExpense(TripExpense tripExp) { allTripBills.Add(tripExp); }