diff --git "a/v1/backend/JAVA_LV_2_\354\225\214\352\263\240\353\246\254\354\246\230\354\235\230_\352\265\264\353\240\210/docs/README.md" "b/v1/backend/JAVA_LV_2_\354\225\214\352\263\240\353\246\254\354\246\230\354\235\230_\352\265\264\353\240\210/docs/README.md" new file mode 100644 index 0000000..3a7afc2 --- /dev/null +++ "b/v1/backend/JAVA_LV_2_\354\225\214\352\263\240\353\246\254\354\246\230\354\235\230_\352\265\264\353\240\210/docs/README.md" @@ -0,0 +1,51 @@ +## 초기 구상 + +### 1. 상속관계 + +#### - Algorithm을 인터페이스로 활용하는 클래스들 + + 1. BFS + 2. BinarySearch + 3. DFS + 4. DP + 5. TwoPointer + +#### -Algorithm 인터페이스 구조 + + 1. isSolution + - 매개변수로 들어온 알고리즘이 현재 객체와 동일한 유형의 객체인지 확인. + + 2. getSolution + - 알고리즘의 종류별로 알맞은 답을 반환하는 매서드 + -> 따라서 각 클래스들은 반환할 답을 프로퍼티로 가지고 있어야 한다. + +### 2. Wchae 구성 + +#### - 프로퍼티 + + - algorithmMap : 푼 알고리즘과 푼 횟수를 저장하는 HashMap + - unsolvedMessage : 풀지 못한 경우의 반환문 + +#### - 메서드 + + 1. solveAlgorithm : Algorithm 인터페이스를 매개변수로 받아 알고리즘을 풀었는지에 따라 다른 결과 반환 + -> 알고리즘을 처음 map에 넣는 경우 풀 확률을 10%로 두고 저장 + +### 3. main 구성 + +#### enum AlgorithmType + + - 각 알고리즘별로 정수값과 알고리즘 이름을 묶어서 명시해둠 + +#### 프로퍼티 + + - random : 문제를 풀 확률 계산을 위한 난수 생성 객체 + - wchae : 문제를 풀 우주 객체 + - list : 각 알고리즘을 종류별로 가지고 있는 ArrayList + +#### 메서드 + + 1. printAlgorithm : 오늘 어떤 알고리즘을 풀지 고르고 출력하는 메서드 + - 하드코딩된 문자열 대신 enum 객체 사용 + + 2. printSolvedMents : 알고리즘 풀이 후 반환된 응답을 올바르게 출력 diff --git "a/v1/backend/JAVA_LV_2_\354\225\214\352\263\240\353\246\254\354\246\230\354\235\230_\352\265\264\353\240\210/src/main/java/com/example/algorithmbot/BFS.java" "b/v1/backend/JAVA_LV_2_\354\225\214\352\263\240\353\246\254\354\246\230\354\235\230_\352\265\264\353\240\210/src/main/java/com/example/algorithmbot/BFS.java" index 6a84351..831663c 100644 --- "a/v1/backend/JAVA_LV_2_\354\225\214\352\263\240\353\246\254\354\246\230\354\235\230_\352\265\264\353\240\210/src/main/java/com/example/algorithmbot/BFS.java" +++ "b/v1/backend/JAVA_LV_2_\354\225\214\352\263\240\353\246\254\354\246\230\354\235\230_\352\265\264\353\240\210/src/main/java/com/example/algorithmbot/BFS.java" @@ -2,4 +2,20 @@ public class BFS implements Algorithm { + private final String solution; + + BFS() { + solution = "주변부터 둘러보기"; + } + + @Override // 기능 : 컴파일러가 오타 등을 확인함(상위 클래스에 메서드 존재 확인), 가독성과 유지보수 향상 + public boolean isSolution(Algorithm algorithm) { + return (algorithm instanceof BFS); + } + + @Override + public String getSolution() { + return (this.solution); + } + } diff --git "a/v1/backend/JAVA_LV_2_\354\225\214\352\263\240\353\246\254\354\246\230\354\235\230_\352\265\264\353\240\210/src/main/java/com/example/algorithmbot/BinarySearch.java" "b/v1/backend/JAVA_LV_2_\354\225\214\352\263\240\353\246\254\354\246\230\354\235\230_\352\265\264\353\240\210/src/main/java/com/example/algorithmbot/BinarySearch.java" index 1981fb0..83061f5 100644 --- "a/v1/backend/JAVA_LV_2_\354\225\214\352\263\240\353\246\254\354\246\230\354\235\230_\352\265\264\353\240\210/src/main/java/com/example/algorithmbot/BinarySearch.java" +++ "b/v1/backend/JAVA_LV_2_\354\225\214\352\263\240\353\246\254\354\246\230\354\235\230_\352\265\264\353\240\210/src/main/java/com/example/algorithmbot/BinarySearch.java" @@ -2,4 +2,19 @@ public class BinarySearch implements Algorithm { + private final String solution; + + BinarySearch() { + solution = "반띵 ㄱㄱ"; + } + + @Override + public boolean isSolution(Algorithm algorithm) { + return (algorithm instanceof BinarySearch); + } + + @Override + public String getSolution() { + return (this.solution); + } } diff --git "a/v1/backend/JAVA_LV_2_\354\225\214\352\263\240\353\246\254\354\246\230\354\235\230_\352\265\264\353\240\210/src/main/java/com/example/algorithmbot/DFS.java" "b/v1/backend/JAVA_LV_2_\354\225\214\352\263\240\353\246\254\354\246\230\354\235\230_\352\265\264\353\240\210/src/main/java/com/example/algorithmbot/DFS.java" index 3685181..a1a86ab 100644 --- "a/v1/backend/JAVA_LV_2_\354\225\214\352\263\240\353\246\254\354\246\230\354\235\230_\352\265\264\353\240\210/src/main/java/com/example/algorithmbot/DFS.java" +++ "b/v1/backend/JAVA_LV_2_\354\225\214\352\263\240\353\246\254\354\246\230\354\235\230_\352\265\264\353\240\210/src/main/java/com/example/algorithmbot/DFS.java" @@ -2,4 +2,19 @@ public class DFS implements Algorithm { + private final String solution; + + DFS() { + solution = "나는 한 놈씩 조져"; + } + + @Override + public boolean isSolution(Algorithm algorithm) { + return (algorithm instanceof DFS); + } + + @Override + public String getSolution() { + return (this.solution); + } } diff --git "a/v1/backend/JAVA_LV_2_\354\225\214\352\263\240\353\246\254\354\246\230\354\235\230_\352\265\264\353\240\210/src/main/java/com/example/algorithmbot/DP.java" "b/v1/backend/JAVA_LV_2_\354\225\214\352\263\240\353\246\254\354\246\230\354\235\230_\352\265\264\353\240\210/src/main/java/com/example/algorithmbot/DP.java" index 083e387..0dd36fa 100644 --- "a/v1/backend/JAVA_LV_2_\354\225\214\352\263\240\353\246\254\354\246\230\354\235\230_\352\265\264\353\240\210/src/main/java/com/example/algorithmbot/DP.java" +++ "b/v1/backend/JAVA_LV_2_\354\225\214\352\263\240\353\246\254\354\246\230\354\235\230_\352\265\264\353\240\210/src/main/java/com/example/algorithmbot/DP.java" @@ -2,4 +2,20 @@ public class DP implements Algorithm { + private final String solution; + + DP() { + solution = "누가 첫날부터 DP문제를 들고옵니까"; + } + + @Override // 기능 : 컴파일러가 오타 등을 확인함(상위 클래스에 메서드 존재 확인), 가독성과 유지보수 향상 + public boolean isSolution(Algorithm algorithm) { + return (algorithm instanceof DP); + } + + @Override + public String getSolution() { + return (this.solution); + } + } diff --git "a/v1/backend/JAVA_LV_2_\354\225\214\352\263\240\353\246\254\354\246\230\354\235\230_\352\265\264\353\240\210/src/main/java/com/example/algorithmbot/Main.java" "b/v1/backend/JAVA_LV_2_\354\225\214\352\263\240\353\246\254\354\246\230\354\235\230_\352\265\264\353\240\210/src/main/java/com/example/algorithmbot/Main.java" index e203928..45787dd 100644 --- "a/v1/backend/JAVA_LV_2_\354\225\214\352\263\240\353\246\254\354\246\230\354\235\230_\352\265\264\353\240\210/src/main/java/com/example/algorithmbot/Main.java" +++ "b/v1/backend/JAVA_LV_2_\354\225\214\352\263\240\353\246\254\354\246\230\354\235\230_\352\265\264\353\240\210/src/main/java/com/example/algorithmbot/Main.java" @@ -1,8 +1,66 @@ package com.example.algorithmbot; +import java.util.ArrayList; +import java.util.Random; + +enum AlgorithmType { + BFS("BFS"), + DFS("DFS"), + BINARY("BinarySearch"), + DP("DP"), + TWOPOINTER("TwoPointer"); + + private final String type; + + AlgorithmType(String type) { + this.type = type; + } + + public String getType() { + return type; + } +} + public class Main { public static void main(String[] args) { + Random random = new Random(); + Wchae wchae = new Wchae(); + ArrayList list = new ArrayList<>(); + + list.add(new BFS()); + list.add(new DFS()); + list.add(new BinarySearch()); + list.add(new DP()); + list.add(new TwoPointer()); + + for (int i = 0; i < 20; i++) { + int firstAlgorithm, secondAlgorithm; + String firstMsg, secondMsg; + + firstAlgorithm = random.nextInt(list.size()); + secondAlgorithm = random.nextInt(list.size()); + printAlgorithm(firstAlgorithm, secondAlgorithm); + + firstMsg = wchae.solveAlgorithm(list.get(firstAlgorithm)); + secondMsg = wchae.solveAlgorithm(list.get(secondAlgorithm)); + printSolvedMents(firstMsg, secondMsg); + } + } + + static void printAlgorithm(int firstAlgorithm, int secondAlgorithm) { + AlgorithmType[] algorithmTypes = {AlgorithmType.BFS, AlgorithmType.DFS, AlgorithmType.BINARY, AlgorithmType.DP, AlgorithmType.TWOPOINTER}; + String DEFAULT = "오늘의 알고리즘 : "; + String DELIMITER = ", "; + + System.out.println(DEFAULT + algorithmTypes[firstAlgorithm].getType() + DELIMITER + algorithmTypes[secondAlgorithm].getType()); + } + + static void printSolvedMents(String firstMsg, String secondMsg) { + String DEFAULT = "우주 : "; + String DELIMITER = " / "; + System.out.println(DEFAULT + firstMsg + DELIMITER + secondMsg); + System.out.println(); } } diff --git "a/v1/backend/JAVA_LV_2_\354\225\214\352\263\240\353\246\254\354\246\230\354\235\230_\352\265\264\353\240\210/src/main/java/com/example/algorithmbot/TwoPointer.java" "b/v1/backend/JAVA_LV_2_\354\225\214\352\263\240\353\246\254\354\246\230\354\235\230_\352\265\264\353\240\210/src/main/java/com/example/algorithmbot/TwoPointer.java" index 15f5ba3..33cea15 100644 --- "a/v1/backend/JAVA_LV_2_\354\225\214\352\263\240\353\246\254\354\246\230\354\235\230_\352\265\264\353\240\210/src/main/java/com/example/algorithmbot/TwoPointer.java" +++ "b/v1/backend/JAVA_LV_2_\354\225\214\352\263\240\353\246\254\354\246\230\354\235\230_\352\265\264\353\240\210/src/main/java/com/example/algorithmbot/TwoPointer.java" @@ -2,4 +2,20 @@ public class TwoPointer implements Algorithm { + private final String solution; + + TwoPointer() { + solution = "너에게 닿기를…"; + } + + @Override + public boolean isSolution(Algorithm algorithm) { + return (algorithm instanceof TwoPointer); + } + + @Override + public String getSolution() { + return (this.solution); + } + } diff --git "a/v1/backend/JAVA_LV_2_\354\225\214\352\263\240\353\246\254\354\246\230\354\235\230_\352\265\264\353\240\210/src/main/java/com/example/algorithmbot/Wchae.java" "b/v1/backend/JAVA_LV_2_\354\225\214\352\263\240\353\246\254\354\246\230\354\235\230_\352\265\264\353\240\210/src/main/java/com/example/algorithmbot/Wchae.java" index 8e3087a..b116b53 100644 --- "a/v1/backend/JAVA_LV_2_\354\225\214\352\263\240\353\246\254\354\246\230\354\235\230_\352\265\264\353\240\210/src/main/java/com/example/algorithmbot/Wchae.java" +++ "b/v1/backend/JAVA_LV_2_\354\225\214\352\263\240\353\246\254\354\246\230\354\235\230_\352\265\264\353\240\210/src/main/java/com/example/algorithmbot/Wchae.java" @@ -1,5 +1,36 @@ package com.example.algorithmbot; +import java.util.HashMap; +import java.util.Optional; +import java.util.Random; + public class Wchae { + private final HashMap algorithmMap; + private final String unsolvecMessage; + private final Integer HUNDRED_PERCENT; + + public Wchae() { + algorithmMap = new HashMap<>(); + unsolvecMessage = "스트레스 받네…"; + HUNDRED_PERCENT = 10; + } + + public String solveAlgorithm(Algorithm algorithm) { + Random random = new Random(); + + Optional> entryOptional = algorithmMap.entrySet().stream() + .filter(entry -> entry.getKey().isSolution(algorithm)) + .findAny(); + if (entryOptional.isPresent()) { + if (entryOptional.get().getValue() + random.nextInt(HUNDRED_PERCENT) >= HUNDRED_PERCENT) { + algorithmMap.put(algorithm, entryOptional.get().getValue() + 1); + return (algorithm.getSolution()); + } else + return (unsolvecMessage); + } else { + algorithmMap.put(algorithm, 1); + return (unsolvecMessage); + } + } }