private void SetExpenseTotals(TripMemberCollection members)
        {
            foreach (var member in members.TripMembers)
                member.TotalExpense = member.Expenses.Sum();

            members.TotalExpense = members.TripMembers.Sum(m => m.TotalExpense);
            members.TotalExpenseAverage = members.TotalExpense/members.TripMembers.Count();
        }
        public TripSettlementCollection GetSettlements(TripMemberCollection members)
        {
            SetExpenseTotals(members);
            SetAmountOwed(members);

            var settlements = GetCalculatedSettlements(members);
            return new TripSettlementCollection { Settlements = settlements };
        }
        private IEnumerable<TripSettlement> GetCalculatedSettlements(TripMemberCollection members)
        {
            var orderedMembers = GetOrderedMembers(members);

            foreach (var sender in GetSenders(orderedMembers))
            {
                foreach (var receiver in GetReceivers(orderedMembers))
                {
                    var transferrableAmount = GetMaximumTransferrableAmount(sender, receiver);
                    sender.AmountTransferred += transferrableAmount;
                    receiver.AmountTransferred -= transferrableAmount;

                    yield return new TripSettlement
                    {
                        SenderName = sender.Name,
                        ReceiverName = receiver.Name,
                        Amount = transferrableAmount
                    };
                }
            }
        }
 private TripMemberCollection GetTwoMemberExpensesCollection()
 {
     var purchases = new TripMemberCollection
     {
         TripMembers = new List<TripMember>
         {
             new TripMember
             {
                 Name = "Amber",
                 Expenses = new[] {1.01M, 1.12M, 2.23M, 3.34M, 5.45M, 8.56M}
             },
             new TripMember
             {
                 Name = "Brandon",
                 Expenses = new[] {2.21M, 3.33M, 6.43M, 9.67M, 15.98M, 28.99M}
             }
         }
     };
     return purchases;
 }
 private TripMemberCollection GetThreeMemberExpensesCollection()
 {
     var purchases = new TripMemberCollection
     {
         TripMembers = new List<TripMember>
         {
             new TripMember
             {
                 Name = "Amber",
                 Expenses = new[] { 1.25M, 1.50M, 5.67M, 98.41M }
             },
             new TripMember
             {
                 Name = "Brandon",
                 Expenses = new[] { 49.96M, 87.12M, 105.78M }
             },
             new TripMember
             {
                 Name = "Catherine",
                 Expenses = new[] { 1.01M, 1.12M, 2.23M, 3.34M, 5.45M, 8.56M }
             }
         }
     };
     return purchases;
 }
 private TripMemberCollection GetOneMemberExpenseCollection()
 {
     var purchases = new TripMemberCollection
     {
         TripMembers = new List<TripMember>
         {
             new TripMember
             {
                 Name = "Amber",
                 Expenses = new[] {0M, 1.01M, 1.12M, 2.23M, 3.34M, 5.45M, 8.56M}
             }
         }
     };
     return purchases;
 }
 private void SetAmountOwed(TripMemberCollection members)
 {
     foreach (var member in members.TripMembers)
         member.AmountOwed = member.TotalExpense - members.TotalExpenseAverage;
 }
 private List<TripMember> GetOrderedMembers(TripMemberCollection members)
 {
     return members.TripMembers.OrderBy(m => m.AmountOwed).ToList();
 }