저번에 했던 Motivation 프로그램에 이어서 이번에는 게시글을 작성해 보려고 해.
게시글도 마찬가지로 CRUD가 가능해야하고 회원가입이랑 로그인기능을 추가적으로 구현 할 계획이야.
이건 Motivation에서 했던 내용이라 쉽게 할 수 있지?? 뭘요..? 이거를 쉽게 하라고요?
- 게시글 작성
int lastArticleId = 0;
List<Article> articles = new ArrayList<>();
while (true) {
System.out.print("명령어) ");
String cmd = sc.nextLine().trim();
if (cmd.length() == 0) {
System.out.println("명령어를 입력하세요");
continue;
}
if (cmd.equals("exit")) {
break;
}
if (cmd.equals("article write")) {
System.out.println("==게시글 작성==");
int id = lastArticleId + 1;
System.out.print("제목 : ");
String title = sc.nextLine().trim();
System.out.print("내용 : ");
String body = sc.nextLine().trim();
Article article = new Article(id, title, body);
articles.add(article);
System.out.println(id + "번 글이 작성되었습니다");
lastArticleId++;
}
class Article {
private int id;
private String title;
private String body;
public Article(int id, String title, String body) {
this.id = id;
this.title = title;
this.body = body;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
} ... 너무 길어서 생략할게
이제 감이 좀 잡히나?
감이요?감하니까 갑자기 홍시가 너무 먹고싶다.. 숫가락으로 퍼 먹..으면 맛있겠는걸..
- 상세보기 작성
else if (cmd.startsWith("article detail")) {
System.out.println("==게시글 상세보기==");
int id = Integer.parseInt(cmd.split(" ")[2]);
Article foundArticle = null;
for (Article article : articles) {
if (article.getId() == id) {
foundArticle = article;
break;
}
}
if (foundArticle == null) {
System.out.println("해당 게시글은 없습니다");
continue;
}
System.out.println("번호 : " + foundArticle.getId());
System.out.println("제목 : " + foundArticle.getTitle());
System.out.println("내용 : " + foundArticle.getBody());
}
- 게시글 삭제
else if (cmd.startsWith("article delete")) {
System.out.println("==게시글 삭제==");
int id = Integer.parseInt(cmd.split(" ")[2]);
Article foundArticle = null;
for (Article article : articles) {
if (article.getId() == id) {
foundArticle = article;
break;
}
}
if (foundArticle == null) {
System.out.println("해당 게시글은 없습니다");
continue;
}
articles.remove(foundArticle);
System.out.println(id + "번 게시글이 삭제되었습니다");
}
- 게시글 수정
else if (cmd.startsWith("article modify")) {
System.out.println("==게시글 수정==");
int id = Integer.parseInt(cmd.split(" ")[2]);
Article foundArticle = null;
for (Article article : articles) {
if (article.getId() == id) {
foundArticle = article;
break;
}
}
if (foundArticle == null) {
System.out.println("해당 게시글은 없습니다");
continue;
}
System.out.println("기존 제목 : " + foundArticle.getTitle());
System.out.println("기존 내용 : " + foundArticle.getBody());
System.out.print("새 제목 : ");
String newTitle = sc.nextLine().trim();
System.out.print("새 내용 : ");
String newBody = sc.nextLine().trim();
foundArticle.setTitle(newTitle);
foundArticle.setBody(newBody);
System.out.println(id + "번 게시글이 수정되었습니다");
} else {
System.out.println("사용할 수 없는 명령어입니다");
}
}
- 날짜등록
날짜 등록은 jdk에 Localdatetime이라고 아주 간편한 기능이 있어서 그걸 가져다가 사용할거야.
일단 날짜 등록은 따로 클래스를 만들어서 분리를 하자.
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class Util {
public static String getNowStr() {
LocalDateTime now = LocalDateTime.now();
String formatedNow = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
return formatedNow;
}
}
if (cmd.equals("article write")) {
System.out.println("==게시글 작성==");
int id = lastArticleId + 1;
String regDate = Util.getNowStr();
String updateDate = Util.getNowStr();
System.out.print("제목 : ");
String title = sc.nextLine().trim();
System.out.print("내용 : ");
String body = sc.nextLine().trim();
이런식으로 작성할때 값을 넘겨주고,
for (int i = forPrintArticles.size() - 1; i >= 0; i--) {
Article article = forPrintArticles.get(i);
if (Util.getNowStr().split(" ")[0].equals(article.getRegDate().split(" ")[0])) {
System.out.printf(" %d / %s / %s / %s \n", article.getId(), article.getRegDate().split(" ")[1], article.getTitle(), article.getBody());
} else {
System.out.printf(" %d / %s / %s / %s \n", article.getId(), article.getRegDate().split(" ")[0], article.getTitle(), article.getBody());
}
}