From 5853313d5c1266f73bd74fbb11e35997b3120450 Mon Sep 17 00:00:00 2001 From: rtanaka Date: Wed, 23 Jul 2025 15:50:25 +0900 Subject: [PATCH 1/2] =?UTF-8?q?paiza=E3=81=AE=E5=95=8F=E9=A1=8CB157?= =?UTF-8?q?=E3=81=AE=E5=9B=9E=E7=AD=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rtanaka/src/Main_B157.java | 47 ++++++++++++ rtanaka/src/SaveMoneyCalculation.java | 65 ++++++++++++++++ rtanaka/src/Supermarket.java | 37 +++++++++ rtanaka/src/Vegetable.java | 24 ++++++ rtanaka/test/SaveMoneyCalculationTest.java | 89 ++++++++++++++++++++++ 5 files changed, 262 insertions(+) create mode 100644 rtanaka/src/Main_B157.java create mode 100644 rtanaka/src/SaveMoneyCalculation.java create mode 100644 rtanaka/src/Supermarket.java create mode 100644 rtanaka/src/Vegetable.java create mode 100644 rtanaka/test/SaveMoneyCalculationTest.java diff --git a/rtanaka/src/Main_B157.java b/rtanaka/src/Main_B157.java new file mode 100644 index 0000000..30f76f3 --- /dev/null +++ b/rtanaka/src/Main_B157.java @@ -0,0 +1,47 @@ + +/** + * @author 田中亮汰 + * @version 1.0 + */ + +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +public class Main_B157 { + + public static void main(String[] args) { + + List supermarkets = new ArrayList<>(); + List vegetables = new ArrayList<>(); + + Scanner sc = new Scanner(System.in); + int supermarketNum = sc.nextInt();// スーパーマーケットの店舗数 + int vegetableNum = sc.nextInt();// 野菜の個数 + + // 野菜に番号(名称)をつけてリストに格納 + for (int vegetableCount = 0; vegetableCount < vegetableNum; vegetableCount++) { + vegetables.add(new Vegetable("Vegetable_" + vegetableCount, vegetableCount)); + } + // 野菜の番号と値段を対応する新しく作ったスーパーマーケットオブジェクトの + // フィールドに保存していく.その後このオブジェクトをリストに格納する + for (int supermarketCount = 0; supermarketCount < supermarketNum; supermarketCount++) { + Supermarket currentSupermarket = new Supermarket("Supermarket_" + supermarketCount, supermarketCount); + for (int vegetableCount = 0; vegetableCount < vegetableNum; vegetableCount++) { + int price = sc.nextInt(); + currentSupermarket.addPrice(vegetables.get(vegetableCount), price); + } + supermarkets.add(currentSupermarket); + } + sc.close(); + + //calculateSupermarketsHopping()内でfindCheapestSupermarketsForVegetables()を呼び出している。 + //vegetablesで取り出したvegetableでsupermarketsの要素を取り出して値段を比較後 + //その野菜が最も安い店を、Mapに野菜と店で格納しSetで重複を省いてそのコレクションのサイズを返している + SaveMoneyCalculation saveMoneyCal = new SaveMoneyCalculation(supermarkets, vegetables); + int minimumSupermarketsHopping = saveMoneyCal.calculateSupermarketsHopping(); + + System.out.println(minimumSupermarketsHopping); + } + +} diff --git a/rtanaka/src/SaveMoneyCalculation.java b/rtanaka/src/SaveMoneyCalculation.java new file mode 100644 index 0000000..db41de2 --- /dev/null +++ b/rtanaka/src/SaveMoneyCalculation.java @@ -0,0 +1,65 @@ + +/** + * @author 田中亮汰 + * @version 1.0 + */ + +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +public class SaveMoneyCalculation { + List supermarkets; + List vegetables; + + public SaveMoneyCalculation(List supermarkets, List vegetables) { + this.supermarkets = supermarkets; + this.vegetables = vegetables; + } + + /** + * vegetablesの中身を取り出してsupermarketに渡して値段を比較し、 + * その野菜が最も安いスーパーを求めcheapestSupermarketsに格納し返すメソッド + * + * @return Mapで(key, value) =(野菜,最安スーパー) + */ + public Map findCheapestSupermarketsForVegetables() { + Map cheapestSupermarkets = new HashMap<>(); + //vegetablesリストの中身と + for (Vegetable vegetable : this.vegetables) { + int minPrice = Integer.MAX_VALUE;//ループ毎に別の野菜を比較するためここで初期化 + Supermarket cheapestSupermarket = null; + + for (Supermarket supermarket : this.supermarkets) { + int currentPrice = supermarket.getPrice(vegetable); + + if (currentPrice < minPrice) { + minPrice = currentPrice; + cheapestSupermarket = supermarket; + } + } + cheapestSupermarkets.put(vegetable, cheapestSupermarket); + } + return cheapestSupermarkets; + } + + /** + * 最安のスーパーを取り出し + * Setで重複したスーパーを統一し返すメソッド + * + * @return 統一後のスーパーの数を返す。 + */ + public int calculateSupermarketsHopping() { + Map cheapestSupermarkets = findCheapestSupermarketsForVegetables(); + Set uniqueSupermarkets = new HashSet<>(); + for (Supermarket supermarket : cheapestSupermarkets.values()) { + + uniqueSupermarkets.add(supermarket); + + } + return uniqueSupermarkets.size(); + + } +} \ No newline at end of file diff --git a/rtanaka/src/Supermarket.java b/rtanaka/src/Supermarket.java new file mode 100644 index 0000000..c395107 --- /dev/null +++ b/rtanaka/src/Supermarket.java @@ -0,0 +1,37 @@ + +/** + * @author 田中亮汰 + * @version 1.0 + */ + +import java.util.HashMap; +import java.util.Map; + +public class Supermarket { + String name; + int id; + Map prices; + + public Supermarket(String name, int id) { + this.name = name; + this.id = id; + this.prices = new HashMap<>(); + } + + void addPrice(Vegetable vegetable, int price) { + prices.put(vegetable, price); + } + + int getPrice(Vegetable vegetable) { + return prices.get(vegetable); + } + + public String getName() { + return name; + } + + public int getId() { + return id; + } + +} \ No newline at end of file diff --git a/rtanaka/src/Vegetable.java b/rtanaka/src/Vegetable.java new file mode 100644 index 0000000..885a901 --- /dev/null +++ b/rtanaka/src/Vegetable.java @@ -0,0 +1,24 @@ + +/** + * @author 田中亮汰 + * @version 1.0 + */ + +public class Vegetable { + String name; + int id; + + public Vegetable(String name, int id) { + this.name = name; + this.id = id; + } + + public String getName() { + return name; + } + + public int getId() { + return id; + } + +} \ No newline at end of file diff --git a/rtanaka/test/SaveMoneyCalculationTest.java b/rtanaka/test/SaveMoneyCalculationTest.java new file mode 100644 index 0000000..f147212 --- /dev/null +++ b/rtanaka/test/SaveMoneyCalculationTest.java @@ -0,0 +1,89 @@ + +/** + * @author 田中亮汰 + * @version 1.0 + */ + +import static org.junit.jupiter.api.Assertions.*; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class SaveMoneyCalculationTest { + + //使用するオブジェクトをフィールド変数として宣言 + private List supermarkets; + private List vegetables; + private SaveMoneyCalculation saveMoneyCal; + private Vegetable carrot; + private Vegetable potato; + private Vegetable onion; + + /** + * オブジェクトの初期化を行うメソッド + */ + @BeforeEach + void setUp() { + supermarkets = new ArrayList<>(); + vegetables = new ArrayList<>(); + + // 野菜の作成 + carrot = new Vegetable("Carrot", 0); + potato = new Vegetable("Potato", 1); + onion = new Vegetable("Onion", 2); + vegetables.add(carrot); + vegetables.add(potato); + vegetables.add(onion); + + // スーパーマーケットの作成と価格の追加 + Supermarket superA = new Supermarket("superA", 0); + superA.addPrice(carrot, 100); + superA.addPrice(potato, 150); + superA.addPrice(onion, 120); + supermarkets.add(superA); + + Supermarket superB = new Supermarket("superB", 1); + superB.addPrice(carrot, 90); // 最も安いニンジン + superB.addPrice(potato, 160); + superB.addPrice(onion, 130); + supermarkets.add(superB); + + Supermarket superC = new Supermarket("superC", 2); + superC.addPrice(carrot, 110); + superC.addPrice(potato, 140); // 最も安いジャガイモ + superC.addPrice(onion, 110); // 最も安いタマネギ + supermarkets.add(superC); + + saveMoneyCal = new SaveMoneyCalculation(supermarkets, vegetables); + } + + @Test + public void コンストラクタのテスト() { + assertEquals(saveMoneyCal.vegetables.get(0).name, ("Carrot")); + assertEquals(saveMoneyCal.supermarkets.get(0).name, ("superA")); + } + + @Test + public void その野菜が一番安いスーパーを返すメソッドのテスト() { + Map cheapestMap = saveMoneyCal.findCheapestSupermarketsForVegetables(); + + // superBのニンジンが最も安いことを確認 + assertEquals("superB", cheapestMap.get(carrot).getName()); + + // superCのジャガイモが最も安いことを確認 + assertEquals("superC", cheapestMap.get(potato).getName()); + + // superCのタマネギが最も安いことを確認 + assertEquals("superC", cheapestMap.get(onion).getName()); + } + + @Test + public void スーパーをいくつ回るか返すメソッドのテスト() { + int minimumSupermarketsHopping = saveMoneyCal.calculateSupermarketsHopping(); + assertEquals(2, minimumSupermarketsHopping); + } +} -- GitLab From 96f81f238a9b4667782d76dc7c8690c15e6de375 Mon Sep 17 00:00:00 2001 From: rtanaka Date: Thu, 24 Jul 2025 14:37:57 +0900 Subject: [PATCH 2/2] =?UTF-8?q?paiza=E3=81=AE=E5=95=8F=E9=A1=8CB157?= =?UTF-8?q?=E3=81=AE=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rtanaka/src/Main_B157.java | 16 +- rtanaka/src/SaveMoneyCalculation.java | 28 ++- rtanaka/src/Supermarket.java | 12 +- rtanaka/src/Vegetable.java | 10 +- rtanaka/test/SaveMoneyCalculationTest.java | 207 ++++++++++++++------- 5 files changed, 176 insertions(+), 97 deletions(-) diff --git a/rtanaka/src/Main_B157.java b/rtanaka/src/Main_B157.java index 30f76f3..3cd537b 100644 --- a/rtanaka/src/Main_B157.java +++ b/rtanaka/src/Main_B157.java @@ -16,20 +16,20 @@ public class Main_B157 { List vegetables = new ArrayList<>(); Scanner sc = new Scanner(System.in); - int supermarketNum = sc.nextInt();// スーパーマーケットの店舗数 - int vegetableNum = sc.nextInt();// 野菜の個数 + int supermarketCount = sc.nextInt();// スーパーマーケットの店舗数 + int vegetableCount = sc.nextInt();// 野菜の個数 // 野菜に番号(名称)をつけてリストに格納 - for (int vegetableCount = 0; vegetableCount < vegetableNum; vegetableCount++) { - vegetables.add(new Vegetable("Vegetable_" + vegetableCount, vegetableCount)); + for (int vegetableNum = 0; vegetableNum < vegetableCount; vegetableNum++) { + vegetables.add(new Vegetable("Vegetable_" + vegetableNum)); } // 野菜の番号と値段を対応する新しく作ったスーパーマーケットオブジェクトの // フィールドに保存していく.その後このオブジェクトをリストに格納する - for (int supermarketCount = 0; supermarketCount < supermarketNum; supermarketCount++) { - Supermarket currentSupermarket = new Supermarket("Supermarket_" + supermarketCount, supermarketCount); - for (int vegetableCount = 0; vegetableCount < vegetableNum; vegetableCount++) { + for (int supermarketNum = 0; supermarketNum < supermarketCount; supermarketNum++) { + Supermarket currentSupermarket = new Supermarket("Supermarket_" + supermarketNum); + for (int vegetableNum = 0; vegetableNum < vegetableCount; vegetableNum++) { int price = sc.nextInt(); - currentSupermarket.addPrice(vegetables.get(vegetableCount), price); + currentSupermarket.addPrice(vegetables.get(vegetableNum), price); } supermarkets.add(currentSupermarket); } diff --git a/rtanaka/src/SaveMoneyCalculation.java b/rtanaka/src/SaveMoneyCalculation.java index db41de2..c38c2eb 100644 --- a/rtanaka/src/SaveMoneyCalculation.java +++ b/rtanaka/src/SaveMoneyCalculation.java @@ -11,12 +11,12 @@ import java.util.Map; import java.util.Set; public class SaveMoneyCalculation { - List supermarkets; - List vegetables; + private List supermarkets; + private List vegetables; public SaveMoneyCalculation(List supermarkets, List vegetables) { - this.supermarkets = supermarkets; - this.vegetables = vegetables; + this.setSupermarkets(supermarkets); + this.setVegetables(vegetables); } /** @@ -28,11 +28,11 @@ public class SaveMoneyCalculation { public Map findCheapestSupermarketsForVegetables() { Map cheapestSupermarkets = new HashMap<>(); //vegetablesリストの中身と - for (Vegetable vegetable : this.vegetables) { + for (Vegetable vegetable : this.getVegetables()) { int minPrice = Integer.MAX_VALUE;//ループ毎に別の野菜を比較するためここで初期化 Supermarket cheapestSupermarket = null; - for (Supermarket supermarket : this.supermarkets) { + for (Supermarket supermarket : this.getSupermarkets()) { int currentPrice = supermarket.getPrice(vegetable); if (currentPrice < minPrice) { @@ -62,4 +62,20 @@ public class SaveMoneyCalculation { return uniqueSupermarkets.size(); } + + public List getVegetables() { + return vegetables; + } + + public void setVegetables(List vegetables) { + this.vegetables = vegetables; + } + + public List getSupermarkets() { + return supermarkets; + } + + public void setSupermarkets(List supermarkets) { + this.supermarkets = supermarkets; + } } \ No newline at end of file diff --git a/rtanaka/src/Supermarket.java b/rtanaka/src/Supermarket.java index c395107..57faa39 100644 --- a/rtanaka/src/Supermarket.java +++ b/rtanaka/src/Supermarket.java @@ -8,13 +8,11 @@ import java.util.HashMap; import java.util.Map; public class Supermarket { - String name; - int id; - Map prices; + private String name; + private Map prices; - public Supermarket(String name, int id) { + public Supermarket(String name) { this.name = name; - this.id = id; this.prices = new HashMap<>(); } @@ -30,8 +28,4 @@ public class Supermarket { return name; } - public int getId() { - return id; - } - } \ No newline at end of file diff --git a/rtanaka/src/Vegetable.java b/rtanaka/src/Vegetable.java index 885a901..d52534d 100644 --- a/rtanaka/src/Vegetable.java +++ b/rtanaka/src/Vegetable.java @@ -5,20 +5,14 @@ */ public class Vegetable { - String name; - int id; + private String name; - public Vegetable(String name, int id) { + public Vegetable(String name) { this.name = name; - this.id = id; } public String getName() { return name; } - public int getId() { - return id; - } - } \ No newline at end of file diff --git a/rtanaka/test/SaveMoneyCalculationTest.java b/rtanaka/test/SaveMoneyCalculationTest.java index f147212..6c6bd47 100644 --- a/rtanaka/test/SaveMoneyCalculationTest.java +++ b/rtanaka/test/SaveMoneyCalculationTest.java @@ -10,80 +10,155 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; -import org.junit.jupiter.api.BeforeEach; +import org.junit.experimental.runners.Enclosed; import org.junit.jupiter.api.Test; +import org.junit.runner.RunWith; +@RunWith(Enclosed.class) public class SaveMoneyCalculationTest { - - //使用するオブジェクトをフィールド変数として宣言 - private List supermarkets; - private List vegetables; - private SaveMoneyCalculation saveMoneyCal; - private Vegetable carrot; - private Vegetable potato; - private Vegetable onion; - - /** - * オブジェクトの初期化を行うメソッド - */ - @BeforeEach - void setUp() { - supermarkets = new ArrayList<>(); - vegetables = new ArrayList<>(); - - // 野菜の作成 - carrot = new Vegetable("Carrot", 0); - potato = new Vegetable("Potato", 1); - onion = new Vegetable("Onion", 2); - vegetables.add(carrot); - vegetables.add(potato); - vegetables.add(onion); - - // スーパーマーケットの作成と価格の追加 - Supermarket superA = new Supermarket("superA", 0); - superA.addPrice(carrot, 100); - superA.addPrice(potato, 150); - superA.addPrice(onion, 120); - supermarkets.add(superA); - - Supermarket superB = new Supermarket("superB", 1); - superB.addPrice(carrot, 90); // 最も安いニンジン - superB.addPrice(potato, 160); - superB.addPrice(onion, 130); - supermarkets.add(superB); - - Supermarket superC = new Supermarket("superC", 2); - superC.addPrice(carrot, 110); - superC.addPrice(potato, 140); // 最も安いジャガイモ - superC.addPrice(onion, 110); // 最も安いタマネギ - supermarkets.add(superC); - - saveMoneyCal = new SaveMoneyCalculation(supermarkets, vegetables); - } - @Test - public void コンストラクタのテスト() { - assertEquals(saveMoneyCal.vegetables.get(0).name, ("Carrot")); - assertEquals(saveMoneyCal.supermarkets.get(0).name, ("superA")); - } + public static class SaveMoneyCalculationFindCheapestMarketTest { + + // 使用するオブジェクトをフィールド変数として宣言 + private List supermarkets; + private List vegetables; + private SaveMoneyCalculation saveMoneyCal; + private Vegetable carrot; + private Vegetable potato; + private Vegetable onion; + + @Test + public void 価格に対して正しい店Aが出力されているかのテスト() { + supermarkets = new ArrayList<>(); + vegetables = new ArrayList<>(); + + // 野菜の作成 + carrot = new Vegetable("Carrot"); + vegetables.add(carrot); + + // スーパーマーケットの作成と価格の追加 + Supermarket superA = new Supermarket("superA"); + superA.addPrice(carrot, 100);// 最も安いニンジン + supermarkets.add(superA); + + Supermarket superB = new Supermarket("superB"); + superB.addPrice(carrot, 110); + supermarkets.add(superB); + + Supermarket superC = new Supermarket("superC"); + superC.addPrice(carrot, 120); + supermarkets.add(superC); + + saveMoneyCal = new SaveMoneyCalculation(supermarkets, vegetables); + Map cheapestMap = saveMoneyCal.findCheapestSupermarketsForVegetables(); + + // superAのニンジンが最も安いことを確認 + assertEquals("superA", cheapestMap.get(carrot).getName()); + } + + @Test + public void 価格に対して正しい店Bが出力されているかのテスト() { + supermarkets = new ArrayList<>(); + vegetables = new ArrayList<>(); + + // 野菜の作成 + potato = new Vegetable("Potato"); + vegetables.add(potato); + + // スーパーマーケットの作成と価格の追加 + Supermarket superA = new Supermarket("superA"); + superA.addPrice(potato, 120); + supermarkets.add(superA); + + Supermarket superB = new Supermarket("superB"); + superB.addPrice(potato, 100); // 最も安いジャガイモ + supermarkets.add(superB); - @Test - public void その野菜が一番安いスーパーを返すメソッドのテスト() { - Map cheapestMap = saveMoneyCal.findCheapestSupermarketsForVegetables(); - - // superBのニンジンが最も安いことを確認 - assertEquals("superB", cheapestMap.get(carrot).getName()); + Supermarket superC = new Supermarket("superC"); + superC.addPrice(potato, 110); + supermarkets.add(superC); - // superCのジャガイモが最も安いことを確認 - assertEquals("superC", cheapestMap.get(potato).getName()); + saveMoneyCal = new SaveMoneyCalculation(supermarkets, vegetables); + Map cheapestMap = saveMoneyCal.findCheapestSupermarketsForVegetables(); + // superBのジャガイモが最も安いことを確認 + assertEquals("superB", cheapestMap.get(potato).getName()); + } - // superCのタマネギが最も安いことを確認 - assertEquals("superC", cheapestMap.get(onion).getName()); + @Test + public void 価格に対して正しい店Cが出力されているかのテスト() { + supermarkets = new ArrayList<>(); + vegetables = new ArrayList<>(); + + // 野菜の作成 + onion = new Vegetable("Onion"); + vegetables.add(onion); + + // スーパーマーケットの作成と価格の追加 + Supermarket superA = new Supermarket("superA"); + superA.addPrice(onion, 110); + supermarkets.add(superA); + + Supermarket superB = new Supermarket("superB"); + superB.addPrice(onion, 120); + supermarkets.add(superB); + + Supermarket superC = new Supermarket("superC"); + superC.addPrice(onion, 100); // 最も安いタマネギ + supermarkets.add(superC); + + saveMoneyCal = new SaveMoneyCalculation(supermarkets, vegetables); + Map cheapestMap = saveMoneyCal.findCheapestSupermarketsForVegetables(); + // superCのタマネギが最も安いことを確認 + assertEquals("superC", cheapestMap.get(onion).getName()); + } } - @Test - public void スーパーをいくつ回るか返すメソッドのテスト() { - int minimumSupermarketsHopping = saveMoneyCal.calculateSupermarketsHopping(); - assertEquals(2, minimumSupermarketsHopping); + public static class SaveMoneyCalculationHoppingTest { + + // 使用するオブジェクトをフィールド変数として宣言 + private List supermarkets; + private List vegetables; + private SaveMoneyCalculation saveMoneyCal; + private Vegetable carrot; + private Vegetable potato; + private Vegetable onion; + + @Test + public void スーパーをいくつ回るか返すメソッドのテスト() { + supermarkets = new ArrayList<>(); + vegetables = new ArrayList<>(); + + // 野菜の作成 + carrot = new Vegetable("Carrot"); + potato = new Vegetable("Potato"); + onion = new Vegetable("Onion"); + vegetables.add(carrot); + vegetables.add(potato); + vegetables.add(onion); + + // スーパーマーケットの作成と価格の追加 + Supermarket superA = new Supermarket("superA"); + superA.addPrice(carrot, 100); + superA.addPrice(potato, 150); + superA.addPrice(onion, 120); + supermarkets.add(superA); + + Supermarket superB = new Supermarket("superB"); + superB.addPrice(carrot, 90); + superB.addPrice(potato, 160); + superB.addPrice(onion, 130); + supermarkets.add(superB); + + Supermarket superC = new Supermarket("superC"); + superC.addPrice(carrot, 110); + superC.addPrice(potato, 140); + superC.addPrice(onion, 110); + supermarkets.add(superC); + + saveMoneyCal = new SaveMoneyCalculation(supermarkets, vegetables); + + int minimumSupermarketsHopping = saveMoneyCal.calculateSupermarketsHopping(); + assertEquals(2, minimumSupermarketsHopping); + } } } -- GitLab