package com.KoreaIT.java.AM;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
System.out.println("==프로그램 시작==");
Scanner sc = new Scanner(System.in);
int lastArticleId = 0;
List<Article> articles = new ArrayList<>();
while (true) {
System.out.print("명령어 > ");
String command = sc.nextLine().trim();
if (command.length() == 0) {
System.out.println("명령어를 입력해주세요");
continue;
}
if (command.equals("exit")) {
break;
}
if (command.equals("article list")) {
if (articles.size() == 0) {
System.out.println("게시글이 없습니다");
} else {
System.out.println(" 번호 // 제목 ");
for (int i = articles.size() - 1; i >= 0; i--) {
Article article = articles.get(i);
System.out.printf(" %d // %s \n", article.id, article.title);
}
}
} else if (command.equals("article write")) {
int id = lastArticleId + 1;
System.out.print("제목 : ");
String regDate = Util.getNowDateTimeStr();
String title = sc.nextLine();
System.out.print("내용 : ");
String body = sc.nextLine();
Article article = new Article(id, regDate, title, body);
articles.add(article);
System.out.printf("%d번글이 생성되었습니다\n", id);
lastArticleId++;
} else if (command.startsWith("article detail")) {
String[] cmdDiv = command.split(" ");
if (cmdDiv.length < 3) {
System.out.println("명령어를 확인해주세요");
continue;
}
int id = Integer.parseInt(cmdDiv[2]);
Article foundArticle = null;
for (int i = 0; i < articles.size(); i++) {
Article article = articles.get(i);
if (article.id == id) {
foundArticle = article;
break;
}
}
if (foundArticle == null) {
System.out.printf("%d번 게시물은 존재하지 않습니다\n", id);
continue;
}
System.out.println("번호 : " + foundArticle.id);
System.out.println("날짜 : " + foundArticle.regDate);
System.out.println("제목 : " + foundArticle.title);
System.out.println("내용 : " + foundArticle.body);
} else {
System.out.println("존재하지 않는 명령어입니다");
}
}
System.out.println("==프로그램 끝==");
sc.close();
}
}
class Article {
int id;
String regDate;
String title;
String body;
Article(int id, String regDate, String title, String body) {
this.id = id;
this.regDate = regDate;
this.title = title;
this.body = body;
}
}
메인 게시판
package com.KoreaIT.java.AM;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class Util {
/** 포맷팅 현재 날짜/시간 반환 Str */
public static String getNowDateTimeStr() {
// 현재 날짜/시간
LocalDateTime now = LocalDateTime.now();
// 포맷팅
String formatedNow = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
// 포맷팅 현재 날짜/시간 출력
return formatedNow;
}
}
시간용 util 패키지를 만들어준다
'java > java 게시판 만들기' 카테고리의 다른 글
게시판 만들기 3일차 "게시글 수정 기능 및 조회수 구현" (0) | 2023.03.30 |
---|---|
게시판 만들기 2일차 "게시물 삭제 기능 추가(index 활용)" (0) | 2023.03.30 |
게시판 만들기 2일차 "게시물 상세보기 글의 유무 판단" (0) | 2023.03.17 |
게시판 만들기 2일차 "게시물 상세보기 구현중" (0) | 2023.03.17 |
게시판 만들기 2일차 "게시물 목록 기능 구현" (0) | 2023.03.15 |