Determine the different ways the system will be used. This includes main functions the system needs to perform and who will use it.
- Each user can create an account as either a user or an admin.
- Users can follow other users.
- Users can send messages to other users using the messaging service.
- Users can create posts through the post service.
- Users can comment on posts made by others.
- Once a user creates a post, all users who follow them will be notified.
- User Can like other user post
- Users can search other users post by their name or hashTag
Based on the requirements and use cases, identify the main objects of the system...
Account,User, Admin, Follow Service,Messaging Service,Message, Post Service,Post,Follow Service, Follow,LikeService,Likee, Comment Service, comment
Determine how these objects will interact with each other to fulfill the use cases...
Each User and Admin are subclasses of Account. Users can utilize the Message Service to add messages to posts, the Post Service to create posts, and can comment on or like posts. Additionally, Users can use the Follow Service to follow other users
Design inheritance trees where applicable to promote code reuse and polymorphism. This step involves identifying common attributes and behaviors that can be abstracted into parent classes...
Each User and Admin are subclasses of Account. Users can utilize the Message Service to add messages to posts, the Post Service to create posts, and can comment on or like posts. Additionally, Users can use the Follow Service to follow other users
Consider using design patterns (e.g., Factory, Singleton, Observer, Strategy) that fit the problem...
We can use factory pattern to create different types of users like admin or
We can user startegy pattern to search with different search startegies like search by userName, search by post
we can user observer design pattern to notify the followers whenever a new post is published
Attributes: For each class, define the attributes (data) it will hold...
Methods: Define the methods (functions) that operate on the attributes. Ensure they align with the object's responsibilities and adhere to the principle of encapsulation.
// File: /mnt/data/src_extracted/src/Main.java
import models.User;
import service.InputUtil;
import service.MessageService;
import service.PostService;
import service.UserService;
import java.awt.im.InputMethodRequests;
import java.net.SocketTimeoutException;
import java.sql.SQLOutput;
import java.util.Scanner;
// Press Shift twice to open the Search Everywhere dialog and type show whitespaces,
// then press Enter. You can now see whitespace characters in your code.
public class Main {
public static void main(String[] args)
{
Scanner scanner=new Scanner(System.in);
UserService userService=new UserService();
PostService postService=new PostService(userService);
InputUtil inputUtil=new InputUtil();
MessageService messageService=new MessageService(userService);
while(true)
{
System.out.println("Select one action");
System.out.println("1 - > Add User");
System.out.println("2 -> Create Post");
System.out.println("3 -> Like Post");
System.out.println("4 -> Comment Post");
System.out.println("5 -> Message Another Usr");
System.out.println("6- List my conversation");
int option= inputUtil.getIntInput("","");
if(option==1)
{
System.out.println("Enter the name of the user");
String name=inputUtil.getStringInput("","");
userService.addUser(name);
}else if(option==2)
{
System.out.println("Enter the content of the post");
String content=inputUtil.getStringInput("","");
postService.createPost(content);
}
else if(option==3)
{
postService.ListPost();
System.out.println("Enter the index of the post you wnat to like");
int index=inputUtil.getIntInput("","");
userService.listUsers();
System.out.println("Enter the name of the user who want to like");
String name=inputUtil.getStringInput("","");;
postService.addLike(index,name);
}
else if(option==4)
{
postService.ListPost();
System.out.println("Enter the index of the post you wnat to like");
int index=InputUtil.getIntInput("","");
userService.listUsers();
System.out.println("Enter the name of the user who want to Comment");
String name=inputUtil.getStringInput("","");;
System.out.println("Enter the comment");
String comment=inputUtil.getStringInput("","");
postService.createComment(index,name,comment);
}
else if (option==5)
{
userService.listUsers();
System.out.println("Enter your name");
String myName=inputUtil.getStringInput("","");
userService.listUsers();
System.out.println("Enter the name of the person who you wnat to sent the messsage");
String recieverName=inputUtil.getStringInput("","");;
System.out.println("Enter the contents of the message");
String messageContent=inputUtil.getStringInput("","");;
messageService.addMessage(myName,recieverName,messageContent);
}
else if (option==6)
{
System.out.println("Enter your name ");
String name=inputUtil.getStringInput("","");
userService.listUsers();
System.out.println("Enter the name of the person who conversation you want to list");
String recieverName=inputUtil.getStringInput("","");;
messageService.getConversation(name,recieverName);
}
}
}
}
// File: /mnt/data/src_extracted/src/models/Account.java
package models;
public abstract class Account
{
String name;
public Account(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
// File: /mnt/data/src_extracted/src/models/Admin.java
package models;
public class Admin extends Account
{
public Admin(String name) {
super(name);
}
}
// File: /mnt/data/src_extracted/src/models/Comment.java
package models;
public class Comment
{
User user;
String comment;
public Comment(User user, String comment) {
this.user = user;
this.comment = comment;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
}
// File: /mnt/data/src_extracted/src/models/Like.java
package models;
public class Like
{
User user;
public Like(User user) {
this.user = user;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
// File: /mnt/data/src_extracted/src/models/Post.java
package models;
import java.util.ArrayList;
import java.util.List;
public class Post
{
User postedUser;
List<Like> recievedLikes=new ArrayList<>();
List<Comment >commentList=new ArrayList<>();
String content;
public Post(User postedUser, List<Like> recievedLikes, List<Comment> commentList, String content) {
this.postedUser = postedUser;
this.recievedLikes = recievedLikes;
this.commentList = commentList;
this.content = content;
}
public Post(String content)
{
this.content=content;
}
public User getPostedUser() {
return postedUser;
}
public void setPostedUser(User postedUser) {
this.postedUser = postedUser;
}
public List<Like> getRecievedLikes() {
return recievedLikes;
}
public void setRecievedLikes(List<Like> recievedLikes) {
this.recievedLikes = recievedLikes;
}
public List<Comment> getCommentList() {
return commentList;
}
public void setCommentList(List<Comment> commentList) {
this.commentList = commentList;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
// File: /mnt/data/src_extracted/src/models/Search.java
package models;
import java.util.List;
public interface Search
{
void search(List<Post>postList);
}
// File: /mnt/data/src_extracted/src/models/User.java
package models;
public class User extends Account
{
public User(String name) {
super(name);
}
}
// File: /mnt/data/src_extracted/src/service/FollowerService.java
package service;
import models.User;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class FollowerService
{
UserService userService;
HashMap<User, List<User>> followers=new HashMap<>();
public FollowerService(UserService userService) {
this.userService = userService;
}
void addFollower(String followerString,String followingString )
{
User follower=userService.getUser(followerString);
User following=userService.getUser(followingString);
List<User>followerList=followers.getOrDefault(following,new ArrayList<>());
followerList.add(follower);
}
void getFollower(String name )
{
User user=userService.getUser(name);
List<User>followerList=followers.get(user);
for(User currUser:followerList)
{
System.out.println(currUser.getName());
}
}
}
// File: /mnt/data/src_extracted/src/service/InputUtil.java
package service;
import java.util.Scanner;
public class InputUtil {
private static final Scanner scanner = new Scanner(System.in);
public static int getIntInput(String prompt, String errorMessage) {
while (true) {
try {
System.out.print(prompt);
return Integer.parseInt(scanner.nextLine().trim());
} catch (NumberFormatException e) {
System.out.println(errorMessage);
}
}
}
public static String getStringInput(String prompt, String errorMessage) {
while (true) {
System.out.print(prompt);
String input = scanner.nextLine().trim();
if (!input.isEmpty()) {
return input;
}
System.out.println(errorMessage);
}
}
}
// File: /mnt/data/src_extracted/src/service/MessageService.java
package service;
import models.User;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class MessageService
{
HashMap<User,HashMap<User, List<String>>>messageService=new HashMap<>();
UserService userService;
public MessageService(UserService userService) {
this.userService = userService;
}
public void addMessage(String fromUser, String toUser, String message)
{
User from=userService.getUser(fromUser);
User to=userService.getUser(toUser);
HashMap<User, List<String>> userMap=messageService.getOrDefault(from,new HashMap<>());
List<String>messages=userMap.getOrDefault(to,new ArrayList<>());
messages.add(message);
userMap.put(to,messages);
messageService.put(from,userMap);
userMap=messageService.getOrDefault(to,new HashMap<>());
messages=userMap.getOrDefault(from,new ArrayList<>());
messages.add(message);
userMap.put(from,messages);
messageService.put(to,userMap);
}
public void getConversation(String fromUser,String toUser)
{
User from=userService.getUser(fromUser);
User to=userService.getUser(toUser);
HashMap<User,List<String>>hashMap=messageService.getOrDefault(from,new HashMap<>());
List<String>messageList=hashMap.getOrDefault(to,new ArrayList<>());
for(String message:messageList)
{
System.out.println(message);
}
}
}
// File: /mnt/data/src_extracted/src/service/PostService.java
package service;
import models.Comment;
import models.Like;
import models.Post;
import models.User;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class PostService
{
UserService userService;
List<Post>postList=new ArrayList<>();
public PostService(UserService userService)
{
this.userService=userService;
}
public void createComment(int index, String commenter, String comment)
{
User user=userService.getUser(commenter);
Post post=postList.get(index);
Comment commentObject=new Comment(user,comment);
post.getCommentList().add(commentObject);
}
public void addLike( int index, String name)
{
Post post=postList.get(index);
HashMap<String,User>hashMap=userService.userHashMap;
if(!hashMap.containsKey(name))
{
System.out.println("User is not present in the database");
}
else
{
User user=hashMap.get(name);
Like like=new Like(user);
post.getRecievedLikes().add(like);
}
}
public void ListPost()
{
int counter=0;
for(Post post:postList)
{
System.out.println(counter+" "+post.getContent());
}
}
public void createPost(String content)
{
Post post=new Post(content);
postList.add(post);
}
}
// File: /mnt/data/src_extracted/src/service/SearchByCreator.java
package service;
import models.Post;
import models.Search;
import java.util.List;
public class SearchByCreator implements Search{
@Override
public void search(List<Post> postList) {
System.out.println("Searching By Creator");
}
}
// File: /mnt/data/src_extracted/src/service/SearchByName.java
package service;
import models.Post;
import models.Search;
import java.util.List;
public class SearchByName implements Search
{
@Override
public void search(List<Post> postList) {
System.out.println("Searching By Name");
}
}
// File: /mnt/data/src_extracted/src/service/UserService.java
package service;
import models.User;
import java.util.HashMap;
public class UserService
{
HashMap<String, User> userHashMap=new HashMap<>();
public void addUser(String firstName)
{
User user=new User(firstName);
userHashMap.put(firstName,user);
}
public void listUsers()
{
int count=0;
for(String firstName:userHashMap.keySet())
{
System.out.println(count+" "+firstName);
count++;
}
}
public User getUser(String firstName)
{
User user=userHashMap.get(firstName);
return user;
}
}
Check and explain whether your design adheres to solid principles (Ask interviewer what SOLID principle is if you can not recall it.)...
single reposibility principle -> Each class has only one responsibility
Open close Principle -> We have used interface and abstract classes in user and search functionality alloweing user to extend these classes
Liskov substitution class -> Each of the subClasses can easily replace their parent
Explain how your design can handle changes in scale and whether it would be easily to extend with new functionalities...
As Desing is following solid and design pattern so the system is easliy scalable and extensible
Try creating a class, flow, state and/or sequence diagram using the diagramming tool. Mermaid flow diagrams can be used to represent system use cases. You can ask the interviewer bot to create a starter diagram if unfamiliar with the tool. Briefly explain your diagrams if necessary...
Added Class Diagram
Critically examine your design for any flaws or areas for future improvement...