본문 바로가기
개발일지

회원제 게시판 프로젝트

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

회원제 게시판을 만드는 프로젝트를 실시하였다

 

package ch11_classes.ex06;

import ch11_classes.ex06.common.commonVariables;
import ch11_classes.ex06.service.BoardService;
import ch11_classes.ex06.service.MemberService;

import java.util.Scanner;

public class MainController {
    public static void main(String[] args) {
        commonVariables commonVariables = new commonVariables();
        Scanner scanner = new Scanner(System.in);
        BoardService boardService = new BoardService();
        MemberService memberService = new MemberService();
        int selectNo = 0;
        boolean run = true;
        while (run){
            System.out.println("메인 페이지");
            System.out.println("1.회원가입 | 2.로그인 | 3.회원목록 | 4.회원수정 | 5.회원탈퇴 | 6.로그아웃 | 7.게시판 메뉴 | 0.종료");
            selectNo = scanner.nextInt();
            if (selectNo == 1){
                memberService.memberJoin();
            } else if (selectNo == 2) {
                memberService.memberLogin();
            } else if (selectNo == 3) {
                memberService.memberList();
            } else if (selectNo == 4) {
                memberService.memberUpdate();
            } else if (selectNo == 5) {
                memberService.memberDelete();
            } else if (selectNo == 6) {
                memberService.memberLogout();
            } else if (selectNo == 7) {
                if (commonVariables.loginEmail != null){
                    int num = 0;
                    while (run){
                        System.out.println("게시판");
                        System.out.println("1.글 작성 | 2.글 목록 | 3.글 조회 | 4.글 수정 | 5.글 삭제 | 6.검색 | 99 sample | 0.메인매뉴");
                        num = scanner.nextInt();
                        if (num == 1){
                            boardService.boardWriting();
                        } else if (num == 2) {
                            boardService.boardList();
                        } else if (num == 3) {
                            boardService.boardCheck();
                        } else if (num == 4) {
                            boardService.boardUpdate();
                        } else if (num == 5) {
                            boardService.boardDelete();
                        } else if (num == 6) {
                            boardService.boardSearch();
                        } else if (num == 0) {
                            break;
                        }
                    }
                } else if (selectNo == 0) {
                    break;
                } else {
                    System.out.println("회원 전용 메뉴입니다 로그인 해주세요");
                }
            }
        }

    }
}

메인페이지이다

 

메인페이지에서 회원가입 로그인등을 진행하고 로그인이 되었을 경우 게시판 페이지로 이동할 수 있다

package ch11_classes.ex06.service;

import ch11_classes.ex06.common.commonVariables;
import ch11_classes.ex06.dto.MemberDTO;
import ch11_classes.ex06.repository.MemberRepository;

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

public class MemberService {
    Scanner scanner = new Scanner(System.in);
    MemberRepository memberRepository = new MemberRepository();
    commonVariables commonVariables = new commonVariables();

    public void memberJoin() {
        boolean chekResult = false;
        String memberEmail = null;
        System.out.println("회원가입");
        do {
            System.out.print("이메일: ");
            memberEmail = scanner.next();
            chekResult = memberRepository.emailChek(memberEmail);
            if (chekResult) {
                System.out.println("사용가능한 이메일 입니다");
            } else {
                System.out.println("이미 사용중인 이메일 입니다");
            }
        } while (!chekResult);
        System.out.print("비밀번호: ");
        String memberPass = scanner.next();
        System.out.print("이름: ");
        String memberName = scanner.next();
        System.out.print("전화번호: ");
        String memberMobile = scanner.next();
        MemberDTO memberDTO = new MemberDTO(memberEmail, memberPass, memberName, memberMobile);
        boolean result = memberRepository.memberJoin(memberDTO);
        if (result) {
            System.out.println("회원가입을 축하드립니다");
        } else {
            System.out.println("회원가입에 실패하였습니다 다시 시도해주십시오");
        }
    }

    public void memberLogin() {
        System.out.print("이메일: ");
        String memberEmail = scanner.next();
        System.out.print("비밀 번호: ");
        String memberPass = scanner.next();
        MemberDTO loginResult = memberRepository.memberLogin(memberEmail, memberPass);
        if (loginResult != null) {
            System.out.println("로그인 되었습니다");
            commonVariables.loginEmail = memberEmail;
        } else {
            System.out.println("로그인에 실패하였습니다 다시 확인해 주십시오");
        }
    }

    public void memberList() {
        List<MemberDTO> memberDTOList = memberRepository.memberList();
        for (MemberDTO memberDTO : memberDTOList) {
            System.out.println("memberDTO = " + memberDTO);
        }
    }

