package practice;
//기본 수정과정
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Member member = new Member();
member.setScanner(scanner);
member.start();
Board board = new Board();
board.setScanner(scanner);
scanner.close();
}
}
// ================== 게시판 클래스
class Board {
Scanner scanner;
int articlesLastIndex;
Article[] articles;
int loginedMember;
Board() {
articles = new Article[100];
loginedMember = 0;
articlesLastIndex = -1;
}
void setScanner(Scanner scanner) {
this.scanner = scanner;
}
void start() {
shwHelp();
while (true) {
System.out.printf("게시판 ) ");
String command = scanner.next().trim();
if (command.equals("exit")) {
doCommandExit();
break;
} else if (command.equals("help")) {
scanner.nextLine();
shwHelp();
} else if (command.equals("add")) {
scanner.nextLine();
doCommandAdd();
} else if (command.equals("list")) {
scanner.nextLine();
doCommandList();
} else if (command.equals("detail")) {
scanner.nextLine();
doCommandDetail();
} else if (command.equals("modify")) {
scanner.nextLine();
doCommandModify();
} else if (command.equals("delete")) {
scanner.nextLine();
doCommandDelete();
} else {
System.out.println("유효하지 않은 명령입니다.");
}
}
}
void doCommandDelete() {
int id = scanner.nextInt();
scanner.nextLine();
int index = -1;
for (int i = 0; i <= articlesLastIndex; i++) {
Article a = articles[i];
if (a.id == id) {
index = i;
System.out.println("해당 게시물이 삭제 되었습니다.");
break;
}
}
if (index == -1) {
System.out.println("해당 게시물을 찾을 수 없습니다.");
} else {
for (int i = index; i <= articlesLastIndex - 1; i++) {
articles[i] = articles[i + 1];
}
}
articlesLastIndex--;
}
private void doCommandModify() {
int id = scanner.nextInt();
scanner.nextLine();
Article article = null;
for (int i = 0; i <= articlesLastIndex; i++) {
Article a = articles[i];
if (a.id == id) {
article = a;
break;
}
}
if (article == null) {
System.out.println("해당 게시물이 존재하지 않습니다.");
} else {
System.out.printf("제목 : ");
String title = scanner.nextLine();
System.out.printf("내용 : ");
String body = scanner.nextLine();
article.title = title;
article.body = body;
}
}
private void doCommandDetail() {
int id = scanner.nextInt();
scanner.nextLine();
Article article = null;
for (int i = 0; i <= articlesLastIndex; i++) {
Article a = articles[i];
if (a.id == id) {
article = a;
break;
}
}
if (article == null) {
System.out.println("해당 게시물이 존재하지 않습니다.");
} else {
article.viewPoint += 1;
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);
System.out.printf("조회수 : %d%n", article.viewPoint);
System.out.printf("추천 수: %d%n", article.like);
System.out.printf("해당 게시물을 추천하시겠습니까? y / n%n");
String a = scanner.nextLine();
if (a.equals("y")) {
article.like += 1;
}
}
}
private void doCommandList() {
System.out.println("== 게시물 리스팅 ==");
System.out.println("번호 / 날짜 / 제목 / 조회수 / 추천수");
for (int i = 0; i <= articlesLastIndex; i++) {
Article article = articles[i];
System.out.printf("%d / %s / %s / %d / %d\r\n", article.id, article.regDate, article.title,
article.viewPoint, article.like);
}
}
void doCommandExit() {
System.out.println("== 게시판 종료 ==");
}
void shwHelp() {
System.out.println("== 명령어 리스트 ==\r\n" + "help : 명령어 리스트\r\n" + "list : 게시물 리스팅\r\n" + "add : 게시물 추가\r\n"
+ "exit : 종료\r\n" + "detail ${게시물 번호} : 게시물 상세보기\r\n" + "modify : 게시물 수정\r\n" + "delete : 게시물 삭제\r\n"
+ "join : 회원가입\r\n" + "login : 로그인\r\n" + "logout : 로그아웃\r\n");
}
void doCommandAdd() {
Article article = new Article();
int newId;
int articlesNewIndex = articlesLastIndex + 1;
Article lastArticle = getLastArticle();
if (lastArticle == null) {
newId = 1;
} else {
newId = lastArticle.id + 1;
}
article.id = newId;
article.regDate = getRegDateStr();
System.out.printf("재목 : ");
article.title = scanner.nextLine();
System.out.printf("내용 : ");
article.body = scanner.nextLine();
article.viewPoint = 0;
article.like = 0;
System.out.printf("%d번 게시물이 작성되었습니다.\r\n", article.id);
articles[articlesNewIndex] = article;
articlesLastIndex++;
}
String getRegDateStr() {
Calendar cal = Calendar.getInstance();
SimpleDateFormat Date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String regDateStr = Date.format(cal.getTime());
return regDateStr;
}
Article getLastArticle() {
if (getArticleCount() > 0) {
return articles[articlesLastIndex];
}
return null;
}
int getArticleCount() {
return articlesLastIndex + 1;
}
}
// ================= 아티클 클래스
class Article {
int id;
String regDate;
String title;
String body;
int viewPoint;
int like;
}
// ================ 멤버 클래스
class Member {
Scanner scanner;
String id;
String pw;
String nickName;
Member[] members;
int ownId;
int membersLastOwnId;
int loginedMember;
Member() {
members = new Member[100];
membersLastOwnId = -1;
loginedMember = 0;
}
void setScanner(Scanner scanner) {
this.scanner = scanner;
}
void start() {
shwMain();
while (true) {
System.out.printf("입력 ) ");
String command = scanner.next().trim();
if (command.equals("signup")) {
scanner.nextLine();
doSignUp();
} else if (command.equals("login")) {
scanner.nextLine();
doLogIn();
}else {
System.out.println("알 수 없는 명령어 입니다.");
}
}
}
void doLogIn() {
Member member;
System.out.printf("아이디를 입력해 주세요 : ");
String logInId = scanner.nextLine();
System.out.printf("비밀번호를 입력해 주세요 :");
String logInPw = scanner.nextLine();
for (int i = 0; i <= membersLastOwnId; i++) {
Member a = new Member();
if (a.id == logInId) {
if(a.pw == logInPw) {
}
}
}
}
void doSignUp() {
System.out.println("== 회원가입 ==");
System.out.printf("사용하실 아이디를 입력해 주세요 : ");
String id = scanner.nextLine();
System.out.printf("사용하실 비밀번호를 입력해 주세요 : ");
String pw = scanner.nextLine();
System.out.printf("사용하실 닉네임을 입력해 주세요 : ");
String nickName = scanner.nextLine();
System.out.println("회원가입을 축하드립니다.");
membersLastOwnId++;
Member member = new Member();
member.ownId = membersLastOwnId + 1;
member.id = id;
member.pw = pw;
member.nickName = nickName;
}
void shwMain() {
System.out.println("환영합니다.");
System.out.println("다음 기능에 따라 입력해 주세요 : ");
System.out.println("회원가입 : signup");
System.out.println("로그인 : login");
System.out.println("로그아웃 : logout");
}
}