import java.util.Scanner;
import java.util.Calendar;
import java.text.SimpleDateFormat;
public class Main {
public static void main(String[] args) {
Scanner sn = new Scanner(System.in);
Board board = new Board();
board.setScanner(sn);
board.start();
sn.close();
}
}
class Board {
Scanner sn;
Article[] articles;
int articlesLastIndex;
Board() {
articles = new Article[100];
articlesLastIndex = -1;
}
// 게시물 객체 개수
int getArticlesCount() {
return articlesLastIndex + 1;
}
// 스캐너 통로
void setScanner(Scanner sn) {
this.sn = sn;
}
// 시작
void start() {
help();
while(true) {
System.out.printf("게시판) ");
String cmd = sn.next().trim();
if(cmd.equals("help")) {
sn.nextLine();
help();
}
else if(cmd.equals("add")) {
sn.nextLine();
doCmdAdd();
}
else if(cmd.equals("list")) {
sn.nextLine();
doCmdList();
}
else if(cmd.equals("detail")) {
int detNm = sn.nextInt();
sn.nextLine();
doCmddetail(detNm);
}
else if(cmd.equals("modify")) {
int modNm = sn.nextInt();
sn.nextLine();
doCmdmodify(modNm);
}
else if(cmd.equals("delete")) {
int delNm = sn.nextInt();
sn.nextLine();
doCmddelete(delNm);
}
else if(cmd.equals("exit")) {
sn.nextLine();
System.out.println("== 게시판 종료 ==");
break;
}
}
}
// 게시물 삭제
private void doCmddelete(int id) {
Article index = getIdIndex(id);
if(index == null) {
System.out.println("게시물이 없습니다.");
return;
} else {
for(int i = id; i <= articlesLastIndex-1; i++) {
articles[i] = articles[i+1];
}
articlesLastIndex--;
System.out.println("게시물 삭제완료.");
}
}
// 게시물 수정
void doCmdmodify(int id) {
Article article = getIdIndex(id);
if(article == null) {
System.out.println("게시물이 없습니다.");
return;
} else {
System.out.print("제목 : ");
String title = sn.nextLine().trim();
System.out.print("내용 : ");
String body = sn.nextLine().trim();
article.title = title;
article.body = body;
}
}
// 게시물 상세
void doCmddetail(int id) {
Article article = getIdIndex(id);
if(article == null) {
System.out.println("게시물이 없습니다.");
return;
} else {
System.out.printf("번호 : %d\n", article.id);
System.out.printf("날짜 : %s\n", article.regDate);
System.out.printf("제목 : %s\n", article.title);
System.out.printf("내용 : %s\n", article.body);
}
}
// 게시물 리스트
void doCmdList() {
if(articlesLastIndex == -1) {
System.out.println("게시물이 없습니다.");
} else {
System.out.println("번호, 날짜, 제목");
for(int i = 0; i <= articlesLastIndex; i++) {
Article article = articles[i];
System.out.printf("%d, %s, %s\n", article.id, article.regDate, article.title);
}
}
}
// 게시물 추가
void doCmdAdd() {
Article article = new Article();
int articleCnt = getArticlesCount();
int newNm;
Article art = getLastArticle();
if(art == null) {
newNm = 1;
} else {
newNm = art.id + 1;
}
article.id = newNm;
article.regDate = getNowDate();
System.out.print("제목 : ");
article.title = sn.nextLine().trim();
System.out.print("내용 : ");
article.body = sn.nextLine().trim();
System.out.printf("%d번 게시물이 저장되었습니다.\n", article.id);
articles[articleCnt] = article;
articlesLastIndex++;
}
// 마지막에 저장된 게시물 리모컨
Article getLastArticle() {
if(getArticlesCount() > 0) {
return articles[articlesLastIndex];
}
return null;
}
// id에 해당하는 객체 리모컨
Article getIdIndex(int id) {
for(int i = 0; i <= articlesLastIndex; i++) {
if(articles[i].id == id) {
return articles[i];
}
}
return null;
}
// 현재 날짜
String getNowDate() {
SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
String dateStr = date.format(cal.getTime());
return dateStr;
}
void help() {
System.out.println("== 명령어리스트 ==");
System.out.println("help : 명령어리스트");
System.out.println("list : 게시물리스트");
System.out.println("add : 게시물 추가");
System.out.println("exit : 종료");
System.out.println("detail ${게시물 번호} : 게시물 상세보기");
}
}
class Article {
int id;
String regDate;
String title;
String body;
}