From 38f2ed21e1e4f47a7040f46e566ff71cfbc6bf50 Mon Sep 17 00:00:00 2001 From: hmhmchm Date: Mon, 23 Dec 2024 15:16:23 +0900 Subject: [PATCH] =?UTF-8?q?feat=20:=20=EB=AA=85=EC=96=B8=20=EA=B2=8C?= =?UTF-8?q?=EC=8B=9C=ED=8C=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 33 ++++ db/wiseSaying/1.json | 1 + db/wiseSaying/2.json | 1 + db/wiseSaying/3.json | 1 + db/wiseSaying/data.json | 1 + db/wiseSaying/lastId.txt | 1 + settings.gradle | 1 + src/main/java/com/example/wisesaying/App.java | 40 ++++ .../java/com/example/wisesaying/Main.java | 13 ++ .../controller/WiseSayingController.java | 35 ++++ .../example/wisesaying/domain/WiseSaying.java | 19 ++ .../wisesaying/service/WiseSayingService.java | 16 ++ .../service/impl/WiseSayingServiceImpl.java | 171 ++++++++++++++++++ src/main/resources/application.properties | 1 + .../JavaExamTestApplicationTests.java | 13 ++ 15 files changed, 347 insertions(+) create mode 100644 build.gradle create mode 100644 db/wiseSaying/1.json create mode 100644 db/wiseSaying/2.json create mode 100644 db/wiseSaying/3.json create mode 100644 db/wiseSaying/data.json create mode 100644 db/wiseSaying/lastId.txt create mode 100644 settings.gradle create mode 100644 src/main/java/com/example/wisesaying/App.java create mode 100644 src/main/java/com/example/wisesaying/Main.java create mode 100644 src/main/java/com/example/wisesaying/controller/WiseSayingController.java create mode 100644 src/main/java/com/example/wisesaying/domain/WiseSaying.java create mode 100644 src/main/java/com/example/wisesaying/service/WiseSayingService.java create mode 100644 src/main/java/com/example/wisesaying/service/impl/WiseSayingServiceImpl.java create mode 100644 src/main/resources/application.properties create mode 100644 src/test/java/com/example/wisesaying/JavaExamTestApplicationTests.java diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000..fbf1305 --- /dev/null +++ b/build.gradle @@ -0,0 +1,33 @@ +plugins { + id 'java' + id 'org.springframework.boot' version '3.4.0' + id 'io.spring.dependency-management' version '1.1.6' +} + +group = 'com.example' +version = '0.0.1-SNAPSHOT' + +java { + toolchain { + languageVersion = JavaLanguageVersion.of(17) + } +} + +repositories { + mavenCentral() +} + +dependencies { + implementation 'org.springframework.boot:spring-boot-starter' + implementation 'org.projectlombok:lombok' + compileOnly 'org.projectlombok:lombok:1.18.28' + implementation 'com.fasterxml.jackson.core:jackson-databind:2.15.2' + + annotationProcessor 'org.projectlombok:lombok:1.18.28' + testImplementation 'org.springframework.boot:spring-boot-starter-test' + testRuntimeOnly 'org.junit.platform:junit-platform-launcher' +} + +tasks.named('test') { + useJUnitPlatform() +} diff --git a/db/wiseSaying/1.json b/db/wiseSaying/1.json new file mode 100644 index 0000000..c33d444 --- /dev/null +++ b/db/wiseSaying/1.json @@ -0,0 +1 @@ +{"id":1,"content":"1","author":"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..4503be4 --- /dev/null +++ b/db/wiseSaying/2.json @@ -0,0 +1 @@ +{"id":2,"content":"2","author":"2"} \ No newline at end of file diff --git a/db/wiseSaying/3.json b/db/wiseSaying/3.json new file mode 100644 index 0000000..06ff9ba --- /dev/null +++ b/db/wiseSaying/3.json @@ -0,0 +1 @@ +{"id":3,"content":"3","author":"3"} \ No newline at end of file diff --git a/db/wiseSaying/data.json b/db/wiseSaying/data.json new file mode 100644 index 0000000..a604ae1 --- /dev/null +++ b/db/wiseSaying/data.json @@ -0,0 +1 @@ +[{"id":1,"content":"1","author":"1"},{"id":2,"content":"2","author":"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..d8263ee --- /dev/null +++ b/db/wiseSaying/lastId.txt @@ -0,0 +1 @@ +2 \ No newline at end of file diff --git a/settings.gradle b/settings.gradle new file mode 100644 index 0000000..2a07be4 --- /dev/null +++ b/settings.gradle @@ -0,0 +1 @@ +rootProject.name = 'javaExamTest' diff --git a/src/main/java/com/example/wisesaying/App.java b/src/main/java/com/example/wisesaying/App.java new file mode 100644 index 0000000..5a91148 --- /dev/null +++ b/src/main/java/com/example/wisesaying/App.java @@ -0,0 +1,40 @@ +package com.example.wisesaying; + +import com.example.wisesaying.controller.WiseSayingController; +import lombok.RequiredArgsConstructor; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; + +@RequiredArgsConstructor +public class App { + private final WiseSayingController controller = new WiseSayingController(); + private BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + + public void start() throws IOException { + String command = ""; + + System.out.println("== 명언 앱 =="); + while (true) { + System.out.println("명령) "); + + command = br.readLine(); + if (command.equals("등록")) { + controller.register(); + } else if (command.equals("목록")) { + controller.getList(); + } else if (command.equals("삭제")) { + controller.delete(); + } else if (command.equals("수정")) { + controller.update(); + } else if (command.equals("빌드")) { + controller.toBuild(); + } else if (command.equals("종료")) { + break; + } + + } + System.out.println("명령) 종료"); + } +} diff --git a/src/main/java/com/example/wisesaying/Main.java b/src/main/java/com/example/wisesaying/Main.java new file mode 100644 index 0000000..3d2f906 --- /dev/null +++ b/src/main/java/com/example/wisesaying/Main.java @@ -0,0 +1,13 @@ +package com.example.wisesaying; + +import org.springframework.boot.autoconfigure.SpringBootApplication; + +import java.io.IOException; + +@SpringBootApplication +public class Main { + public static void main(String[] args) throws IOException { + new App().start(); + } + +} \ No newline at end of file diff --git a/src/main/java/com/example/wisesaying/controller/WiseSayingController.java b/src/main/java/com/example/wisesaying/controller/WiseSayingController.java new file mode 100644 index 0000000..69a6377 --- /dev/null +++ b/src/main/java/com/example/wisesaying/controller/WiseSayingController.java @@ -0,0 +1,35 @@ +package com.example.wisesaying.controller; + +import com.example.wisesaying.service.WiseSayingService; +import com.example.wisesaying.service.impl.WiseSayingServiceImpl; +import org.springframework.stereotype.Controller; + +import java.io.IOException; + +//@RequiredArgsConstructor +@Controller +public class WiseSayingController{ + + private final WiseSayingService wiseSayingService = new WiseSayingServiceImpl(); + + // 등록 + public void register() throws IOException { + wiseSayingService.register(); + } + + public void getList() { + wiseSayingService.getList(); + } + + public void delete() { + wiseSayingService.delete(); + } + + public void update() { + wiseSayingService.update(); + } + + public void toBuild() { + wiseSayingService.toBuild(); + } +} diff --git a/src/main/java/com/example/wisesaying/domain/WiseSaying.java b/src/main/java/com/example/wisesaying/domain/WiseSaying.java new file mode 100644 index 0000000..51e21a9 --- /dev/null +++ b/src/main/java/com/example/wisesaying/domain/WiseSaying.java @@ -0,0 +1,19 @@ +package com.example.wisesaying.domain; + +import lombok.*; + +@Getter +@Setter +public class WiseSaying { + + private long id; + private String content; + private String author; + + public WiseSaying(long id, String content, String author) { + this.id = id; + this.content = content; + this.author = author; + } + +} diff --git a/src/main/java/com/example/wisesaying/service/WiseSayingService.java b/src/main/java/com/example/wisesaying/service/WiseSayingService.java new file mode 100644 index 0000000..740dffa --- /dev/null +++ b/src/main/java/com/example/wisesaying/service/WiseSayingService.java @@ -0,0 +1,16 @@ +package com.example.wisesaying.service; + +import java.io.IOException; + +public interface WiseSayingService { + + void register() throws IOException; + + void getList(); + + void delete(); + + void update(); + + void toBuild(); +} diff --git a/src/main/java/com/example/wisesaying/service/impl/WiseSayingServiceImpl.java b/src/main/java/com/example/wisesaying/service/impl/WiseSayingServiceImpl.java new file mode 100644 index 0000000..cc746c2 --- /dev/null +++ b/src/main/java/com/example/wisesaying/service/impl/WiseSayingServiceImpl.java @@ -0,0 +1,171 @@ +package com.example.wisesaying.service.impl; + +import com.example.wisesaying.domain.WiseSaying; +import com.example.wisesaying.service.WiseSayingService; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.springframework.stereotype.Service; + +import java.io.*; +import java.util.ArrayList; + +@Service +public class WiseSayingServiceImpl implements WiseSayingService { + + private final String DB_FOLDER = "db/wiseSaying"; + + private final ArrayList wiseSayingArrayList = new ArrayList<>(); + private long n = 1; + + private final ObjectMapper objectMapper = new ObjectMapper(); + + public WiseSayingServiceImpl() { + // db/wiseSaying 폴더 생성 + File folder = new File(DB_FOLDER); + if (!folder.exists()) { + folder.mkdirs(); + } + + // lastId.txt 파일 생성 (없을 경우) + File lastIdFile = new File(DB_FOLDER + "/lastId.txt"); + if (!lastIdFile.exists()) { + try (FileWriter writer = new FileWriter(lastIdFile)) { + writer.write("1"); // 초기값 1 + } catch (IOException e) { + throw new RuntimeException(e); + } + } + } + + // lastId.txt 수정 + private void updateLastId(long id) throws IOException { + try (FileWriter writer = new FileWriter(DB_FOLDER + "/lastId.txt")) { + writer.write(String.valueOf(id)); + } + } + + /*// lastId.txt 읽기 + private long getLastId() throws IOException { + String lastIdContent = new String(Files.readAllBytes(Paths.get(DB_FOLDER + "/lastId.txt"))); + return Long.parseLong(lastIdContent.trim()); + }*/ + + @Override + public void register() throws IOException { + + String content = ""; + String author = ""; + try { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + + content = br.readLine(); + author = br.readLine(); + } catch (IOException e) { + e.printStackTrace(); + } + + System.out.println("명언 : " + content); + System.out.println("작가 : " + author); + System.out.println(n + "번 명언이 등록되었습니다."); + + wiseSayingArrayList.add(new WiseSaying(n, content, author)); + + // 파일생성 + saveToFile(new WiseSaying(n, content, author)); + + File lastIdFile = new File(DB_FOLDER + "/lastId.txt"); + if (lastIdFile.exists()) { + System.out.println("txt 파일 존재"); + try { + updateLastId(n); + System.out.println(n); + } catch (IOException e) { + e.printStackTrace(); + } + } + + n += 1; + } + + // JSON 파일로 저장 + private void saveToFile(WiseSaying wiseSaying) throws IOException { + File file = new File(DB_FOLDER + "/" + wiseSaying.getId() + ".json"); + objectMapper.writeValue(file, wiseSaying); + } + + @Override + public void getList() { + System.out.println("번호 / 작가 / 명언"); + System.out.println("---------------------"); + + for (int i = wiseSayingArrayList.size() - 1; i >= 0; i--) { + WiseSaying w = wiseSayingArrayList.get(i); + System.out.println(w.getId() + " / " + w.getAuthor() + " / " + w.getContent()); + } + } + + @Override + public void delete() { + try { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + int number = Integer.parseInt(br.readLine()); + for (int i = 0; i < wiseSayingArrayList.size(); i++) { + if (wiseSayingArrayList.get(i).getId() == number) { + // 삭제하기 + wiseSayingArrayList.remove(i); + System.out.println(number + "번 명언이 삭제되었습니다."); + break; + } else { + System.out.println(number + "번 명언은 존재하지 않습니다."); + } + } + } catch (IOException e) { + e.printStackTrace(); + } + } + + @Override + public void update() { + try { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + int number = Integer.parseInt(br.readLine()); + for (int i = 0; i < wiseSayingArrayList.size(); i++) { + if (wiseSayingArrayList.get(i).getId() == number) { + // 수정하기 + System.out.println("명언(기존) : " + wiseSayingArrayList.get(i).getContent()); + String content = br.readLine(); + + wiseSayingArrayList.get(i).setContent(content); + + System.out.println("명언 : " + content); + + System.out.println("작가(기존) : " + wiseSayingArrayList.get(i).getAuthor()); + + String author = br.readLine(); + + wiseSayingArrayList.get(i).setAuthor(author); + System.out.println("작가 : " + author); + + break; + } else { + System.out.println(number + "번 명언은 존재하지 않습니다."); + } + } + } catch (IOException e) { + e.printStackTrace(); + } + } + + @Override + public void toBuild() { + try { + File file = new File(DB_FOLDER + "/" + "data" + ".json"); + objectMapper.writeValue(file, wiseSayingArrayList); + } catch (IOException e) { + throw new RuntimeException(e); + } + + System.out.println("data.json 파일의 내용이 갱신되었습니다."); + } + + +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties new file mode 100644 index 0000000..ab49f60 --- /dev/null +++ b/src/main/resources/application.properties @@ -0,0 +1 @@ +spring.application.name=javaExamTest diff --git a/src/test/java/com/example/wisesaying/JavaExamTestApplicationTests.java b/src/test/java/com/example/wisesaying/JavaExamTestApplicationTests.java new file mode 100644 index 0000000..4c9f878 --- /dev/null +++ b/src/test/java/com/example/wisesaying/JavaExamTestApplicationTests.java @@ -0,0 +1,13 @@ +package com.example.wisesaying; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class JavaExamTestApplicationTests { + + @Test + void contextLoads() { + } + +}