Putting it together
- Java course
- Intermediate
- Lesson 69/116
- 6 min
Combine the Blog class with a menu loop.
Putting the blog together
In the final lesson we combine all pieces into a working blog. The Post, Comment and Blog classes are ready. In main we add two posts, attach one comment to each and print the post titles.
This shows the power of object-oriented design: each class does one thing, classes link via composition, and Blog owns the management.
The assembly order
In the final step we call all pieces in a logical order: first an empty blog with Blog blog = new Blog();, then posts with addPost, then a comment on each with addComment. Finally we print titles with blog.posts.get(i).title.
This order respects dependencies: a comment requires a post to exist first. The flow mirrors a real app's data flow at a small scale.
Practice Assemble the whole blog
Bring every piece together. Write the Post, Comment and Blog classes (including the comments list and findByTitle). In main, create a Blog blog = new Blog();, add two posts, attach one comment to each and print each post's title on its own line: System.out.println(blog.posts.get(0).title); and System.out.println(blog.posts.get(1).title);.
Starter code
import java.util.List;
import java.util.ArrayList;
class Post {
String title;
String author;
List<Comment> comments = new ArrayList<>();
Post(String title, String author) {
this.title = title;
this.author = author;
}
void addComment(Comment c) { comments.add(c); }
}
class Comment {
String author;
String text;
Comment(String author, String text) {
this.author = author;
this.text = text;
}
}
class Blog {
List<Post> posts = new ArrayList<>();
void addPost(Post post) { posts.add(post); }
}
List<Post> findByTitle(String keyword) {
List<Post> result = new ArrayList<>();
for (Post post : posts) {
if (post.title.contains(keyword)) {
result.add(post);
}
}
return result;
}
// main: create a blog, add two posts, attach a comment to each, print titlesSample solution
class Post {
String title;
String author;
List<Comment> comments = new ArrayList<>();
Post(String title, String author) {
this.title = title;
this.author = author;
}
void addComment(Comment c) { comments.add(c); }
}
class Comment {
String author;
String text;
Comment(String author, String text) {
this.author = author;
this.text = text;
}
}
class Blog {
List<Post> posts = new ArrayList<>();
void addPost(Post post) { posts.add(post); }
List<Post> findByTitle(String keyword) {
List<Post> result = new ArrayList<>();
for (Post post : posts) {
if (post.title.contains(keyword)) {
result.add(post);
}
}
return result;
}
}
public class Main {
public static void main(String[] args) {
Blog blog = new Blog();
blog.addPost(new Post("Java intro", "Ayşe"));
blog.addPost(new Post("Other", "Mehmet"));
blog.posts.get(0).addComment(new Comment("Ali", "Nice"));
blog.posts.get(1).addComment(new Comment("Veli", "Thanks"));
System.out.println(blog.posts.get(0).title);
System.out.println(blog.posts.get(1).title);
}
}Expected output
Java intro Other
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Where is composition used in the blog project?
- Blog inherits from Post
- Comment implements Blog
- Post contains a Comment list Correct answer
- Post is an abstract class
Explanation: Post contains its comments via List<Comment>; this is composition.
2 Complete the post-access call
blog.posts.____(0).addComment(new Comment("Ali", "Nice"));
blog.posts.get(0).addComment(new Comment("Ali", "Nice"));
Explanation: get(0) returns the first post.
3 Match the blog project summary
| Term | Meaning |
|---|---|
Post |
Holds post data |
Comment |
Holds comment data |
Blog |
Manages the post collection |
composition |
Post contains a Comment list |