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
33 changes: 33 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -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()
}
1 change: 1 addition & 0 deletions db/wiseSaying/1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"id":1,"content":"1","author":"1"}
1 change: 1 addition & 0 deletions db/wiseSaying/2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"id":2,"content":"2","author":"2"}
1 change: 1 addition & 0 deletions db/wiseSaying/3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"id":3,"content":"3","author":"3"}
1 change: 1 addition & 0 deletions db/wiseSaying/data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"id":1,"content":"1","author":"1"},{"id":2,"content":"2","author":"2"}]
1 change: 1 addition & 0 deletions db/wiseSaying/lastId.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rootProject.name = 'javaExamTest'
40 changes: 40 additions & 0 deletions src/main/java/com/example/wisesaying/App.java
Original file line number Diff line number Diff line change
@@ -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("명령) 종료");
}
}
13 changes: 13 additions & 0 deletions src/main/java/com/example/wisesaying/Main.java
Original file line number Diff line number Diff line change
@@ -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();
}

}
Original file line number Diff line number Diff line change
@@ -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();
}
}
19 changes: 19 additions & 0 deletions src/main/java/com/example/wisesaying/domain/WiseSaying.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
@@ -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();
}
Original file line number Diff line number Diff line change
@@ -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<WiseSaying> 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 파일의 내용이 갱신되었습니다.");
}


}
1 change: 1 addition & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
spring.application.name=javaExamTest
Original file line number Diff line number Diff line change
@@ -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() {
}

}