Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Station15 #10

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/main/java/com/example/cart/Cart.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.example.cart;

public class Cart {

}
11 changes: 11 additions & 0 deletions src/main/java/com/example/coupon/CouponA.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
18 changes: 18 additions & 0 deletions src/main/java/com/example/coupon/CouponB.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
18 changes: 18 additions & 0 deletions src/main/java/com/example/coupon/CouponC.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
36 changes: 36 additions & 0 deletions src/main/java/com/example/coupon/CouponCalculator.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
9 changes: 9 additions & 0 deletions src/main/java/com/example/item/Item.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.example.item;

public class Item {
private String name = "未設定";

public void setName(String newName) {
name = newName;
}
}
1 change: 1 addition & 0 deletions src/main/java/com/example/station1/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
public class Main{
public static void main(String[] args) {
// ここから
System.out.println("Hello TechTrain!");
// ここまで
}
}
10 changes: 10 additions & 0 deletions src/main/java/com/example/station10/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
// ここまで
}
20 changes: 20 additions & 0 deletions src/main/java/com/example/station11_12/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
// ここまで
}
3 changes: 3 additions & 0 deletions src/main/java/com/example/station2/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) ;
// ここまで
}
}
1 change: 1 addition & 0 deletions src/main/java/com/example/station3/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
public class Main {
public static void main(String[] args) {
// ここから
System.out.println("/^^^^^^^^^^^\\\n|\"TechTrain\"|\n\\___________/");
// ここまで
}
}
2 changes: 2 additions & 0 deletions src/main/java/com/example/station4/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
// ここまで
}
}
7 changes: 7 additions & 0 deletions src/main/java/com/example/station5/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
// ここまで
}
}
8 changes: 8 additions & 0 deletions src/main/java/com/example/station6/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
// ここまで
}
}
5 changes: 5 additions & 0 deletions src/main/java/com/example/station7/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
// ここまで
}
}
27 changes: 27 additions & 0 deletions src/main/java/com/example/station8/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
// ここまで
}

Expand Down
41 changes: 39 additions & 2 deletions src/main/java/com/example/station9/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}