-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChangeMaking.java
More file actions
51 lines (39 loc) · 1.07 KB
/
ChangeMaking.java
File metadata and controls
51 lines (39 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import java.util.*;
enum Coin {
ONE(1),
TWO(2),
FIVE(5),
TEN(10),
TWENTY(20),
FIFTY(50);
private final int amount;
Coin(int amount) {
this.amount = amount;
}
public int amount() {
return amount;
}
public static Coin[] valuesAmountDesc() {
return Arrays.stream(values())
.sorted(Comparator.comparingInt(Coin::amount).reversed())
.toArray(Coin[]::new);
}
}
public class ChangeMaking {
public static void main(String[] args) {
int[] change = changeMoney(17);
Arrays.stream(change)
.forEach(System.out::println);
}
private static int[] changeMoney(int amount) {
List<Integer> change = new ArrayList<>();
for (Coin coin : Coin.valuesAmountDesc()) {
while (amount - coin.amount() >= 0) {
amount -= coin.amount();
change.add(coin.amount());
}
}
return change.stream()
.mapToInt(Integer::intValue)
.toArray();
}