Skip to content

Instantly share code, notes, and snippets.

@osamutake
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save osamutake/eef52076810a996831c6 to your computer and use it in GitHub Desktop.
Save osamutake/eef52076810a996831c6 to your computer and use it in GitHub Desktop.
http://bufferings.hatenablog.com/entry/2015/05/06/000943 で紹介されていた「手続き型で典型的」かつ、かなりアレなコードを自分なりにリファクタリングしてみました。
public Result execute(Input in) {
try {
Integer productId = in.getSelected();
List<Integer> coinsInserted = in.getCoins();
if ( !IsProductIdInRange(productId, 1, 10) ||
!IsCoinCountInRange(coinsInserted, 1, 100) ) {
throw new IllegalArgumentException();
}
List<Integer> coinsToReturn = new ArrayList<>();
int deposit = calcDeposit(coinsInserted, coinsToReturn);
Product product = dao.findById(productId);
if ( product == null || product.getPrice() > deposit ) {
return createResultWitoutProduct(coinsInserted);
}
deposit -= product.getPrice();
calcChange(deposit, coinsToReturn);
Result result = new Result();
result.setProduct(product);
result.setCoins(coinsToReturn);
return result;
} catch (Exception e) {
LOG.error(e);
return createResultWitoutProduct(coinsInserted);
}
}
boolean IsProductIdInRange(Integer productId, int from, int to)
{
return productId != null && from <= productId && productId <= to;
}
boolean IsCoinCountInRange(List<Integer> coins, int from, int to)
{
return coins != null && from <= coins.size() && coins.size() <= to;
}
Result createResultWitoutProduct(List<Integer> coins)
{
result = new Result();
result.setCoins(coins);
result.setProduct(null);
return result;
}
// TODO: 本来なら定数を用意すべき
List<Integer> getCoinListAllowedForChange()
{
List<Integer> list = new ArrayList<>();
list.add(500);
list.add(100);
list.add(50);
list.add(10);
return list;
}
int calcDeposit(List<Integer> coins, List<Integer> coinsToReturn)
{
int deposit = 0;
for (Integer coin : coins) {
if (coin == 1 || coin == 5) {
coinsToReturn.add(coin);
} else
if (coin == 10 || coin == 50 || coin == 100 || coin == 500 ) {
deposit += coin;
} else {
// throw new IllegalArgumentException();
}
}
return deposit;
}
void calcChange(int deposit, List<Integer> coinsToReturn)
{
for (Integer coin : getCoinListAllowedForChange()) {
while (deposit > coin) {
coinsToReturn.add(coin);
deposit -= coin;
}
}
// if (deposit != 0) {
// // product.Price() が 10 で割り切れない!
// throw new IllegalArgumentException();
// }
}
@osamutake
Copy link
Author

編集履歴についてのコメントは http://dora.bk.tsukuba.ac.jp/~takeuchi/?プログラミング%2F作法%2FHello.java にあります。

Mitsuyuki.Shiiba さんのオリジナルのコードが Gist 上に https://gist.github.com/bufferings/9414995751687177e882 としてあるのですが、特定のリビジョンから Fork する方法が解らず、コピペで新しい Gist を作ることになってしまいました。たぶん間違った方法なんだと思うのですが、どうもすみません。

ここからも解るように、私は Gist も、実は Java もあまりよく分かっていません。普段は Ruby とか JavaScript とか C# とか良く書いてます。Java のコンパイル環境もないので、タイプミスについてもご容赦下さい。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment