Java/Java icia 9일차

배운것들을 이용해서 게시판 만들어보기

swkn 2023. 3. 6. 19:54
package day9;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class Board {
	private static int number = 0;
	//final 값이 변하지 않는 상수값 (final , 대문자로 지정) 변경불가
	private final static DateTimeFormatter DTF = DateTimeFormatter.ofPattern("yyyy년MM월dd일 hh:mm:ss");
	//공유 ,고유값을 가지고 있음  
	private int bno;
	private String pw;
	private String title;
	private String writer;
	private int cnt;
	private String postDate;
	
	
	public Board(){
		
		this.bno = ++number;
		//전치 증강 객체 생성시 증가 
		this.cnt = 0;
		this.postDate = DTF.format(LocalDateTime.now());
		//dtf.format(LocalDateTime.now()); 현재날짜를 포맷해서 집어넣음
	}
	
	public Board(String pw, String title , String writer) {
		this.bno = ++number;
		this.pw = pw;
		this.title = title;
		this.writer = writer;
		this.cnt = 0;
		this.postDate = DTF.format(LocalDateTime.now());
	}
	public String getPw() {
		return this.pw;
	}
	public void setPw(String pw) {
		this.pw = pw;
	}
	public int getBno() {
		return this.bno;
	}
	public String getTitle() {
		return this.title;
	}
	public void setTitle(String title) {
		 this.title = title;
	}
	public String getWriter() {
		return this.writer;
	}
	public void setWriter(String writer) {
		this.writer = writer;
	}
	public void increaseCnt() {
		this.cnt++;
	}
	public void print() {
		System.out.printf("%d\t%s\t%s\t%d\t%s\n",bno,title,writer,cnt,postDate);
	}

}

 

메인

 

package day9;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import day8.User2;

public class BoardMain {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		List<Board> list = new ArrayList<>();

		while (true) {
			System.out.println("==================게시판================");
			System.out.println("1.게시글등록 2.리스트 3.읽기 4.수정 5.삭제 0.종료");
			System.out.print("메뉴선택>> ");
			int menu = sc.nextInt();
			sc.nextLine();

			if (menu == 1) {
				System.out.print("제목> ");
				String title = sc.nextLine();
				System.out.print("작성자>>");
				String writer = sc.next();
				System.out.println("비밀번호>  ");
				String pw = sc.next();
				Board board = new Board(pw, title, writer);
				list.add(board);
				System.out.println(title + "이(가) 등록되었습니다");
			} else if (menu == 2) {
				System.out.println("글번호\t\t제목\t작성자\t조회수\t게시일");
				System.out.println("----------------------------------------------");
				for (Board b : list) {
					b.print();
				}
			} else if (menu == 3) {
				System.out.print("읽을 글 번호> ");
				int bno = sc.nextInt();
				System.out.println("글번호\t\t제목\t작성자\t조회수\t게시일");
				System.out.println("----------------------------------------------");
				boolean find = false;
				for (Board b : list) {
					if (bno == b.getBno()) {
						b.increaseCnt();
						b.print();
						find = true;
						break;
					}
				}
				if (!find) {
					System.out.println("조회 할 수 없는 글 번호 입니다 ");
				}
			} else if (menu == 4) {
				System.out.println("수정할 글번호> ");
				int updateBno = sc.nextInt();sc.nextLine();
				System.out.print("비밀번호 확인>> ");
				String updatePw = sc.next();sc.nextLine();
				boolean find = false;
				for (Board b : list) {
					if (updateBno == b.getBno()) {
						if (updatePw.equals(b.getPw())) {
							System.out.print("수정할 제목> ");
							String updateTitle = sc.nextLine();
							System.out.print("수정할 작성자> ");
							String updateWriter = sc.next();
							b.setTitle(updateTitle);
							b.setTitle(updateTitle);
							System.out.println("업데이트 성공");
							find = true;
							break;

						} else {
							System.out.println("비밀번호틀렸습니다");
							find = true;
						}
					}
				}
				if (!find) {
					System.out.println("다시 입력해주세요");
				}
			}else if (menu == 5) {
					System.out.print("삭제할 글번호> ");
					int deleteBno = sc.nextInt();
					System.out.print("비밀번호>");
					String deletePw = sc.next();
					for (Board b : list) {
						if (deleteBno == b.getBno() && deletePw.equals(b.getPw())) {
							System.out.println(b.getTitle() + "이(가) 삭제성공");
							list.remove(b);
							break;
						}
					}
				}

				else if (menu == 0) {
					break;
				} else {
					System.out.println("다시 입력해주세요");
				}
				System.out.println();
			}
			System.out.println("프로그램 종료");
		}

	}