From e38b251da059ba337e1b9182bacf4bb3405ba183 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=96=A2=20=E5=A4=A7=E8=BC=9D?= Date: Fri, 7 Jul 2023 13:53:27 +0900 Subject: [PATCH 1/6] =?UTF-8?q?paiza=20C048=E3=81=AE=E3=82=B3=E3=83=BC?= =?UTF-8?q?=E3=83=89=E4=BD=9C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dseki/paiza/src/C048.java | 64 ++++++++++++++++++++++++++++++++ dseki/paiza/test/C048Test.java | 68 ++++++++++++++++++++++++++++++++++ 2 files changed, 132 insertions(+) create mode 100644 dseki/paiza/src/C048.java create mode 100644 dseki/paiza/test/C048Test.java diff --git a/dseki/paiza/src/C048.java b/dseki/paiza/src/C048.java new file mode 100644 index 0000000..8c805a7 --- /dev/null +++ b/dseki/paiza/src/C048.java @@ -0,0 +1,64 @@ +import java.util.Scanner; + +/** + * タダ飲みコーヒー問題. + */ +public class C048 { + 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); + final int coffeePrice = sc.nextInt(); // コーヒーの元値 + final int discountRate = sc.nextInt(); // 割引率 + C048 c048 = new C048(coffeePrice, discountRate); + c048.judgeDiscountRate(); + System.out.println(c048.getTotalPriceOfCoffee()); + sc.close(); + } + + /** + * 存在する割引率かを判断するメソッド. + */ + public void judgeDiscountRate() { + if (this.discountRate <= 0 || this.discountRate > 100) + 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 0000000..a87d308 --- /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)); + } + +} -- GitLab From 621624050c29d3059e0c78e6327848043e354661 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=96=A2=20=E5=A4=A7=E8=BC=9D?= Date: Fri, 7 Jul 2023 14:29:06 +0900 Subject: [PATCH 2/6] =?UTF-8?q?paiza=20C048=E3=81=AE=E3=82=B3=E3=83=BC?= =?UTF-8?q?=E3=83=89=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dseki/paiza/src/C048.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/dseki/paiza/src/C048.java b/dseki/paiza/src/C048.java index 8c805a7..49ee8d5 100644 --- a/dseki/paiza/src/C048.java +++ b/dseki/paiza/src/C048.java @@ -4,8 +4,10 @@ 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; // 割引率 + private final int discountRate; // 割引率 C048(final int coffeePrice, final int discountRate) { this.coffeePrice = coffeePrice; @@ -19,7 +21,7 @@ public class C048 { final Scanner sc = new Scanner(System.in); final int coffeePrice = sc.nextInt(); // コーヒーの元値 final int discountRate = sc.nextInt(); // 割引率 - C048 c048 = new C048(coffeePrice, discountRate); + final C048 c048 = new C048(coffeePrice, discountRate); c048.judgeDiscountRate(); System.out.println(c048.getTotalPriceOfCoffee()); sc.close(); @@ -29,8 +31,9 @@ public class C048 { * 存在する割引率かを判断するメソッド. */ public void judgeDiscountRate() { - if (this.discountRate <= 0 || this.discountRate > 100) + if (this.discountRate <= DISCOUNT_RATE_MIN || this.discountRate > DISCOUNT_RATE_MAX) { throw new IllegalArgumentException("割引率" + this.discountRate + "%は存在しません"); + } } /** -- GitLab From 653a2bc795fcf69550393683d3868d2a89c69c74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=96=A2=20=E5=A4=A7=E8=BC=9D?= Date: Fri, 7 Jul 2023 16:30:01 +0900 Subject: [PATCH 3/6] =?UTF-8?q?paiza=20C048=E3=81=AE=E3=82=B3=E3=83=BC?= =?UTF-8?q?=E3=83=89=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dseki/paiza/src/C048.java | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/dseki/paiza/src/C048.java b/dseki/paiza/src/C048.java index 49ee8d5..d65a42d 100644 --- a/dseki/paiza/src/C048.java +++ b/dseki/paiza/src/C048.java @@ -21,17 +21,17 @@ public class C048 { final Scanner sc = new Scanner(System.in); final int coffeePrice = sc.nextInt(); // コーヒーの元値 final int discountRate = sc.nextInt(); // 割引率 + sc.close(); final C048 c048 = new C048(coffeePrice, discountRate); c048.judgeDiscountRate(); System.out.println(c048.getTotalPriceOfCoffee()); - sc.close(); } /** * 存在する割引率かを判断するメソッド. */ public void judgeDiscountRate() { - if (this.discountRate <= DISCOUNT_RATE_MIN || this.discountRate > DISCOUNT_RATE_MAX) { + if (this.discountRate <= DISCOUNT_RATE_MIN || DISCOUNT_RATE_MAX < this.discountRate) { throw new IllegalArgumentException("割引率" + this.discountRate + "%は存在しません"); } } @@ -46,10 +46,12 @@ public class C048 { public int getTotalPriceOfCoffee() { int total = 0; int coffeePriceAfterDiscount = this.coffeePrice; - while (coffeePriceAfterDiscount > 0) { - total += coffeePriceAfterDiscount; - coffeePriceAfterDiscount -= discountCoffeePrice(coffeePriceAfterDiscount); - Math.floor(coffeePriceAfterDiscount); + if (DISCOUNT_RATE_MIN < this.discountRate && this.discountRate <= DISCOUNT_RATE_MAX) { + while (coffeePriceAfterDiscount > 0) { + total += coffeePriceAfterDiscount; + coffeePriceAfterDiscount -= discountCoffeePrice(coffeePriceAfterDiscount); + Math.floor(coffeePriceAfterDiscount); + } } return total; } -- GitLab From 52aa2a397e715011efeebe7dbf33c8659e45b995 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=96=A2=20=E5=A4=A7=E8=BC=9D?= Date: Fri, 7 Jul 2023 16:31:01 +0900 Subject: [PATCH 4/6] =?UTF-8?q?paiza=20C048=E3=81=AE=E3=82=B3=E3=83=BC?= =?UTF-8?q?=E3=83=89=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dseki/paiza/src/C048.java | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/dseki/paiza/src/C048.java b/dseki/paiza/src/C048.java index d65a42d..2478ebf 100644 --- a/dseki/paiza/src/C048.java +++ b/dseki/paiza/src/C048.java @@ -46,12 +46,10 @@ public class C048 { public int getTotalPriceOfCoffee() { int total = 0; int coffeePriceAfterDiscount = this.coffeePrice; - if (DISCOUNT_RATE_MIN < this.discountRate && this.discountRate <= DISCOUNT_RATE_MAX) { - while (coffeePriceAfterDiscount > 0) { - total += coffeePriceAfterDiscount; - coffeePriceAfterDiscount -= discountCoffeePrice(coffeePriceAfterDiscount); - Math.floor(coffeePriceAfterDiscount); - } + while (coffeePriceAfterDiscount > 0) { + total += coffeePriceAfterDiscount; + coffeePriceAfterDiscount -= discountCoffeePrice(coffeePriceAfterDiscount); + Math.floor(coffeePriceAfterDiscount); } return total; } -- GitLab From f4d425839ac972dbf63046b5ccc58310dd22c799 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=96=A2=20=E5=A4=A7=E8=BC=9D?= Date: Fri, 7 Jul 2023 17:02:28 +0900 Subject: [PATCH 5/6] =?UTF-8?q?paiza=20C048=E3=81=AE=E3=82=B3=E3=83=BC?= =?UTF-8?q?=E3=83=89=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dseki/paiza/src/C048.java | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/dseki/paiza/src/C048.java b/dseki/paiza/src/C048.java index 2478ebf..452b58f 100644 --- a/dseki/paiza/src/C048.java +++ b/dseki/paiza/src/C048.java @@ -21,10 +21,15 @@ public class C048 { final Scanner sc = new Scanner(System.in); final int coffeePrice = sc.nextInt(); // コーヒーの元値 final int discountRate = sc.nextInt(); // 割引率 - sc.close(); final C048 c048 = new C048(coffeePrice, discountRate); - c048.judgeDiscountRate(); - System.out.println(c048.getTotalPriceOfCoffee()); + try { + c048.judgeDiscountRate(); + System.out.println(c048.getTotalPriceOfCoffee()); + } catch (Exception e) { + System.out.println("例外が発生しました"); + } finally { + sc.close(); + } } /** -- GitLab From 34765afe11ff24019dd0a34d8ad06b67b1a08bc8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=96=A2=20=E5=A4=A7=E8=BC=9D?= Date: Fri, 7 Jul 2023 17:31:38 +0900 Subject: [PATCH 6/6] =?UTF-8?q?paiza=20C048=E3=81=AE=E3=82=B3=E3=83=BC?= =?UTF-8?q?=E3=83=89=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dseki/paiza/src/C048.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/dseki/paiza/src/C048.java b/dseki/paiza/src/C048.java index 452b58f..ac5809e 100644 --- a/dseki/paiza/src/C048.java +++ b/dseki/paiza/src/C048.java @@ -19,10 +19,10 @@ public class C048 { */ public static void main(final String[] args) { final Scanner sc = new Scanner(System.in); - final int coffeePrice = sc.nextInt(); // コーヒーの元値 - final int discountRate = sc.nextInt(); // 割引率 - final C048 c048 = new C048(coffeePrice, discountRate); 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) { -- GitLab