Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions .idea/.gitignore

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

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

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

6 changes: 6 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.

1 change: 1 addition & 0 deletions db/wiseSaying/1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"author":"작자미상","id":1,"content":"현재를 사랑하라."}
1 change: 1 addition & 0 deletions db/wiseSaying/10.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"author":"작자미상 10","id":10,"content":"명언 10"}
1 change: 1 addition & 0 deletions db/wiseSaying/11.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"author":"작자미상 11","id":11,"content":"명언 11"}
1 change: 1 addition & 0 deletions db/wiseSaying/2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"author":"작자미상 2","id":2,"content":"명언 2"}
1 change: 1 addition & 0 deletions db/wiseSaying/3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"author":"작자미상 3","id":3,"content":"명언 3"}
1 change: 1 addition & 0 deletions db/wiseSaying/4.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"author":"작자미상 4","id":4,"content":"명언 4"}
1 change: 1 addition & 0 deletions db/wiseSaying/5.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"author":"작자미상 5","id":5,"content":"명언 5"}
1 change: 1 addition & 0 deletions db/wiseSaying/6.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"author":"작자미상 6","id":6,"content":"명언 6"}
1 change: 1 addition & 0 deletions db/wiseSaying/7.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"author":"작자미상 7","id":7,"content":"명언 ㅕ7"}
1 change: 1 addition & 0 deletions db/wiseSaying/8.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"author":"작자미상 8","id":8,"content":"명언 8"}
1 change: 1 addition & 0 deletions db/wiseSaying/9.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"author":"작자미상 9","id":9,"content":"명언 9"}
1 change: 1 addition & 0 deletions db/wiseSaying/data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"author":"작자미상 1","id":1,"content":"명언 1"},{"author":"작자미상 2","id":2,"content":"명언 2"},{"author":"작자미상 3","id":3,"content":"명언 3"},{"author":"작자미상 4","id":4,"content":"명언 4"},{"author":"작자미상 5","id":5,"content":"명언 5"},{"author":"작자미상 6","id":6,"content":"명언 6"},{"author":"작자미상 7","id":7,"content":"명언 ㅕ7"},{"author":"작자미상 8","id":8,"content":"명언 8"},{"author":"작자미상 9","id":9,"content":"명언 9"},{"author":"작자미상 10","id":10,"content":"명언 10"},{"author":"작자미상 11","id":11,"content":"명언 11"}]
1 change: 1 addition & 0 deletions db/wiseSaying/lastId.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
11
54 changes: 54 additions & 0 deletions src/main/java/org/example/App.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package org.example.wiseSaying;

import org.json.simple.parser.ParseException;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class App {
private final WiseSayingRepository wiseSayingRepository = new WiseSayingRepository();
private final WiseSayingService wiseSayingService = new WiseSayingService(wiseSayingRepository);
private final WiseSayingController wiseSayingController = new WiseSayingController(wiseSayingService);

public void run() throws IOException, ParseException {
System.out.println("== 명언 앱 ==");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
wiseSayingRepository.initDatabase();

while (true) {
System.out.print("명령) ");
String command = br.readLine();

if (command.equals("종료")) {
System.out.println("명언 앱을 종료합니다.");
break;
}

handleCommand(command, br);
}
}

/// 명령어 핸들러
public void handleCommand(String command, BufferedReader br) throws IOException {
if (command.equals("등록")) {
wiseSayingController.createWiseSaying(br);
} else if (command.startsWith("목록?page=") || command.equals("목록")) {
wiseSayingController.printWiseSayings(command);
} else if (command.startsWith("삭제?id=")) {
int id = Integer.parseInt(command.split("=")[1]);
wiseSayingController.deleteWiseSayingById(id);
} else if (command.startsWith("수정?id=")) {
int id = Integer.parseInt(command.split("=")[1]);
wiseSayingController.updateWiseSayingById(id, br);
} else if (command.equals("빌드")) {
wiseSayingController.buildWiseSaying();
} else if (command.startsWith("목록?keywordType=")) {
String keywordType = command.split("=")[1].split("&")[0];
String keyword = command.split("=")[2];
wiseSayingController.searchWiseSaying(keywordType, keyword);
}else {
System.out.println("알 수 없는 명령입니다.");
}
}
}
12 changes: 12 additions & 0 deletions src/main/java/org/example/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.example;

