Backend
////////
home
💪🏻

게시판 관련 메소드 정리

게시판 관련 기능을 담은 인터페이스 구현 로직
package com.practice; import java.util.List; import java.util.Map; public interface PostSystem { // 특정 작성자(author)가 작성한 게시물들을 필터링하여 반환하는 메소드 List<Post> filterPostsByAuthor(List<Post> posts, String author); // 모든 게시물의 댓글을 추출하여 리스트로 반환하는 메소드 List<Comment> getAllComments(List<Post> posts); // 모든 게시물의 제목을 추출하여 리스트로 반환하는 메소드 List<String> getAllPostTitles(List<Post> posts); // 특정 키워드(keyword)를 포함하는 게시물들을 필터링하여 반환하는 메소드 List<Post> filterPostsByKeyword(List<Post> posts, String keyword); // 각 게시물의 댓글 수를 계산하여 맵 형태로 반환하는 메소드 // 게시물 ID를 키로 하고 댓글 수를 값으로 하는 맵 Map<Integer, Integer> getCommentCountByPost(List<Post> posts); // 최신 게시물 3개를 추출하여 리스트로 반환하는 메소드 List<Post> getLatestPosts(List<Post> posts); // 게시물 번호 중 가장 높은 게시물 번호를 반환하는 메소드 int getHighestPostId(List<Post> posts); // 게시물 입력 받기 메소드 void insertPost(List<Post> pList); }
Java
복사
인터페이스에서 구현한 메소드의 구체적인 동작 구현을 정리한 코드
package com.practice; import java.time.LocalDateTime; import java.util.*; import java.util.stream.Collectors; public class PostSystemImpl implements PostSystem { // 특정 작성자(author)가 작성한 게시물들을 필터링하여 반환하는 메소드 @Override public List<Post> filterPostsByAuthor(List<Post> posts, String author) { return posts .stream() .filter(post -> post.getAuthor().equals(author)) .collect(Collectors.toList()); } // 모든 게시물의 댓글을 추출하여 리스트로 반환하는 메소드 - flatMap 활용하여 stream 변환 @Override public List<Comment> getAllComments(List<Post> posts) { return posts .stream() .flatMap(post -> post.getComments().stream()) // stream으로 변환 .collect(Collectors.toList()); } // 모든 게시물의 제목을 추출하여 리스트로 반환하는 메소드 @Override public List<String> getAllPostTitles(List<Post> posts) { return posts.stream() .map(Post::getTitle) .collect(Collectors.toList()); } // 특정 키워드(keyword)를 포함하는 게시물들을 필터링하여 반환하는 메소드 - 제목 or 내용 포함 @Override public List<Post> filterPostsByKeyword(List<Post> posts, String keyword) { return posts.stream() .filter(post -> post.getContent().contains(keyword) || post.getTitle().contains(keyword)) .collect(Collectors.toList()); } // 각 게시물의 댓글 수를 계산하여 맵 형태로 반환하는 메소드 // 게시물 ID를 키로 하고 댓글 수를 값으로 하는 맵 @Override public Map<Integer, Integer> getCommentCountByPost(List<Post> posts) { return posts .stream() .collect(Collectors.toMap( post -> post.getId(), post -> post.getComments().size() )); } // 최신 게시물 3개를 추출하여 리스트로 반환하는 메소드 @Override public List<Post> getLatestPosts(List<Post> posts) { return posts .stream() // .sorted((p1, p2) -> p2.getCreatedAt().compareTo(p1.getCreatedAt())) .sorted(Comparator.comparing(Post::getCreatedAt).reversed()) .limit(3) // 페이지 검색 조회 수 제한 .collect(Collectors.toList()); } // 게시물 번호 중 가장 높은 게시물 번호를 반환하는 메소드 @Override public int getHighestPostId(List<Post> posts) { Optional<Post> optMaxPost = posts .stream() .max(Comparator.comparing(Post::getId)); Post p = optMaxPost.orElse(null); return p == null ? 0 : p.getId(); } // 게시물 입력 받기 메소드 @Override public void insertPost(List<Post> pList) { System.out.println("게시물 작성"); Scanner sc = new Scanner(System.in); System.out.println("제목 입력 : "); String title = sc.nextLine(); System.out.println("내용 입력 : "); String content = sc.nextLine(); System.out.println("이름 입력 : "); String author = sc.nextLine(); pList.add(new Post(getHighestPostId(pList) + 1, title, content, author, LocalDateTime.now(), new ArrayList<Comment>())); sc.close(); } }
Java
복사