-
Notifications
You must be signed in to change notification settings - Fork 28
[BE5] 박종현 #6
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?
[BE5] 박종현 #6
Changes from 2 commits
1d41a95
1334ba7
964bc42
de81d87
b24dd9a
d24b097
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| # code-review |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| { "id": 1, "proverb": "언젠가 오늘은 간다", "author": "박종현1" } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| { "id": 2, "proverb": "언젠가 내일은 오겠지", "author": "박종현2" } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| { "id": 3, "proverb": "어제는 왔었네?", "author": "박종현3" } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| { "id": 4, "proverb": "하늘은 푸르다", "author": "작자미상" } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| { "id": 5, "proverb": "하늘은 어둡다", "author": "작자미상" } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| { "id": 6, "proverb": "별이 쏟아진다", "author": "작자미상" } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| { "id": 7, "proverb": "달이 빛난다", "author": "박종현4" } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| { "id": 8, "proverb": "매일은 매일우유", "author": "메일우유" } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| [ | ||
| { | ||
| "id": 1, | ||
| "proverb": "언젠가 오늘은 간다", | ||
| "author": "종현1" | ||
| }, | ||
| { | ||
| "id": 2, | ||
| "proverb": "언젠가 내일은 오겠지", | ||
| "author": "종현2" | ||
| }, | ||
| { | ||
| "id": 3, | ||
| "proverb": "어제는 왔었네?", | ||
| "author": "종현3" | ||
| }, | ||
| { | ||
| "id": 4, | ||
| "proverb": "하늘은 푸르다", | ||
| "author": "자미상" | ||
| }, | ||
| { | ||
| "id": 5, | ||
| "proverb": "하늘은 어둡다", | ||
| "author": "자미상" | ||
| }, | ||
| { | ||
| "id": 6, | ||
| "proverb": "별이 쏟아진다", | ||
| "author": "자미상" | ||
| }, | ||
| { | ||
| "id": 7, | ||
| "proverb": "달이 빛난다", | ||
| "author": "종현4" | ||
| }, | ||
| { | ||
| "id": 8, | ||
| "proverb": "매일은 매일우유", | ||
| "author": "일우유" | ||
| } | ||
| ] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| 9 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| 1 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package src.미니과제.App; | ||
|
|
||
|
|
||
| import src.미니과제.BulletinBoardController.BulletinBoardController; | ||
|
|
||
| public class App { | ||
| private final BulletinBoardController controller; | ||
|
|
||
| public App() { | ||
| this.controller = new BulletinBoardController(); | ||
| } | ||
|
|
||
| public void run() { | ||
| controller.start(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| package src.미니과제.BulletinBoardController; | ||
|
|
||
|
|
||
| import src.미니과제.BulletinBoardService.BulletinBoardService; | ||
|
|
||
| import java.util.Scanner; | ||
|
|
||
| public class BulletinBoardController { | ||
| private final BulletinBoardService service; | ||
| private final Scanner scanner; | ||
|
|
||
| public BulletinBoardController() { | ||
| this.service = new BulletinBoardService(); | ||
| this.scanner = new Scanner(System.in); | ||
| } | ||
|
|
||
| public void start() { | ||
| System.out.println("== 명언 =="); | ||
|
|
||
| while (true) { | ||
| System.out.print("명령) "); | ||
| String command = scanner.nextLine(); | ||
|
|
||
| if (command.equals("등록")) { | ||
| registerProverb(); | ||
| } else if (command.equals("목록")) { | ||
| listProverb(); | ||
| } else if (command.startsWith("검색")) { | ||
| searchProverb(command); | ||
| } else if (command.startsWith("삭제?id=")) { | ||
| deleteProverb(command); | ||
| } else if (command.startsWith("수정?id=")) { | ||
| updateProverb(command); | ||
| } else if (command.equals("빌드")) { | ||
| buildDataFile(); // 빌드 명령 처리 | ||
| } else if (command.equals("종료")) { | ||
| System.out.println("프로그램이 종료되었습니다."); | ||
| break; | ||
| } else { | ||
| System.out.println("잘못된 명령어입니다."); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private void registerProverb() { | ||
| System.out.print("명언 : "); | ||
| String proverb = scanner.nextLine(); | ||
|
|
||
| System.out.print("작가 : "); | ||
| String author = scanner.nextLine(); | ||
|
|
||
| if (proverb.trim().isEmpty() || author.trim().isEmpty()) { | ||
| System.out.println("명언과 작가는 반드시 입력해야 합니다."); | ||
| return; | ||
| } | ||
|
|
||
| service.addProverb(proverb, author); | ||
| } | ||
|
|
||
| private void listProverb() { | ||
| System.out.print("페이지 번호 입력: "); | ||
| int page = Integer.parseInt(scanner.nextLine()); | ||
| service.listProverbs(page); | ||
| } | ||
|
|
||
| private void searchProverb(String command) { | ||
| String[] parts = command.split("\\?"); | ||
|
|
||
| if (parts.length < 2) { | ||
| System.out.println("잘못된 검색 명령어입니다. 형식: 검색?keywordType=<검색타입>&keyword=<검색어>"); | ||
| return; | ||
| } | ||
|
|
||
| String[] keywords = parts[1].split("&"); | ||
| if (keywords.length < 2) { | ||
| System.out.println("검색타입 또는 검색어가 누락되었습니다."); | ||
| return; | ||
| } | ||
|
|
||
| String keywordType = keywords[0].split("=")[1]; | ||
| String keyword = keywords[1].split("=")[1]; | ||
| service.searchProverb(keywordType, keyword); | ||
| } | ||
|
|
||
| private void deleteProverb(String command) { | ||
| int id = Integer.parseInt(command.split("=")[1]); | ||
| service.deleteProverb(id); | ||
| } | ||
|
|
||
| private void updateProverb(String command) { | ||
| int id = Integer.parseInt(command.split("=")[1]); | ||
| service.updateProverb(id, scanner); | ||
| } | ||
|
|
||
| private void buildDataFile() { | ||
| service.saveProverbsToData(); // 데이터 파일 빌드 | ||
| System.out.println("data.json 파일이 생성되었습니다."); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| package src.미니과제.BulletinBoardRepository; | ||
|
|
||
| import 미니과제.Proverb; | ||
|
|
||
| import java.io.*; | ||
| import java.util.*; | ||
|
|
||
| public class BulletinBoardRepository { | ||
| private static final String DB = "src/미니과제/db/"; | ||
| private static final String LAST_ID_FILE = DB + "lastId.txt"; | ||
| private static final String DATA_PROVERB_FILE = DB + "data.json"; | ||
|
|
||
| public int readLastId() { | ||
| File file = new File(LAST_ID_FILE); | ||
| if (!file.exists()) return 1; | ||
|
|
||
| try (BufferedReader br = new BufferedReader(new FileReader(file))) { | ||
| return Integer.parseInt(br.readLine()); | ||
| } catch (IOException e) { | ||
| e.printStackTrace(); | ||
| } | ||
| return 1; | ||
| } | ||
|
|
||
| public void saveLastId(int id) { | ||
| try (BufferedWriter bw = new BufferedWriter(new FileWriter(LAST_ID_FILE))) { | ||
| bw.write(String.valueOf(id)); | ||
| } catch (IOException e) { | ||
| e.printStackTrace(); | ||
| } | ||
| } | ||
|
|
||
| public List<Proverb> loadProverbs() { | ||
| List<Proverb> proverbs = new ArrayList<>(); | ||
| File folder = new File(DB); | ||
|
|
||
| File[] files = folder.listFiles((dir, name) -> name.endsWith(".json")); | ||
| if (files != null) { | ||
| for (File file : files) { | ||
| try (BufferedReader br = new BufferedReader(new FileReader(file))) { | ||
| String json = br.readLine(); // 한 줄만 읽기 | ||
| // JSON 형식에서 id, content, author 추출 | ||
| try { | ||
| String idString = json.substring(json.indexOf(":") + 1, json.indexOf(",")).trim(); | ||
| String proverb = json.substring(json.indexOf("proverb") + 11, json.indexOf(",", json.indexOf("proverb")) - 1).trim(); | ||
| String author = json.substring(json.indexOf("author") + 11, json.lastIndexOf("\"")).trim(); | ||
|
|
||
| int id = Integer.parseInt(idString); | ||
| proverbs.add(new Proverb(id, proverb, author)); | ||
| } catch (StringIndexOutOfBoundsException e) { | ||
| System.out.println("JSON 형식이 잘못되었습니다: " + json); | ||
| } | ||
| } catch (IOException e) { | ||
| e.printStackTrace(); | ||
| } | ||
| } | ||
| } | ||
| return proverbs; | ||
| } | ||
|
|
||
| public void saveProverb(Proverb proverb) { | ||
| File file = new File(DB + proverb.getId() + ".json"); | ||
| try (BufferedWriter bw = new BufferedWriter(new FileWriter(file))) { | ||
| String json = "{ \"id\": " + proverb.getId() + ", \"proverb\": \"" + proverb.getProverb() + "\", \"author\": \"" + proverb.getAuthor() + "\" }"; | ||
| bw.write(json); | ||
| } catch (IOException e) { | ||
| e.printStackTrace(); | ||
| } | ||
| } | ||
|
|
||
| public void deleteProverbFile(int id) { | ||
| File file = new File(DB + id + ".json"); | ||
| if (file.exists()) file.delete(); | ||
| } | ||
|
|
||
| public void saveProverbsToData(List<Proverb> proverbsList) { | ||
| try (BufferedWriter bw = new BufferedWriter(new FileWriter(DATA_PROVERB_FILE))) { | ||
| bw.write("[\n"); | ||
| for (int i = 0; i < proverbsList.size(); i++) { | ||
| Proverb proverb = proverbsList.get(i); | ||
| String json = " {\n" + | ||
| " \"id\": " + proverb.getId() + ",\n" + | ||
| " \"proverb\": \"" + proverb.getProverb() + "\",\n" + | ||
| " \"author\": \"" + proverb.getAuthor() + "\"\n" + | ||
| " }"; | ||
| bw.write(json); | ||
| if (i < proverbsList.size() - 1) { | ||
| bw.write(",\n"); | ||
| } | ||
| } | ||
| bw.write("\n]"); | ||
| } catch (IOException e) { | ||
| e.printStackTrace(); | ||
| } | ||
| } | ||
| } | ||
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.
json까지 라이브러리를 사용하지 않고 구현하는 건 생각하지 못했는데 이렇게 하는 게 과제 조건에 더 부합하는 것 같네요!
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.
새삼 라이브러리의 편리성을 느끼게 하는 부분이었습니다..