728x90
import java.util.Scanner;
import java.util.*;
import java.text.*;
class Main{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Board board = new Board();
board.setScanner(scanner);
board.start();
}
}
class Board{
Scanner scanner;
void setScanner(Scanner scanner) {
this.scanner = scanner;
}
Article[] articleArr = new Article[100];
int articleArrLastIndex = -1;
int lastArticleId = 0;
void start() {
commandHelp();
while(true) {
System.out.println("명령어입력:");
String command = scanner.next().trim();
if(command.equals("help")) {
scanner.nextLine();
commandHelp();
} else if(command.equals("add")) {
scanner.nextLine();
commandAdd();
} else if(command.equals("list")) {
scanner.nextLine();
commandList();
} else if(command.equals("detail")) {
int detailIn = scanner.nextInt();
scanner.nextLine();
commandDetail(detailIn);
} else if(command.equals("modify")) {
int modifyIn = scanner.nextInt();
scanner.nextLine();
commandModify(modifyIn);
} else if(command.equals("delete")) {
int deleteIn = scanner.nextInt();
scanner.nextLine();
commandDelete(deleteIn);
commandList();
} else if(command.equals("exit")) {
scanner.nextLine();
System.out.println("게시판종료");
break;
}
}
}
void commandHelp() {
System.out.println("===명령어 리스트===");
System.out.println("help");
System.out.println("list");
System.out.println("add");
System.out.println("modify");
System.out.println("delete");
System.out.println("detail + 게시물번호");
System.out.println("exit");
}
void commandAdd() {
Article newArticle = new Article();
lastArticleId ++;
System.out.printf("제목: ");
newArticle.title = scanner.next();
scanner.nextLine();
System.out.printf("내용: ");
newArticle.body=scanner.next();
newArticle.date= getNowDateStr();
System.out.printf("날짜: %s\n", newArticle.date);
newArticle.id = lastArticleId;
System.out.printf("%d번 게시물이 추가되었습니다.\n", newArticle.id);
articleArrLastIndex ++;
articleArr[articleArrLastIndex] = newArticle;
}
void commandList() {
System.out.println("=게시물 리스트=");
System.out.println("번호 |날짜 |제목");
for(int i=0; i<=articleArrLastIndex; i++) {
System.out.printf("%d |%s |%s\n",articleArr[i].id,articleArr[i].date,articleArr[i].title);
}
}
Article getArticleById(int num) {
for(int i=0; i<=articleArrLastIndex; i++) {
if(articleArr[i].id == num) {
return articleArr[i];
}
}
return null;
}
void commandDetail(int num) {
Article temp = getArticleById(num);
if(temp==null) {
System.out.println("해당 게시물은 존재하지 않습니다.");
return;
}
System.out.println("=게시물 상세=");
System.out.printf("번호:%d \n",temp.id);
System.out.printf("날짜:%s \n",temp.date);
System.out.printf("제목:%s \n",temp.title);
System.out.printf("내용:%s \n",temp.body);
}
void commandModify(int num) {
Article temp = getArticleById(num);
if(temp==null) {
System.out.println("해당 게시물은 존재하지 않습니다.");
return;
}
System.out.println("=게시물 수정=");
System.out.printf("제목: ");
temp.title = scanner.next();
scanner.nextLine();
System.out.printf("내용: ");
temp.body=scanner.next();
}
void commandDelete(int num) {
Article temp = getArticleById(num);
if(temp==null) {
System.out.println("해당 게시물은 존재하지 않습니다.");
return;
}
int index = temp.id -1 ;
for(int i=index; i<=articleArrLastIndex-1; i++) {
articleArr[i] = articleArr[i+1];
}
System.out.printf("%d번 게시물이 삭제되었습니다.", temp.id);
articleArrLastIndex --;
}
String getNowDateStr() {
SimpleDateFormat formatter = new SimpleDateFormat ( "yyyy.MM.dd HH:mm:ss", Locale.KOREA );
Date currentTime = new Date ( );
String dTime = formatter.format ( currentTime );
return dTime;
}
}
class Article{
int id;
String date;
String title;
String body;
}
728x90
반응형
'IT관련 > JAVA_자유 노트' 카테고리의 다른 글
[수업창작]게시판만들기 20-05-17 (0) | 2020.05.17 |
---|---|
[수업창작] 게시판만들기 20-05-15(PP) (0) | 2020.05.15 |
[수업창작] 게시판만들기 20-05-11 (0) | 2020.05.11 |
[수업창작] 게시판만들기 20-05-10 (0) | 2020.05.10 |
[수업창작] 게시판만들기 20-05-08 (0) | 2020.05.08 |
댓글