diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/.gitignore @@ -0,0 +1,38 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### 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..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..aa00ffa --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..6d9a075 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ 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..f55f626 100644 --- a/README.md +++ b/README.md @@ -1 +1,120 @@ -# code-review \ No newline at end of file +# 명령 게시판 + +## 목차 +1. [출력 및 코드 설명](#출력-및-코드-설명) + - [레벨 1](#레벨-1) + - [레벨 2](#레벨-2) + - [레벨 3, 4](#레벨-3-4) + - [레벨 5](#레벨-5) + - [레벨 6](#레벨-6) + - [레벨 7](#레벨-7) + - [레벨 8](#레벨-8) + - [레벨 9](#레벨-9) + - [레벨 10](#레벨-10) + +--- + +## 출력 및 코드 설명 + +### 레벨 1 +#### 출력 결과 +![레벨 1 출력](./images/level1.png) + +#### 코드 설명 +- **종료 기능**: + `while` 문에서 명령어로 `"종료"`가 입력되면 `break`를 통해 프로그램을 종료합니다. + +--- + +### 레벨 2 +#### 출력 결과 +![레벨 2 출력](./images/level2.png) + +#### 코드 설명 +- **등록 기능**: + - 명령어로 `"등록"`이 입력되면 사용자로부터 **명언**과 **작가**에 대한 입력을 받습니다. + +--- + +### 레벨 3, 4 +#### 출력 결과 +![레벨 3, 4 출력](./images/level3,4.png) + +#### 코드 설명 +- **등록 횟수 관리**: + - `count` 변수를 활용해 **명언 번호**를 등록 횟수에 따라 증가시킵니다. + +--- + +### 레벨 5 +#### 출력 결과 +![레벨 5 출력](./images/level5.png) + +#### 코드 설명 +- **Switch 활용**: + - 명령어를 추가할 때의 편의성을 위해 `switch` 문으로 변경했습니다. +- **클래스 설계**: + - `noSpring.Quote` 클래스를 `public class`로 선언. + - **Getter**와 **Constructor**를 추가했습니다. +- **리스트 관리**: + - `List quotes`를 선언해 명언 객체를 저장합니다. +- **등록 및 출력 기능**: + - `"등록"` 명령어: 입력받은 데이터를 `quotes` 리스트에 추가. + - `"목록"` 명령어: 예제에 맞게 명언을 출력. + +--- + +### 레벨 6 +#### 출력 결과 +![레벨 6 출력](./images/level6.png) + +#### 코드 설명 +- **삭제 명령어 추가**: + - `switch`에서 명령어를 처리할 때 입력을 `'?'` 기준으로 나눕니다. + - `"삭제"` 명령어 입력 시 조건에 맞는 데이터를 삭제합니다. + +--- + +### 레벨 7 +#### 출력 결과 +![레벨 7 출력](./images/level7.png) + +#### 코드 설명 +- **예외 처리**: + - **`flag`** 변수를 사용해 예외 상황을 처리합니다. + +--- + +### 레벨 8 +#### 출력 결과 +![레벨 8 출력](./images/level8.png) + +#### 코드 설명 +- **데이터 수정 기능**: + - `newQuote`와 `newAuthor`를 통해 새로운 데이터를 입력받습니다. + - `noSpring.Quote` 클래스에 **Setter**를 추가해 데이터를 수정 가능하도록 합니다. + - 수정 불가능한 `id`는 **`final`**로 선언. +- **코드 흐름**: + - 전반적인 로직은 `"삭제"` 기능과 유사하게 작성되었습니다. + +### 레벨 9 +#### 출력 결과 +![레벨 9 출력](./images/level9.png) +- **종료 후** + +![영속성 출력](./images/level9-1.png) +![파일 생성 출력](./images/level9-2.png) + +#### 코드 설명 +- **주요 변경점**: + - 데이터 저장 방식 : 메모리 -> 파일 + - 프로그램이 종료되어도 영속성을 부여 + - Jackson 라이브러리를 통해 JSON 활용 + +### 레벨 10 +#### 출력 결과 +![레벨 10 출력](./images/level10.png) +![레벨 10 출력](./images/level10-1.png) + +#### 코드 설명 +- "빌드" 명령어를 추가하였다. \ No newline at end of file diff --git a/db/wiseSaying/2.json b/db/wiseSaying/2.json new file mode 100644 index 0000000..df4a2ea --- /dev/null +++ b/db/wiseSaying/2.json @@ -0,0 +1,5 @@ +{ + "id" : 2, + "author" : "홍길동", + "content" : "현재와 자신을 사랑하라." +} \ No newline at end of file diff --git a/db/wiseSaying/data.json b/db/wiseSaying/data.json new file mode 100644 index 0000000..aef61f5 --- /dev/null +++ b/db/wiseSaying/data.json @@ -0,0 +1,5 @@ +[ { + "id" : 2, + "author" : "홍길동", + "content" : "현재와 자신을 사랑하라." +} ] \ 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/images/level1.png b/images/level1.png new file mode 100644 index 0000000..2c37de3 Binary files /dev/null and b/images/level1.png differ diff --git a/images/level10-1.png b/images/level10-1.png new file mode 100644 index 0000000..662775f Binary files /dev/null and b/images/level10-1.png differ diff --git a/images/level10.png b/images/level10.png new file mode 100644 index 0000000..6d00afb Binary files /dev/null and b/images/level10.png differ diff --git a/images/level2.png b/images/level2.png new file mode 100644 index 0000000..8d2d895 Binary files /dev/null and b/images/level2.png differ diff --git a/images/level3,4.png b/images/level3,4.png new file mode 100644 index 0000000..b6a6243 Binary files /dev/null and b/images/level3,4.png differ diff --git a/images/level5.png b/images/level5.png new file mode 100644 index 0000000..81b42c4 Binary files /dev/null and b/images/level5.png differ diff --git a/images/level6.png b/images/level6.png new file mode 100644 index 0000000..6bb4ad5 Binary files /dev/null and b/images/level6.png differ diff --git a/images/level7.png b/images/level7.png new file mode 100644 index 0000000..f55763a Binary files /dev/null and b/images/level7.png differ diff --git a/images/level8.png b/images/level8.png new file mode 100644 index 0000000..a5c4c52 Binary files /dev/null and b/images/level8.png differ diff --git a/images/level9-1.png b/images/level9-1.png new file mode 100644 index 0000000..5c29454 Binary files /dev/null and b/images/level9-1.png differ diff --git a/images/level9-2.png b/images/level9-2.png new file mode 100644 index 0000000..22044ef Binary files /dev/null and b/images/level9-2.png differ diff --git a/images/level9.png b/images/level9.png new file mode 100644 index 0000000..9923db0 Binary files /dev/null and b/images/level9.png differ diff --git a/img.png b/img.png new file mode 100644 index 0000000..6cc5230 Binary files /dev/null and b/img.png differ diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..07bdbc8 --- /dev/null +++ b/pom.xml @@ -0,0 +1,31 @@ + + + 4.0.0 + + org.example + quote + 1.0-SNAPSHOT + + + + 21 + 21 + UTF-8 + + + + + com.google.code.gson + gson + 2.10.1 + + + + com.fasterxml.jackson.core + jackson-databind + 2.15.2 + + + \ No newline at end of file diff --git a/src/main/java/.idea/.gitignore b/src/main/java/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/src/main/java/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/src/main/java/.idea/misc.xml b/src/main/java/.idea/misc.xml new file mode 100644 index 0000000..84154e1 --- /dev/null +++ b/src/main/java/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/main/java/.idea/modules.xml b/src/main/java/.idea/modules.xml new file mode 100644 index 0000000..122a905 --- /dev/null +++ b/src/main/java/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/src/main/java/.idea/vcs.xml b/src/main/java/.idea/vcs.xml new file mode 100644 index 0000000..c2365ab --- /dev/null +++ b/src/main/java/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/main/java/Spring/App.java b/src/main/java/Spring/App.java new file mode 100644 index 0000000..2658bd7 --- /dev/null +++ b/src/main/java/Spring/App.java @@ -0,0 +1,7 @@ +package Spring; + +public class App { + public void run() { + + } +} diff --git a/src/main/java/Spring/Main.java b/src/main/java/Spring/Main.java new file mode 100644 index 0000000..ba58b2a --- /dev/null +++ b/src/main/java/Spring/Main.java @@ -0,0 +1,7 @@ +package Spring; + +public class Main { + public static void main(String[] args) { + new App().run(); + } +} diff --git a/src/main/java/Spring/WiseSaying.java b/src/main/java/Spring/WiseSaying.java new file mode 100644 index 0000000..e15d615 --- /dev/null +++ b/src/main/java/Spring/WiseSaying.java @@ -0,0 +1,4 @@ +package Spring; + +public class WiseSaying { +} diff --git a/src/main/java/Spring/WiseSayingController.java b/src/main/java/Spring/WiseSayingController.java new file mode 100644 index 0000000..250df6b --- /dev/null +++ b/src/main/java/Spring/WiseSayingController.java @@ -0,0 +1,4 @@ +package Spring; + +public class WiseSayingController { +} diff --git a/src/main/java/Spring/WiseSayingRepository.java b/src/main/java/Spring/WiseSayingRepository.java new file mode 100644 index 0000000..848e3f0 --- /dev/null +++ b/src/main/java/Spring/WiseSayingRepository.java @@ -0,0 +1,4 @@ +package Spring; + +public class WiseSayingRepository { +} diff --git a/src/main/java/Spring/WiseSayingService.java b/src/main/java/Spring/WiseSayingService.java new file mode 100644 index 0000000..360fb4d --- /dev/null +++ b/src/main/java/Spring/WiseSayingService.java @@ -0,0 +1,4 @@ +package Spring; + +public class WiseSayingService { +} diff --git a/src/main/java/noSpring/Level1.java b/src/main/java/noSpring/Level1.java new file mode 100644 index 0000000..61ece38 --- /dev/null +++ b/src/main/java/noSpring/Level1.java @@ -0,0 +1,18 @@ +package noSpring; + +import java.util.Scanner; + +public class Level1 { + public static void main(String[] args) { + Scanner sc=new Scanner(System.in); + System.out.println("== 명언 앱 =="); + + while (true) { + System.out.print("명령) "); + String command=sc.nextLine().trim(); + if (command.equals("종료")) { + break; + } + } + } +} diff --git a/src/main/java/noSpring/Level10.java b/src/main/java/noSpring/Level10.java new file mode 100644 index 0000000..d441135 --- /dev/null +++ b/src/main/java/noSpring/Level10.java @@ -0,0 +1,179 @@ +package noSpring; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.ObjectWriter; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.*; + +public class Level10 { + private static final String DB_PATH = "db/wiseSaying/"; + private static final String LAST_ID_FILE = DB_PATH + "lastId.txt"; + private static final ObjectMapper objectMapper = new ObjectMapper(); + private static final ObjectWriter objectWriter = objectMapper.writerWithDefaultPrettyPrinter(); + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + System.out.println("== 명언 앱 =="); + + List quotes = loadQuotes(); + int cnt = loadLastId(); + + while (true) { + System.out.print("명령) "); + String command = sc.nextLine().trim(); + + switch (command.split("\\?")[0]) { + case "종료": { + saveLastId(cnt); + return; + } + case "등록": { + System.out.print("명언 : "); + String quote = sc.nextLine().trim(); + + System.out.print("작가 : "); + String author = sc.nextLine().trim(); + + Quote newQuote = new Quote(cnt++, quote, author); + quotes.add(newQuote); + saveQuoteToFile(newQuote); + + System.out.printf("%d번 명언이 등록되었습니다.\n", newQuote.getId()); + break; + } + case "목록": { + System.out.println("번호 / 작가 / 명언"); + System.out.println("----------------------"); + quotes.stream() + .sorted(Comparator.comparingInt(Quote::getId).reversed()) + .forEach(System.out::println); + break; + } + case "삭제": { + String data = command.split("\\?")[1]; // id=1 + int findId = Integer.parseInt(data.split("=")[1]); + + boolean flag = false; + + for (int i = 0; i < quotes.size(); i++) { + if (quotes.get(i).getId() == findId) { + quotes.remove(i); + deleteQuoteFile(findId); + System.out.printf("%d번 명언이 삭제되었습니다.\n", findId); + flag = true; + break; + } + } + + if (!flag) { + System.out.printf("%d번 명언은 존재하지 않습니다.\n", findId); + } + break; + } + case "수정": { + String data = command.split("\\?")[1]; // id=1 + int findId = Integer.parseInt(data.split("=")[1]); + + boolean flag = false; + + for (int i = 0; i < quotes.size(); i++) { + Quote curQuote = quotes.get(i); + if (curQuote.getId() == findId) { + System.out.println("명언(기존) : " + curQuote.getContent()); + System.out.print("명언 : "); + String newContent = sc.nextLine().trim(); + + System.out.println("작가(기존) : " + curQuote.getAuthor()); + System.out.print("작가 : "); + String newAuthor = sc.nextLine().trim(); + + curQuote.setContent(newContent); + curQuote.setAuthor(newAuthor); + saveQuoteToFile(curQuote); + flag = true; + break; + } + } + + if (!flag) { + System.out.printf("%d번 명언은 존재하지 않습니다.\n", findId); + } + break; + } + case "빌드" : { + makeDataJson(quotes); + } + default: + break; + } + } + } + + private static void makeDataJson(List quotes) { + File file=new File(DB_PATH+"data.json"); + try { + objectWriter.writeValue(file,quotes); + System.out.println("data.json 파일의 내용이 갱신되었습니다."); + } + catch (IOException e) { + System.out.println("data.json 생성 실패"); + } + } + + private static List loadQuotes() { + List quotes = new ArrayList<>(); + File dir = new File(DB_PATH); + File[] files = dir.listFiles((d, name) -> name.endsWith(".json")); + + for (File file : files) { + try { + Quote quote = objectMapper.readValue(file, Quote.class); + quotes.add(quote); + } catch (IOException e) { + System.out.println("파일 로드 실패: " + file.getName()); + } + } + return quotes; + } + + private static int loadLastId() { + Path path = Path.of(LAST_ID_FILE); + if (Files.exists(path)) { + try { + String content = Files.readString(path).trim(); + return Integer.parseInt(content) + 1; + } catch (IOException e) { + System.out.println("lastId 파일 로드 실패"); + } + } + return 1; + } + + private static void saveLastId(int lastId) { + try { + Files.writeString(Path.of(LAST_ID_FILE), String.valueOf(lastId - 1)); + } catch (IOException e) { + System.out.println("lastId 저장 실패"); + } + } + + private static void saveQuoteToFile(Quote quote) { + File file = new File(DB_PATH + quote.getId() + ".json"); + try { + objectWriter.writeValue(file, quote); + } catch (IOException e) { + System.out.println("파일 저장 실패: " + file.getName()); + } + } + + private static void deleteQuoteFile(int id) { + File file = new File(DB_PATH + id + ".json"); + if (file.exists() && !file.delete()) { + System.out.println("파일 삭제 실패: " + file.getName()); + } + } +} \ No newline at end of file diff --git a/src/main/java/noSpring/Level2.java b/src/main/java/noSpring/Level2.java new file mode 100644 index 0000000..4a9cec4 --- /dev/null +++ b/src/main/java/noSpring/Level2.java @@ -0,0 +1,26 @@ +package noSpring; + +import java.util.Scanner; + +public class Level2 { + public static void main(String[] args) { + Scanner sc=new Scanner(System.in); + System.out.println("== 명언 앱 =="); + + while (true) { + System.out.print("명령) "); + String command=sc.nextLine().trim(); + if (command.equals("종료")) { + break; + } + else if (command.equals("등록")) { + System.out.print("명언 : "); + String quote=sc.nextLine().trim(); + + System.out.print("작가 : "); + String author=sc.nextLine().trim(); + + } + } + } +} diff --git a/src/main/java/noSpring/Level3.java b/src/main/java/noSpring/Level3.java new file mode 100644 index 0000000..bdf9604 --- /dev/null +++ b/src/main/java/noSpring/Level3.java @@ -0,0 +1,29 @@ +package noSpring; + +import java.util.Scanner; + +public class Level3 { + public static void main(String[] args) { + Scanner sc=new Scanner(System.in); + System.out.println("== 명언 앱 =="); + + int cnt=1; + while (true) { + System.out.print("명령) "); + String command=sc.nextLine().trim(); + if (command.equals("종료")) { + break; + } + else if (command.equals("등록")) { + System.out.print("명언 : "); + String quote = sc.nextLine().trim(); + + System.out.print("작가 : "); + String author = sc.nextLine().trim(); + + System.out.printf("%d번 명언이 등록되었습니다.", cnt); + System.out.println(); + } + } + } +} \ No newline at end of file diff --git a/src/main/java/noSpring/Level4.java b/src/main/java/noSpring/Level4.java new file mode 100644 index 0000000..6aae39c --- /dev/null +++ b/src/main/java/noSpring/Level4.java @@ -0,0 +1,29 @@ +package noSpring; + +import java.util.Scanner; + +public class Level4 { + public static void main(String[] args) { + Scanner sc=new Scanner(System.in); + System.out.println("== 명언 앱 =="); + + int cnt=1; + while (true) { + System.out.print("명령) "); + String command=sc.nextLine().trim(); + if (command.equals("종료")) { + break; + } + else if (command.equals("등록")) { + System.out.print("명언 : "); + String quote = sc.nextLine().trim(); + + System.out.print("작가 : "); + String author = sc.nextLine().trim(); + + System.out.printf("%d번 명언이 등록되었습니다.", cnt++); + System.out.println(); + } + } + } +} \ No newline at end of file diff --git a/src/main/java/noSpring/Level5.java b/src/main/java/noSpring/Level5.java new file mode 100644 index 0000000..e52c58f --- /dev/null +++ b/src/main/java/noSpring/Level5.java @@ -0,0 +1,51 @@ +package noSpring; + +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +public class Level5 { + public static void main(String[] args) { + Scanner sc=new Scanner(System.in); + System.out.println("== 명언 앱 =="); + List quotes=new ArrayList<>(); + + + int cnt=1; + while (true) { + System.out.print("명령) "); + String command=sc.nextLine().trim(); + switch (command) { + case "종료": { + return; + } + case "등록": { + System.out.print("명언 : "); + String quote = sc.nextLine().trim(); + + System.out.print("작가 : "); + String author = sc.nextLine().trim(); + + quotes.add(new Quote(cnt++,author,quote)); // 배열에 추가 + + System.out.printf("%d번 명언이 등록되었습니다.", cnt-1); + System.out.println(); + + break; + } + case "목록" : { + System.out.println("번호 / 작가 / 명언"); + System.out.println("---------------------------"); + for (int i = quotes.size()-1; i >=0 ; i--) { + System.out.println(quotes.get(i)); + } + break; + } + default: + break; + } + } + } +} + + diff --git a/src/main/java/noSpring/Level6.java b/src/main/java/noSpring/Level6.java new file mode 100644 index 0000000..4d0ac36 --- /dev/null +++ b/src/main/java/noSpring/Level6.java @@ -0,0 +1,65 @@ +package noSpring; + +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +public class Level6 { + public static void main(String[] args) { + Scanner sc=new Scanner(System.in); + System.out.println("== 명언 앱 =="); + List quotes=new ArrayList<>(); + + + int cnt=1; + while (true) { + System.out.print("명령) "); + String command=sc.nextLine().trim(); + // 삭제 기능을 위해 ? 기준으로 분리 + switch (command.split("\\?")[0]) { + case "종료": { + return; + } + case "등록": { + System.out.print("명언 : "); + String quote = sc.nextLine().trim(); + + System.out.print("작가 : "); + String author = sc.nextLine().trim(); + + quotes.add(new Quote(cnt++,author,quote)); // 배열에 추가 + + System.out.printf("%d번 명언이 등록되었습니다.", cnt-1); + System.out.println(); + + break; + } + case "목록" : { + System.out.println("번호 / 작가 / 명언"); + System.out.println("---------------------------"); + for (int i = quotes.size()-1; i >=0 ; i--) { + System.out.println(quotes.get(i)); + } + break; + } + case "삭제" : { + String data=command.split("\\?")[1]; // id=1 + int id=Integer.parseInt(data.split("=")[1]); + + for (int i=0;i quotes=new ArrayList<>(); + + + int cnt=1; + while (true) { + System.out.print("명령) "); + String command=sc.nextLine().trim(); + // 삭제 기능을 위해 ? 기준으로 분리 + switch (command.split("\\?")[0]) { + case "종료": { + return; + } + case "등록": { + System.out.print("명언 : "); + String quote = sc.nextLine().trim(); + + System.out.print("작가 : "); + String author = sc.nextLine().trim(); + + quotes.add(new Quote(cnt++,author,quote)); // 배열에 추가 + + System.out.printf("%d번 명언이 등록되었습니다.", cnt-1); + System.out.println(); + + break; + } + case "목록" : { + System.out.println("번호 / 작가 / 명언"); + System.out.println("---------------------------"); + for (int i = quotes.size()-1; i >=0 ; i--) { + System.out.println(quotes.get(i)); + } + break; + } + case "삭제" : { + String data=command.split("\\?")[1]; // id=1 + int findId=Integer.parseInt(data.split("=")[1]); + + boolean flag=false; + + for (int i=0;i quotes=new ArrayList<>(); + + + int cnt=1; + while (true) { + System.out.print("명령) "); + String command=sc.nextLine().trim(); + // 삭제 기능을 위해 ? 기준으로 분리 + switch (command.split("\\?")[0]) { + case "종료": { + return; + } + case "등록": { + System.out.print("명언 : "); + String quote = sc.nextLine().trim(); + + System.out.print("작가 : "); + String author = sc.nextLine().trim(); + + quotes.add(new Quote(cnt++,author,quote)); // 배열에 추가 + + System.out.printf("%d번 명언이 등록되었습니다.", cnt-1); + System.out.println(); + + break; + } + case "목록" : { + System.out.println("번호 / 작가 / 명언"); + System.out.println("---------------------------"); + for (int i = quotes.size()-1; i >=0 ; i--) { + System.out.println(quotes.get(i)); + } + break; + } + case "삭제" : { + String data=command.split("\\?")[1]; // id=1 + int findId=Integer.parseInt(data.split("=")[1]); + + boolean flag=false; + + for (int i=0;i quotes = loadQuotes(); + int cnt = loadLastId(); + + while (true) { + System.out.print("명령) "); + String command = sc.nextLine().trim(); + + switch (command.split("\\?")[0]) { + case "종료": { + saveLastId(cnt); + return; + } + case "등록": { + System.out.print("명언 : "); + String quote = sc.nextLine().trim(); + + System.out.print("작가 : "); + String author = sc.nextLine().trim(); + + Quote newQuote = new Quote(cnt++, quote, author); + quotes.add(newQuote); + saveQuoteToFile(newQuote); + + System.out.printf("%d번 명언이 등록되었습니다.\n", newQuote.getId()); + break; + } + case "목록": { + System.out.println("번호 / 작가 / 명언"); + System.out.println("----------------------"); + quotes.stream() + .sorted(Comparator.comparingInt(Quote::getId).reversed()) + .forEach(System.out::println); + break; + } + case "삭제": { + String data = command.split("\\?")[1]; // id=1 + int findId = Integer.parseInt(data.split("=")[1]); + + boolean flag = false; + + for (int i = 0; i < quotes.size(); i++) { + if (quotes.get(i).getId() == findId) { + quotes.remove(i); + deleteQuoteFile(findId); + System.out.printf("%d번 명언이 삭제되었습니다.\n", findId); + flag = true; + break; + } + } + + if (!flag) { + System.out.printf("%d번 명언은 존재하지 않습니다.\n", findId); + } + break; + } + case "수정": { + String data = command.split("\\?")[1]; // id=1 + int findId = Integer.parseInt(data.split("=")[1]); + + boolean flag = false; + + for (int i = 0; i < quotes.size(); i++) { + Quote curQuote = quotes.get(i); + if (curQuote.getId() == findId) { + System.out.println("명언(기존) : " + curQuote.getContent()); + System.out.print("명언 : "); + String newContent = sc.nextLine().trim(); + + System.out.println("작가(기존) : " + curQuote.getAuthor()); + System.out.print("작가 : "); + String newAuthor = sc.nextLine().trim(); + + curQuote.setContent(newContent); + curQuote.setAuthor(newAuthor); + saveQuoteToFile(curQuote); + flag = true; + break; + } + } + + if (!flag) { + System.out.printf("%d번 명언은 존재하지 않습니다.\n", findId); + } + break; + } + default: + break; + } + } + } + + private static List loadQuotes() { + List quotes = new ArrayList<>(); + File dir = new File(DB_PATH); + File[] files = dir.listFiles((d, name) -> name.endsWith(".json")); + + for (File file : files) { + try { + Quote quote = objectMapper.readValue(file, Quote.class); + quotes.add(quote); + } catch (IOException e) { + System.out.println("파일 로드 실패: " + file.getName()); + } + } + return quotes; + } + + private static int loadLastId() { + Path path = Path.of(LAST_ID_FILE); + if (Files.exists(path)) { + try { + String content = Files.readString(path).trim(); + return Integer.parseInt(content) + 1; + } catch (IOException | NumberFormatException e) { + System.out.println("lastId 파일 로드 실패"); + } + } + return 1; + } + + private static void saveLastId(int lastId) { + try { + Files.writeString(Path.of(LAST_ID_FILE), String.valueOf(lastId - 1)); + } catch (IOException e) { + System.out.println("lastId 저장 실패"); + } + } + + private static void saveQuoteToFile(Quote quote) { + File file = new File(DB_PATH + quote.getId() + ".json"); + try { + objectWriter.writeValue(file, quote); + } catch (IOException e) { + System.out.println("파일 저장 실패: " + file.getName()); + } + } + + private static void deleteQuoteFile(int id) { + File file = new File(DB_PATH + id + ".json"); + if (file.exists() && !file.delete()) { + System.out.println("파일 삭제 실패: " + file.getName()); + } + } +} \ No newline at end of file diff --git a/src/main/java/noSpring/Quote.java b/src/main/java/noSpring/Quote.java new file mode 100644 index 0000000..2eaaf9b --- /dev/null +++ b/src/main/java/noSpring/Quote.java @@ -0,0 +1,46 @@ +package noSpring; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonPropertyOrder; + +public class Quote { + private int id; + + private String author; + + private String content; + + public Quote() { + } + + public Quote(int id, String content, String author) { + this.id = id; + this.content = content; + this.author = author; + } + + public int getId() { + return id; + } + + public String getAuthor() { + return author; + } + + public String getContent() { + return content; + } + + public void setAuthor(String author) { + this.author = author; + } + + public void setContent(String content) { + this.content = content; + } + + @Override + public String toString() { + return id+" / "+author+" / "+ content; + } +} diff --git a/src/main/java/out/production/main/.idea/.gitignore b/src/main/java/out/production/main/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/src/main/java/out/production/main/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/src/main/java/out/production/main/.idea/misc.xml b/src/main/java/out/production/main/.idea/misc.xml new file mode 100644 index 0000000..84154e1 --- /dev/null +++ b/src/main/java/out/production/main/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/main/java/out/production/main/.idea/modules.xml b/src/main/java/out/production/main/.idea/modules.xml new file mode 100644 index 0000000..122a905 --- /dev/null +++ b/src/main/java/out/production/main/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/src/main/java/out/production/main/.idea/vcs.xml b/src/main/java/out/production/main/.idea/vcs.xml new file mode 100644 index 0000000..c2365ab --- /dev/null +++ b/src/main/java/out/production/main/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/main/java/out/production/main/Level1.class b/src/main/java/out/production/main/Level1.class new file mode 100644 index 0000000..d8a9ee9 Binary files /dev/null and b/src/main/java/out/production/main/Level1.class differ diff --git a/src/main/java/out/production/main/Level2.class b/src/main/java/out/production/main/Level2.class new file mode 100644 index 0000000..9643e26 Binary files /dev/null and b/src/main/java/out/production/main/Level2.class differ diff --git a/src/main/java/out/production/main/Level3.class b/src/main/java/out/production/main/Level3.class new file mode 100644 index 0000000..9eea849 Binary files /dev/null and b/src/main/java/out/production/main/Level3.class differ diff --git a/src/main/java/out/production/main/Level4.class b/src/main/java/out/production/main/Level4.class new file mode 100644 index 0000000..2522d63 Binary files /dev/null and b/src/main/java/out/production/main/Level4.class differ diff --git a/src/main/java/out/production/main/Level5.class b/src/main/java/out/production/main/Level5.class new file mode 100644 index 0000000..e7fc243 Binary files /dev/null and b/src/main/java/out/production/main/Level5.class differ diff --git a/src/main/java/out/production/main/Level6.class b/src/main/java/out/production/main/Level6.class new file mode 100644 index 0000000..04dff74 Binary files /dev/null and b/src/main/java/out/production/main/Level6.class differ diff --git a/src/main/java/out/production/main/Level7.class b/src/main/java/out/production/main/Level7.class new file mode 100644 index 0000000..8dd9f72 Binary files /dev/null and b/src/main/java/out/production/main/Level7.class differ diff --git a/src/main/java/out/production/main/Level8.class b/src/main/java/out/production/main/Level8.class new file mode 100644 index 0000000..aa66c01 Binary files /dev/null and b/src/main/java/out/production/main/Level8.class differ diff --git a/src/main/java/out/production/main/Quote.class b/src/main/java/out/production/main/Quote.class new file mode 100644 index 0000000..3c68eae Binary files /dev/null and b/src/main/java/out/production/main/Quote.class differ