-
Notifications
You must be signed in to change notification settings - Fork 0
Be/main/yeoshin java lv2 #22
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 : 알고리즘 풀이 후 반환된 응답을 올바르게 출력 |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -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; | ||||||
| } | ||||||
| } | ||||||
|
Comment on lines
+6
to
+22
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. enum 내부 필드 활용 굿 |
||||||
|
|
||||||
| public class Main { | ||||||
|
|
||||||
| public static void main(String[] args) { | ||||||
| Random random = new Random(); | ||||||
| Wchae wchae = new Wchae(); | ||||||
| ArrayList<Algorithm> 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); | ||||||
|
Comment on lines
+41
to
+43
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. int보다 랜덤한 AlgorithmType을 뽑은 후 해당 타입을 넘기는게 좋을 것 같아요 |
||||||
|
|
||||||
| 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}; | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Enum 클래스 내부의 static 상수로 옮겨주세요 |
||||||
| String DEFAULT = "오늘의 알고리즘 : "; | ||||||
| String DELIMITER = ", "; | ||||||
|
|
||||||
| System.out.println(DEFAULT + algorithmTypes[firstAlgorithm].getType() + DELIMITER + algorithmTypes[secondAlgorithm].getType()); | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
처럼 하나의 문자열로 만들어서 넘겨주세요 |
||||||
| } | ||||||
|
|
||||||
| static void printSolvedMents(String firstMsg, String secondMsg) { | ||||||
| String DEFAULT = "우주 : "; | ||||||
| String DELIMITER = " / "; | ||||||
|
|
||||||
| System.out.println(DEFAULT + firstMsg + DELIMITER + secondMsg); | ||||||
| System.out.println(); | ||||||
|
Comment on lines
+63
to
+64
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 상동 |
||||||
| } | ||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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<Algorithm, Integer> 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<HashMap.Entry<Algorithm, Integer>> 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); | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
변수로 둘 거라면 상수화를 추천합니다