[자바] 콘솔 창으로 미니 게시판 만들기
Board.java
package bulletinboard;
public class Board {
private String register;
private String email;
private String content;
private String title;
private String password;
public Board(String register, String email, String content, String title, String password) {
super();
this.register = register;
this.email = email;
this.content = content;
this.title = title;
this.password = password;
}
public String getRegister() {
return register;
}
public void setRegister(String register) {
this.register = register;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public void deleteContent() {
this.content = "";
}
@Override
public String toString() {
// TODO Auto-generated method stub
// return super.toString();
return "작성자 : " + this.register + " 이메일 : "+ this.email + " 제목 : " + this.title + " 글 내용 : " + this.content;
}
}
BoardMVC.java
package bulletinboard;
import java.util.ArrayList;
import java.util.Scanner;
public class BoardMVC {
ArrayList<Board> boardList;
public BoardMVC() {
super();
this.boardList = new ArrayList<>();
}
// 외부에서 글자를 입력받으려면 Scanner 클래스 필요
// 버퍼에 '\n'을 없애기 위해서 scanner.nextline()을 먼저 해줘야 함.
public void writeArticle(Scanner scanner) {
System.out.println("게시판 글을 작성하시오");
System.out.printf("%s ", "작성자");
scanner.nextLine();
String register = scanner.nextLine();
System.out.printf("%s ", "이메일");
String email = scanner.nextLine();
System.out.printf("%s ", "비밀번호");
String password = scanner.nextLine();
System.out.printf("%s ", "제목");
String title = scanner.nextLine();
System.out.printf("%s ", "글 내용");
String content = scanner.nextLine();
Board board = new Board(register, email, content, title, password);
this.addArticle(board);
}
public void addArticle(Board board) {
this.boardList.add(board);
}
public void removeArticle(Scanner scanner) {
System.out.println("삭제할 글의 작성자와 비밀번호를 입력하세요");
System.out.printf("%s ", "작성자");
String register = scanner.nextLine();
System.out.printf("%s ", "비밀번호");
String password = scanner.nextLine();
this.removeArticle(register, password);
}
private void removeArticle(String register, String password) {
if( boardList.size() > 0 ) {
for( Board i : boardList) {
if(i.getRegister().equals(register) && i.getPassword().equals(password)) {
System.out.printf("%s \r\n", i.toString());
i.deleteContent();
System.out.println("내용을 삭제했습니다.");
}
}
} else {
System.out.println("저장한 게시물이 없습니다.");
}
}
public void showArticle(Scanner scanner) {
if(this.boardList.size() > 0) {
for(Board i : boardList) {
System.out.printf("%s \r\n", i.getContent());
}
} else {
System.out.println("등록된 게시물이 없습니다.");
}
}
}
BoardController.java
package bulletinboard;
import java.util.Scanner;
public class BoardController {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner = new Scanner(System.in);
boolean isContinue = false;
BoardMVC boardMVC = new BoardMVC();
do {
isContinue = true;
System.out.println("메뉴를 선택하세요");
System.out.println("1. 게시판 글쓰기");
System.out.println("2. 글 목록 보기");
System.out.println("3. 글 삭제");
System.out.println("4. 종료");
String menu = scanner.next();
switch(menu) {
case "1":
boardMVC.writeArticle(scanner);
break;
case "2":
boardMVC.showArticle(scanner);
break;
case "3":
boardMVC.removeArticle(scanner);
break;
case "4":
isContinue = false;
break;
default:
System.out.println("잘못 눌렀습니다.");
}
}while(isContinue);
}
}