import java.util.Scanner;
import java.util.Calendar;
import java.text.SimpleDateFormat;
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 = new Article[1];
int articleLastIndex = -1;
int articleListNum = 0;
Member[] members = new Member[2];
int memberLastIndex = 0;
boolean joinId; // 회원가입 판별
Member logIdPw; // 로그인 판별
boolean statelogin; // 로그인, 로그아웃 판별
int adminMatche; // 관리자 판별
int joinNm = 0; // 회원 넘버
// 스캐너 통로
void setScanner(Scanner sc) {
this.sc = sc;
}
int getArticlesCount() {
return articleLastIndex + 1;
}
// 시작 함수
void start() {
help();
while(true) {
System.out.println("");
System.out.print("게시판 명령어 : ");
String cmd = sc.next().trim();
System.out.println("");
if(cmd.equals("help")) {
sc.nextLine();
help();
} else if(cmd.equals("exit")) {
sc.nextLine();
exit();
break;
} else if(cmd.equals("list")) {
System.out.println("== 게시물 리스트 ==");
sc.nextLine();
list();
} else if(cmd.equals("add")) {
System.out.println("== 게시물 추가 ==");
sc.nextLine();
add();
} else if(cmd.equals("detail")) {
System.out.println("== 게시물 상세보기 ==");
System.out.println("어떤 게시물을 보시겠습니까?");
System.out.print("> ");
int detNm = sc.nextInt();
sc.nextLine();
detail(detNm);
} else if(cmd.equals("delete")) {
System.out.println("== 게시물 삭제 ==");
System.out.println("어떤 게시물을 삭제겠습니까?");
System.out.print("> ");
int delNm = sc.nextInt();
sc.nextLine();
delete(delNm);
} else if(cmd.equals("modify")) {
System.out.println("== 게시물 수정 ==");
System.out.println("몇번째 게시물을 수정하시겠습니까?");
System.out.print("> ");
int modiNm = sc.nextInt();
sc.nextLine();
modify(modiNm);
} else if(cmd.equals("join")) {
System.out.println("== 회원가입 ==");
sc.nextLine();
join();
} else if(cmd.equals("login")) {
System.out.println("== 로그인 ==");
sc.nextLine();
login();
} else if(cmd.equals("logout")) {
System.out.println("== 로그아웃 ==");
sc.nextLine();
logout();
} else {
System.out.println("올바른 명령어를 입력해 주세요.");
}
}
}
// 게시물 리스트
void list() {
int article = getArticlesCount();
if(article == 0) {
System.out.println("게시물이 없습니다.");
} else {
System.out.printf("번호 | 날짜 | 조회수 | 제목 | 작성자\n");
for (int i = 0; i <= articleLastIndex; i++) {
System.out.printf("%d | %s | %d | %s | %s\n", articles[i].num, articles[i].date, articles[i].view, articles[i].title, articles[i].memberId);
}
}
}
// 게시물 추가
void add() {
Article article = new Article();
if(statelogin || adminMatche == 1) {
articleLastIndex++;
article.num = articleListNum+1;
article.date = getNowDate();
if(adminMatche == 1) {
article.memberNum = 0;
} else {
article.memberNum = logIdPw.mNum;
}
System.out.print("제목 : ");
article.title = sc.nextLine().trim();
System.out.print("내용 : ");
article.body = sc.nextLine().trim();
article.memberId = logIdPw.loginId;
System.out.printf("%d번 게시물이 추가되었습니다.\n", articleListNum+1);
articleListNum++;
articles[articleLastIndex] = article;
sizeUpArticleIfFull();
} else {
System.out.println("로그인 후 이용하실 수 있습니다.");
}
}
// 게시물 상세
void detail(int num) {
Article article = getArticleByNum(num);
if(statelogin || adminMatche == 1) {
if(article == null) {
System.out.printf("%d번에 해당하는 게시물이 없습니다.\n", num);
} else {
article.view++;
System.out.printf("번호 : %d\n", article.num);
System.out.printf("날짜 : %s\n", article.date);
System.out.printf("제목 : %s\n", article.title);
System.out.printf("내용 : %s\n", article.body);
System.out.printf("작성자 : %s\n", logIdPw.name);
System.out.printf("조회수 : %d\n", article.view);
}
} else {
System.out.println("로그인 후 이용하실 수 있습니다.");
}
}
// 게시물 수정
void modify(int num) {
Article article = getArticleByNum(num);
if(statelogin || adminMatche == 1) {
if(article == null) {
System.out.printf("%d번에 해당하는 게시물이 없습니다.\n", num);
} else if(logIdPw.mNum == article.memberNum || adminMatche == 1) {
System.out.print("수정하실 제목을 입력해주세요 : ");
article.title = sc.nextLine();
System.out.print("수정하실 내용을 입력해주세요 : ");
article.body = sc.nextLine();
article.date = getNowDate();
System.out.println("게시물 수정이 완료되었습니다.");
} else {
System.out.println("작성자만 수정 가능합니다.");
}
}
else {
System.out.println("로그인 후 이용하실 수 있습니다.");
}
}
// 게시물 삭제
void delete(int num) {
Article article = getArticleByNum(num);
int index = -1;
for (int i = 0; i <= articleLastIndex; i++) {
Article a = articles[i];
if (a.num == num) {
index = i;
break;
}
}
if(statelogin || adminMatche == 1) {
if(index == -1) {
System.out.printf("%d번에 해당하는 게시물이 없습니다.\n", num);
} else if(logIdPw.mNum == article.memberNum || adminMatche == 1) {
for (int i = index; i <= articleLastIndex - 1; i++) {
articles[i] = articles[i + 1];
}
System.out.println("게시물 삭제가 완료되었습니다.");
articleLastIndex--;
} else {
System.out.println("작성자만 수정 가능합니다.");
}
} else {
System.out.println("로그인 후 이용하실 수 있습니다.");
}
}
// 회원가입
void join() {
System.out.println("환영합니다! 지금부터 회원가입을 진행하겠습니다.");
Member member = new Member();
do {
joinId = false;
System.out.print("ID : ");
member.loginId = sc.next().trim();
for(int i = 1; i <= memberLastIndex; i++) {
if(members[i].loginId.equals(member.loginId)) {
System.out.println("이미 존재하는 아이디 입니다.");
joinId = true;
}
}
} while(joinId);
System.out.print("PW : ");
member.loginPw = sc.next().trim();
System.out.print("Name : ");
member.name = sc.next().trim();
joinNm++;
member.mNum = joinNm;
System.out.println("회원가입이 완료되었습니다.");
memberLastIndex++;
members[memberLastIndex] = member;
sizeUpArticleIfFull();
}
// 관리자 판별
boolean adminMatchedId(String id, String pw) {
Member member = new Member();
if(id.equals("admin") && pw.equals("ad123")) {
member.mNum = 0;
member.loginId = id;
member.loginPw = pw;
member.name = "admin";
member.mDate = getNowDate();
members[0] = member;
logIdPw = members[0];
statelogin = true;
return true;
}
return false;
}
// 로그인 함수
boolean anyMatchedMember(String id, String pw) {
for(int i = 1; i <= memberLastIndex; i++) {
if(members[i].loginId.equals(id) && members[i].loginPw.equals(pw)) {
logIdPw = members[i];
return true;
}
}
return false;
}
// 로그인 아이디 판별
boolean anyMatchedId(String id) {
for(int i = 1; i <= memberLastIndex; i++) {
if(members[i].loginId.equals(id)) {
return false;
}
}
return true;
}
// 로그인
void login() {
if(logIdPw != null) {
System.out.println("현재 로그인 상태입니다.");
return;
}
System.out.print("ID : ");
String id = sc.next().trim();
System.out.print("PW : ");
String pw = sc.next().trim();
if(adminMatchedId(id, pw)) {
adminMatche = 1;
statelogin = true;
System.out.println("게시판 관리자 admin님이 로그인 하셨습니다.");
return;
}
if(anyMatchedId(id)) {
System.out.println("존재하지 않는 아이디 입니다.");
} else if(anyMatchedMember(id, pw)) {
System.out.printf("%s님이 로그인 하셨습니다.\n", logIdPw.name);
statelogin = true;
} else {
System.out.println("ID 또는 PW가 맞지 않습니다.");
login();
}
}
// 로그아웃
void logout() {
if(!statelogin) {
System.out.println("로그인을 먼저 해주세요.");
} else {
System.out.printf("%s님이 로그아웃 하셨습니다.\n", logIdPw.name);
statelogin = false;
adminMatche = 0;
logIdPw = null;
}
}
//시작 함수
void help() {
System.out.println("== 명령어 리스트 ==");
System.out.println("help : 명령어 리스트");
System.out.println("list : 게시물 리스트");
System.out.println("add : 게시물 추가");
System.out.println("detail : 게시물 상세");
System.out.println("delete : 게시물 삭제");
System.out.println("modify : 게시물 수정");
System.out.println("join : 회원가입");
System.out.println("login : 로그인");
System.out.println("logout : 로그아웃");
System.out.println("exit : 게시판 종료");
}
// 종료 함수
void exit() {
System.out.println("== 게시판 종료 ==");
}
// 가장 마지막에 저장된 객체 리모컨
Article getLastIndex() {
if(getArticlesCount() > 0) {
return articles[articleLastIndex];
}
return null;
}
// num에 해당하는 객체 리모컨
Article getArticleByNum(int num) {
for(int i = 0; i <= articleLastIndex; i++) {
if(articles[i].num == num) {
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;
}
private void sizeUpArticle() {
Article[] newArticles = new Article[articles.length * 2];
Member[] newMembers = new Member[members.length * 2];
for ( int i = 0; i < articles.length; i++ ) {
newArticles[i] = articles[i];
}
articles = newArticles;
for ( int i = 0; i < members.length; i++ ) {
newMembers[i] = members[i];
}
members = newMembers;
}
private void sizeUpArticleIfFull() {
if ( isFull() ) {
int oldDatasLength = articles.length;
sizeUpArticle();
int newDatasLength = articles.length;
System.out.println("배열 증가 : " + oldDatasLength + " => " + newDatasLength);
}
if ( isFullm() ) {
int oldDatasLength1 = members.length;
sizeUpArticle();
int newDatasLength1 = members.length;
System.out.println("배열 증가 : " + oldDatasLength1 + " => " + newDatasLength1);
}
}
private boolean isFull() {
return articleLastIndex + 1 == articles.length;
}
private boolean isFullm() {
return memberLastIndex + 1 == members.length;
}
}
class Article {
int num;
int view;
int memberNum;
String title;
String date;
String body;
String memberId;
}
class Member {
int mNum;
String loginId;
String loginPw;
String mDate;
String name;
}