JX405기_비트/Java

Day05-5 4일차 숙제 강사님 풀이 01

_하루살이_ 2023. 1. 16. 16:23

연결한 게시판과 로그인에 댓글 기능 넣기

 

ReplyDTO

package model;

public class ReplyDTO {
    private int id;
    private String content;
    private int boardId;
    private int writerId;
    private String writerNickname;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }
    public int getBoardId() {
        return boardId;
    }

    public void setBoardId(int boardId) {
        this.boardId = boardId;
    }

    public int getWriterId() {
        return writerId;
    }

    public void setWriterId(int writerId) {
        this.writerId = writerId;
    }

    public String getWriterNickname() {
        return writerNickname;
    }

    public void setWriterNickname(String writerNickname) {
        this.writerNickname = writerNickname;
    }

    public ReplyDTO(){

    }

    public ReplyDTO(ReplyDTO original){
        this.id = original.id;
        this.content = original.content;
        this.boardId = original.boardId;
        this.writerId = original.writerId;
        this.writerNickname = original.writerNickname;

    }

    public boolean equals(Object o){
        if (o instanceof ReplyDTO){
            ReplyDTO r = (ReplyDTO) o;
            return id == r.id;
        }
        return false;
    }

    public ReplyDTO(int id){
        this.id = id;
    }


}

ReplyController

package controller;

import model.ReplyDTO;

import java.util.ArrayList;

public class ReplyController {
    private ArrayList<ReplyDTO> list;
    private int nextId;

    public ReplyController(){
        list = new ArrayList<>();
        nextId = 1;

    }

    public void add(ReplyDTO replyDTO){
        replyDTO.setId(nextId++);
        list.add(replyDTO);
    }

    // 댓글 리스트 불러오기
    public ArrayList<ReplyDTO> selectAll(int boardId){
        ArrayList<ReplyDTO> temp = new ArrayList<>();
        for (ReplyDTO r : list){
            if (r.getBoardId() == boardId){
                temp.add(new ReplyDTO(r)); // 깊은 복사
            }
        }

        return temp;
    }

    // 특정 댓글 목록 불러오기
    public ReplyDTO selectOne(int id){
        ReplyDTO r = new ReplyDTO(id);
        r.setId(id);
        if (list.contains(r)){
            return new ReplyDTO(list.get(list.indexOf(r)));

        } else {
            return null;
        }
    }

    public void update(ReplyDTO replyDTO){
        list.set(list.indexOf(replyDTO), replyDTO);
    }

    public void delete(int id){
        ReplyDTO r = new ReplyDTO();
        r.setId(id);
        list.remove(r);
    }
}

ReplyViewer

package viewer;

import controller.ReplyController;
import model.ReplyDTO;
import model.UserDTO;
import util.ScannerUtil;


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

public class ReplyViewer {
    private Scanner SCANNER;
    private ReplyController replyController;
    private UserDTO logIn;

    public ReplyViewer(Scanner scanner){
        this.SCANNER = scanner;
        replyController = new ReplyController();
    }

    public void setLogin(UserDTO logIn){
        this.logIn = logIn;
    }

    public void printAll(int boardId){
        ArrayList<ReplyDTO> list = replyController.selectAll(boardId);
        for (ReplyDTO r : list){
            System.out.printf("%d. %s : %s\n", r.getId(), r.getWriterNickname(), r.getContent());

        }
    }

    public void showMenu(int boardId){
        String message = "1. 댓글 등록 2. 댓글 수정 3. 댓글 삭제 4. 뒤로 가기";
        int userChoice = ScannerUtil.nextInt(SCANNER, message);
        if (userChoice == 1) {
            writeReply(boardId);
        } else if (userChoice != 4){
            message = "수정/삭제할 댓글 번호나 뒤로 가실려면 0를 입력해주세요.";
            int targetId = ScannerUtil.nextInt(SCANNER, message);
            while (targetId != 0 && replyController.selectOne(targetId) == null){
                System.out.println("잘못 입력하셨습니다.");
                targetId = ScannerUtil.nextInt(SCANNER, message);
            }

            if (userChoice == 2) {
                updateReply(targetId, boardId);
            } else if (userChoice == 3) {
                deleteReply(targetId, boardId);
            }
        }
    }

    private void writeReply(int boardId){
        ReplyDTO r = new ReplyDTO();
        r.setBoardId(boardId);
        r.setWriterId(logIn.getId());
        r.setWriterNickname(logIn.getNickname());

        String message = "댓글 내용을 입력해주세요.";
        r.setContent(ScannerUtil.nextLine(SCANNER, message));

        replyController.add(r);
    }


    private void deleteReply(int id, int boardId){
        ReplyDTO r = replyController.selectOne(id);
        if (r !=null && r.getBoardId() == boardId && r.getWriterId() == logIn.getId()){
            String message = "정말로 삭제하시겠습니까? Y/N";
            String yesNo = ScannerUtil.nextLine(SCANNER, message);
            if (yesNo.equalsIgnoreCase("Y")){
                replyController.delete(id);
            }
        }
    }


