Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions .idea/code-review.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions out/production/code-review/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions out/production/code-review/.idea/code-review.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions out/production/code-review/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions out/production/code-review/.idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions out/production/code-review/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions out/production/code-review/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# code-review
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
1 change: 1 addition & 0 deletions out/production/code-review/src/미니과제/db/1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "id": 1, "proverb": "언젠가 오늘은 간다", "author": "박종현1" }
1 change: 1 addition & 0 deletions out/production/code-review/src/미니과제/db/2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "id": 2, "proverb": "언젠가 내일은 오겠지", "author": "박종현2" }
1 change: 1 addition & 0 deletions out/production/code-review/src/미니과제/db/3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "id": 3, "proverb": "어제는 왔었네?", "author": "박종현3" }
1 change: 1 addition & 0 deletions out/production/code-review/src/미니과제/db/4.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "id": 4, "proverb": "하늘은 푸르다", "author": "작자미상" }
1 change: 1 addition & 0 deletions out/production/code-review/src/미니과제/db/5.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "id": 5, "proverb": "하늘은 어둡다", "author": "작자미상" }
1 change: 1 addition & 0 deletions out/production/code-review/src/미니과제/db/6.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "id": 6, "proverb": "별이 쏟아진다", "author": "작자미상" }
1 change: 1 addition & 0 deletions out/production/code-review/src/미니과제/db/7.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "id": 7, "proverb": "달이 빛난다", "author": "박종현4" }
1 change: 1 addition & 0 deletions out/production/code-review/src/미니과제/db/8.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "id": 8, "proverb": "매일은 매일우유", "author": "메일우유" }
42 changes: 42 additions & 0 deletions out/production/code-review/src/미니과제/db/data.json
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": "일우유"
}
]
1 change: 1 addition & 0 deletions out/production/code-review/src/미니과제/db/lastId.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
9
1 change: 1 addition & 0 deletions out/production/code-review/src/미니과제/dblastId.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1
Binary file not shown.
16 changes: 16 additions & 0 deletions src/미니과제/App/App.java
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) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

json까지 라이브러리를 사용하지 않고 구현하는 건 생각하지 못했는데 이렇게 하는 게 과제 조건에 더 부합하는 것 같네요!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

새삼 라이브러리의 편리성을 느끼게 하는 부분이었습니다..

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();
}
}
}
Loading