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
29 changes: 29 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
### IntelliJ IDEA ###
out/
!**/src/main/**/out/
!**/src/test/**/out/

### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/

### VS Code ###
.vscode/

### Mac OS ###
.DS_Store
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
# code-review
# 명언 게시판 만들기
DevCourse Backend 4기


https://velog.io/@jxxnging/JAVA-명언-게시판-만들기-1

https://velog.io/@jxxnging/JAVA-명언-게시판-만들기-2
5 changes: 5 additions & 0 deletions db/wiseSaying/1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"id": 1,
"content": "너 자신을 알라",
"author": "소크라테스"
}
5 changes: 5 additions & 0 deletions db/wiseSaying/2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"id": 2,
"content": "주사위는 던져졌다",
"author": "율리우스 카이사르"
}
5 changes: 5 additions & 0 deletions db/wiseSaying/3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"id": 3,
"content": "그래도 지구는 돈다",
"author": "갈릴레오 갈릴레이"
}
5 changes: 5 additions & 0 deletions db/wiseSaying/5.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"id": 5,
"content": "집에 가고 싶다",
"author": "작자미상"
}
22 changes: 22 additions & 0 deletions db/wiseSaying/data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[
{
"id": 5,
"content": "집에 가고 싶다",
"author": "작자미상"
},
{
"id": 3,
"content": "그래도 지구는 돈다",
"author": "갈릴레오 갈릴레이"
},
{
"id": 2,
"content": "주사위는 던져졌다",
"author": "율리우스 카이사르"
},
{
"id": 1,
"content": "너 자신을 알라",
"author": "소크라테스"
}
]
1 change: 1 addition & 0 deletions db/wiseSaying/lastId.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
5
237 changes: 237 additions & 0 deletions src/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
import java.io.*;
import java.util.*;

public class Main {
public static void main(String[] args) {
String action = "";
String content = "";
String author = "";

int targetId = 0;
int id = 1;
if (getLastId() != 1) {
id = getLastId() + 1;
}

ArrayList<WiseSaying> list = load();

Scanner sc = new Scanner(System.in);

System.out.println("== 명언 앱 ==");

while (!action.equals("종료")) {
System.out.print("명령) ");
action = sc.nextLine();

switch (action) {
case "등록":
System.out.print("명언 : ");
content = sc.nextLine();
System.out.print("작가 : ");
author = sc.nextLine();

WiseSaying saying = new WiseSaying(id, content, author);
save(saying);
list.add(saying);
System.out.println(id + "번 명언이 등록되었습니다.");

id++;
updateLastId(id);
break;

case "목록":
Collections.sort(list, new Comparator<WiseSaying>() {
public int compare(WiseSaying ws1, WiseSaying ws2) {
return Integer.compare(ws2.getId(), ws1.getId());
}
});

System.out.println("번호 / 작가 / 명언");
System.out.println("----------------------");

if (list.isEmpty()) {
System.out.println("등록된 명언이 없습니다.");
} else {
for (WiseSaying ws : list) {
System.out.println(ws.getId() + " / " + ws.getAuthor() + " / " + ws.getContent());
}
}
break;

case "삭제":
System.out.print("id=");
targetId = sc.nextInt();
sc.nextLine();

WiseSaying deleteWS = getSaying(list, targetId);
if (deleteWS != null) {
list.remove(deleteWS);
delete(deleteWS);
}
break;

case "수정":
System.out.print("id=");
targetId = sc.nextInt();
sc.nextLine();

WiseSaying updateWS = getSaying(list, targetId);
if (updateWS != null) {
System.out.println("명언(기존) : " + updateWS.getContent());
System.out.print("명언 : ");
content = sc.nextLine();

System.out.println("작가(기존) : " + updateWS.getAuthor());
System.out.print("작가 : ");
author = sc.nextLine();

updateWS.setContent(content);
updateWS.setAuthor(author);
save(updateWS);
System.out.println(targetId + "번 명언이 수정되었습니다.");
} else {
System.out.println(targetId + "번 명언은 존재하지 않습니다.");
}
break;

case "빌드":
build(list);
break;

case "종료":
if (!list.isEmpty()) {
updateLastId(id - 1);
} else {
updateLastId(1);
}
break;

default:
System.out.println("올바른 명령어를 입력해주세요.");
break;
}
}

sc.close();
}

public static int getLastId() {
try {
BufferedReader reader = new BufferedReader(new FileReader("db/wiseSaying/lastId.txt"));
String lastId = reader.readLine();
reader.close();
return Integer.parseInt(lastId);
} catch (IOException e) {
return 1;
}
}

public static void updateLastId(int id) {
try {
FileWriter writer = new FileWriter("db/wiseSaying/lastId.txt");
writer.write(String.valueOf(id));
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}

public static void save(WiseSaying ws) {
try {
String json = ws.toJson();

File file = new File("db/wiseSaying/" + ws.getId() + ".json");
file.getParentFile().mkdirs();

FileWriter writer = new FileWriter(file);
writer.write(json);
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}

public static void delete(WiseSaying ws) {
try {
File file = new File("db/wiseSaying/" + ws.getId() + ".json");
if (file.exists()) {
if (file.delete()) {
System.out.println(ws.getId() + "번 명언이 삭제되었습니다.");
}
} else {
System.out.println(ws.getId() + "번 명언은 존재하지 않습니다.");
}
} catch (Exception e) {
e.printStackTrace();
}
}

public static void build(ArrayList<WiseSaying> list) {
try {
StringBuilder jsonBuilder = new StringBuilder("[\n");

for (int i = 0; i < list.size(); i++) {
jsonBuilder.append("\t{\n")
.append("\t\t\"id\": ").append(list.get(i).getId()).append(",\n")
.append("\t\t\"content\": \"").append(list.get(i).getContent()).append("\",\n")
.append("\t\t\"author\": \"").append(list.get(i).getAuthor()).append("\"\n")
.append("\t}");
if (i < list.size() - 1) {
jsonBuilder.append(",");
}
jsonBuilder.append("\n");
}
jsonBuilder.append("]");

File file = new File("db/wiseSaying/data.json");
file.getParentFile().mkdirs();

FileWriter writer = new FileWriter(file);
writer.write(jsonBuilder.toString());
writer.close();

System.out.println("data.json 파일의 내용이 갱신되었습니다.");
} catch (IOException e) {
e.printStackTrace();
}
}

public static ArrayList<WiseSaying> load() {
ArrayList<WiseSaying> list = new ArrayList<WiseSaying>();
File dir = new File("db/wiseSaying");
File[] files = dir.listFiles((d, name) -> name.endsWith(".json"));

if (files != null) {
for (File file : files) {
if (file.getName().equals("data.json")) {
continue;
}

try {
BufferedReader reader = new BufferedReader(new FileReader(file));
StringBuilder json = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
json.append(line);
}
reader.close();

WiseSaying ws = WiseSaying.fromJson(json.toString());
list.add(ws);
} catch (IOException e) {
e.printStackTrace();
}
}
}
return list;
}

public static WiseSaying getSaying(ArrayList<WiseSaying> list, int id) {
for (WiseSaying ws : list) {
if (ws.getId() == id) {
return ws;
}
}
return null;
}
}
Loading