    private void updateReply(int id, int boardId){
        ReplyDTO r = replyController.selectOne(id);
        if (r !=null && r.getBoardId() == boardId && r.getWriterId() == logIn.getId()){
            String message = "새로운 내용을 입력해주세요.";
            r.setContent(ScannerUtil.nextLine(SCANNER,message));

            replyController.update(r);

        }
    }

}

BoardViewer

package viewer;

import controller.BoardController;
import model.BoardDTO;
import model.UserDTO;
import util.ScannerUtil;


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

public class BoardViewer {
    private BoardController boardController;
    private UserViewer userViewer;

    private ReplyViewer replyViewer;
    private final Scanner SCANNER;
    private UserDTO logIn;

    public BoardViewer(Scanner scanner) {
        boardController = new BoardController();
        SCANNER = scanner;
    }

    public void setUserViewer(UserViewer userViewer) {
        this.userViewer = userViewer;
    }

    public void setReplyViewer(ReplyViewer replyViewer) {
        this.replyViewer = replyViewer;
    }

    public void setLogIn(UserDTO logIn) {
        this.logIn = logIn;
    }

    public void showMenu() {
        String message = "1. 새 글 작성하기 2. 글 목록 보기 3. 종료";
        while (true) {
            int userChoice = ScannerUtil.nextInt(SCANNER, message);
            if (userChoice == 1) {
                writeBoard();
            } else if (userChoice == 2) {
                printList();
            } else if (userChoice == 3) {
                System.out.println("사용해주셔서 감사합니다.");
                break;
            }
        }
    }

    private void writeBoard() {
        BoardDTO boardDTO = new BoardDTO();

        boardDTO.setWriterId(logIn.getId());
        boardDTO.setWriterNickname(logIn.getNickname());

        String message = "글의 제목을 입력해주세요.";
        boardDTO.setTitle(ScannerUtil.nextLine(SCANNER, message));

        message = "글의 내용을 입력해주세요.";
        boardDTO.setContent(ScannerUtil.nextLine(SCANNER, message));

        boardController.add(boardDTO);
    }

    private void printList() {
        ArrayList<BoardDTO> list = boardController.selectAll();

        if (list.isEmpty()) {
            System.out.println("아직 등록된 글이 없습니다.");
        } else {
            for (BoardDTO b : list) {
                System.out.printf("%d. %s\n", b.getId(), b.getTitle());
            }

            String message = "상세보기할 글의 번호나 뒤로 가실려면 0을 입력해주세요.";
            int userChoice = ScannerUtil.nextInt(SCANNER, message);

            while (userChoice != 0 && !list.contains(new BoardDTO(userChoice))) {
                System.out.println("잘못 입력하셨습니다.");
                userChoice = ScannerUtil.nextInt(SCANNER, message);
            }

            if (userChoice != 0) {
                printOne(userChoice);
            }
        }
    }

    private void printOne(int id) {
        BoardDTO boardDTO = boardController.selectOne(id);
        System.out.println("=================================================");
        System.out.println(boardDTO.getTitle());
        System.out.println("-------------------------------------------------");
        System.out.println("글 번호: " + boardDTO.getId());
        System.out.println("글 작성자: " + boardDTO.getWriterNickname());
        System.out.println("-------------------------------------------------");
        System.out.println(boardDTO.getContent());
        System.out.println("-------------------------------------------------");
        System.out.println("댓글");
        System.out.println("-------------------------------------------------");
        replyViewer.printAll(id);
        System.out.println("=================================================");
        String message;
        int userChoice;

        if (boardDTO.getWriterId() == logIn.getId()) {
            message = "1. 수정 2. 삭제 3. 댓글 메뉴 4. 뒤로 가기";
            userChoice = ScannerUtil.nextInt(SCANNER, message, 1, 4);
        } else {
            message = "3. 댓글 메뉴 4. 뒤로 가기";
            userChoice = ScannerUtil.nextInt(SCANNER, message, 3, 4);
        }

        if (userChoice == 1) {
            update(id);
        } else if (userChoice == 2) {
            delete(id);
        } else if (userChoice == 3) {
            replyViewer.showMenu(id);
            printOne(id);
        } else if (userChoice == 4) {
            printList();
        }
    }

    private void update(int id) {
        BoardDTO b = boardController.selectOne(id);

        String message = "새로운 제목을 입력해주세요.";
        b.setTitle(ScannerUtil.nextLine(SCANNER, message));

        message = "새로운 내용을 입력해주세요.";
        b.setContent(ScannerUtil.nextLine(SCANNER, message));

        boardController.update(b);
    }

    private void delete(int id) {
        String message = "정말로 삭제하시겠습니까? Y/N";
        String yesNo = ScannerUtil.nextLine(SCANNER, message);
        if (yesNo.equalsIgnoreCase("Y")) {
            boardController.delete(id);
            printList();
        } else {
            printOne(id);
        }
    }
}

UserViewer

package viewer;

import controller.UserController;
import model.UserDTO;
import util.ScannerUtil;

import java.util.Scanner;

public class UserViewer {
    private final Scanner SCANNER;
    private UserController userController;
    private BoardViewer boardViewer;