import org.json.simple.parser.ParseException;

import java.io.IOException;

public class Main {
public static void main(String[] args) throws IOException, ParseException {
App app = new App();
app.run();
}
}
37 changes: 37 additions & 0 deletions src/main/java/org/example/WiseSaying.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.example.wiseSaying;

/**
* 명언 객체
*/

public class WiseSaying {
private final int id;
private String author;
private String content;

public WiseSaying(int id, String author, String content) {
this.id = id;
this.author = author;
this.content = content;
}

public int getId() {
return id;
}

public String getAuthor() {
return author;
}

public String getContent() {
return content;
}

public void setAuthor(String author) {
this.author = author;
}

public void setContent(String content) {
this.content = content;
}
}
109 changes: 109 additions & 0 deletions src/main/java/org/example/WiseSayingController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package org.example.wiseSaying;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.List;

/**
* 컨트롤러
*/

public class WiseSayingController {
private final WiseSayingService wiseSayingService;
private final String DB_PATH = "db/wiseSaying";
private int lastId = 0;

public WiseSayingController(WiseSayingService wiseSayingService) {
this.wiseSayingService = wiseSayingService;
}

/// 빌드 명령어
public void buildWiseSaying() throws IOException {
List<WiseSaying> wiseSayings = wiseSayingService.findAllWiseSayings();

JSONArray jsonArray = new JSONArray();

for (WiseSaying wiseSaying : wiseSayings) {
JSONObject json = new JSONObject();
json.put("id", wiseSaying.getId());
json.put("author", wiseSaying.getAuthor());
json.put("content", wiseSaying.getContent());
jsonArray.add(json);
}

File file = new File(DB_PATH, "data.json");
Files.writeString(file.toPath(), jsonArray.toJSONString());
System.out.println("data.json 파일의 내용이 갱신되었습니다.");
}

/// 명언 등록
public void createWiseSaying(BufferedReader br) throws IOException {
System.out.print("명언: ");
String content = br.readLine();
System.out.print("작가: ");
String author = br.readLine();
lastId++;

WiseSaying wiseSaying = new WiseSaying(lastId, author, content);
wiseSayingService.addWiseSaying(wiseSaying);

System.out.println(lastId + "번 명언이 등록되었습니다.");
}


/// 명언 조회
public void printWiseSayings(String command) {
System.out.println("번호 / 작가 / 명언");
System.out.println("----------------------");

wiseSayingService.printWiseSaying(command);
}

/// 명언 수정
public void updateWiseSayingById(int id, BufferedReader br) throws IOException {
if (id == -1) return;

WiseSaying wiseSaying = wiseSayingService.findWiseSayingById(id);

if (wiseSaying == null) {
System.out.println(id + "번 명언은 존재하지 않습니다.");
}

System.out.println("명언(기존): " + wiseSaying.getContent());
System.out.print("명언: ");
String newContent = br.readLine();
System.out.println("작가(기존): " + wiseSaying.getAuthor());
System.out.print("작가: ");
String newAuthor = br.readLine();

wiseSayingService.updateWiseSaying(wiseSaying, newContent, newAuthor);

System.out.println(id + "번 명언이 수정되었습니다.");
}

/// 명언 삭제
public void deleteWiseSayingById(int id) throws IOException {
if (wiseSayingService.deleteWiseSaying(id)) {
System.out.println(id + "번 명언이 삭제되었습니다.");
} else {
System.out.println(id + "번 명언은 존재하지 않습니다.");
}
}

/// 명언 검색
public void searchWiseSaying(String keywordType, String keyword) {
System.out.println("----------------------");
System.out.println("검색타입 : " + keywordType);
System.out.println("검색어 : " + keyword);
System.out.println("----------------------");
System.out.println("번호 / 작가 / 명언");
System.out.println("----------------------");

wiseSayingService.printWiseSayingByKeyword(keywordType, keyword);
}
}
Loading