From 99366ec29f38a5ba6f54aa2ae1be4bf4e1582fcc Mon Sep 17 00:00:00 2001 From: Shunya1228 Date: Fri, 16 Aug 2024 13:26:31 +0900 Subject: [PATCH 1/2] =?UTF-8?q?=E2=80=9CStation=E2=80=9D14=E3=81=BE?= =?UTF-8?q?=E3=81=A7=E3=81=AE=E5=86=85=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/example/cart/Cart.java | 5 +++ src/main/java/com/example/item/Item.java | 9 ++++ src/main/java/com/example/station1/Main.java | 1 + src/main/java/com/example/station10/Main.java | 10 +++++ .../java/com/example/station11_12/Main.java | 20 +++++++++ src/main/java/com/example/station2/Main.java | 3 ++ src/main/java/com/example/station3/Main.java | 1 + src/main/java/com/example/station4/Main.java | 2 + src/main/java/com/example/station5/Main.java | 7 ++++ src/main/java/com/example/station6/Main.java | 8 ++++ src/main/java/com/example/station7/Main.java | 5 +++ src/main/java/com/example/station8/Main.java | 27 ++++++++++++ src/main/java/com/example/station9/Main.java | 41 ++++++++++++++++++- 13 files changed, 137 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/example/cart/Cart.java create mode 100644 src/main/java/com/example/item/Item.java diff --git a/src/main/java/com/example/cart/Cart.java b/src/main/java/com/example/cart/Cart.java new file mode 100644 index 0000000..0d0dac1 --- /dev/null +++ b/src/main/java/com/example/cart/Cart.java @@ -0,0 +1,5 @@ +package com.example.cart; + +public class Cart { + +} diff --git a/src/main/java/com/example/item/Item.java b/src/main/java/com/example/item/Item.java new file mode 100644 index 0000000..88e8893 --- /dev/null +++ b/src/main/java/com/example/item/Item.java @@ -0,0 +1,9 @@ +package com.example.item; + +public class Item { + private String name = "未設定"; + + public void setName(String newName) { + name = newName; + } +} diff --git a/src/main/java/com/example/station1/Main.java b/src/main/java/com/example/station1/Main.java index 26f7575..fc346b2 100644 --- a/src/main/java/com/example/station1/Main.java +++ b/src/main/java/com/example/station1/Main.java @@ -3,6 +3,7 @@ public class Main{ public static void main(String[] args) { // ここから + System.out.println("Hello TechTrain!"); // ここまで } } diff --git a/src/main/java/com/example/station10/Main.java b/src/main/java/com/example/station10/Main.java index 68df331..e4b19e3 100644 --- a/src/main/java/com/example/station10/Main.java +++ b/src/main/java/com/example/station10/Main.java @@ -9,5 +9,15 @@ public static void main(String[] args) { } // ここから + float applyTaxAndShippingFee(int noTaxSubtotal){ + float sum; + + if(noTaxSubtotal >= 10000) { + sum = noTaxSubtotal *1.1f; + } else { + sum = (noTaxSubtotal +800)*1.1f; + } + return sum; + } // ここまで } diff --git a/src/main/java/com/example/station11_12/Main.java b/src/main/java/com/example/station11_12/Main.java index e9b178c..ab65a6e 100644 --- a/src/main/java/com/example/station11_12/Main.java +++ b/src/main/java/com/example/station11_12/Main.java @@ -16,5 +16,25 @@ private void test() { } // ここから + class Item { + String name; + int price; + String category; + String[] tags; + + Item (String name, int price, String category, String[] tags) { + this.name = name; + this.price = price; + this.category = category; + this.tags = tags; + } + float getPriceWithTax (boolean isKeigenzeiritu) { + if(isKeigenzeiritu == true) { + return price * 1.08f; + } else { + return price * 1.1f; + } + } + } // ここまで } diff --git a/src/main/java/com/example/station2/Main.java b/src/main/java/com/example/station2/Main.java index b11e5de..42dd20e 100644 --- a/src/main/java/com/example/station2/Main.java +++ b/src/main/java/com/example/station2/Main.java @@ -3,6 +3,9 @@ public class Main { public static void main(String[] args) { // ここから + System.out.println(false) ; + System.out.println(42) ; + System.out.println(3.141592) ; // ここまで } } diff --git a/src/main/java/com/example/station3/Main.java b/src/main/java/com/example/station3/Main.java index 49fafea..2ce78b0 100644 --- a/src/main/java/com/example/station3/Main.java +++ b/src/main/java/com/example/station3/Main.java @@ -3,6 +3,7 @@ public class Main { public static void main(String[] args) { // ここから + System.out.println("/^^^^^^^^^^^\\\n|\"TechTrain\"|\n\\___________/"); // ここまで } } diff --git a/src/main/java/com/example/station4/Main.java b/src/main/java/com/example/station4/Main.java index 57a39b5..3b8f84d 100644 --- a/src/main/java/com/example/station4/Main.java +++ b/src/main/java/com/example/station4/Main.java @@ -3,6 +3,8 @@ public class Main { public static void main(String[] args) { // ここから + System.out.println( 18*28 == 24*21 ); + System.out.println( 3.141592f < 21.9f/7 ); // ここまで } } diff --git a/src/main/java/com/example/station5/Main.java b/src/main/java/com/example/station5/Main.java index 8c66c8a..25be70e 100644 --- a/src/main/java/com/example/station5/Main.java +++ b/src/main/java/com/example/station5/Main.java @@ -3,6 +3,13 @@ public class Main { public static void main(String[] args) { // ここから + int userA = 23; + int userB = 36; + int userC = 31; + int userD = 48; + int userE = 58; + double all = userA + userB + userC + userD + userE; + System.out.println(all / 5); // ここまで } } diff --git a/src/main/java/com/example/station6/Main.java b/src/main/java/com/example/station6/Main.java index 2d5cd9a..2692487 100644 --- a/src/main/java/com/example/station6/Main.java +++ b/src/main/java/com/example/station6/Main.java @@ -3,6 +3,14 @@ public class Main { public static void main(String[] args) { // ここから + int [] users = new int [5]; + users[0] = 23; + users[1] = 36; + users[2] = 31; + users[3] = 48; + users[4] = 58; + double all = users[0] + users[1] + users[2] + users[3] + users[4] ; + System.out.println( all / 5); // ここまで } } diff --git a/src/main/java/com/example/station7/Main.java b/src/main/java/com/example/station7/Main.java index abb8b4a..3d671ab 100644 --- a/src/main/java/com/example/station7/Main.java +++ b/src/main/java/com/example/station7/Main.java @@ -4,6 +4,11 @@ public class Main { public static void main(String[] args) { int[] itemPrices = { 1200, 2300, 3500, 2500, 1000, 2980, 9800, 4600, 5000, 2000 }; // ここから + int sum = 0; + for (int i =0; i < itemPrices.length; i = i + 1) { + sum += itemPrices[i]; + } + System.out.println(sum); // ここまで } } diff --git a/src/main/java/com/example/station8/Main.java b/src/main/java/com/example/station8/Main.java index 9a1fe38..9ba6d70 100644 --- a/src/main/java/com/example/station8/Main.java +++ b/src/main/java/com/example/station8/Main.java @@ -4,12 +4,39 @@ public class Main { public static void question1(int subtotal) { // Station 8 問題(1) // ここから + if(subtotal < 10000) { + System.out.println("送料800円を追加します"); + } + int total = 0; + if(subtotal < 10000) { + total = subtotal + 800; + } else if (subtotal >= 10000) { + total = subtotal; + } + System.out.println(total); // ここまで } public static void question2(int categoryId) { // Station 8 問題(2) // ここから + switch(categoryId) { + case 1: + System.out.println("食品"); + break; + case 2: + System.out.println("家電"); + break; + case 3: + System.out.println("家具"); + break; + case 99: + System.out.println("その他"); + break; + default: + //何も出力したくない場合は空でOK + break; + } // ここまで } diff --git a/src/main/java/com/example/station9/Main.java b/src/main/java/com/example/station9/Main.java index 431dc06..5bf5c85 100644 --- a/src/main/java/com/example/station9/Main.java +++ b/src/main/java/com/example/station9/Main.java @@ -5,16 +5,53 @@ public static void main(String[] args) { int[] itemPrices1 = {1000, 1100, 1200, 1300}; int[] itemPrices2 = {1000, 1100, 1200, 1300, 1600, 1800}; int[] itemPrices3 = {1000, 2100, 3200, 2300}; + System.out.print("カート内: 1000円, 1100円, 1200円, 1300円。期待されるクーポン: A。結果: "); test(itemPrices1); + System.out.print("カート内: 1000円, 1100円, 1200円, 1300円, 1600円, 1800円。期待されるクーポン: B。結果: "); test(itemPrices2); + System.out.print("カート内: 1000円, 2100円, 3200円, 2300円。期待されるクーポン: C。結果: "); test(itemPrices3); } public static void test(int[] itemPrices) { - // ここから - // ここまで + // Aクーポンを使用→合計金額のまま(sumA) + int sumA = 0; + for (int price : itemPrices) { + sumA += price; + } + + // Bクーポンを使用した場合→5000円以上で10%引き + double sumB = sumA; + if (sumA >= 5000) { + sumB *= 0.9; + } + + // Cクーポンを使用した場合→2000円以上の場合、20%引き + double sumC = 0; + for (int price : itemPrices) { + if (price >= 2000) { + sumC += price * 0.8; + } else { + sumC += price; + } + } + + // 最も金額が低いものを選ぶ + double minSum = Math.min(sumA, Math.min(sumB, sumC)); + + // 最も金額が低いクーポンを決定する + String coupon = ""; + if (minSum == sumA) { + coupon = "A"; + } else if (minSum == sumB) { + coupon = "B"; + } else if (minSum == sumC) { + coupon = "C"; + } + + System.out.println(coupon); } } From bb197ed8d692de3ed98452cfc862ce3f2109b81e Mon Sep 17 00:00:00 2001 From: Shunya1228 Date: Sat, 17 Aug 2024 16:05:50 +0900 Subject: [PATCH 2/2] Station15 --- src/main/java/com/example/coupon/CouponA.java | 11 ++++++ src/main/java/com/example/coupon/CouponB.java | 18 ++++++++++ src/main/java/com/example/coupon/CouponC.java | 18 ++++++++++ .../com/example/coupon/CouponCalculator.java | 36 +++++++++++++++++++ 4 files changed, 83 insertions(+) create mode 100644 src/main/java/com/example/coupon/CouponA.java create mode 100644 src/main/java/com/example/coupon/CouponB.java create mode 100644 src/main/java/com/example/coupon/CouponC.java create mode 100644 src/main/java/com/example/coupon/CouponCalculator.java diff --git a/src/main/java/com/example/coupon/CouponA.java b/src/main/java/com/example/coupon/CouponA.java new file mode 100644 index 0000000..9ebc2b7 --- /dev/null +++ b/src/main/java/com/example/coupon/CouponA.java @@ -0,0 +1,11 @@ +package com.example.coupon; + +public class CouponA { + public float calculate(int[] cartItems) { + int totalA = 0; + for (int price : cartItems) { + totalA += price; + } + return totalA; + } +} diff --git a/src/main/java/com/example/coupon/CouponB.java b/src/main/java/com/example/coupon/CouponB.java new file mode 100644 index 0000000..d2396d7 --- /dev/null +++ b/src/main/java/com/example/coupon/CouponB.java @@ -0,0 +1,18 @@ +package com.example.coupon; + +public class CouponB { + public float calculate(int[] cartItems) { + int totalA = 0; + float totalB = 0; + for (int price : cartItems) { + totalA += price; + } + totalB = totalA * 0.9f; + + if(totalB < 5000) { + totalB =totalB + 800; + } + + return totalB; + } +} diff --git a/src/main/java/com/example/coupon/CouponC.java b/src/main/java/com/example/coupon/CouponC.java new file mode 100644 index 0000000..c0fc494 --- /dev/null +++ b/src/main/java/com/example/coupon/CouponC.java @@ -0,0 +1,18 @@ +package com.example.coupon; + +public class CouponC { + public float calculate(int[] cartItems) { + float totalC = 0; + for (int price : cartItems) { + if (price >= 2000) { + totalC += price * 0.8f; + } else { + totalC += price; + } + } + if(totalC < 5000) { + totalC =totalC + 800; + } + return totalC; + } +} diff --git a/src/main/java/com/example/coupon/CouponCalculator.java b/src/main/java/com/example/coupon/CouponCalculator.java new file mode 100644 index 0000000..d30ef29 --- /dev/null +++ b/src/main/java/com/example/coupon/CouponCalculator.java @@ -0,0 +1,36 @@ +package com.example.coupon; + +public class CouponCalculator { + public String findBestCoupon(int[] cartItems) { + // 各クラスのインスタンスを作成 + CouponA couponA = new CouponA(); + CouponB couponB = new CouponB(); + CouponC couponC = new CouponC(); + + // 各クラスの calculate メソッドを呼び出し結果を取得 + float totalA = couponA.calculate(cartItems); + float totalB = couponB.calculate(cartItems); + float totalC = couponC.calculate(cartItems); + + // 最小値を求める + float minTotal; + if (totalA <= totalB && totalA <= totalC) { + minTotal = totalA; + } else if (totalB <= totalA && totalB <= totalC) { + minTotal = totalB; + } else { + minTotal = totalC; + } + + // 最も金額が低いクーポンを決定する + String coupon = ""; + if (minTotal == totalA) { + coupon = "A"; + } else if (minTotal == totalB) { + coupon = "B"; + } else if (minTotal == totalC) { + coupon = "C"; + } + return coupon; + } +}