From 7ba802b605a4ff786a868cb88bec06378ce2a0de Mon Sep 17 00:00:00 2001 From: Kim Uijin <127496156+sansan20535@users.noreply.github.com> Date: Wed, 9 Oct 2024 21:11:11 +0900 Subject: [PATCH 1/5] =?UTF-8?q?[FEAT]=20:=201=EC=B0=A8=20=EC=84=B8?= =?UTF-8?q?=EB=AF=B8=EB=82=98=20=EA=B3=BC=EC=A0=9C=20=ED=95=84=EC=88=98=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/git_toolbox_prj.xml | 15 ++ .idea/gradle.xml | 17 ++ .idea/misc.xml | 9 + .idea/vcs.xml | 6 + .idea/workspace.xml | 109 ++++++++--- src/main/java/org/sopt/week1/Diary.java | 21 +++ .../java/org/sopt/week1/DiaryController.java | 47 +++++ .../java/org/sopt/week1/DiaryRepository.java | 44 +++++ .../java/org/sopt/week1/DiaryService.java | 26 +++ src/main/java/org/sopt/week1/Main.java | 173 ++++++++++++++++++ 10 files changed, 442 insertions(+), 25 deletions(-) create mode 100644 .idea/git_toolbox_prj.xml create mode 100644 .idea/gradle.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/vcs.xml create mode 100644 src/main/java/org/sopt/week1/Diary.java create mode 100644 src/main/java/org/sopt/week1/DiaryController.java create mode 100644 src/main/java/org/sopt/week1/DiaryRepository.java create mode 100644 src/main/java/org/sopt/week1/DiaryService.java create mode 100644 src/main/java/org/sopt/week1/Main.java diff --git a/.idea/git_toolbox_prj.xml b/.idea/git_toolbox_prj.xml new file mode 100644 index 0000000..02b915b --- /dev/null +++ b/.idea/git_toolbox_prj.xml @@ -0,0 +1,15 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml new file mode 100644 index 0000000..43cc8e4 --- /dev/null +++ b/.idea/gradle.xml @@ -0,0 +1,17 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..340c0ad --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 9f14b44..b2364b2 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -5,15 +5,7 @@ - - - - - - - - - + + + + + + + + + + { + "associatedIndex": 8 +} + - { + "keyToString": { + "Gradle.assignment [:Main.main()].executor": "Run", + "RunOnceActivity.OpenProjectViewOnStart": "true", + "RunOnceActivity.ShowReadmeOnStart": "true", + "git-widget-placeholder": "seminar/#1", + "kotlin-language-version-configured": "true", + "node.js.detected.package.eslint": "true", + "node.js.detected.package.tslint": "true", + "node.js.selected.package.eslint": "(autodetect)", + "node.js.selected.package.tslint": "(autodetect)", + "nodejs_package_manager_path": "npm", + "onboarding.tips.debug.path": "/Users/a./Desktop/AND-SOPT/assignment/src/main/java/org/sopt/Main.java", + "project.structure.last.edited": "Project", + "project.structure.proportion": "0.0", + "project.structure.side.proportion": "0.0", + "settings.editor.selected.configurable": "org.jetbrains.plugins.github.ui.GithubSettingsConfigurable", + "vue.rearranger.settings.migration": "true" } -}]]> +} + + + + + + + + + @@ -67,6 +122,10 @@ @@ -84,7 +143,7 @@ - diff --git a/src/main/java/org/sopt/week1/Diary.java b/src/main/java/org/sopt/week1/Diary.java new file mode 100644 index 0000000..8525e80 --- /dev/null +++ b/src/main/java/org/sopt/week1/Diary.java @@ -0,0 +1,21 @@ +package org.sopt.week1; + +import java.time.LocalDateTime; + +public class Diary { + private final Long id; + private final String body; + + public Diary(Long id, String body) { + this.id = id; + this.body = body; + } + + public Long getId() { + return this.id; + } + + public String getBody() { + return this.body; + } +} diff --git a/src/main/java/org/sopt/week1/DiaryController.java b/src/main/java/org/sopt/week1/DiaryController.java new file mode 100644 index 0000000..d2f2463 --- /dev/null +++ b/src/main/java/org/sopt/week1/DiaryController.java @@ -0,0 +1,47 @@ +package org.sopt.week1; + +import java.util.List; + +public class DiaryController { + private Status status = Status.READY; + private final DiaryService diaryService = new DiaryService(); + + Status getStatus() { + return status; + } + + void boot() { + this.status = Status.RUNNING; + } + + void finish() { + this.status = Status.FINISHED; + } + + // APIS + final List getList() { + return diaryService.getDiaryList(); + } + + final void post(final String body) { + if (body.length() > 30) { + throw new IllegalArgumentException(); + } + diaryService.writeDiary(body); + } + + final void delete(final String id) { + diaryService.deleteDiary(Long.parseLong(id)); + } + + final void patch(final String id, final String body) { + diaryService.updateDiary(Long.parseLong(id), body); + } + + enum Status { + READY, + RUNNING, + FINISHED, + ERROR, + } +} diff --git a/src/main/java/org/sopt/week1/DiaryRepository.java b/src/main/java/org/sopt/week1/DiaryRepository.java new file mode 100644 index 0000000..ae8699d --- /dev/null +++ b/src/main/java/org/sopt/week1/DiaryRepository.java @@ -0,0 +1,44 @@ +package org.sopt.week1; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicLong; + +public class DiaryRepository { + private final Map storage = new ConcurrentHashMap<>(); + private final AtomicLong numbering = new AtomicLong(); + + void save(final Diary diary) { + // 채번 과정 + final long id = numbering.addAndGet(1); + // 저장 과정 + storage.put(id, diary.getBody()); + } + + List findAll() { + // (1) diary를 담을 자료구조 + final List diaryList = new ArrayList<>(); + // (2) 저장한 값을 불러오는 반복 구조 + for (long index = 1; index <= numbering.intValue(); index++) { + final String body = storage.get(index); + // (2-1) 저장한 값을 불러오는 반복 구조 + if (body != null) { + diaryList.add(new Diary(index, body)); + } + } + // (3) 불러온 자료구조를 응답 + return diaryList; + } + + void deleteById(final Long id) { + storage.remove(id); + } + + void updateById(final Long id, final String body) { + storage.replace(id, body); + } + +} diff --git a/src/main/java/org/sopt/week1/DiaryService.java b/src/main/java/org/sopt/week1/DiaryService.java new file mode 100644 index 0000000..96aa495 --- /dev/null +++ b/src/main/java/org/sopt/week1/DiaryService.java @@ -0,0 +1,26 @@ +package org.sopt.week1; + +import java.util.List; + +public class DiaryService { + private final DiaryRepository diaryRepository = new DiaryRepository(); + + void writeDiary(final String body) { + final Diary diary = new Diary(null, body); + + diaryRepository.save(diary); + } + + List getDiaryList() { + return diaryRepository.findAll(); + } + + void deleteDiary(final Long id) { + diaryRepository.deleteById(id); + } + + void updateDiary(final Long id, final String body) { + diaryRepository.updateById(id, body); + } + +} diff --git a/src/main/java/org/sopt/week1/Main.java b/src/main/java/org/sopt/week1/Main.java new file mode 100644 index 0000000..1b4df8e --- /dev/null +++ b/src/main/java/org/sopt/week1/Main.java @@ -0,0 +1,173 @@ +package org.sopt.week1; + +import java.io.*; + +public class Main { + public static void main(String[] args) { + final UI ui; + try { + ui = new DiaryUI(new DiaryController()); + ui.runRepeatedly(); + } catch (Throwable t) { + + } + } + + interface UI { + void runRepeatedly() throws IOException; + + class UIException extends RuntimeException { + } + + class InvalidInputException extends UIException { + } + } + + static class DiaryUI implements UI { + private final DiaryController server; + private String selected; + + public DiaryUI(DiaryController server) throws IOException { + this.server = server; + server.boot(); + ConsoleIO.printLine(getStartMessage()); + } + + public void runRepeatedly() throws IOException { + + do { + if (onMenu()) { + ConsoleIO.printLine(""); + ConsoleIO.printLine(getMenu()); + selected = ConsoleIO.readLine().trim().toUpperCase(); + } + + try { + run(); + } catch (InvalidInputException e) { + ConsoleIO.printLine("잘못된 값을 입력하였습니다."); + } + + if (isFinished()) { + ConsoleIO.printLine(getFinishMessage()); + break; + } + + selected = null; + } while (isRunning()); + } + + private void run() throws IOException { + switch (server.getStatus()) { + case READY, FINISHED, ERROR -> throw new UIException(); + + case RUNNING -> { + switch (selected) { + case "GET" -> { + server.getList().forEach(diary -> { + try { + ConsoleIO.printLine(diary.getId() + " : " + diary.getBody()); + } catch (IOException e) { + throw new RuntimeException(e); + } + }); + } + case "POST" -> { + ConsoleIO.printLine("한 줄 일기를 작성해주세요!"); + final String input = ConsoleIO.readLine(); + server.post(input); + } + + case "DELETE" -> { + ConsoleIO.printLine("삭제할 id 를 입력하세요!"); + final String input = ConsoleIO.readLine(); + server.delete(input); + } + case "PATCH" -> { + ConsoleIO.printLine("수정할 id 를 입력하세요!"); + final String inputId = ConsoleIO.readLine(); + + ConsoleIO.printLine("수정 body 를 입력하세요!"); + final String inputBody = ConsoleIO.readLine(); + + server.patch(inputId, inputBody); + } + case "FINISH" -> { + server.finish(); + } + default -> { + throw new InvalidInputException(); + } + } + } + + } + } + + private boolean isRunning() { + return server.getStatus() == DiaryController.Status.RUNNING; + } + + private boolean isFinished() { + return server.getStatus() == DiaryController.Status.FINISHED; + } + + private boolean onMenu() { + return selected == null; + } + + private String getMenu() { + return """ + ============================ + - GET : 일기 불러오기 + - POST : 일기 작성하기 + - DELETE : 일기 제거하기 + - PATCH : 일기 수정하기 + """; + + } + + private String getStartMessage() { + return "시작합니다 :)"; + } + + private String getFinishMessage() { + return "종료됩니다 :)"; + } + } + + // not thread safe + private static class ConsoleIO { + private final static BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(System.out)); + private final static BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); + private final static StringBuilder sb = new StringBuilder(); + + public static void printLine(final String toPrint) throws IOException { + if (toPrint == null) { + throw new IllegalArgumentException("console can not print null"); + } + + appendLine(toPrint); + print(); + clearStringBuilder(); + } + + public static String readLine() throws IOException { + return bufferedReader.readLine(); + } + + private static void appendLine(final String toPrint) { + sb.append(toPrint); + sb.append("\n"); + } + + private static void print() throws IOException { + bufferedWriter.write(sb.toString()); + bufferedWriter.flush(); + } + + private static void clearStringBuilder() { + sb.setLength(0); + } + } +} From 24a6973f009b98a93bf2e4be4038400507e4a39f Mon Sep 17 00:00:00 2001 From: Kim Uijin <127496156+sansan20535@users.noreply.github.com> Date: Wed, 9 Oct 2024 21:28:00 +0900 Subject: [PATCH 2/5] =?UTF-8?q?[FEAT]=20:=201=EC=B0=A8=20=EC=84=B8?= =?UTF-8?q?=EB=AF=B8=EB=82=98=20=EA=B3=BC=EC=A0=9C=20=EC=84=A0=ED=83=9D=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84=20-=20=EC=82=AD=EC=A0=9C=EB=90=9C=20?= =?UTF-8?q?=EC=9D=BC=EA=B8=B0=20=EB=B3=B5=EA=B5=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/workspace.xml | 47 +++++++++++-------- .../java/org/sopt/week1/DiaryController.java | 4 ++ .../java/org/sopt/week1/DiaryRepository.java | 8 ++++ .../java/org/sopt/week1/DiaryService.java | 3 ++ src/main/java/org/sopt/week1/Main.java | 6 +++ 5 files changed, 49 insertions(+), 19 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index b2364b2..d9b8eb0 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -6,6 +6,10 @@ + + + +