class A{
m1(){-}
m2(){-}
m3(){-}
...
}
int no;
String title;
String content;
no = 1;
title = "제목입니다.";
content = "okok"
=> 클래스에서 하는 일반적인 변수 선언에서 새 클래스에 필요한 변수 선언으로 변경class Board{
int no;
String title;
String content;
}
Board b1 = new Board()
b.no = 100;
b.title = "aaa";
b.content = "ok";
Board b2;
b2 = b1;
b2.no =333;
System.out.println(b1.no) ==> 333
public class Board {
// 인스턴스를 생성할 때 준비되는 메모리를 선언
int no;
String title;
String content;
String writer;
String password;
int viewCount;
long createdDate;
}
static int boardCount = 0; // 저장된 게시글의 개수
static final int SIZE = 3;
static Board[] boards = new Board[SIZE];
static void processInput() {
//------------------- menu 3 --------------------------
System.out.println("[게시글 등록]");
if (SIZE == boardCount) {
System.out.println(
"더 이상 게시글을 등록할 수 없습니다.
최대 게시물 등록 수를 초과했습니다!"
);
return;
}
Board board = new Board();
boards[boardCount] = board;
board.title = Prompt.inputString("제목? ");
board.content = Prompt.inputString("내용? ");
board.writer= Prompt.inputString("작성자? ");
board.password = Prompt.inputString("암호? ");
board.no = boardCount == 0 ? 1 : boards[boardCount - 1].no + 1;
boards[boardCount].createdDate = System.currentTimeMillis();
boards[boardCount].viewCount = 0;
// 새로만든 인스턴스 주소를 레퍼런스 배열에 저장한다.
boardCount++;
}
가독성을 위하여 레퍼런스 변수 board를 선언해 주고 그 주소를 현재 게시글의 레퍼런스 배열에 연결시켜준다.
레퍼런스 배열로 구성했으므로 인덱스가 아닌, 이전 레퍼런스 배열의 no값을 가져온다.
static void processList() {
//------------------- menu 1 --------------------------
java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat(
"yyyy-MM-dd hh:mm:ss"
);
System.out.println("[게시글 목록]");
System.out.println("번호\t제목\t\t조회수\t작성자\t등록일");
for (int i = 0; i < boardCount; i++) {
Board board = boards[i];
java.util.Date date = new java.util.Date(board.createdDate);
String dateStr = formatter.format(date);
System.out.printf(
"%d\t%s\t\t%d\t%s\t%s\n",
board.no,
board.title,
board.viewCount,
board.writer,
dateStr
);
}
}
가독성을 위하여 Board 객체 board를 만들어서 for문을 통해 boards[i]의 주소를 각각 넣어주고, 객체 board를 이용해 출력해준다.
static void processDetail() {
//------------------- menu 2 --------------------------
Board board = null;
System.out.println("[게시판 상세보기]");
int noBoard = Prompt.inputInt("몇 번 게시물을 조회하시겠습까? ");
for (int i = 0; i < boardCount; i++) {
if (boards[i].no == noBoard) {
board = boards[i];
break;
}
}
if (board == null) {
System.out.println("해당 번호의 게시물은 존재하지 않습니다!");
return; // 메소드를 호출할 곳으로 돌아가고 싶을 때.
}
board.viewCount++;
System.out.printf("번호: %d\n", board.no);
System.out.printf("제목: %s\n", board.title);
System.out.printf("내용: %s\n", board.content);
System.out.printf("조회수: %d\n", board.viewCount);
System.out.printf("작성자: %s\n", board.writer);
java.util.Date date = new java.util.Date(board.createdDate);
System.out.printf("등록일: %1$tY-%1$tm-%1$td/%1$tA %1$tH:%1$tM\n", date);
}
메소드 안에 board 객체를 null로 선언해주고, 조회하려는 입력 번호와 일치하는 게시글의 번호가 있을 경우 null이었던 board 객체에 일치하는 게시글의 번호가 들어있는 레퍼런스 배열 원소를 넣어준다.
만약 board가 여전히 null이라면 일치하는 boardCount가 존재하지 않았던 것이다.
static void processDelete() {
System.out.println("[게시글 삭제]");
int deleteNo = Prompt.inputInt("삭제하시고 싶은 게시글의 번호를 입력해 주시길 바랍니다.");
int deleteIdx = -1;
for(int i=0;i<boardCount;i++) {
if(deleteNo == boards[i].no) {
System.out.println("삭제되었습니다.");
deleteIdx=i;
delete(deleteIdx);
break;
}
}
if(deleteIdx == -1) {
System.out.println("잘못된 번호를 입력하셨습니다. 해당 게시글 번호는 존재하지 않습니다.\n올바른 번호를 입력해 주십시오.");
return;
}
}
static void delete(int deleteIdx) {
for(int i=deleteIdx+1;i<boardCount;i++) {
boards[i-1] = boards[i]; //마지막 인덱스 고려하기
}
boardCount--;
boards[boardCount]=null; // 없앤 값은 null 처리하기
//boards[--boardCount]=null도 가능!
}
for(int i=deleteIdx+1;i<boardCount;i++) {
boards[i-1] = boards[i]; //마지막 인덱스 고려하기
}
또한 중요한 점은, 이제 쓸모 없어진 삭제된 게시글 배열 인덱스 레퍼런스 변수를 null처리 해주는 것이다. public static void processUpdate() {
System.out.println("[게시글 수정]");
int editNo = Prompt.inputInt("변경할 게시글 번호?");
int editIdx = -1;
for(int i=0;i<boardCount;i++) {
if(editNo == boards[i].no) {
editIdx=i;
edit(editIdx);
break;
}
}
if(editIdx == -1) {
System.out.println("잘못된 번호를 입력하셨습니다. 해당 게시글 번호는 존재하지 않습니다.\n올바른 번호를 입력해 주십시오.");
return;
}
}
static void edit(int editIdx) {
Board board = boards[editIdx];
String title= Prompt.inputString("제목? " + "("+board.title+")");
String content= Prompt.inputString("내용? "+ "("+board.content+")");
String writer = Prompt.inputString("작성자? "+ "("+board.writer+")");
String password = Prompt.inputString("암호? "+ "("+board.password+")");
if(isEdit()) {
board.title =title;
board.content =content;
board.writer=writer;
board.password = password;
System.out.println("성공적으로 변경되었습니다!");
}else {
System.out.println("변경을 취소하였습니다.");
return;
}
}
static boolean isEdit() {
char ans = Prompt.inputChar("변경하시겠습니까?(y/n) ");
return ans =='y'? true:false;
}
String[] names = new String[29];
names[0] = String("jimin");
String[] names = {"jimin", "jiyeon", ...};