Last active
August 29, 2015 14:20
-
-
Save bufferings/9414995751687177e882 to your computer and use it in GitHub Desktop.
My Favorite Code
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
public Result execute(Input in) { | |
checkNotNull(in, "In is null."); | |
try { | |
Integer productId = in.getSelected(); | |
checkProductId(productId); | |
List<Integer> inputCoins = in.getCoins(); | |
checkInputCoins(inputCoins); | |
Product product = dao.findById(productId); | |
checkArgument(product != null, "Product doesn't exist."); | |
return buyProduct(product, inputCoins); | |
} catch (Exception e) { | |
LOG.error(e); | |
return resultOf(in.getCoins()); | |
} | |
} | |
void checkProductId(Integer aProductId) { | |
checkNotNull(aProductId, "ProductId must not be null."); | |
int productId = aProductId.intValue(); | |
checkArgument(1 <= productId && productId <= 10, | |
"ProductId must be in the range 1-10."); | |
} | |
void checkInputCoins(List<Integer> aInputCoins) { | |
checkArgument(CollectionUtils.isNotEmpty(aInputCoins), | |
"InputCoins must not be null nor empty."); | |
checkArgument(aInputCoins.size() <= 100, | |
"InputCoins size must be up to 100."); | |
} | |
Result buyProduct(Product aProduct, List<Integer> aInputCoins) { | |
int sum = sumUpValidCoins(aInputCoins); | |
if (isShortOfMoney(sum, aProduct)) { | |
return resultOf(aInputCoins); | |
} | |
List<Integer> invalidCoins = getInvalidCoins(aInputCoins); | |
int change = sum - aProduct.getPrice(); | |
List<Integer> changeCoins = getChangeCoins(change); | |
List<Integer> returnCoins = ListUtils.union(invalidCoins, changeCoins); | |
return resultOf(aProduct, returnCoins); | |
} | |
static final ImmutableSet<Integer> VALID_COINS = ImmutableSet.of(10, 50, 100, 500); | |
int sumUpValidCoins(List<Integer> aInputCoins) { | |
return aInputCoins.stream() | |
.filter(coin -> VALID_COINS.contains(coin)) | |
.mapToInt(i -> i) | |
.sum(); | |
} | |
boolean isShortOfMoney(int aSum, Product aProduct) { | |
return aSum < aProduct.getPrice(); | |
} | |
List<Integer> getInvalidCoins(List<Integer> aInputCoins) { | |
return aInputCoins.stream() | |
.filter(coin -> not(VALID_COINS.contains(coin))) | |
.collect(Collectors.toList()); | |
} | |
// お釣り計算のため、大きい順に並んでいる必要がある | |
static final ImmutableList<Integer> COINS_FOR_CHANGE = ImmutableList.of(500, 100, 50, 10); | |
List<Integer> getChangeCoins(int aChange) { | |
List<Integer> changeCoins = new ArrayList<>(); | |
int rest = aChange; | |
for (Integer coin : COINS_FOR_CHANGE) { | |
while (rest > coin) { | |
changeCoins.add(coin); | |
rest -= coin; | |
} | |
} | |
return changeCoins; | |
} | |
Result resultOf(Product aProduct, List<Integer> aReturnCoins) { | |
Result result = new Result(); | |
result.setProduct(aProduct); | |
result.setCoins(aReturnCoins); | |
return result; | |
} | |
Result resultOf(List<Integer> aInputCoins) { | |
Result result = new Result(); | |
result.setProduct(null); | |
result.setCoins(aInputCoins); | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment