반응형
To be
import java.util.Arrays;
import java.util.function.Predicate;
public enum Ranking {
FIRST(2000_000_000,6,false),
SECOND(30_000_000,5, true),
THIRD(1_500_000,5,false),
FOURTH(50_000,4,false),
FIFTH(5_000,3,false),
NONE(0,0,false);
private final int prize;
private final int count;
private final boolean hasBonusNumber;
Ranking(int prize,int count,boolean hasBonusNumber){
this.prize=prize;
this.count=count;
this.hasBonusNumber= hasBonusNumber;
}
public static Ranking findRanking(int cnt, boolean hasBonusNumber){
return Arrays.stream(Ranking.values()).filter(ranking -> ranking.count == cnt && ranking.hasBonusNumber == hasBonusNumber).findAny().orElse(NONE);
}
}
- Predicate : Type T 인자를 하나만 받아 boolean을 return
- BiPredicate : T, U 인자 두개를 받아 boolean을 return
import java.util.Arrays;
import java.util.function.BiPredicate;
import java.util.function.Predicate;
public enum Ranking {
FIRST(2000_000_000, (count, bonus) -> count == 6),
SECOND(30_000_000, (count, bonus) -> count == 5 && bonus),
THIRD(1_500_000, (count, bonus) -> count == 5 && !bonus),
FOURTH(50_000, (count, bonus) -> count == 4),
FIFTH(5_000, (count, bonus) -> count == 3),
NONE(0, (count, bonus) -> count < 3);
private final int prize;
BiPredicate<Integer,Boolean> condition;
Ranking(int prize, BiPredicate<Integer,Boolean> condition){
this.prize=prize;
this.condition=condition;
}
public static Ranking findRanking(int cnt, boolean hasBonusNumber){
return Arrays.stream(Ranking.values()).filter(ranking -> ranking.condition.test(cnt,hasBonusNumber))
.findAny().orElse(NONE);
}
}
반응형