diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..79bee6f --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.idea +.out diff --git a/com/ll/wiseSaying/src/App.java b/com/ll/wiseSaying/src/App.java new file mode 100644 index 0000000..62093fb --- /dev/null +++ b/com/ll/wiseSaying/src/App.java @@ -0,0 +1,74 @@ +package com.ll.wiseSaying.src; + +import java.util.Scanner; + +public class App { + public Scanner sc; + static String[] commands = {"등록", "목록", "빌드", "종료"}; + static String patternDelete = "삭제\\?id=\\d+"; + static String patternUpdate = "수정\\?id=\\d+"; + + static boolean commandNeedId = false; + + WiseSayingController wiseSayingController; + + public void run(Scanner sc) { + + this.sc = sc; + wiseSayingController = new WiseSayingController(sc); + System.out.println("== 명언 앱 =="); + while (true) { + System.out.printf("명령) "); + String commandInput = sc.nextLine(); + String command = ""; + int id = 0; + if(!validCommand(commandInput)) { + System.out.println("올바른 명령어가 아닙니다."); + continue; + } + if(commandNeedId){ + command = commandInput.split("\\?")[0]; + id = Integer.parseInt(commandInput.split("=")[1]); + }else{ + command = commandInput; + } + switch (command) { + case "등록": + wiseSayingController.addWiseSaying(); + break; + case "목록": + wiseSayingController.showWiseSaying(); + break; + case "빌드": + wiseSayingController.buildJson(); + break; + case "삭제": + wiseSayingController.removeWiseSaying(id); + break; + case "수정": + wiseSayingController.updateWiseSaying(id); + break; + case "종료": + wiseSayingController.terminate(); + sc.close(); + return; + } + } + } + + public boolean validCommand(String command) { + if(command.length() == 2) { + commandNeedId = false; + for (String c : commands) { + if (c.equals(command)) { + return true; + } + } + } else if (command.matches(patternDelete) || command.matches(patternUpdate)) { + commandNeedId = true; + return true; + } + return false; + } + +} diff --git a/com/ll/wiseSaying/src/Main.java b/com/ll/wiseSaying/src/Main.java new file mode 100644 index 0000000..3b5e5df --- /dev/null +++ b/com/ll/wiseSaying/src/Main.java @@ -0,0 +1,11 @@ +package com.ll.wiseSaying.src; + +import java.util.Scanner; + +public class Main { + public static void main(String[] args) { + App app = new App(); + Scanner scanner = new Scanner(System.in); + app.run(scanner); + } +} diff --git a/com/ll/wiseSaying/src/WiseSaying.java b/com/ll/wiseSaying/src/WiseSaying.java new file mode 100644 index 0000000..b25845e --- /dev/null +++ b/com/ll/wiseSaying/src/WiseSaying.java @@ -0,0 +1,23 @@ +package com.ll.wiseSaying.src; + +import org.json.simple.JSONObject; + +public class WiseSaying { + int id; + String content; + String author; + + public WiseSaying(int id, String content, String author) { + this.id = id; + this.content = content; + this.author = author; + } + + public JSONObject toJson() { + JSONObject obj = new JSONObject(); + obj.put("id", this.id); + obj.put("quote", this.content); + obj.put("author", this.author); + return obj; + } +} diff --git a/com/ll/wiseSaying/src/WiseSayingController.java b/com/ll/wiseSaying/src/WiseSayingController.java new file mode 100644 index 0000000..c3814eb --- /dev/null +++ b/com/ll/wiseSaying/src/WiseSayingController.java @@ -0,0 +1,59 @@ +package com.ll.wiseSaying.src; + +import java.util.Scanner; + +public class WiseSayingController { + WiseSayingService wiseSayingService = new WiseSayingService(); + Scanner sc; + + public WiseSayingController(Scanner sc) { + this.sc = sc; + } + public void addWiseSaying() { + System.out.printf("명언 : "); + String content = sc.nextLine(); + System.out.printf("작가 : "); + String author = sc.nextLine(); + int id = wiseSayingService.addWiseSaying(content, author); + System.out.println(id + "번 명언이 등록되었습니다."); + } + + public void removeWiseSaying(int id) { + try { + wiseSayingService.removeWiseSaying(id); + System.out.println(id + "번째 명언이 삭제되었습니다."); + }catch (Exception e){ + System.out.println(e); + } + + } + + public void updateWiseSaying(int id) { + try { + System.out.printf("명언 : "); + String content = sc.nextLine(); + System.out.printf("작가 : "); + String author = sc.nextLine(); + wiseSayingService.updateWiseSaying(id, content, author); + System.out.println(id + "번 명언이 수정되었습니다."); + } catch (Exception e){ + System.out.println(e); + } + + } + + public void showWiseSaying() { + System.out.println("번호 / 작가 / 명언"); + System.out.println("----------------------"); + System.out.println(wiseSayingService.getAllWiseSaying()); + } + + public void buildJson() { + wiseSayingService.buildJson(); + System.out.println("data.json 파일의 내용이 갱신되었습니다."); + } + + public void terminate(){ + wiseSayingService.saveLastId(); + } +} diff --git a/com/ll/wiseSaying/src/WiseSayingRepository.java b/com/ll/wiseSaying/src/WiseSayingRepository.java new file mode 100644 index 0000000..1195290 --- /dev/null +++ b/com/ll/wiseSaying/src/WiseSayingRepository.java @@ -0,0 +1,32 @@ +package com.ll.wiseSaying.src; +import java.util.HashMap; +import java.util.Map; + +public class WiseSayingRepository { + public Map wiseSayingList = new HashMap<>(); + public int lastId = 0; + + public void addWiseSaying(WiseSaying wiseSaying) { + wiseSayingList.put(wiseSaying.id, wiseSaying); + } + + public void removeWiseSaying(int id) { + wiseSayingList.remove(id); + } + + public WiseSaying getWiseSaying(int id) { + return wiseSayingList.get(id); + } + + public Map getAllWiseSayings() { + return wiseSayingList; + } + + public void updateWiseSaying(int id, String quote, String author) { + wiseSayingList.put(id, new WiseSaying(id, quote, author)); + } + + public int getLastId() { + return lastId; + } +} diff --git a/com/ll/wiseSaying/src/WiseSayingService.java b/com/ll/wiseSaying/src/WiseSayingService.java new file mode 100644 index 0000000..09c9a69 --- /dev/null +++ b/com/ll/wiseSaying/src/WiseSayingService.java @@ -0,0 +1,79 @@ +package com.ll.wiseSaying.src; +import java.io.FileWriter; +import java.io.IOException; +import java.util.Map; + +import org.json.simple.JSONArray; +public class WiseSayingService { + WiseSayingRepository wiseSayingRepository = new WiseSayingRepository(); + static String dirName = "db/wiseSaying/"; + + /* + * @return 생성된 명언의 id + * */ + public int addWiseSaying(String content, String author) { + WiseSaying wiseSaying = new WiseSaying(++wiseSayingRepository.lastId, content, author); + wiseSayingRepository.addWiseSaying(wiseSaying); + saveWiseSayingAsFile(wiseSaying); + return wiseSaying.id; + } + + public void removeWiseSaying(int id) { + if(wiseSayingRepository.getWiseSaying(id) != null){ + wiseSayingRepository.removeWiseSaying(id); + } else { + throw new IllegalArgumentException(String.format("%d번 명언은 존재하지 않습니다.", id)); + } + } + + public void updateWiseSaying(int id, String content, String author) { + if(wiseSayingRepository.getWiseSaying(id) != null){ + wiseSayingRepository.updateWiseSaying(id, content, author); + } else { + throw new IllegalArgumentException(String.format("%d번 명언은 존재하지 않습니다.", id)); + } + } + + public String getAllWiseSaying() { + StringBuilder sb = new StringBuilder(); + Map wiseSayingList = wiseSayingRepository.wiseSayingList; + String result = ""; + for (int i : wiseSayingList.keySet()) { + WiseSaying ws = wiseSayingList.get(i); + result = sb.append(ws.id).append(" / ").append(ws.author).append(" / ").append(ws.content).append("\n").toString(); + } + return result; + } + + public void buildJson() { + String fileName = dirName + "data.json"; + Map wiseSayingList = wiseSayingRepository.wiseSayingList; + JSONArray jsonList = new JSONArray(); + for (int i : wiseSayingList.keySet()) { + jsonList.add(wiseSayingList.get(i).toJson()); + } + saveFile(fileName, jsonList.toJSONString()); + } + + + + public void saveWiseSayingAsFile(WiseSaying wiseSaying) { + String fileName = String.format("%s%d.json", dirName, wiseSaying.id); + saveFile(fileName, wiseSaying.toJson().toJSONString()); + } + public void saveFile(String fileName, String content) { + try { + FileWriter file = new FileWriter(fileName); + file.write(content); + file.flush(); + file.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public void saveLastId() { + String fileName = dirName + "lastId.txt"; + saveFile(fileName, String.valueOf(wiseSayingRepository.lastId)); + } +} diff --git a/com/ll/wiseSaying/test/TestUtil.java b/com/ll/wiseSaying/test/TestUtil.java new file mode 100644 index 0000000..558b454 --- /dev/null +++ b/com/ll/wiseSaying/test/TestUtil.java @@ -0,0 +1,32 @@ +package com.ll.wiseSaying.test; + +import java.io.*; +import java.util.Scanner; + +public class TestUtil { + // gen == generate 생성하다. + // 테스트용 스캐너 생성 + public static Scanner genScanner(final String input) { + final InputStream in = new ByteArrayInputStream(input.getBytes()); + + return new Scanner(in); + } + + // System.out의 출력을 스트림으로 받기 + public static ByteArrayOutputStream setOutToByteArray() { + final ByteArrayOutputStream output = new ByteArrayOutputStream(); + System.setOut(new PrintStream(output)); + + return output; + } + + // setOutToByteArray 함수의 사용을 완료한 후 정리하는 함수, 출력을 다시 정상화 하는 함수 + public static void clearSetOutToByteArray(final ByteArrayOutputStream output) { + System.setOut(new PrintStream(new FileOutputStream(FileDescriptor.out))); + try { + output.close(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } +} diff --git a/com/ll/wiseSaying/test/WiseSayingControllerTest.java b/com/ll/wiseSaying/test/WiseSayingControllerTest.java new file mode 100644 index 0000000..0e53062 --- /dev/null +++ b/com/ll/wiseSaying/test/WiseSayingControllerTest.java @@ -0,0 +1,75 @@ +package com.ll.wiseSaying.test; + +import com.ll.wiseSaying.src.App; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; +import java.util.*; +import java.io.ByteArrayOutputStream; + +public class WiseSayingControllerTest { + App app; + @BeforeEach + void beforeEach() { + app = new App(); + } + + @Test + @DisplayName("등록 명령 처리") + void addWiseSaying() { + String input = """ + 등록 + 현재를 사랑하라. + 작자미상 + 종료 + """; + + Scanner scanner = TestUtil.genScanner(input); + + ByteArrayOutputStream outputStream = TestUtil.setOutToByteArray(); + + app.run(scanner); + String output = outputStream.toString().trim(); + assertThat(output) + .contains("== 명언 앱 ==") + .contains("명언 :") + .contains("작가 :") + .contains("1번 명언이 등록되었습니다."); + TestUtil.clearSetOutToByteArray(outputStream); + } + + @Test + @DisplayName("목록 명령 처리") + void showWiseSaying() { + String input = """ + 등록 + 현재를 사랑하라. + 작자미상 + 등록 + 과거에 집착하지 마라. + 작자미상 + 목록 + 삭제?id=1 + 삭제?id=1 + """; + + Scanner scanner = TestUtil.genScanner(input); + + ByteArrayOutputStream outputStream = TestUtil.setOutToByteArray(); + + app.run(scanner); + String output = outputStream.toString().trim(); + + assertThat(output) + .contains("== 명언 앱 ==") + .contains("1 / 작자미상 / 현재를 사랑하라.") + .contains("2 / 작자미상 / 과거에 집착하지 마라.") + .contains("1번 명언이 삭제되었습니다.") + .contains("1번 명언은 존재하지 않습니다."); + + TestUtil.clearSetOutToByteArray(outputStream); + } + +} diff --git a/db/wiseSaying/1.json b/db/wiseSaying/1.json new file mode 100644 index 0000000..51cb308 --- /dev/null +++ b/db/wiseSaying/1.json @@ -0,0 +1 @@ +{"quote":"현재를 사랑하라.","author":"작자미상","id":1} \ No newline at end of file diff --git a/db/wiseSaying/2.json b/db/wiseSaying/2.json new file mode 100644 index 0000000..b67f41d --- /dev/null +++ b/db/wiseSaying/2.json @@ -0,0 +1 @@ +{"quote":"과거에 집착하지 마라","author":"작자미상","id":2} \ No newline at end of file diff --git a/db/wiseSaying/data.json b/db/wiseSaying/data.json new file mode 100644 index 0000000..17b0b4b --- /dev/null +++ b/db/wiseSaying/data.json @@ -0,0 +1 @@ +[{"quote":"adfadsf","author":"ㅁㅇㄹㅁㅇ","id":1},{"quote":"과거에 집착하지 마라","author":"작자미상","id":2}] \ No newline at end of file diff --git a/db/wiseSaying/lastId.txt b/db/wiseSaying/lastId.txt new file mode 100644 index 0000000..56a6051 --- /dev/null +++ b/db/wiseSaying/lastId.txt @@ -0,0 +1 @@ +1 \ No newline at end of file diff --git "a/out/production/\353\252\205\354\226\270\352\262\214\354\213\234\355\214\220/.gitignore" "b/out/production/\353\252\205\354\226\270\352\262\214\354\213\234\355\214\220/.gitignore" new file mode 100644 index 0000000..79bee6f --- /dev/null +++ "b/out/production/\353\252\205\354\226\270\352\262\214\354\213\234\355\214\220/.gitignore" @@ -0,0 +1,2 @@ +.idea +.out diff --git "a/out/production/\353\252\205\354\226\270\352\262\214\354\213\234\355\214\220/README.md" "b/out/production/\353\252\205\354\226\270\352\262\214\354\213\234\355\214\220/README.md" new file mode 100644 index 0000000..4444cb9 --- /dev/null +++ "b/out/production/\353\252\205\354\226\270\352\262\214\354\213\234\355\214\220/README.md" @@ -0,0 +1 @@ +# code-review \ No newline at end of file diff --git "a/out/production/\353\252\205\354\226\270\352\262\214\354\213\234\355\214\220/com/ll/wiseSaying/src/App.class" "b/out/production/\353\252\205\354\226\270\352\262\214\354\213\234\355\214\220/com/ll/wiseSaying/src/App.class" new file mode 100644 index 0000000..e6fb679 Binary files /dev/null and "b/out/production/\353\252\205\354\226\270\352\262\214\354\213\234\355\214\220/com/ll/wiseSaying/src/App.class" differ diff --git "a/out/production/\353\252\205\354\226\270\352\262\214\354\213\234\355\214\220/com/ll/wiseSaying/src/Main.class" "b/out/production/\353\252\205\354\226\270\352\262\214\354\213\234\355\214\220/com/ll/wiseSaying/src/Main.class" new file mode 100644 index 0000000..e9b913a Binary files /dev/null and "b/out/production/\353\252\205\354\226\270\352\262\214\354\213\234\355\214\220/com/ll/wiseSaying/src/Main.class" differ diff --git "a/out/production/\353\252\205\354\226\270\352\262\214\354\213\234\355\214\220/com/ll/wiseSaying/src/WiseSaying.class" "b/out/production/\353\252\205\354\226\270\352\262\214\354\213\234\355\214\220/com/ll/wiseSaying/src/WiseSaying.class" new file mode 100644 index 0000000..6c06e97 Binary files /dev/null and "b/out/production/\353\252\205\354\226\270\352\262\214\354\213\234\355\214\220/com/ll/wiseSaying/src/WiseSaying.class" differ diff --git "a/out/production/\353\252\205\354\226\270\352\262\214\354\213\234\355\214\220/com/ll/wiseSaying/src/WiseSayingController.class" "b/out/production/\353\252\205\354\226\270\352\262\214\354\213\234\355\214\220/com/ll/wiseSaying/src/WiseSayingController.class" new file mode 100644 index 0000000..d69c8fe Binary files /dev/null and "b/out/production/\353\252\205\354\226\270\352\262\214\354\213\234\355\214\220/com/ll/wiseSaying/src/WiseSayingController.class" differ diff --git "a/out/production/\353\252\205\354\226\270\352\262\214\354\213\234\355\214\220/com/ll/wiseSaying/src/WiseSayingRepository.class" "b/out/production/\353\252\205\354\226\270\352\262\214\354\213\234\355\214\220/com/ll/wiseSaying/src/WiseSayingRepository.class" new file mode 100644 index 0000000..00c9a77 Binary files /dev/null and "b/out/production/\353\252\205\354\226\270\352\262\214\354\213\234\355\214\220/com/ll/wiseSaying/src/WiseSayingRepository.class" differ diff --git "a/out/production/\353\252\205\354\226\270\352\262\214\354\213\234\355\214\220/com/ll/wiseSaying/src/WiseSayingService.class" "b/out/production/\353\252\205\354\226\270\352\262\214\354\213\234\355\214\220/com/ll/wiseSaying/src/WiseSayingService.class" new file mode 100644 index 0000000..16fe4a8 Binary files /dev/null and "b/out/production/\353\252\205\354\226\270\352\262\214\354\213\234\355\214\220/com/ll/wiseSaying/src/WiseSayingService.class" differ diff --git "a/out/production/\353\252\205\354\226\270\352\262\214\354\213\234\355\214\220/com/ll/wiseSaying/test/TestUtil.class" "b/out/production/\353\252\205\354\226\270\352\262\214\354\213\234\355\214\220/com/ll/wiseSaying/test/TestUtil.class" new file mode 100644 index 0000000..14bd934 Binary files /dev/null and "b/out/production/\353\252\205\354\226\270\352\262\214\354\213\234\355\214\220/com/ll/wiseSaying/test/TestUtil.class" differ diff --git "a/out/production/\353\252\205\354\226\270\352\262\214\354\213\234\355\214\220/com/ll/wiseSaying/test/WiseSayingControllerTest.class" "b/out/production/\353\252\205\354\226\270\352\262\214\354\213\234\355\214\220/com/ll/wiseSaying/test/WiseSayingControllerTest.class" new file mode 100644 index 0000000..8410185 Binary files /dev/null and "b/out/production/\353\252\205\354\226\270\352\262\214\354\213\234\355\214\220/com/ll/wiseSaying/test/WiseSayingControllerTest.class" differ diff --git "a/out/production/\353\252\205\354\226\270\352\262\214\354\213\234\355\214\220/db/wiseSaying/1.json" "b/out/production/\353\252\205\354\226\270\352\262\214\354\213\234\355\214\220/db/wiseSaying/1.json" new file mode 100644 index 0000000..51cb308 --- /dev/null +++ "b/out/production/\353\252\205\354\226\270\352\262\214\354\213\234\355\214\220/db/wiseSaying/1.json" @@ -0,0 +1 @@ +{"quote":"현재를 사랑하라.","author":"작자미상","id":1} \ No newline at end of file diff --git "a/out/production/\353\252\205\354\226\270\352\262\214\354\213\234\355\214\220/db/wiseSaying/2.json" "b/out/production/\353\252\205\354\226\270\352\262\214\354\213\234\355\214\220/db/wiseSaying/2.json" new file mode 100644 index 0000000..b67f41d --- /dev/null +++ "b/out/production/\353\252\205\354\226\270\352\262\214\354\213\234\355\214\220/db/wiseSaying/2.json" @@ -0,0 +1 @@ +{"quote":"과거에 집착하지 마라","author":"작자미상","id":2} \ No newline at end of file diff --git "a/out/production/\353\252\205\354\226\270\352\262\214\354\213\234\355\214\220/db/wiseSaying/data.json" "b/out/production/\353\252\205\354\226\270\352\262\214\354\213\234\355\214\220/db/wiseSaying/data.json" new file mode 100644 index 0000000..17b0b4b --- /dev/null +++ "b/out/production/\353\252\205\354\226\270\352\262\214\354\213\234\355\214\220/db/wiseSaying/data.json" @@ -0,0 +1 @@ +[{"quote":"adfadsf","author":"ㅁㅇㄹㅁㅇ","id":1},{"quote":"과거에 집착하지 마라","author":"작자미상","id":2}] \ No newline at end of file diff --git "a/out/production/\353\252\205\354\226\270\352\262\214\354\213\234\355\214\220/db/wiseSaying/lastId.txt" "b/out/production/\353\252\205\354\226\270\352\262\214\354\213\234\355\214\220/db/wiseSaying/lastId.txt" new file mode 100644 index 0000000..56a6051 --- /dev/null +++ "b/out/production/\353\252\205\354\226\270\352\262\214\354\213\234\355\214\220/db/wiseSaying/lastId.txt" @@ -0,0 +1 @@ +1 \ No newline at end of file diff --git "a/out/production/\353\252\205\354\226\270\352\262\214\354\213\234\355\214\220/wiseSaying10/Main.class" "b/out/production/\353\252\205\354\226\270\352\262\214\354\213\234\355\214\220/wiseSaying10/Main.class" new file mode 100644 index 0000000..262e7a1 Binary files /dev/null and "b/out/production/\353\252\205\354\226\270\352\262\214\354\213\234\355\214\220/wiseSaying10/Main.class" differ diff --git "a/out/production/\353\252\205\354\226\270\352\262\214\354\213\234\355\214\220/wiseSaying10/Quote.class" "b/out/production/\353\252\205\354\226\270\352\262\214\354\213\234\355\214\220/wiseSaying10/Quote.class" new file mode 100644 index 0000000..ddaedf4 Binary files /dev/null and "b/out/production/\353\252\205\354\226\270\352\262\214\354\213\234\355\214\220/wiseSaying10/Quote.class" differ diff --git a/wiseSaying10/Main.java b/wiseSaying10/Main.java new file mode 100644 index 0000000..0d2b879 --- /dev/null +++ b/wiseSaying10/Main.java @@ -0,0 +1,128 @@ +package wiseSaying10; + +import java.util.*; +import java.io.FileWriter; +import java.io.IOException; +import org.json.simple.JSONArray; + +public class Main { + static Map quotes = new HashMap<>(); + static int n = 0; + static Scanner sc = new Scanner(System.in); + static StringBuilder sb = new StringBuilder(); + + static String dirName = "db/wiseSaying/"; + public static void main(String[] args) { + System.out.println("== 명언 앱 =="); + boolean continueFlag = true; + + while(continueFlag) { + System.out.printf("명령) "); + String[] command = sc.nextLine().split("\\?"); + int id=0; + if(command.length > 1){ + id = Integer.parseInt(command[1].split("=")[1]); + } + switch (command[0]) { + case "등록": + addQuote(); + break; + case "목록": + showQuote(); + break; + case "삭제": + deleteQuote(id); + break; + case "수정": + modifyQuote(id); + break; + case "종료": + sc.close(); + continueFlag = false; + saveLastId(); + break; + case "빌드": + buildJson(); + break; + default: + System.out.println("알 수 없는 명령어입니다."); + } + } + } + + public static void addQuote() { + n++; + System.out.printf("명언: "); + String quote = sc.nextLine(); + System.out.printf("작가: "); + String author = sc.nextLine(); + Quote q = new Quote(n, quote, author); + quotes.put(n, q); + saveQuoteAsFile(q); + System.out.println(n + "번 명언이 등록되었습니다."); + } + public static void saveQuoteAsFile(Quote q) { + String fileName = String.format("%s%d.json", dirName, q.id); + saveFile(fileName, q.toJson().toJSONString()); + } + public static void deleteQuote(int id) { + if (quotes.get(id) == null) { + System.out.printf("%d번 명언은 존재하지 않습니다. %n", id); + } else { + quotes.remove(id); + System.out.println(id + "번째 명언이 삭제되었습니다."); + } + } + public static void showQuote() { + System.out.println("번호 / 작가 / 명언"); + System.out.println("----------------------"); + for (int i : quotes.keySet()) { + String result = sb.append(quotes.get(i).id).append(" / ").append(quotes.get(i).getAuthor()).append(" / ").append(quotes.get(i).getQuote()).toString(); + System.out.println(result); + sb.setLength(0); + } + } + + public static void modifyQuote(int id) { + if(quotes.get(id) == null) { + System.out.printf("%d번 명언은 존재하지 않습니다. %n", id); + return; + } + + System.out.printf("명언(기존): %s%n", quotes.get(id).getQuote()); + System.out.printf("명언: "); + String quote2 = sc.nextLine(); + System.out.printf("작가(기존): %s%n", quotes.get(id).getAuthor()); + System.out.printf("작가: "); + String author2 = sc.nextLine(); + + quotes.put(id, new Quote(id, quote2, author2)); + System.out.println(id + "번 명언이 수정되었습니다."); + } + + public static void saveLastId() { + String fileName = dirName + "lastId.txt"; + saveFile(fileName, String.valueOf(n)); + } + + public static void buildJson() { + String fileName = dirName + "data.json"; + JSONArray jsonList = new JSONArray(); + for (int i : quotes.keySet()) { + jsonList.add(quotes.get(i).toJson()); + } + saveFile(fileName, jsonList.toJSONString()); + System.out.println("data.json 파일의 내용이 갱신되었습니다."); + } + + public static void saveFile(String fileName, String content) { + try { + FileWriter file = new FileWriter(fileName); + file.write(content); + file.flush(); + file.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } +} diff --git a/wiseSaying10/Quote.java b/wiseSaying10/Quote.java new file mode 100644 index 0000000..7301544 --- /dev/null +++ b/wiseSaying10/Quote.java @@ -0,0 +1,33 @@ +package wiseSaying10; + +import java.util.Map; +import org.json.simple.JSONObject; + +public class Quote { + Map quoteMap; + int id; + private String quote; + private String author; + + public Quote(int id, String quote, String author) { + this.id = id; + this.quote = quote; + this.author = author; + } + + public String getQuote() { + return this.quote; + } + + public String getAuthor() { + return this.author; + } + + public JSONObject toJson() { + JSONObject obj = new JSONObject(); + obj.put("id", this.id); + obj.put("quote", this.quote); + obj.put("author", this.author); + return obj; + } +} \ No newline at end of file