diff --git a/knoda/src/B084_RecommendationStore.java b/knoda/src/B084_RecommendationStore.java new file mode 100644 index 0000000000000000000000000000000000000000..ee69a37a779a7a3ca73a3a4109f422d50cf75778 --- /dev/null +++ b/knoda/src/B084_RecommendationStore.java @@ -0,0 +1,140 @@ +package hellojunit; + +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; +import java.util.Set; +import java.util.TreeSet; + +public class B084_RecommendationStore { + + public static void main(String[] args) { + Scanner scan = new Scanner(System.in); + + /* + * storeNum : お店の数 + * otherUserNum : 自分以外のユーザーの数 + * criterionNum : 自分と好みが似ていると言える基準の数 + */ + final int storeNum = scan.nextInt(); + final int otherUserNum = scan.nextInt(); + final int criterionNum = scan.nextInt(); + + /* + * それぞれのお店に対する自分の評価を入力する + * oneselfEvaluation : それぞれのお店に対する自分の評価を格納したリスト + */ + final List oneselfEvaluation = new ArrayList(); + for (int store = 0; store < storeNum; store++) { + oneselfEvaluation.add(scan.nextInt()); + } + + /* + * それぞれのお店に対する自分以外のユーザーの評価を入力する + * otherEvaluation : それぞれのお店に対する自分以外のユーザーの評価を格納したリスト + */ + final List> otherEvaluation = new ArrayList>(); + for (int user = 0; user < otherUserNum; user++) { + final List userEvaluation = new ArrayList(); + for (int store = 0; store < storeNum; store++) { + userEvaluation.add(scan.nextInt()); + } + otherEvaluation.add(userEvaluation); + } + + RecommendationStore recommendationStore = new RecommendationStore(); + // おすすめのお店を取得する + final Set storeSet = recommendationStore.getRecommendStore(oneselfEvaluation, otherEvaluation, criterionNum); + // おすすめのお店を出力する + if (storeSet != null) { + int count = 0; + for (Integer store : storeSet) { + if (count != storeSet.size() - 1) { + System.out.print((store + 1) + " "); + } else { + System.out.println(store + 1); + } + count++; + } + } else { + System.out.println("no"); + } + scan.close(); + } + +} + +// おすすめのお店を算出するクラス +class RecommendationStore { + private static final int MAX_EVALUATION = 3; // 評価が3のとき + private static final int ZERO_EVALUATION = 0; // 評価が0のとき + + // おすすめのお店を取得するメソッド + public Set getRecommendStore(final List oneselfEvaluation, final List> otherEvaluation, + final int criterionNum) { + + /* + * oneselfMaxEvaluationStore : 自分が3と評価したお店を格納するリスト + * resembleUser : 自分と好みが似ているユーザを格納したリスト + */ + + // 自分が3と評価したお店を取得する + final List oneselfMaxEvaluationStore = getEvaluationStore(oneselfEvaluation, MAX_EVALUATION); + final List resembleUser = new ArrayList(); + + // 自分と好みが似ているユーザを算出する + for (int user = 0; user < otherEvaluation.size(); user++) { + int count = 0; + for (Integer store : oneselfMaxEvaluationStore) { + if (otherEvaluation.get(user).get(store) == MAX_EVALUATION) { + // 自分とあるユーザの両方が3と評価したお店の個数を算出する + count++; + } + } + + if (count >= criterionNum) { + // 自分とあるユーザの両方が3と評価したお店の個数が基準値以上のとき + resembleUser.add(user); + } + } + + if (resembleUser.size() != 0) { + // 自分と好みが似ているユーザが1人以上存在するとき + + /* + * zeroEvaluationStore : 自分が0と評価したお店を格納したリスト + * recommendStore : おすすめのお店を格納したセット + */ + final List zeroEvaluationStore = getEvaluationStore(oneselfEvaluation, ZERO_EVALUATION); + final Set recommendStore = new TreeSet(); + + // おすすめのお店を算出する + for (Integer user : resembleUser) { + for (Integer store : zeroEvaluationStore) { + if (otherEvaluation.get(user).get(store) == MAX_EVALUATION) { + recommendStore.add(store); + } + } + } + + if (recommendStore.size() == 0) { + // おすすめのお店が存在しないとき + return null; + } + + return recommendStore; + } + return null; + } + + // 自分が評価したお店を取得する + private List getEvaluationStore(final List oneselfEvaluation, final int evaluation) { + final List evaluationStore = new ArrayList(); + for (int store = 0; store < oneselfEvaluation.size(); store++) { + if (oneselfEvaluation.get(store) == evaluation) { + evaluationStore.add(store); + } + } + return evaluationStore; + } +} diff --git a/knoda/test/B084_RecommendationStoreTest.java b/knoda/test/B084_RecommendationStoreTest.java new file mode 100644 index 0000000000000000000000000000000000000000..372c0b5fb306de26e218f05c3b924d899a5bccbf --- /dev/null +++ b/knoda/test/B084_RecommendationStoreTest.java @@ -0,0 +1,83 @@ +package hellojunit; + +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.*; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Set; + +import org.junit.Test; + +public class B084_RecommendationStoreTest { + int criterionNum; + List oneselfEvaluation; + List> otherEvaluation; + RecommendationStore sut; + + @Test + public void 自分が3と評価したお店が基準値を満たしていないときはnoと出力() { + criterionNum = 2; + oneselfEvaluation = new ArrayList(Arrays.asList(1, 3, 0, 0, 2)); + otherEvaluation = new ArrayList>(Arrays.asList( + new ArrayList(Arrays.asList(1, 3, 3, 0, 2)), + new ArrayList(Arrays.asList(2, 3, 1, 0, 2)), + new ArrayList(Arrays.asList(1, 3, 0, 0, 2)) + )); + sut = new RecommendationStore(); + Set actual = sut.getRecommendStore(oneselfEvaluation, otherEvaluation, criterionNum); + Set expected = null; + assertThat(actual, is(expected)); + } + + @Test + public void 自分が3と評価したお店が基準値を満たしているときお店の番号が出力されるテスト() { + criterionNum = 3; + oneselfEvaluation = new ArrayList(Arrays.asList(0, 3, 3, 3, 0)); + otherEvaluation = new ArrayList>(Arrays.asList( + new ArrayList(Arrays.asList(1, 3, 3, 3, 3)), + new ArrayList(Arrays.asList(2, 3, 1, 0, 2)), + new ArrayList(Arrays.asList(1, 3, 3, 3, 2)) + )); + sut = new RecommendationStore(); + Set actualSet = sut.getRecommendStore(oneselfEvaluation, otherEvaluation, criterionNum); + List actualList = new ArrayList(actualSet); + List expectedList = new ArrayList(Arrays.asList(5)); + + for (int i = 0; i < actualList.size(); i++) { + assertThat(actualList.get(i) + 1, is(expectedList.get(i))); + } + } + + @Test + public void 自分が3と評価したお店が基準値を満たしているときnoが出力されるテスト() { + criterionNum = 2; + oneselfEvaluation = new ArrayList(Arrays.asList(1, 3, 0, 3, 2)); + otherEvaluation = new ArrayList>(Arrays.asList( + new ArrayList(Arrays.asList(1, 0, 1, 0, 1)), + new ArrayList(Arrays.asList(3, 0, 2, 0, 1)), + new ArrayList(Arrays.asList(3, 0, 3, 0, 3)) + )); + sut = new RecommendationStore(); + Set actual = sut.getRecommendStore(oneselfEvaluation, otherEvaluation, criterionNum); + Set expected = null; + assertThat(actual, is(expected)); + } + + @Test + public void 好みのユーザが自分の行ったことのないお店に対して3と評価していないときはnoと出力() { + criterionNum = 2; + oneselfEvaluation = new ArrayList(Arrays.asList(1, 3, 3, 0, 0)); + otherEvaluation = new ArrayList>(Arrays.asList( + new ArrayList(Arrays.asList(2, 3, 3, 0, 2)), + new ArrayList(Arrays.asList(2, 3, 1, 0, 2)), + new ArrayList(Arrays.asList(1, 3, 3, 1, 2)) + )); + sut = new RecommendationStore(); + Set actual = sut.getRecommendStore(oneselfEvaluation, otherEvaluation, criterionNum); + Set expected = null; + assertThat(actual, is(expected)); + } + +}