ReturnChange { public List<Integer> calculate(int[] moneys, int billAmount) { validateInputData(moneys, billAmount); List<Integer> change = new LinkedList<Integer>(); while (billAmount > 0 && moneys.length > 0) { for (int i = moneys.length - 1; i >= 0; i--) { if (billAmount - moneys[i] >= 0) { change.add(moneys[i]); billAmount -= moneys[i]; break; } } } return change; }  List<Integer> calculate(int[] moneys, int billAmount);  }
@Test(expected = IllegalArgumentException.class) public void shouldNotAcceptNullArrayOfMoneys() { returnChange.calculate(null, 7); }
@Test(expected = IllegalArgumentException.class) public void shouldNotAcceptEmptyArrayOfMoneys() { returnChange.calculate(new int[0], 7); }
@Test(expected = IllegalArgumentException.class) public void shouldNotAcceptZeroAsInputValue() { returnChange.calculate(new int[0], 0); }
@Test(expected = IllegalArgumentException.class) public void shouldNotAcceptNegativeValuesAsInputValue() { returnChange.calculate(new int[0], -7); }
@Test public void shouldReturnChangeUsingJustTheBiggestMoney() { int[] moneys = { 5, 10, 15, 50 }; List<Integer> change = returnChange.calculate(moneys, 200); int[] expectedChange = { 50, 50, 50, 50 }; assertChangeEqualsTo(expectedChange, change); }
@Test public void shouldReturnChangeUsingMoreThanOneMoney() { int[] moneys = { 5, 10, 15, 50 }; List<Integer> change = returnChange.calculate(moneys, 80); int[] expectedChange = { 50, 15, 15 }; assertChangeEqualsTo(expectedChange, change); }