diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f68d109 --- /dev/null +++ b/.gitignore @@ -0,0 +1,29 @@ +### IntelliJ IDEA ### +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..417e36d --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..631004f --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/README.md b/README.md index 4444cb9..99693a9 100644 --- a/README.md +++ b/README.md @@ -1 +1,7 @@ -# code-review \ No newline at end of file +# 명언 게시판 만들기 +DevCourse Backend 4기 + + +https://velog.io/@jxxnging/JAVA-명언-게시판-만들기-1 + +https://velog.io/@jxxnging/JAVA-명언-게시판-만들기-2 \ No newline at end of file diff --git a/db/wiseSaying/1.json b/db/wiseSaying/1.json new file mode 100644 index 0000000..7d1e394 --- /dev/null +++ b/db/wiseSaying/1.json @@ -0,0 +1,5 @@ +{ + "id": 1, + "content": "너 자신을 알라", + "author": "소크라테스" +} \ No newline at end of file diff --git a/db/wiseSaying/2.json b/db/wiseSaying/2.json new file mode 100644 index 0000000..f7ad27d --- /dev/null +++ b/db/wiseSaying/2.json @@ -0,0 +1,5 @@ +{ + "id": 2, + "content": "주사위는 던져졌다", + "author": "율리우스 카이사르" +} \ No newline at end of file diff --git a/db/wiseSaying/3.json b/db/wiseSaying/3.json new file mode 100644 index 0000000..300fbe4 --- /dev/null +++ b/db/wiseSaying/3.json @@ -0,0 +1,5 @@ +{ + "id": 3, + "content": "그래도 지구는 돈다", + "author": "갈릴레오 갈릴레이" +} \ No newline at end of file diff --git a/db/wiseSaying/5.json b/db/wiseSaying/5.json new file mode 100644 index 0000000..60ae912 --- /dev/null +++ b/db/wiseSaying/5.json @@ -0,0 +1,5 @@ +{ + "id": 5, + "content": "집에 가고 싶다", + "author": "작자미상" +} \ No newline at end of file diff --git a/db/wiseSaying/data.json b/db/wiseSaying/data.json new file mode 100644 index 0000000..c44ecdc --- /dev/null +++ b/db/wiseSaying/data.json @@ -0,0 +1,22 @@ +[ + { + "id": 5, + "content": "집에 가고 싶다", + "author": "작자미상" + }, + { + "id": 3, + "content": "그래도 지구는 돈다", + "author": "갈릴레오 갈릴레이" + }, + { + "id": 2, + "content": "주사위는 던져졌다", + "author": "율리우스 카이사르" + }, + { + "id": 1, + "content": "너 자신을 알라", + "author": "소크라테스" + } +] \ No newline at end of file diff --git a/db/wiseSaying/lastId.txt b/db/wiseSaying/lastId.txt new file mode 100644 index 0000000..7813681 --- /dev/null +++ b/db/wiseSaying/lastId.txt @@ -0,0 +1 @@ +5 \ No newline at end of file diff --git a/src/Main.java b/src/Main.java new file mode 100644 index 0000000..9e1b9e6 --- /dev/null +++ b/src/Main.java @@ -0,0 +1,237 @@ +import java.io.*; +import java.util.*; + +public class Main { + public static void main(String[] args) { + String action = ""; + String content = ""; + String author = ""; + + int targetId = 0; + int id = 1; + if (getLastId() != 1) { + id = getLastId() + 1; + } + + ArrayList list = load(); + + Scanner sc = new Scanner(System.in); + + System.out.println("== 명언 앱 =="); + + while (!action.equals("종료")) { + System.out.print("명령) "); + action = sc.nextLine(); + + switch (action) { + case "등록": + System.out.print("명언 : "); + content = sc.nextLine(); + System.out.print("작가 : "); + author = sc.nextLine(); + + WiseSaying saying = new WiseSaying(id, content, author); + save(saying); + list.add(saying); + System.out.println(id + "번 명언이 등록되었습니다."); + + id++; + updateLastId(id); + break; + + case "목록": + Collections.sort(list, new Comparator() { + public int compare(WiseSaying ws1, WiseSaying ws2) { + return Integer.compare(ws2.getId(), ws1.getId()); + } + }); + + System.out.println("번호 / 작가 / 명언"); + System.out.println("----------------------"); + + if (list.isEmpty()) { + System.out.println("등록된 명언이 없습니다."); + } else { + for (WiseSaying ws : list) { + System.out.println(ws.getId() + " / " + ws.getAuthor() + " / " + ws.getContent()); + } + } + break; + + case "삭제": + System.out.print("id="); + targetId = sc.nextInt(); + sc.nextLine(); + + WiseSaying deleteWS = getSaying(list, targetId); + if (deleteWS != null) { + list.remove(deleteWS); + delete(deleteWS); + } + break; + + case "수정": + System.out.print("id="); + targetId = sc.nextInt(); + sc.nextLine(); + + WiseSaying updateWS = getSaying(list, targetId); + if (updateWS != null) { + System.out.println("명언(기존) : " + updateWS.getContent()); + System.out.print("명언 : "); + content = sc.nextLine(); + + System.out.println("작가(기존) : " + updateWS.getAuthor()); + System.out.print("작가 : "); + author = sc.nextLine(); + + updateWS.setContent(content); + updateWS.setAuthor(author); + save(updateWS); + System.out.println(targetId + "번 명언이 수정되었습니다."); + } else { + System.out.println(targetId + "번 명언은 존재하지 않습니다."); + } + break; + + case "빌드": + build(list); + break; + + case "종료": + if (!list.isEmpty()) { + updateLastId(id - 1); + } else { + updateLastId(1); + } + break; + + default: + System.out.println("올바른 명령어를 입력해주세요."); + break; + } + } + + sc.close(); + } + + public static int getLastId() { + try { + BufferedReader reader = new BufferedReader(new FileReader("db/wiseSaying/lastId.txt")); + String lastId = reader.readLine(); + reader.close(); + return Integer.parseInt(lastId); + } catch (IOException e) { + return 1; + } + } + + public static void updateLastId(int id) { + try { + FileWriter writer = new FileWriter("db/wiseSaying/lastId.txt"); + writer.write(String.valueOf(id)); + writer.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public static void save(WiseSaying ws) { + try { + String json = ws.toJson(); + + File file = new File("db/wiseSaying/" + ws.getId() + ".json"); + file.getParentFile().mkdirs(); + + FileWriter writer = new FileWriter(file); + writer.write(json); + writer.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public static void delete(WiseSaying ws) { + try { + File file = new File("db/wiseSaying/" + ws.getId() + ".json"); + if (file.exists()) { + if (file.delete()) { + System.out.println(ws.getId() + "번 명언이 삭제되었습니다."); + } + } else { + System.out.println(ws.getId() + "번 명언은 존재하지 않습니다."); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + public static void build(ArrayList list) { + try { + StringBuilder jsonBuilder = new StringBuilder("[\n"); + + for (int i = 0; i < list.size(); i++) { + jsonBuilder.append("\t{\n") + .append("\t\t\"id\": ").append(list.get(i).getId()).append(",\n") + .append("\t\t\"content\": \"").append(list.get(i).getContent()).append("\",\n") + .append("\t\t\"author\": \"").append(list.get(i).getAuthor()).append("\"\n") + .append("\t}"); + if (i < list.size() - 1) { + jsonBuilder.append(","); + } + jsonBuilder.append("\n"); + } + jsonBuilder.append("]"); + + File file = new File("db/wiseSaying/data.json"); + file.getParentFile().mkdirs(); + + FileWriter writer = new FileWriter(file); + writer.write(jsonBuilder.toString()); + writer.close(); + + System.out.println("data.json 파일의 내용이 갱신되었습니다."); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public static ArrayList load() { + ArrayList list = new ArrayList(); + File dir = new File("db/wiseSaying"); + File[] files = dir.listFiles((d, name) -> name.endsWith(".json")); + + if (files != null) { + for (File file : files) { + if (file.getName().equals("data.json")) { + continue; + } + + try { + BufferedReader reader = new BufferedReader(new FileReader(file)); + StringBuilder json = new StringBuilder(); + String line; + while ((line = reader.readLine()) != null) { + json.append(line); + } + reader.close(); + + WiseSaying ws = WiseSaying.fromJson(json.toString()); + list.add(ws); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + return list; + } + + public static WiseSaying getSaying(ArrayList list, int id) { + for (WiseSaying ws : list) { + if (ws.getId() == id) { + return ws; + } + } + return null; + } +} diff --git a/src/WiseSaying.java b/src/WiseSaying.java new file mode 100644 index 0000000..b4a0d5f --- /dev/null +++ b/src/WiseSaying.java @@ -0,0 +1,69 @@ +public class WiseSaying { + private int id; + private String content; + private String author; + + public WiseSaying(int id, String content, String author) { + this.id = id; + this.content = content; + this.author = author; + } + + public int getId() { + return id; + } + + public String getContent() { + return content; + } + + public String getAuthor() { + return author; + } + + public void setContent(String content) { + this.content = content; + } + + public void setAuthor(String author) { + this.author = author; + } + + public String toJson() { + return "{\n" + + " \"id\": " + id + ",\n" + + " \"content\": \"" + content + "\",\n" + + " \"author\": \"" + author + "\"\n" + + "}"; + } + + public static WiseSaying fromJson(String json) { + int id = Integer.parseInt(extractValue(json, "\"id\":")); + String content = extractValue(json, "\"content\":"); + String author = extractValue(json, "\"author\":"); + + return new WiseSaying(id, content, author); + } + + private static String extractValue(String json, String key) { + int startIndex = json.indexOf(key); + if (startIndex == -1) { + throw new IllegalArgumentException("키를 찾을 수 없음: " + key); + } + + startIndex = json.indexOf(":", startIndex) + 1; + int endIndex = json.indexOf(",", startIndex); + + if (endIndex == -1) { + endIndex = json.indexOf("}", startIndex); + } + + String value = json.substring(startIndex, endIndex).trim(); + + if (value.startsWith("\"") && value.endsWith("\"")) { + value = value.substring(1, value.length() - 1); + } + + return value; + } +} diff --git a/wise-saying.iml b/wise-saying.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/wise-saying.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file