본문 바로가기

JAVA/Mission

게시판 구현해 보기(미완성 정렬)

What to do!


더보기

게시판 구현하기 1개의 글

: seq(글 번호), title(글 제목), writer(작성자), contents(글 내용), regdate(작성 일자), hitcount(조회수)

1. 글쓰기 입력: 글 제목, 작성자, 글 내용 => Board객체 생성

2. 전체 출력 글 번호 글 제목 작성자 작성자 일자 조회수

3. 글 세부조회 글 번호를 통해 해당 글 조회 글 번호, 글 제목, 작성자, 글 내용, 작성 일자, 조회수 => 1개 Board객체

4. 수정/삭제

5. 검색(제목, 작성자) 내가 원하는 글들만 출력

6. 정렬(기본) : 최근 입력 순서(내림차순)

7. 댓글 : 글 세부 기능 => 댓글 쓰기 , 해당 글 세부 보기 => 댓글 목록


My plan!


더보기

계획

Board

-BoardPost[] posts

-int postsNum

 

BoardPost

-int seq=0(글 번호), 객체생성시 ++

-String title(글 제목)

-String writer(작성자),

-String contents(글 내용)

-String regdate(작성 일자)

-int hitcount(조회수), 출력, 수정, 세부조회할 때 마다 ++

-BoardReply[] replies(댓글)

 

BoardReply

-seq(글 번호) -String replyWriter

-String[] replies -int replyNum -writeReply()

-readReply() -

 

 

 

Board

package person.mission;

