diff --git a/dseki/paiza/src/C048.java b/dseki/paiza/src/C048.java new file mode 100644 index 0000000000000000000000000000000000000000..ac5809e16f49f56af0fbdf1127b4725f9bb854f1 --- /dev/null +++ b/dseki/paiza/src/C048.java @@ -0,0 +1,72 @@ +import java.util.Scanner; + +/** + * タダ飲みコーヒー問題. + */ +public class C048 { + private static final int DISCOUNT_RATE_MIN = 0; + private static final int DISCOUNT_RATE_MAX = 100; + private final int coffeePrice; // コーヒーの価格 + private final int discountRate; // 割引率 + + C048(final int coffeePrice, final int discountRate) { + this.coffeePrice = coffeePrice; + this.discountRate = discountRate; + } + + /** + * メインメソッド. + */ + public static void main(final String[] args) { + final Scanner sc = new Scanner(System.in); + try { + final int coffeePrice = sc.nextInt(); // コーヒーの元値 + final int discountRate = sc.nextInt(); // 割引率 + final C048 c048 = new C048(coffeePrice, discountRate); + c048.judgeDiscountRate(); + System.out.println(c048.getTotalPriceOfCoffee()); + } catch (Exception e) { + System.out.println("例外が発生しました"); + } finally { + sc.close(); + } + } + + /** + * 存在する割引率かを判断するメソッド. + */ + public void judgeDiscountRate() { + if (this.discountRate <= DISCOUNT_RATE_MIN || DISCOUNT_RATE_MAX < this.discountRate) { + throw new IllegalArgumentException("割引率" + this.discountRate + "%は存在しません"); + } + } + + /** + * コーヒーに支払った合計額を返すメソッド. + * コーヒーから割引額を引いたものの小数点は切り捨てる。 + * コーヒーを割引していき、0未満になれば処理終了して、合計額を返す + * + * @return コーヒーに支払った合計額を返す + */ + public int getTotalPriceOfCoffee() { + int total = 0; + int coffeePriceAfterDiscount = this.coffeePrice; + while (coffeePriceAfterDiscount > 0) { + total += coffeePriceAfterDiscount; + coffeePriceAfterDiscount -= discountCoffeePrice(coffeePriceAfterDiscount); + Math.floor(coffeePriceAfterDiscount); + } + return total; + } + + /** + * コーヒーの割引額を返すメソッド. + * + * @param coffeePrice コーヒーの元値 + * @return コーヒーの割引額を返す + */ + public double discountCoffeePrice(final int coffeePrice) { + final double discountCoffeePrice = coffeePrice * ((double) this.discountRate / 100); + return discountCoffeePrice; + } +} \ No newline at end of file diff --git a/dseki/paiza/test/C048Test.java b/dseki/paiza/test/C048Test.java new file mode 100644 index 0000000000000000000000000000000000000000..a87d308bc81f7872ea3ecdf730b144cae26d15a6 --- /dev/null +++ b/dseki/paiza/test/C048Test.java @@ -0,0 +1,68 @@ +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.*; +import org.junit.Test; + +public class C048Test { + + @Test(expected = IllegalArgumentException.class) + public void judgementDiscountRateは割引率0だと例外が発生する() throws Exception { + C048 c048 = new C048(100, 0); + c048.judgeDiscountRate(); + } + + @Test(expected = IllegalArgumentException.class) + public void judgementDiscountRateは割引率101だと例外が発生する() throws Exception { + C048 c048 = new C048(100, 101); + c048.judgeDiscountRate(); + } + + @Test + public void discountCoffeePriceで100の10割引き価格が取得できる() { + C048 c048 = new C048(100, 100); + double expected = 100.0; + double actual = c048.discountCoffeePrice(100); + assertThat(actual, is(expected)); + } + + @Test + public void discountCoffeePriceで150の3割引き価格が取得できる() { + C048 c048 = new C048(150, 30); + double expected = 45.0; + double actual = c048.discountCoffeePrice(150); + assertThat(actual, is(expected)); + } + + @Test + public void discountCoffeePriceで100の0割引き価格が取得できる() { + C048 c048 = new C048(100, 0); + double expected = 0.0; + double actual = c048.discountCoffeePrice(100); + assertThat(actual, is(expected)); + } + + + @Test + public void getTotalPriceOfCoffeeで金額が100で割引率100のコーヒーにかかった合計の金額を取得できる() { + C048 c048 = new C048(100, 100); + double expected = 100; + double actual = c048.getTotalPriceOfCoffee(); + assertThat(actual, is(expected)); + } + + @Test + public void getTotalPriceOfCoffeeで金額が150で割引率30のコーヒーにかかった合計の金額を取得できる() { + C048 c048 = new C048(150, 30); + double expected = 479; + double actual = c048.getTotalPriceOfCoffee(); + assertThat(actual, is(expected)); + } + + @Test + public void getTotalPriceOfCoffeeで金額が300で割引率50のコーヒーにかかった合計の金額を取得できる() { + C048 c048 = new C048(300, 50); + double expected = 596; + double actual = c048.getTotalPriceOfCoffee(); + assertThat(actual, is(expected)); + } + +}