본문 바로가기
개발일지

개발일지 18일차 - 게시판 만들기 -

by 태운콩즙 2023. 12. 19.
728x90
반응형

오늘은 게시판 만들기 프로젝트를 진행하였다

<DTO>

package ch11_classes.ex4;

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

public class boardDTO {
    private Long id;
    private String boardTitle;
    private String boardWriter;
    private String boardContents;
    private String boardPassword;
    private Long Check;
    private String createdAt;

    public void setCheck(Long Check) {
        this.Check = Check;
    }

    public Long getCheck() {
        return Check;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public void setBoardTitle(String boardTitle) {
        this.boardTitle = boardTitle;
    }

    public void setBoardWriter(String boardWriter) {
        this.boardWriter = boardWriter;
    }

    public void setBoardContents(String boardContents) {
        this.boardContents = boardContents;
    }

    public void setBoardPassword(String boardPassword) {
        this.boardPassword = boardPassword;
    }

    public Long getId() {
        return id;
    }

    public String getBoardTitle() {
        return boardTitle;
    }

    public String getBoardWriter() {
        return boardWriter;
    }

    public String getBoardContents() {
        return boardContents;
    }

    public String getBoardPassword() {
        return boardPassword;
    }

    public void setCreatedAt(String createdAt) {
        this.createdAt = createdAt;
    }

    public String getCreatedAt() {
        return createdAt;
    }

    public boardDTO() {

    }

    private static Long idValue = 1L;
    private static Long CheckValue = 0L;


    public boardDTO(String boardTitle, String boardWriter, String boardContents, String boardPassword) {
        this.id = idValue++;
        this.Check = CheckValue;
        this.boardTitle = boardTitle;
        this.boardWriter = boardWriter;
        this.boardContents = boardContents;
        this.boardPassword = boardPassword;
        this.createdAt = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));


    }

    @Override
    public String toString() {
        return "boardDTO{" +
                "id=" + id +
                ", boardTitle='" + boardTitle + '\'' +
                ", boardWriter='" + boardWriter + '\'' +
                ", boardContents='" + boardContents + '\'' +
                ", boardPassword='" + boardPassword + '\'' +
                ", Check=" + Check +
                ", createdAt='" + createdAt + '\'' +
                '}';
    }
}

필드값에 날짜 정보를 다루는 필드를 넣어줌으로 배열에 새로운 값이 추가될때 추가된 시간이 출력

<Service>

package ch11_classes.ex4;

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

public class boardSevice {
    static boardRepository boardRepository = new boardRepository();
    Scanner scanner = new Scanner(System.in);
    public void boardWriting() {
        System.out.print("글 제목: ");
        String boardTitle = scanner.next();
        System.out.print("작성자: ");
        String boardWriter = scanner.next();
        System.out.print("내용: ");
        String boardContents = scanner.next();
        System.out.println("비밀번호: ");
        String boardPassword = scanner.next();
        boardDTO boardDTO = new boardDTO(boardTitle,boardWriter,boardContents,boardPassword);
        boolean result = boardRepository.boardWriting(boardDTO);
        if (result){
            System.out.println("작성 완료");
        }else {
            System.out.println("작성 실패");
        }
    }

    public void boardList() {
        List<boardDTO> boardDTOList = boardRepository.boardList();
        System.out.println("id\t" + "title\t" + "writer\t" + "hits"+"date\t");
        for (boardDTO boardDTO : boardDTOList) {
            listPrint(boardDTOList);
        }
    }
    public void boardCheck() {
        System.out.println("조회할 글 번호: ");
        Long id = scanner.nextLong();
        boardRepository.check(id);
        boardDTO boardDTO = boardRepository.boardCheck(id);
        if (boardDTO != null){
            System.out.println("boardDTO = " + boardDTO);
        }else {
            System.out.println("요청하신 정보를 찾을수 없습니다");
        }
    }

    public void boardUpdate() {
        System.out.println("조회할 id: ");
        Long id = scanner.nextLong();
        System.out.println("비밀번호 입력: ");
        String passWord = scanner.next();
        boardDTO boardDTO = boardRepository.boardCheck(id);
        if (boardDTO != null){
            System.out.println("수정할 제목: ");
            String boardTitle = scanner.next();
            System.out.println("수정할 내용: ");
            String boardContents = scanner.next();
            boolean updateResult = boardRepository.boardUpdate(id, passWord, boardTitle , boardContents);
            if (updateResult){
                System.out.println("수정성공");
            }else {
                System.out.println("수정실패");
            }
        }


    }

    public void boardDelete() {
        System.out.println("삭제할 id");
        Long id = scanner.nextLong();
        System.out.println("비밀번호를 입력하세요");
        String password = scanner.next();
        boolean result = boardRepository.boardDelete(id,password);
        if (result){
            System.out.println("삭제 성공");
        }else {
            System.out.println("삭제 실패");
        }
    }

    public void boardSearch() {
        System.out.println("검색어: ");
        String boardTitle = scanner.next();
        List<boardDTO>boardDTOList = boardRepository.boardSearch(boardTitle);
        if (boardDTOList.size()>0){
            System.out.println("검색 결과");
            listPrint(boardDTOList);
        }else {
            System.out.println("검색 결과가 없습니다");
        }
    }