public class Board {
	private int seq;
	private String title;
	private String writer;
	private String content;
	private String regdate;
	private int hitcount;
	private BoardReply[] boardReplies;
	private int replyNum=0;
	public Board() {
		// TODO Auto-generated constructor stub
	}
	public Board(int seq,String title, String writer, String content) {
		super();
		this.seq = seq;
		this.title = title;
		this.writer = writer;
		this.content = content;
		this.regdate = "2021";
		this.hitcount = 0;
		this.boardReplies=new BoardReply[10];
	
	}
	public int getSeq() {
		return seq;
	}
	public void setSeq(int seq) {
		this.seq = seq;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getWriter() {
		return writer;
	}
	public void setWriter(String writer) {
		this.writer = writer;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public String getRegdate() {
		return regdate;
	}
	public void setRegdate(String regdate) {
		this.regdate = regdate;
	}
	public int getHitcount() {
		return hitcount;
	}
	public void setHitcount(int hitcount) {
		this.hitcount = hitcount;
	}
	public BoardReply[] getBoardReplies() {
		return boardReplies;
	}
	public void setBoardReplies(BoardReply[] boardReplies) {
		this.boardReplies = boardReplies;
	}
	public int getReplyNum() {
		return replyNum;
	}
	public void setReplyNum(int replyNum) {
		this.replyNum = replyNum;
	}
	public int checkNum() {
		int num=getHitcount()+1;
		setHitcount(num);
		return num;
	}
	public BoardReply[] readReply() {
		BoardReply[] br=new BoardReply[this.replyNum];
		System.arraycopy(getBoardReplies(), 0, br, 0, replyNum);
		for (int i = 0; i < br.length; i++) {
			br[i]=boardReplies[i];
		}
		return br;
	}
	
	public void writeReply(BoardReply br) {
		this.boardReplies[this.replyNum++]=br;
	}
}

BoardPost

package person.mission;

public class BoardPost {
	private Board[] posts;
	private int postNum;
	public BoardPost() {
		this.posts = new Board[100];
		this.postNum = 0;
	}
	
	public BoardPost(Board[] posts, int postNum) {
		super();
		this.posts = posts;
		this.postNum = postNum;
	}

	public Board[] getPosts() {
		return posts;
	}
	public void setPosts(Board[] posts) {
		this.posts = posts;
	}
	public int getPostNum() {
		return postNum;
	}
	public void setPostNum(int postNum) {
		this.postNum = postNum;
	}
	
	public Board searchBoard(int seq) {
		Board bp=null;
		for (int i = 0; i < postNum; i++) {
			if(posts[i].getSeq()==seq) {
				bp=posts[i];
			}
		}
		return bp;
	}
	
	public void writeBoard(Board bp) {
		posts[postNum++]=bp;
		
	}
	public Board[] readBoard() {
		Board[] bps=new Board[postNum];
		System.arraycopy(posts, 0, bps, 0, postNum);
		for (int i = 0; i < bps.length; i++) {
			bps[i]=posts[i];
		}
		return bps;
	}
	public Board readBoard(int seq) {
		Board bp=null;
		for (int i = 0; i < postNum; i++) {
			if(posts[i].getSeq()==seq) {
				bp=posts[i];
				posts[i].checkNum();
			}
		}
		return bp;
		
	}
	public void reviseBoard(int seq) {
		int idx=-1;
		for (int i = 0; i < postNum; i++) {
			if(posts[i].getSeq()==seq) {
				idx=i;
				break;
			}
		}
		for (int i = idx; i < postNum; i++) {
			posts[i]=posts[i+1];
		}
		postNum--;
	}
	public void reviseBoard(int seq, String content) {
		for (int i = 0; i < postNum; i++) {
			if(posts[i].getSeq()==seq) {
				posts[i].setContent(content);;
			
			}
		}
	}
	public void sortBoard() {
		
	}
}

BoardReply

package person.mission;

public class BoardReply {
	private String replyWriter;
	private String reply;
	public BoardReply() {
		// TODO Auto-generated constructor stub
	}
	public BoardReply(String replyWriter, String reply) {
		super();
		this.replyWriter = replyWriter;
		this.reply = reply;
	}
	public String getReplyWriter() {
		return replyWriter;
	}
	public void setReplyWriter(String replyWriter) {
		this.replyWriter = replyWriter;
	}
	public String getReply() {
		return reply;
	}
	public void setReply(String reply) {
		this.reply = reply;
	}
	
	
	
}

BoardMain

package person.mission;

import java.util.Scanner;

public class BoardMain {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner scan=new Scanner(System.in);
		System.out.print("이름:");
		String writer=scan.nextLine();
		BoardPost board=new BoardPost();
		int seq=0;
			while(true) {
				System.out.println("<<< 게 시 판 >>>");
				System.out.println("1.게시판 글 작성 2.전체 글 출력 3.글 조회(글번호) 4.수정/삭제 5.정렬 6.댓글쓰기 7.댓글확인 q.종료");
				System.out.print("번호 입력:");
				String key=scan.nextLine();
				switch (key) {
				case "1":
					System.out.print("글 제목:");
					String title=scan.nextLine();
					System.out.print("글 내용:");
					String content=scan.nextLine();
					board.writeBoard(new Board(seq++,title, writer, content));
					break;
				case "2":
					Board[] posts=board.readBoard();
					for (int i = 0; i < posts.length; i++) {
						System.out.println("글번호:"+posts[i].getSeq());
						System.out.println("작성자:"+posts[i].getWriter());
						System.out.println("글제목:"+posts[i].getTitle());
						System.out.println("글내용:"+posts[i].getContent());
						System.out.println("날  짜:"+posts[i].getRegdate());
						System.out.println("조회수:"+posts[i].getHitcount());
					}
					break;
				case "3":
					System.out.print("조회할 글 번호:");
					int num=scan.nextInt();
					Board post=board.readBoard(num);
					System.out.println("글번호:"+post.getSeq());
					System.out.println("작성자:"+post.getWriter());
					System.out.println("글제목:"+post.getTitle());
					System.out.println("글내용:"+post.getContent());
					System.out.println("날  짜:"+post.getRegdate());
					System.out.println("조회수:"+post.getHitcount());
					break;
				case "4":
					System.out.println("수정 : a 클릭, 삭제 : b 클릭");
					System.out.print("입력: ");
					String str=scan.nextLine();
					if(str.equals("a")) {
						System.out.print("수정할 글 번호 : ");
						num=Integer.parseInt(scan.nextLine());
						System.out.print("수정할 내용 입력:");
						content=scan.nextLine();
						board.reviseBoard(num, content);
					}else {
						System.out.print("삭제할 번호:");
						num=Integer.parseInt(scan.nextLine());
						board.reviseBoard(num);
					}
					break;
				case "5":
			
					
					break;
				case "6":
					System.out.print("댓글 쓸 글 번호 : ");
					num=Integer.parseInt(scan.nextLine());
					if( board.searchBoard(num)==null) {
						System.out.println("찾으시는 글이 존재하지 않습니다.");
					}else {
						post=board.searchBoard(num);
						System.out.print("댓글입력하는 사람: ");
						String replyWriter=scan.nextLine();
						System.out.print("입력할 댓글 내용: ");
						String reply=scan.nextLine();
						post.writeReply(new BoardReply(replyWriter, reply));
					}
					
					break;
				case "7":
					System.out.print("조회할 글 번호:");
					num=Integer.parseInt(scan.nextLine());
					post=board.readBoard(num);
					if( board.searchBoard(num)==null) {
						System.out.println("찾으시는 글이 존재하지 않습니다.");
					}else {
						post=board.searchBoard(num);
						BoardReply[] br=post.readReply();
						for (int i = 0; i < br.length; i++) {
							System.out.println(i+"의 댓글 작성자:"+br[i].getReplyWriter());
							System.out.println(i+"의 댓글 내용:"+br[i].getReply());
						}
					}
					
					break;
				case "q":
					System.out.println("프로그램 종료");
					return;

				}
			}
	}

}

'JAVA > Mission' 카테고리의 다른 글

비디오방(일반+특별회원)관리  (0) 2021.02.01
전화번호정보입력(PhoneInfo) 동창, 회사, 친구,가족 등  (0) 2021.02.01
주문(Order)  (0) 2021.02.01
장바구니(Cart)  (0) 2021.02.01
중간Test01_Bank  (0) 2021.01.27