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/workspace.xml b/.idea/workspace.xml
index a4a3077..a1992c8 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -5,13 +5,20 @@
+
+
+
+
+
+
+
@@ -72,6 +79,7 @@
-
+
@@ -104,8 +112,33 @@
+
+
+
+
+
+
+
+
+
+
+ false
+ true
+ false
+ true
+
+
+
@@ -131,6 +164,8 @@
+
+
@@ -148,7 +183,7 @@
-
+
diff --git a/diary.txt b/diary.txt
new file mode 100644
index 0000000..3397f58
--- /dev/null
+++ b/diary.txt
@@ -0,0 +1,4 @@
+1 test
+3 test3
+4 testest
+2 update
diff --git a/id.txt b/id.txt
new file mode 100644
index 0000000..bf0d87a
--- /dev/null
+++ b/id.txt
@@ -0,0 +1 @@
+4
\ No newline at end of file
diff --git a/src/main/java/org/sopt/week1/DiaryRepository.java b/src/main/java/org/sopt/week1/DiaryRepository.java
index 46057da..b96695f 100644
--- a/src/main/java/org/sopt/week1/DiaryRepository.java
+++ b/src/main/java/org/sopt/week1/DiaryRepository.java
@@ -1,60 +1,214 @@
package org.sopt.week1;
+import java.io.*;
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 Map trash = new HashMap<>();
- private final AtomicLong numbering = new AtomicLong();
+ File diaryFile = new File("diary.txt");
+ File idFile = new File("id.txt");
+ File trashFile = new File("trash.txt");
+ File tempFile = new File("tempDiary.txt");
void save(final Diary diary) {
- // 채번 과정
- final long id = numbering.addAndGet(1);
- // 저장 과정
- storage.put(id, diary.getBody());
+
+ long lastId = loadLastId();
+ final long id = lastId + 1;
+
+ try {
+ BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(diaryFile, true));
+
+ if (diaryFile.isFile() && diaryFile.canWrite()) {
+ bufferedWriter.write(id + " " + diary.getBody());
+ bufferedWriter.newLine();
+ bufferedWriter.close();
+ }
+
+ saveLastId(id);
+
+ } catch (IOException e) {
+ System.out.println(e.getMessage());
+ }
}
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));
+
+ try {
+ FileReader fileReader = new FileReader(diaryFile);
+ BufferedReader bufferedReader = new BufferedReader(fileReader);
+ String diaryFileInfo = "";
+
+ while ((diaryFileInfo = bufferedReader.readLine()) != null) {
+ List diaryInfo = TextUtils.splitDiaryInfo(diaryFileInfo);
+ diaryList.add(new Diary(Long.parseLong(diaryInfo.get(0)), diaryInfo.get(1)));
}
+
+ } catch (IOException e) {
+ System.out.println(e.getMessage());
}
- // (3) 불러온 자료구조를 응답
return diaryList;
}
void deleteById(final Long id) {
isValidateId(id);
- trash.put(id, storage.get(id));
- storage.remove(id);
+ try {
+ // diaryFile에서 삭제할 ID를 제외하고 임시 파일에 저장
+ BufferedWriter tempWriter = new BufferedWriter(new FileWriter(tempFile));
+ BufferedReader diaryReader = new BufferedReader(new FileReader(diaryFile));
+ BufferedWriter trashWriter = new BufferedWriter(new FileWriter(trashFile, true));
+
+ String currentLine;
+ boolean isFound = false;
+
+ while ((currentLine = diaryReader.readLine()) != null) {
+ List diaryInfo = TextUtils.splitDiaryInfo(currentLine);
+ if (diaryInfo.get(0).equals(id.toString())) {
+ // 삭제할 Diary를 trash.txt에 저장
+ trashWriter.write(currentLine);
+ trashWriter.newLine();
+ isFound = true; // ID를 찾았음을 표시
+ } else {
+ tempWriter.write(currentLine);
+ tempWriter.newLine();
+ }
+ }
+
+ tempWriter.close();
+ diaryReader.close();
+ trashWriter.close();
+
+ // ID가 존재했으면 임시 파일을 원본 파일로 대체
+ if (isFound) {
+ diaryFile.delete();
+ tempFile.renameTo(diaryFile);
+ } else {
+ // ID가 존재하지 않을 경우 임시 파일을 삭제
+ tempFile.delete();
+ throw new IllegalArgumentException("해당 ID의 Diary가 존재하지 않습니다.");
+ }
+
+ } catch (IOException e) {
+ System.out.println(e.getMessage());
+ }
}
void updateById(final Long id, final String body) {
isValidateId(id);
- storage.replace(id, body);
+ try {
+ BufferedWriter tempWriter = new BufferedWriter(new FileWriter(tempFile));
+ BufferedReader diaryReader = new BufferedReader(new FileReader(diaryFile));
+
+ String currentLine;
+ boolean isFound = false;
+
+ while ((currentLine = diaryReader.readLine()) != null) {
+ List diaryInfo = TextUtils.splitDiaryInfo(currentLine);
+ if (diaryInfo.get(0).equals(id.toString())) {
+ // 수정된 내용으로 기록
+ tempWriter.write(id + " " + body);
+ tempWriter.newLine();
+ isFound = true; // ID를 찾았음을 표시
+ } else {
+ tempWriter.write(currentLine);
+ tempWriter.newLine();
+ }
+ }
+
+ tempWriter.close();
+ diaryReader.close();
+
+ // ID가 존재했으면 임시 파일을 원본 파일로 대체
+ if (isFound) {
+ diaryFile.delete();
+ tempFile.renameTo(diaryFile);
+ } else {
+ // ID가 존재하지 않을 경우 임시 파일을 삭제
+ tempFile.delete();
+ throw new IllegalArgumentException("해당 ID의 Diary가 존재하지 않습니다.");
+ }
+
+ } catch (IOException e) {
+ System.out.println(e.getMessage());
+ }
}
void restoreAll() {
- for (Long id : trash.keySet()) {
- storage.put(id, trash.get(id));
+ try {
+ BufferedReader trashReader = new BufferedReader(new FileReader(trashFile));
+ BufferedWriter diaryWriter = new BufferedWriter(new FileWriter(diaryFile, true)); // append mode로 diary.txt에 추가
+
+ String currentLine;
+ boolean isRestored = false;
+
+ while ((currentLine = trashReader.readLine()) != null) {
+ diaryWriter.write(currentLine);
+ diaryWriter.newLine();
+ isRestored = true; // 복원 작업이 있음을 표시
+ }
+
+ trashReader.close();
+ diaryWriter.close();
+
+ // 복원이 완료되었으면 trash.txt 파일 비우기
+ if (isRestored) {
+ BufferedWriter trashCleaner = new BufferedWriter(new FileWriter(trashFile)); // trash.txt 초기화
+ trashCleaner.write("");
+ trashCleaner.close();
+ }
+
+ } catch (IOException e) {
+ System.out.println(e.getMessage());
}
- trash.clear();
}
+
void isValidateId(final Long id) {
- if (storage.get(id) == null) {
+ List diaryIds = new ArrayList();
+
+ try {
+ FileReader fileReader = new FileReader(diaryFile);
+ BufferedReader bufferedReader = new BufferedReader(fileReader);
+ String diaryFileInfo = "";
+
+
+ while ((diaryFileInfo = bufferedReader.readLine()) != null) {
+ List diaryInfo = TextUtils.splitDiaryInfo(diaryFileInfo);
+ diaryIds.add(Long.parseLong(diaryInfo.get(0)));
+ }
+ } catch (IOException e) {
+ System.out.println(e.getMessage());
+ }
+ if (!diaryIds.contains(id)) {
throw new IllegalArgumentException("존재하지 않는 일기 id입니다.");
}
}
+
+ private long loadLastId() {
+ long lastId = 0;
+ if (idFile.exists()) {
+ try {
+ BufferedReader bufferedReader = new BufferedReader(new FileReader(idFile));
+ String idStr = bufferedReader.readLine(); // ID 값을 한 줄로 저장
+ if (idStr != null) {
+ lastId = Long.parseLong(idStr);
+ }
+ bufferedReader.close();
+ } catch (IOException e) {
+ System.out.println(e.getMessage());
+ }
+ }
+ return lastId;
+ }
+
+ private void saveLastId(long id) {
+ try {
+ BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(idFile));
+ bufferedWriter.write(Long.toString(id));
+ bufferedWriter.close();
+ } catch (IOException e) {
+ System.out.println(e.getMessage());
+ }
+ }
+
}
diff --git a/src/main/java/org/sopt/week1/TextUtils.java b/src/main/java/org/sopt/week1/TextUtils.java
index 750dc0e..6abd078 100644
--- a/src/main/java/org/sopt/week1/TextUtils.java
+++ b/src/main/java/org/sopt/week1/TextUtils.java
@@ -1,5 +1,7 @@
package org.sopt.week1;
+import java.util.ArrayList;
+import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -7,6 +9,7 @@ public class TextUtils {
private static final Pattern graphemePattern = Pattern.compile("\\X");
private static final Matcher graphemeMatcher = graphemePattern.matcher("");
+ private static final String SPLIT_CHARACTERS = " ";
public static int getLengthOfBody(final String body) {
if (body == null) {
@@ -21,4 +24,8 @@ public static int getLengthOfBody(final String body) {
}
return count;
}
+
+ public static List splitDiaryInfo(String diaryInfo) {
+ return new ArrayList(List.of(diaryInfo.split(SPLIT_CHARACTERS)));
+ }
}
diff --git a/trash.txt b/trash.txt
new file mode 100644
index 0000000..e69de29