    public void memberUpdate() {
        commonVariables memberLogin = commonVariables;
        if (memberLogin != null) {
            System.out.print("수정할 이름: ");
            String memberName = scanner.next();
            System.out.print("수정할 전화번호: ");
            String memberMobile = scanner.next();
            boolean updateResult = memberRepository.memberUpdate(commonVariables.loginEmail, memberName, memberMobile);
            if (updateResult) {
                System.out.println("수정 성공");
            } else {
                System.out.println("수정 실패");
            }

        }
    }

    public void memberDelete() {
        int num = 0;
        if (commonVariables.loginEmail != null) {
            System.out.println("탈퇴 하시겠습니까?");
            System.out.println("1.예 | 2.아니오");
            num = scanner.nextInt();
            if (num == 1) {
                System.out.println("비밀번호: ");
                String memberPass = scanner.next();
                MemberDTO memberDTO = memberRepository.memberLogin(commonVariables.loginEmail, memberPass);
                if (memberDTO != null) {
                    boolean result = memberRepository.memberDelete(commonVariables.loginEmail);
                    if (result) {
                        System.out.println("탈퇴 처리가 완료되었습니다");
                    } else {
                        System.out.println("탈퇴 처리가 실행되지 않았습니다");
                    }
                }
            }
        } else {
            System.out.println("로그인 해주세요");
        }
    }

    public void memberLogout() {

        if (commonVariables.loginEmail != null){
        int num = 0;
        System.out.println("로그아웃 하시겠습니까?");
        System.out.println("1.예 | 2.아니오");
        num = scanner.nextInt();
        if (num == 1){
            commonVariables.loginEmail= null;
            System.out.println("로그아웃 되었습니다");
        }else {
            System.out.println("취소되었습니다");
        }

        }else {
            System.out.println("로그인 해주세요");
        }
    }
}

회원가입과 관련된 기능을 해주는 코드이다 회원가입을 할 때 중복확인을 할 수 있게 만들어 주었고

회원 정보 수정 및 회원 탈퇴

로그아웃기능을 만들어 주었다

package ch11_classes.ex06.repository;


import ch11_classes.ex06.common.commonVariables;
import ch11_classes.ex06.dto.MemberDTO;

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

public class MemberRepository {
    List<MemberDTO> memberDTOList = new ArrayList<>();

    public boolean emailChek(String memberEmail) {
        boolean result = true;
        for (int i = 0; i < memberDTOList.size(); i++) {
            if (memberEmail.equals(memberDTOList.get(i).getMemberEmail())){
                result =false;
            }
        }
        return result;
    }

    public boolean memberJoin(MemberDTO memberDTO) {
        return memberDTOList.add(memberDTO);
    }

    public MemberDTO memberLogin(String memberEmail, String memberPass) {
        MemberDTO result = null;
        for (int i = 0; i < memberDTOList.size(); i++) {
            if (memberEmail.equals(memberDTOList.get(i).getMemberEmail()) && memberPass.equals(memberDTOList.get(i).getMemberPass())){
                result = memberDTOList.get(i);
            }
        }
        return result;
    }

    public List<MemberDTO> memberList() {
        return memberDTOList;
    }

    public boolean memberUpdate(String loginEmail, String memberName, String memberMobile) {
        boolean result = false;
        for (int i = 0; i < memberDTOList.size(); i++) {
            if (loginEmail.equals(memberDTOList.get(i).getMemberEmail())){
                memberDTOList.get(i).setMemberName(memberName);
                memberDTOList.get(i).setMemberMobile(memberMobile);
                result = true;
            }
        }
        return result;
    }

    public boolean memberDelete(String loginEmali) {
        boolean result = false;
        for (int i = 0; i < memberDTOList.size(); i++) {
            if (loginEmali.equals(memberDTOList.get(i).getMemberEmail())){
                memberDTOList.remove(i);
                result = true;
            }

        }
        return result;
    }
}

회원가입 및 회원의 정보를 필드에서 가져와주며 회원메뉴에 기능들이다

 

2 < 게시판 기능>

 

package ch11_classes.ex06.service;

import ch11_classes.ex06.common.commonVariables;
import ch11_classes.ex06.dto.BoardDTO;
import ch11_classes.ex06.dto.CommentDTO;
import ch11_classes.ex06.dto.MemberDTO;
import ch11_classes.ex06.repository.BoardRepository;
import ch11_classes.ex06.repository.CommentRepository;

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

public class BoardService {
    CommentRepository commentRepository = new CommentRepository();
    commonVariables commonVariables = new commonVariables();
    BoardRepository boardRepository = new BoardRepository();
    Scanner scanner = new Scanner(System.in);

    public void boardWriting() {
        System.out.print("글 제목: ");
        String boardTitle = scanner.next();
        System.out.print("내용: ");
        String boardContents = scanner.next();
        BoardDTO boardDTO = new BoardDTO(boardTitle, commonVariables.loginEmail, boardContents, 0L);
        boolean result = boardRepository.boardWriting(boardDTO);
        if (result) {
            System.out.println("작성 완료");
        } else {
            System.out.println("작성 실패");
        }
    }

