게시글의 작성일, 수정일
댓글의 작성일, 수정일
BoardDTO
package model;
import java.util.Date;
public class BoardDTO {
private int id;
private String title;
private int writerId;
private String writerNickname;
private String content;
private Date entryDate;
private Date modifyDate;
public Date getEntryDate() {
return entryDate;
}
public void setEntryDate(Date entryDate) {
this.entryDate = entryDate;
}
public Date getModifyDate() {
return modifyDate;
}
public void setModifyDate(Date modifyDate) {
this.modifyDate = modifyDate;
}
public int getWriterId() {
return writerId;
}
public void setWriterId(int writerId) {
this.writerId = writerId;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getWriterNickname() {
return writerNickname;
}
public void setWriterNickname(String writerNickname) {
this.writerNickname = writerNickname;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public boolean equals(Object o) {
if (o instanceof BoardDTO) {
BoardDTO b = (BoardDTO) o;
return id == b.id;
}
return false;
}
public BoardDTO(BoardDTO origin) {
id = origin.id;
title = origin.title;
writerId = origin.writerId;
writerNickname = origin.writerNickname;
content = origin.content;
entryDate = origin.entryDate;
modifyDate = origin.modifyDate;
}
public BoardDTO() {
}
public BoardDTO(int id) {
this.id = id;
}
}
ReplyDTO
package model;
import java.util.Date;
public class ReplyDTO {
private int id;
private String content;
private int boardId;
private int writerId;
private String writerNickname;
private Date entryDate;
private Date modifyDate;
public Date getEntryDate() {
return entryDate;
}
public void setEntryDate(Date entryDate) {
this.entryDate = entryDate;
}
public Date getModifyDate() {
return modifyDate;
}
public void setModifyDate(Date modifyDate) {
this.modifyDate = modifyDate;
}
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;
this.entryDate = original.entryDate;
this.modifyDate = original.modifyDate;
}
public boolean equals(Object o){
if (o instanceof ReplyDTO){
ReplyDTO r = (ReplyDTO) o;
return id == r.id;
}
return false;
}
}
BoardController
package controller;
import model.BoardDTO;
import java.util.ArrayList;
import java.util.Date;
public class BoardController {
private ArrayList<BoardDTO> list;
private int nextId;
public BoardController(){
list = new ArrayList<>();
nextId = 1;
for (int i = 1; i <= 4; i++) {
BoardDTO b = new BoardDTO();
b.setTitle("제목 " + i);
b.setWriterId(1);
b.setWriterNickname("일반회원1");
b.setContent(i + "번째 게시글의 내용입니다.");
add(b);
}
}
public void add(BoardDTO boardDTO){
boardDTO.setId(nextId++);
boardDTO.setEntryDate(new Date());
boardDTO.setModifyDate(new Date());
list.add(boardDTO);
}
public BoardDTO selectOne(int id){
BoardDTO temp = new BoardDTO(id);
if (list.contains(temp)){
return new BoardDTO(list.get(list.indexOf(temp))); // 깊은 복사
}
return null;
}
public ArrayList<BoardDTO> selectAll(){
ArrayList<BoardDTO> temp = new ArrayList<>();
for (BoardDTO b : list){
temp.add(new BoardDTO(b)); // 깊은 복사
}
return temp;
}
public void update(BoardDTO boardDTO){
boardDTO.setModifyDate(new Date());
list.set(list.indexOf(boardDTO), boardDTO);
}
public void delete(int id){
list.remove(new BoardDTO(id));
}
}
ReplyController
package controller;
import model.ReplyDTO;
import java.util.ArrayList;
import java.util.Date;
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++);
replyDTO.setEntryDate(new Date());
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){
replyDTO.setModifyDate(new Date());
list.set(list.indexOf(replyDTO), replyDTO);
}
public void delete(int id){
ReplyDTO r = new ReplyDTO();
r.setId(id);
list.remove(r);
}
}
BoardViewer
package viewer;
import controller.BoardController;
import model.BoardDTO;
import model.UserDTO;
import util.ScannerUtil;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;
public class BoardViewer {
private BoardController boardController;
private UserViewer userViewer;
private ReplyViewer replyViewer;
private final Scanner SCANNER;
private final String DATE_FORMAT = "yy/MM/dd HH:mm:ss";
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 {
DateFormat df = new SimpleDateFormat(DATE_FORMAT);
for (BoardDTO b : list) {
System.out.printf("%d. %s\n", b.getId(), b.getTitle(), df.format(b.getEntryDate()));
}
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) {
DateFormat df = new SimpleDateFormat(DATE_FORMAT);
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("작성일: " + df.format(boardDTO.getEntryDate()));
System.out.println("작성일: "+ df.format(boardDTO.getModifyDate()));
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);
}
}
}
ReplyViewer
package viewer;
import controller.ReplyController;
import model.ReplyDTO;
import model.UserDTO;
import util.ScannerUtil;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
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);
SimpleDateFormat sdf = new SimpleDateFormat("yyMd H:m:s");
for (ReplyDTO r : list){
String date = sdf.format(r.getEntryDate());
if (r.getModifyDate() != null) {
date = sdf.format(r.getModifyDate());
}
System.out.printf("%d. %s(%s) : %s\n", r.getId(), r.getWriterNickname(), date, 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);
}
}
}
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();
}
}
'JX405기_비트 > Java' 카테고리의 다른 글
Day05-6 ToString (0) | 2023.01.16 |
---|---|
Day05-5 4일차 숙제 강사님 풀이 01 (0) | 2023.01.16 |
Day05-4 정규표현식 (0) | 2023.01.16 |
Day05-3 Connection을 이용한 db연결 만들기 (0) | 2023.01.16 |
Day05-2 인터페이스 상속 (0) | 2023.01.16 |