Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
kiwi.win
committed
Apr 5, 2012
1 parent
f248707
commit 1e94071
Showing
2 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package MakeChange154; | ||
|
||
import java.util.*; | ||
|
||
public class Change { | ||
|
||
private Vector<Integer> units; | ||
|
||
public Change(Vector<Integer> units) { | ||
this.units = units; | ||
} | ||
|
||
public Vector<Integer> makeChange(Integer money) { | ||
Vector<Integer> result = new Vector<Integer>(); | ||
|
||
Integer remainMoney = money; | ||
|
||
for (Integer unit : units) { | ||
for (int num = 1; num <= remainMoney / unit; num++) { | ||
result.add(unit); | ||
} | ||
remainMoney -= remainMoney / unit * unit; | ||
if (remainMoney <= 0) break; | ||
} | ||
|
||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package MakeChange154Test; | ||
|
||
import MakeChange154.*; | ||
import org.testng.annotations.*; | ||
|
||
import java.util.*; | ||
|
||
import static junit.framework.Assert.*; | ||
|
||
public class ChangeTest { | ||
|
||
private Change change; | ||
|
||
@BeforeClass | ||
private void setup_change() { | ||
Vector<Integer> moneyUnits = new Vector<Integer>(); | ||
moneyUnits.add(25); | ||
moneyUnits.add(10); | ||
moneyUnits.add(5); | ||
moneyUnits.add(1); | ||
|
||
change = new Change(moneyUnits); | ||
} | ||
|
||
@Test | ||
public void should_return_1_for_change_1() { | ||
Vector<Integer> expectedChanges = new Vector<Integer>(); | ||
expectedChanges.add(1); | ||
|
||
assertVectorIntegerEquals(expectedChanges, change.makeChange(1)); | ||
} | ||
|
||
@Test | ||
public void should_return_5_1_for_change_6() { | ||
Vector<Integer> expectedChanges = new Vector<Integer>(); | ||
expectedChanges.add(5); | ||
expectedChanges.add(1); | ||
|
||
assertVectorIntegerEquals(expectedChanges, change.makeChange(6)); | ||
} | ||
|
||
@Test | ||
public void should_return_25_10_10_1_1_1_1_for_change_49() { | ||
Vector<Integer> expectedChanges = new Vector<Integer>(); | ||
expectedChanges.add(25); | ||
expectedChanges.add(10); | ||
expectedChanges.add(10); | ||
expectedChanges.add(1); | ||
expectedChanges.add(1); | ||
expectedChanges.add(1); | ||
expectedChanges.add(1); | ||
|
||
assertVectorIntegerEquals(expectedChanges, change.makeChange(49)); | ||
} | ||
|
||
private void assertVectorIntegerEquals(Vector<Integer> expected, Vector<Integer> actual) { | ||
assertEquals("length not equal", expected.size(), actual.size()); | ||
|
||
for (int i = 0; i < expected.size(); i++) { | ||
assertEquals(i + "th value not equal", expected.get(i), actual.get(i)); | ||
} | ||
} | ||
} |