본문 바로가기

강의

200506 - 텍스트 게시판 기능 구현(첫번째)

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Board board = new Board();
board.setScanner(sc);
board.start();
sc.close();
}
}

class Board {
Scanner sc;
Article[] articles;
int articlesLastIndex;

Board() {
articlesLastIndex = -1; // 아무것도 입력되지 않은 상태
articles = new Article[100];
}

int getArticlesCount() {
return articlesLastIndex + 1;
}

void setScanner(Scanner sc) {
this.sc = sc;
}

void start() {
showHelp();

while (true) {
System.out.print("게시판) ");
String command = sc.next().trim();

if (command.equals("help")) {
sc.nextLine();
doCommandHelp();
} else if (command.equals("list")) {
sc.nextLine();
doCommandList();
} else if (command.equals("add")) {
sc.nextLine();
doCommandAdd();
} else if (command.equals("detail")) {
int idToDetail = sc.nextInt();
sc.nextLine();
doCommandDetail(idToDetail);
} else if (command.equals("exit")) {
sc.nextLine();
doCommandExit();
break;
} else {
sc.nextLine();
System.out.println("일치하는 명령어가 없습니다.");
}
}
}

void doCommandDetail(int idToDetail) {
System.out.println("== 게시물 상세 ==");
// 구현하기
if (idToDetail > articlesLastIndex + 1 || idToDetail <= 0) {
System.out.println("해당 게시물은 존재하지 않습니다.");
} else {
idToDetail -= 1;
System.out.println("번호 : " + articles[idToDetail].id);
System.out.println("날짜 : " + articles[idToDetail].regDate);
System.out.println("제목 : " + articles[idToDetail].title);
System.out.println("내용 : " + articles[idToDetail].body);
}
}

void doCommandAdd() {
System.out.println("== 게시물 추가 ==");
// 구현하기
Article tempList = new Article();
articlesLastIndex = getArticlesCount();
tempList.id = getArticlesCount();
System.out.print("제목 : ");
tempList.title = sc.nextLine().trim();
System.out.print("내용 : ");
tempList.body = sc.nextLine().trim();
tempList.regDate = getNowDateStr();
System.out.printf("%d번 글이 생성되었습니다.\n", tempList.id);
articles[articlesLastIndex] = tempList;
}

void doCommandList() {
System.out.println("== 게시물 리스트 ==");
if (articlesLastIndex == -1) {
System.out.println("게시물이 존재하지 않습니다.");
} else {
System.out.println("번호 | 날짜             | 제목");
for (int i = 0; i <= articlesLastIndex; i++) {
System.out.println((articles[i].id) + " | " + articles[i].regDate + " | " + articles[i].title);
}
}
}

String getNowDateStr() {
Calendar cal = Calendar.getInstance();
SimpleDateFormat Date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateStr = Date.format(cal.getTime());
return dateStr;
}

void doCommandHelp() {
showHelp();
}

void showHelp() {
System.out.println("== 명령어 리스트 ==");
System.out.println("help : 명령어 리스트");
System.out.println("list : 게시물 리스팅");
System.out.println("add : 게시물 추가");
System.out.println("exit : 종료");
System.out.println("detail {$게시물 번호} : 게시물 상세보기");
}

void doCommandExit() {
System.out.println("== 게시판 종료 ==");
}
}

class Article {
int id;
String regDate;
String title;
String body;
}

'강의' 카테고리의 다른 글

200507 - 게시판 만들기  (0) 2020.05.07
200507 - 게시판 만들기(코드)  (0) 2020.05.07
200506 - 게시물 삭제, 수정 추가  (0) 2020.05.07
2020년 04월 14일, 공부계획  (0) 2020.04.14
2020년 04월 09일, 공부계획  (0) 2020.04.09