public double GetAmountOwed(Trip trip) { var expenses = trip.Expenses; if (expenses.Any(x => x < 0)) { throw new ArgumentException("At least one of the expense values is invalid."); } var tripCost = expenses.Sum(); var costPerPayer = tripCost / expenses.Count; double owedTotal = 0; foreach(var expense in expenses) { var difference = costPerPayer - expense; if(difference > 0) { owedTotal = owedTotal + difference; } } return Math.Truncate(owedTotal * 100) / 100; }
public void HappyPathZeroesInDecimalPlaces() { var trip = new Trip { Expenses = new List<double> { 10, 20, 30 } }; var retval = expenseCalculator.GetAmountOwed(trip); Assert.AreEqual(10, retval); }
public void NegativeExpenseValueThrowsException() { var trip = new Trip { Expenses = new List<double> { -15.01, 15, 3, 3.01 } }; Assert.Throws<ArgumentException>(() => expenseCalculator.GetAmountOwed(trip)); }
public void HappyPathValuesInDecimalPlaces() { var trip = new Trip { Expenses = new List<double> { 15.01, 15, 3, 3.01 } }; var retval = expenseCalculator.GetAmountOwed(trip); Assert.AreEqual(11.99, retval); }