    public void boardList() {
        List<BoardDTO> boardDTOList = boardRepository.boardList();
        for (BoardDTO boardDTO : boardDTOList)
            listPrint(boardDTOList);
    }

    private void listPrint(List<BoardDTO> boardDTOList) {
        System.out.println("글번호\t" + "제목\t" + "작성자\t" + "조회수" + "작성일\t");
        for (BoardDTO boardDTO : boardDTOList) {
            System.out.println(boardDTO.getId() + "\t" + boardDTO.getBoardTitle() + "\t" + boardDTO.getBoardWriter() + "\t" + boardDTO.getBoardHits() + "\t" + boardDTO.getCreatedAt());
        }
    }

    public void boardCheck() {
        List<BoardDTO> boardDTOList = boardRepository.boardList();
        System.out.println("조회하실 글 번호를 입력하세요: ");
        Long id = scanner.nextLong();
        boardRepository.boardHits(id);
        BoardDTO boardDTO = boardRepository.bordChck(id);
        if (boardDTO != null) {
            System.out.println("글번호\t" + "제목\t" + "내용\t" + "조회수" + "작성일\t");
            System.out.println(boardDTO.getId() + "\t" + boardDTO.getBoardTitle() + "\t" + boardDTO.getBoardContents() + "\t" + boardDTO.getBoardHits() + "\t" + boardDTO.getCreatedAt());
            System.out.println("===========댓글창==================");
            List<CommentDTO> commentDTOList = commentRepository.commentList();
            if (commentDTOList.isEmpty()){
                System.out.println("작성된 댓글이 없습니다");
                System.out.println("댓글을 작성 하시려면 1번 아니면 2번을 선택하세요");
                int num = scanner.nextInt();
                if (num == 1){
                    System.out.print("댓글을 입력 하세요");
                    String commentContents = scanner.next();
                    CommentDTO commentDTO = new CommentDTO(0L ,commonVariables.loginEmail ,commentContents  );
                    boolean result = commentRepository.commentSave(commentDTO);
                    if (result){
                        System.out.println("등록성공");
                    } else {
                        System.out.println("등록 실패");
                    }
                }else {
                    return;
                }
            } else if (commentDTOList !=null) {
                System.out.println(commentDTOList);
            }
        } else {
        }
    }public void boardUpdate() {
        System.out.println("조회할 글 번호: ");
        Long id = scanner.nextLong();
        BoardDTO boardDTO = boardRepository.bordChck(id);
        if (boardDTO != null) {
            System.out.println("수정할 제목: ");
            String boardTitle = scanner.next();
            System.out.println("수정할 내용: ");
            String boardContrnts = scanner.next();
            boolean updateResult = boardRepository.boardUpdate(id, commonVariables.loginEmail, boardTitle, boardContrnts);
            if (updateResult) {
                System.out.println("수정 성공");
            } else {
                System.out.println("본인의 글 만 수정할수 있습니다");
            }
        }
    }

    public void boardDelete() {
        System.out.println("삭제할 글 번호");
        Long id = scanner.nextLong();
        boolean result = boardRepository.boardDelete(id, commonVariables.loginEmail);
        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("검색결과가 없습니다");
        }
    }
}

게시판에 글 작성 및 조회 수정 삭제 기능들이다

글 조회를 하면 댓글이 보이고 댓글이 없을 경우 댓글을 달수 있게 만들어 주었다

package ch11_classes.ex06.repository;

import ch11_classes.ex06.dto.BoardDTO;

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

public class BoardRepository {
    List<BoardDTO> boardDTOList = new ArrayList<>();
    public boolean boardWriting(BoardDTO boardDTO) {
        return boardDTOList.add(boardDTO);
    }

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

    public BoardDTO bordChck(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 void boardHits(Long id) {
        for (int i = 0; i < boardDTOList.size(); i++) {
            if (id.equals(boardDTOList.get(i).getId())){
                Long hits = boardDTOList.get(i).getBoardHits();
                boardDTOList.get(i).setBoardHits(++hits);
            }
        }
    }

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

    }

    public boolean boardDelete(Long id, String loginEmail) {
        boolean result = false;
        for (int i = 0; i < boardDTOList.size(); i++) {
            if (loginEmail.equals(boardDTOList.get(i).getBoardWriter())){
                if (id.equals(boardDTOList.get(i).getId())){
                    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;
    }
}

 

3 <댓글>

package ch11_classes.ex06.repository;

import ch11_classes.ex06.dto.CommentDTO;

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

public class CommentRepository {
    List<CommentDTO> commentDTOList = new ArrayList<>();



    public List<CommentDTO> commentList() {
        return commentDTOList;
    }

    public boolean commentSave(CommentDTO commentDTO) {
        return commentDTOList.add(commentDTO);
    }
}

댓글 작성 기능을 실행시켰을 때 댓글을 저장해 주고 그 정보를 불러와주는 코드이다

728x90
반응형