    // 목록 출력 전용 메서드
    //findAll,search 메서드로 부터 list 데이터를 전달받아서 출력을 하는 메서드
    private void listPrint(List<boardDTO>boardDTOList){
        System.out.println("id\t"+"title\t"+"writer\t"+"hits\t");
        for (boardDTO boardDTO:boardDTOList){
            System.out.println(boardDTO.getId() + "\t" + boardDTO.getBoardTitle() + "\t" +
                    boardDTO.getBoardWriter() + "\t" + boardDTO.getCheck() + "\t"+boardDTO.getCreatedAt());
        }
    }
}

목록을 출력할때에 같은 내용이 나오는 코드들은 listPrint를 만들어 주어서 실행

 

<Repository>

package ch11_classes.ex4;

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

public class boardRepository {
    private static List<boardDTO> boardDTOList = new ArrayList<>();

    public boolean boardWriting(boardDTO boardDTO) {
        return boardDTOList.add(boardDTO);
    }

    public List<boardDTO> boardList() {
        return boardDTOList;
    }

    public boardDTO boardCheck(Long id) {
        boardDTO boardDTO = null;
        for (int i = 0; i < boardDTOList.size(); i++) {
            if (id.equals(boardDTOList.get(i).getId())) {
                boardDTO = boardDTOList.get(i);


            }
        }

        return boardDTO;
    }

    public boolean boardUpdate(Long id, String passWord, String boardTitle, String boardContents) {
        boolean result= false;
        for (int i = 0; i < boardDTOList.size(); i++) {
            if (id.equals(boardDTOList.get(i).getId())){
                if (passWord.equals(boardDTOList.get(i).getBoardPassword())){
                    boardDTOList.get(i).setBoardTitle(boardTitle);
                    boardDTOList.get(i).setBoardContents(boardContents);
                }
            }
            result= true;
        }
            return result;
    }

    public boolean boardDelete(Long id, String password) {
        boolean result = false;
        for (int i = 0; i < boardDTOList.size(); i++) {
            if (id.equals(boardDTOList.get(i).getId())){
                if (password.equals(boardDTOList.get(i).getBoardPassword())){
                    boardDTOList.remove(i);
                    result =true;
                }
            }
        }
        return result;
    }

    public List<boardDTO> boardSearch(String boardTitle) {
        List<boardDTO>boardDTOS=new ArrayList<>();
        for (int i = 0; i < boardDTOList.size(); i++) {
            if (boardDTOList.get(i).getBoardTitle().contains(boardTitle)){
                boardDTOS.add(boardDTOList.get(i));
            }

        }
        return boardDTOS;
    }

    public void check(Long id) {
        for (int i = 0; i < boardDTOList.size(); i++) {
            if (id.equals(boardDTOList.get(i).getId())){
                Long hits = boardDTOList.get(i).getCheck();

                boardDTOList.get(i).setCheck(++hits);
            }
        }
    }
}

검색을 실행하였을경우 조회수가 1씩 상승해주기위해 메서드를 생성한뒤 입력한 id와 일치할경우 조회수가 상승하게 만들어주었다

<Main>

package ch11_classes.ex4;

import java.util.Scanner;

public class boardMain {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        boardSevice boardSevice = new boardSevice();
        int selecNo = 0;
        boolean run = true;
        while (run){
            System.out.println("1. 글작성 | 2. 글목록 | 3. 글조회 | 4.글수정 | 5.글삭제 | 6.검색");
            selecNo = scanner.nextInt();
            if (selecNo == 1){
                boardSevice.boardWriting();
            } else if (selecNo == 2) {
                boardSevice.boardList();
            } else if (selecNo == 3) {
                boardSevice.boardCheck();
            } else if (selecNo == 4) {
                boardSevice.boardUpdate();
            } else if (selecNo == 5) {
                boardSevice.boardDelete();
            } else if (selecNo == 6) {
                boardSevice.boardSearch();
            }

        }
    }
}

 

<날짜 정보 다루기>

package ch11_classes.ex4;

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

public class EX_Date {
    public static void main(String[] args) {
        // 날짜 정보 다루기
        // 현재시간 확인
        LocalDateTime now = LocalDateTime.now();
        System.out.println("now = " + now);
        // 날짜 데이터를 원하는 형식으로 다루기
        String year = now.format(DateTimeFormatter.ofPattern("yyyy"));
        System.out.println("year = " + year);
        String month = now.format(DateTimeFormatter.ofPattern("MM"));
        System.out.println("month = " + month);
        String day = now.format(DateTimeFormatter.ofPattern("dd"));
        System.out.println("day = " + day);
        String hour = now.format(DateTimeFormatter.ofPattern("HH"));
        System.out.println("hour = " + hour);
        String minute = now.format(DateTimeFormatter.ofPattern("MM"));
        System.out.println("minute = " + minute);
        String second = now.format(DateTimeFormatter.ofPattern("ss"));
        System.out.println("second = " + second);
        String createAt = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        System.out.println("createAt = " + createAt);
        createAt = now.format(DateTimeFormatter.ofPattern("yyyy년MM월dd일 HH시mm분:ss초"));
        System.out.println("createAt = " + createAt);
    }
}
728x90
반응형