Pertemuan 11 PBO - Studi Kasus Network Project (Inheritance)
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
public class User
{
private Network network;
private String username;
private UUID id;
private HashSet<UUID> postReference;
private HashSet<UUID> commentReference;
private HashSet<UUID> followersReference;
public User()
{
postReference = new HashSet<>();
}
public void set(Network network, UUID id, String username)
{
this.id = id;
this.network = network;
this.username = username;
postReference = new HashSet<>();
commentReference = new HashSet<>();
followersReference = new HashSet<>();
}
public String getUsername()
{
return username;
}
public UUID getId()
{
return id;
}
public int getFollowersCount()
{
return followersReference.size();
}
public void addPostReference(UUID id)
{
postReference.add(id);
}
public void addCommentReference(UUID id)
{
commentReference.add(id);
}
public void follow(UUID id)
{
network.follow(this.id, id);
}
public void unfollow(UUID id)
{
network.unfollow(this.id, id);
}
public void addFollowReference(UUID secret, UUID id)
{
if(!network.isValidSecret(secret))
{
System.out.println("Cannot follow from unauthorized source");
return;
}
followersReference.add(id);
}
public void unfollowReference(UUID secret, UUID id)
{
if(!network.isValidSecret(secret))
{
System.out.println("Cannot unfollow from unauthorized source");
return;
}
followersReference.remove(id);
}
}
import java.lang.annotation.Retention;
import java.time.LocalDateTime;
import java.util.*;
public abstract class Post
{
private UUID id;
private UUID author;
protected Network network;
private HashSet<UUID> userLikesReference;
private HashSet<UUID> userDislikesReference;
private HashSet<UUID> commentsReference;
private LocalDateTime created;
public Post(Network network, UUID id, UUID author)
{
this.id = id;
this.author = author;
this.network = network;
userLikesReference = new HashSet<>();
userDislikesReference = new HashSet<>();
commentsReference = new HashSet<>();
created = LocalDateTime.now();
}
public UUID getId()
{
return id;
}
public UUID getAuthor()
{
return author;
}
public int getLikesCount()
{
return userLikesReference.size();
}
public int getDislikesCount()
{
return userDislikesReference.size();
}
public void like(UUID user)
{
network.like(user, id);
}
public void dislike(UUID user)
{
network.dislike(user, id);
}
public void addCommentReference(UUID networkSecret, UUID comment)
{
if(!network.isValidSecret(networkSecret))
{
System.out.println("Cannot make comment from unauthorized source");
return;
}
commentsReference.add(comment);
}
public void addLikeReference(UUID networkSecret, UUID user)
{
if(!network.isValidSecret(networkSecret))
{
System.out.println("Cannot like from unauthorized source");
return;
}
userLikesReference.add(user);
}
public void addDislikeReference(UUID networkSecret, UUID user)
{
if(!network.isValidSecret(networkSecret))
{
System.out.println("Cannot dislike from unauthorized source");
return;
}
userDislikesReference.add(user);
}
public abstract String getDisplay(int tabCount);
protected Iterator<UUID> getComments()
{
return commentsReference.iterator();
}
protected String getCommentDisplay(int tabCount)
{
String display = "";
Iterator<UUID> iterator = getComments();
if(iterator.hasNext())
{
display = "Comments\n";
}
while (iterator.hasNext())
{
UUID comment = iterator.next();
display += network.getDisplay(comment, tabCount);
}
return display;
}
protected static String getTabs(int tabCount)
{
String tab = "";
for(int i = 0; i < tabCount; i++)
{
tab += "\t";
}
return tab;
}
}
Kelas Post: Ini adalah kelas abstrak yang mendefinisikan struktur dasar dari sebuah postingan dalam jaringan sosial, termasuk atribut seperti id, author, referensi userLikesReference, userDislikesReference, dan commentsReference. Kelas ini memiliki fungsi dasar untuk menambahkan likes, dislikes, dan komentar dengan verifikasi dari jaringan sosial (Network) melalui networkSecret. Kelas ini juga menyediakan metode untuk menampilkan postingan dengan getDisplay, yang akan diimplementasikan oleh kelas turunan untuk menampilkan konten spesifik dari masing-masing jenis postingan.
public class MessagePost extends Post
{
private String caption;
private String message;
public MessagePost(Network network, UUID id, UUID user)
{
super(network, id, user);
}
public void setCaption(String caption)
{
this.caption = caption;
}
public void setMessage(String message)
{
this.message = message;
}
@Override
public String getDisplay(int tabCount) {
return getTabs(tabCount) + "Author : " + network.getUsername(getAuthor()) + ", Caption : " + caption +
", Message : " + message + ", Like/Dislike : " + getLikesCount() + "/" + getDislikesCount() + "\n" + getCommentDisplay(tabCount + 1);
}
}
Kelas MessagePost: Kelas ini juga turunan dari Post, tetapi mewakili postingan berupa pesan teks. Kelas ini memiliki atribut caption dan message, serta metode setCaption dan setMessage untuk menetapkan konten dan keterangan postingan. Implementasi getDisplay akan menampilkan informasi pengirim, jumlah like/dislike, serta isi pesan dengan format yang mirip dengan PhotoPost.
public class PhotoPost extends Post
{
private String caption;
private String photo;
public PhotoPost(Network network, UUID id, UUID user)
{
super(network, id, user);
}
public void setCaption(String caption)
{
this.caption = caption;
}
public void setPhoto(String photo)
{
this.photo = photo;
}
@Override
public String getDisplay(int tabCount) {
return getTabs(tabCount) + "Author : " + network.getUsername(getAuthor()) + ", Caption : " + caption +
", Photo : " + photo + ", Like/Dislike : " + getLikesCount() + "/" + getDislikesCount() +
"\n" + getCommentDisplay(tabCount + 1);
}
}
PhotoPost: Ini adalah turunan dari Post yang mewakili postingan berupa foto. Kelas ini menambahkan atribut caption dan photo serta metode setCaption dan setPhoto untuk menetapkan keterangan dan gambar yang ingin ditampilkan. Implementasi getDisplay pada kelas ini dirancang untuk menampilkan informasi pengguna yang memposting foto, jumlah like/dislike, serta keterangan dan tautan ke foto.
public class CommentPost extends Post
{
private String comment;
public CommentPost(Network network, UUID id, UUID user)
{
super(network, id, user);
}
public void setComment(String comment)
{
this.comment = comment;
}
@Override
public String getDisplay(int tabCount)
{
return getTabs(tabCount) + "Author : " + network.getUsername(getAuthor()) + ", Comment : " + comment +
", Like/Dislike : " + getLikesCount() + "/" + getDislikesCount() + "\n" + getCommentDisplay(tabCount + 1);
}
}
public abstract class PostFactory<T>
{
public abstract T create(Network network, UUID id, UUID userId);
public static class MessagePostFactory extends PostFactory<MessagePost> {
@java.lang.Override
public MessagePost create(Network network, UUID id, UUID userId)
{
return new MessagePost(network, id, userId);
}
}
public static class PhotoPostFactory extends PostFactory<PhotoPost>
{
@java.lang.Override
public PhotoPost create(Network network, UUID id, UUID userId)
{
return new PhotoPost(network, id, userId);
}
}
}
import java.util.HashMap;
import java.util.Iterator;
import java.util.UUID;
import java.util.function.Supplier;
public class Network
{
private UUID secretKey;
private HashMap<UUID, Post> postDatabase;
private HashMap<UUID, User> userDatabase;
private HashMap<Class, PostFactory> factory;
public Network()
{
secretKey = UUID.randomUUID();
postDatabase = new HashMap<>();
userDatabase = new HashMap<>();
factory = new HashMap<>();
factory.put(MessagePost.class, new PostFactory.MessagePostFactory());
factory.put(PhotoPost.class, new PostFactory.PhotoPostFactory());
}
public void like(UUID user, UUID post)
{
Post postInstance = postDatabase.getOrDefault(post, null);
User userInstance = userDatabase.getOrDefault(user, null);
if(postInstance == null || userInstance == null)
{
System.out.println("Cannot like post! invalid parameters");
return;
}
postInstance.addLikeReference(secretKey, user);
}
public void dislike(UUID user, UUID post)
{
Post postInstance = postDatabase.getOrDefault(post, null);
User userInstance = userDatabase.getOrDefault(user, null);
if(postInstance == null || userInstance == null)
{
System.out.println("Cannot dislike post! invalid parameters");
return;
}
postInstance.addDislikeReference(secretKey, user);
}
public UUID createUser(String username)
{
User user = new User();
UUID id = UUID.randomUUID();
user.set(this, id, username);
userDatabase.put(id, user);
return id;
}
public String getUsername(UUID id)
{
return userDatabase.get(id).getUsername();
}
public String getDisplay(UUID post, int tabCount)
{
return postDatabase.get(post).getDisplay(tabCount);
}
public String getAllDisplay()
{
String display = "Social Network\n";
Iterator<UUID> postIterator = postDatabase.keySet().iterator();
display += "-------------------------------------------------------------------------------" +
"------------------------------------------------------------------------------------------\n";
while (postIterator.hasNext())
{
UUID id = postIterator.next();
Post post = postDatabase.get(id);
if(post instanceof CommentPost)
{
continue;
}
display += getDisplay(id, 0);
display += "-------------------------------------------------------------------------------" +
"------------------------------------------------------------------------------------------\n";
}
return display;
}
public <T extends Post> T createPost(Class<T> type, UUID user)
{
User userInstance = userDatabase.getOrDefault(user, null);
if(userInstance == null)
{
System.out.println("Cannot create post, unknown user id!");
return null;
}
PostFactory factory = this.factory.get(type);
UUID id = UUID.randomUUID();
T post = (T)factory.create(this, id, user);
postDatabase.put(id, post);
userInstance.addPostReference(id);
return post;
}
public CommentPost createComment(UUID user, UUID post)
{
Post postInstance = postDatabase.getOrDefault(post, null);
User userInstance = userDatabase.getOrDefault(user, null);
if(postInstance == null || userInstance == null)
{
System.out.println("Cannot create comment! invalid parameters");
return null;
}
UUID id = UUID.randomUUID();
CommentPost comment = new CommentPost(this, id, user);
postDatabase.put(id, comment);
userInstance.addCommentReference(id);
postInstance.addCommentReference(secretKey, id);
return comment;
}
public void follow(UUID user, UUID target)
{
if(user == target)
{
return;
}
User targetInstance = userDatabase.get(target);
targetInstance.addFollowReference(secretKey, user);
}
public void unfollow(UUID user, UUID target)
{
if(user == target)
{
return;
}
User targetInstance = userDatabase.get(target);
targetInstance.unfollowReference(secretKey, user);
}
public int getFollowers(UUID user)
{
return userDatabase.get(user).getFollowersCount();
}
public boolean isValidSecret(UUID other)
{
return other.equals(secretKey);
}
}
public class Main
{
public static void main(String[] args)
{
Network network = new Network();
UUID fajar = network.createUser("Fajar");
UUID randi = network.createUser("Randi");
UUID arya = network.createUser("Arya");
UUID arda = network.createUser("Arda");
UUID nanda = network.createUser("Nanda");
network.follow(fajar, randi);
network.follow(randi, fajar);
network.follow(arda, arya);
network.follow(randi, nanda);
network.follow(randi, arya);
network.follow(fajar, arya);
MessagePost fajarMessage = network.createPost(MessagePost.class, fajar);
fajarMessage.setCaption("Im in departement");
fajarMessage.setMessage("Hello guys im currently in informatika");
Comment(network, fajarMessage, arya, "Where are you i can't seem to find you");
fajarMessage.like(randi);
fajarMessage.like(arya);
MessagePost randiMessage = network.createPost(MessagePost.class, randi);
randiMessage.setCaption("Im doing my PBO Assignment");
randiMessage.setMessage("Its very easy bro, i need harder assignment");
Comment(network, randiMessage, fajar, "You should teach me king");
Comment(network, randiMessage, arya, "Me too bro!");
randiMessage.dislike(nanda);
randiMessage.dislike(arda);
randiMessage.dislike(fajar);
randiMessage.dislike(arya);
PhotoPost aryaPhoto = network.createPost(PhotoPost.class, arya);
aryaPhoto.setCaption("Me with my friends!");
aryaPhoto.setPhoto("/img/arya/3123jhjhhwjer1432423.jpg");
CommentPost randiComment = Comment(network, aryaPhoto, randi, "Sorry i missclick dislike");
aryaPhoto.dislike(randi);
aryaPhoto.like(fajar);
aryaPhoto.like(arda);
aryaPhoto.like(nanda);
randiComment.like(fajar);
randiComment.like(arya);
System.out.println(network.getAllDisplay());
}
public static CommentPost Comment(Network network, Post post, UUID userComment, String message)
{
CommentPost postInstance = network.createComment(userComment, post.getId());
postInstance.setComment(message);
return postInstance;
}
}
Comments
Post a Comment