    private ReplyViewer replyViewer;
    private UserDTO logIn = null;

    public UserViewer(Scanner scanner) {
        SCANNER = scanner;
        userController = new UserController();
    }

    public void setBoardViewer(BoardViewer boardViewer) {
        this.boardViewer = boardViewer;
    }

    public void setReplyViewer(ReplyViewer replyViewer) {
        this.replyViewer = replyViewer;
    }

    public void showIndex() {
        String message = "1. 로그인 2. 회원가입 3. 종료";
        while (true) {
            int userChoice = ScannerUtil.nextInt(SCANNER, message);
            if (userChoice == 1) {
                auth();
                if (logIn != null) {
                    boardViewer.setLogIn(logIn);
                    replyViewer.setLogin(logIn);
                    showMenu();
                }
            } else if (userChoice == 2) {
                register();
            } else if (userChoice == 3) {
                System.out.println("사용해주셔서 감사합니다.");
                break;
            }
        }
    }

    private void register() {
        String message;
        message = "사용하실 아이디를 입력해주세요.";
        String username = ScannerUtil.nextLine(SCANNER, message);

        while (!userController.validateUsername(username)) {
            System.out.println("해당 아이디는 사용하실 수 없습니다.");
            message = "사용하실 아이디나 뒤로 가실려면 \"X\"를 입력해주세요.";
            username = ScannerUtil.nextLine(SCANNER, message);

            if (username.equalsIgnoreCase("X")) {
                break;
            }
        }

        if (!username.equalsIgnoreCase("X")) {
            UserDTO u = new UserDTO();
            u.setUsername(username);

            message = "사용하실 비밀번호를 입력해주세요.";
            u.setPassword(ScannerUtil.nextLine(SCANNER, message));

            message = "사용하실 닉네임을 입력해주세요.";
            u.setNickname(ScannerUtil.nextLine(SCANNER, message));

            userController.insert(u);
        }
    }

    private void auth() {
        String message;
        message = "아이디를 입력해주세요.";
        String username = ScannerUtil.nextLine(SCANNER, message);

        message = "비밀번호를 입력해주세요.";
        String password = ScannerUtil.nextLine(SCANNER, message);

        logIn = userController.auth(username, password);

        if (logIn == null) {
            System.out.println("로그인 정보가 정확하지 않습니다.");
        }
    }

    private void showMenu() {
        String message = "1. 게시판으로 2. 회원 정보 관리 3. 로그아웃";
        while (logIn != null) {
            int userChoice = ScannerUtil.nextInt(SCANNER, message);
            if (userChoice == 1) {
                boardViewer.showMenu();
            } else if (userChoice == 2) {
                printOne();
            } else if (userChoice == 3) {
                logIn = null;
                System.out.println("정상적으로 로그아웃되었습니다.");
            }
        }
    }

    private void printOne() {
        System.out.println("회원 번호: " + logIn.getId());
        System.out.println("회원 닉네임: " + logIn.getNickname());
        System.out.println("---------------------------------------------");
        String message = "1. 수정 2. 탈퇴 3. 뒤로가기";
        int userChoice = ScannerUtil.nextInt(SCANNER, message);
        if (userChoice == 1) {
            update();
        } else if (userChoice == 2) {
            delete();
        }
    }

    private void update() {

        String message = "새로운 비밀번호를 입력해주세요.";
        String newPassword = ScannerUtil.nextLine(SCANNER, message);

        message = "새로운 닉네임을 입력해주세요.";
        String newNickname = ScannerUtil.nextLine(SCANNER, message);

        message = "기존 비밀번호를 입력해주세요.";
        String oldPassword = ScannerUtil.nextLine(SCANNER, message);

        if (logIn.getPassword().equals(oldPassword)) {
            logIn.setNickname(newNickname);
            logIn.setPassword(newPassword);

            userController.update(logIn);
        } else {
            System.out.println("회원 정보 변경에 실패하였습니다.");
        }
    }

    private void delete() {
        String message = "정말로 삭제하시겠습니까? Y/N";
        String yesNo = ScannerUtil.nextLine(SCANNER, message);

        if (yesNo.equalsIgnoreCase("Y")) {
            message = "비밀번호를 입력해주세요.";
            String password = ScannerUtil.nextLine(SCANNER, message);

            if (password.equals(logIn.getPassword())) {
                userController.delete(logIn.getId());
                logIn = null;
            }
        }
    }
}

Ex05Board03

package day0112;

import viewer.BoardViewer;
import viewer.ReplyViewer;
import viewer.UserViewer;

import java.util.Scanner;

public class Ex05Board03 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        BoardViewer boardViewer = new BoardViewer(scanner);
        UserViewer userViewer = new UserViewer(scanner);
        ReplyViewer replyViewer = new ReplyViewer(scanner);

        userViewer.setBoardViewer(boardViewer); // Setter 를 통해서 해당 boardViewer 필드를 주입
        userViewer.setReplyViewer(replyViewer);

        boardViewer.setUserViewer(userViewer);
        boardViewer.setReplyViewer(replyViewer);


        userViewer.showIndex();
    }
}