diff --git a/rtanaka/src/Main_B157.java b/rtanaka/src/Main_B157.java new file mode 100644 index 0000000000000000000000000000000000000000..3cd537b66528f736ce76543425eeb979a974366b --- /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 supermarketCount = sc.nextInt();// スーパーマーケットの店舗数 + int vegetableCount = sc.nextInt();// 野菜の個数 + + // 野菜に番号(名称)をつけてリストに格納 + for (int vegetableNum = 0; vegetableNum < vegetableCount; vegetableNum++) { + vegetables.add(new Vegetable("Vegetable_" + vegetableNum)); + } + // 野菜の番号と値段を対応する新しく作ったスーパーマーケットオブジェクトの + // フィールドに保存していく.その後このオブジェクトをリストに格納する + 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(vegetableNum), 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 0000000000000000000000000000000000000000..c38c2ebe9020e499fa6c1e6fae608b8a1d63cbdf --- /dev/null +++ b/rtanaka/src/SaveMoneyCalculation.java @@ -0,0 +1,81 @@ + +/** + * @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 { + private List supermarkets; + private List vegetables; + + public SaveMoneyCalculation(List supermarkets, List vegetables) { + this.setSupermarkets(supermarkets); + this.setVegetables(vegetables); + } + + /** + * vegetablesの中身を取り出してsupermarketに渡して値段を比較し、 + * その野菜が最も安いスーパーを求めcheapestSupermarketsに格納し返すメソッド + * + * @return Mapで(key, value) =(野菜,最安スーパー) + */ + public Map findCheapestSupermarketsForVegetables() { + Map cheapestSupermarkets = new HashMap<>(); + //vegetablesリストの中身と + for (Vegetable vegetable : this.getVegetables()) { + int minPrice = Integer.MAX_VALUE;//ループ毎に別の野菜を比較するためここで初期化 + Supermarket cheapestSupermarket = null; + + for (Supermarket supermarket : this.getSupermarkets()) { + 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(); + + } + + 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 new file mode 100644 index 0000000000000000000000000000000000000000..57faa39a1a889a61210c9eac98b7f06cecf9a4dd --- /dev/null +++ b/rtanaka/src/Supermarket.java @@ -0,0 +1,31 @@ + +/** + * @author 田中亮汰 + * @version 1.0 + */ + +import java.util.HashMap; +import java.util.Map; + +public class Supermarket { + private String name; + private Map prices; + + public Supermarket(String name) { + this.name = name; + 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; + } + +} \ No newline at end of file diff --git a/rtanaka/src/Vegetable.java b/rtanaka/src/Vegetable.java new file mode 100644 index 0000000000000000000000000000000000000000..d52534d31fc0d674de26b902ee3c693b36ac59d2 --- /dev/null +++ b/rtanaka/src/Vegetable.java @@ -0,0 +1,18 @@ + +/** + * @author 田中亮汰 + * @version 1.0 + */ + +public class Vegetable { + private String name; + + public Vegetable(String name) { + this.name = name; + } + + public String getName() { + return name; + } + +} \ No newline at end of file diff --git a/rtanaka/test/SaveMoneyCalculationTest.java b/rtanaka/test/SaveMoneyCalculationTest.java new file mode 100644 index 0000000000000000000000000000000000000000..6c6bd477deccb48da09416a42a5bada0f2d7fd41 --- /dev/null +++ b/rtanaka/test/SaveMoneyCalculationTest.java @@ -0,0 +1,164 @@ + +/** + * @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.experimental.runners.Enclosed; +import org.junit.jupiter.api.Test; +import org.junit.runner.RunWith; + +@RunWith(Enclosed.class) +public class SaveMoneyCalculationTest { + + 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); + + Supermarket superC = new Supermarket("superC"); + superC.addPrice(potato, 110); + supermarkets.add(superC); + + saveMoneyCal = new SaveMoneyCalculation(supermarkets, vegetables); + Map cheapestMap = saveMoneyCal.findCheapestSupermarketsForVegetables(); + // superBのジャガイモが最も安いことを確認 + assertEquals("superB", cheapestMap.get(potato).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()); + } + } + + 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); + } + } +}