-
Notifications
You must be signed in to change notification settings - Fork 351
Expand file tree
/
Copy pathCoin.java
More file actions
45 lines (37 loc) · 1.23 KB
/
Copy pathCoin.java
File metadata and controls
45 lines (37 loc) · 1.23 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
package vendingmachine;
import camp.nextstep.edu.missionutils.Randoms;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import vendingmachine.message.ExceptionMessage;
public enum Coin {
COIN_500(500),
COIN_100(100),
COIN_50(50),
COIN_10(10);
private final int amount;
Coin(final int amount) {
this.amount = amount;
}
public static Coin getRandomCoin() {
List<Integer> amountList = Arrays.stream(Coin.values())
.map(coin -> coin.amount)
.collect(Collectors.toList());
int randomAmount = Randoms.pickNumberInList(amountList);
return findByAmount(randomAmount);
}
private static Coin findByAmount(int amount) {
return Arrays.stream(Coin.values())
.filter(coin -> coin.amount == amount)
.findFirst()
.orElseThrow(() -> new IllegalArgumentException(ExceptionMessage.INVALID_COIN));
}
public static List<Coin> getCoinOrderedList() {
return Arrays.stream(Coin.values())
.sorted((o1, o2) -> o2.amount - o1.amount)
.collect(Collectors.toList());
}
public int getAmount() {
return amount;
}
}