diff --git a/ntakanabe/src/CoinPayment.java b/ntakanabe/src/CoinPayment.java new file mode 100644 index 0000000000000000000000000000000000000000..1667f5ba4c901985fa7f029927664bec7ccfc7fc --- /dev/null +++ b/ntakanabe/src/CoinPayment.java @@ -0,0 +1,71 @@ +package paiza.src; + +import java.util.Scanner; + +/** + * . C166 硬貨の支払いに必要な最低枚数を調べるクラス + * + * @author ntakanabe + */ + +public class CoinPayment { + + /** + * . 入力メソッド、最低硬貨枚数の計算メソッド、出力メソッドの呼び出しを行うmainメソッド + */ + public static void main(final String[] args) { + + final int price = getInputPrice(); + final int totalCoinsCount = calculateMinCoins(price); + displayResult(totalCoinsCount); + } + + /** + * . 標準入力から購入金額を取得するメソッド + * + * @return 入力された購入金額 + */ + private static int getInputPrice() { + + final Scanner sc = new Scanner(System.in); + final int price = sc.nextInt(); + sc.close(); + + return price; + } + + /** + * . 入力された金額を支払うために必要な硬貨の最低枚数を計算するメソッド + * + * @param price 購入金額 + * @return 支払いに必要な硬貨の合計枚数 + */ + + private static int calculateMinCoins(final int price) { + int totalCoinsCount = 0; + int calcprice = price; + + // 大きい金額の硬貨から順に並んだ配列 + final int[] coins = { + 500, 100, 50, 10, 5, 1 + }; + + // 金額の高い硬貨から順に購入金額を割っていき、カウント + for (int i = 0; i < coins.length; i++) { + final int numCoins = calcprice / coins[i]; + totalCoinsCount += numCoins; + calcprice %= coins[i]; + } + + return totalCoinsCount; + } + + /** + * . 計算結果を表示するメソッド + * + * @param totalCoinsCount 支払いに必要な硬貨の合計枚数 + */ + private static void displayResult(final int totalCoinsCount) { + System.out.println(totalCoinsCount); + } +}