Fill This Form To Receive Instant Help
Homework answers / question archive / Create a code in which Marks A social networking website hires you to meet the following requirements: 1
Create a code in which Marks A social networking website hires you to meet the following requirements: 1. User can create their profile 2. User can check for mutual friends 3. User can add new friends 4. User can delete friends 5. Display user's profile Solution Guidelines: Create a class Profile having name, emailId, city , country, friendlist[] ( Keep the fields private) Profile class will contain following methods: o createProfile(String name , String emailId, String city, String country): allow user to set his information o addFriends(Profile profile): sends request, if a person accepts the request add the person object into friendlist[] Note: Achieve Method Overloading keep the parameters as (Profile profile , String Message) giving Access to user to send request with a personalized message. o removeFriend(Profile profile):can remove a friend from the friendlist o showFriends(): display the friendlist o search(Profile profile):searches for profile in the list if it exists returns true else return false o checkMutualFriends(Profile profile): returns an array of the common friends
Creating a code in which Marks A social networking website hires you to meet the following requirements
Step-by-step explanation
Explanation:
import java.io.*; public class Profile { private String name; private String emailId; private String city; private String country; private Profile friendlist[]; private Profile pendingRequests[]; private int friendCount; private int pendingCount; private static final int MAX = 1000; public void createProfile(String name, String emailId, String city, String country) { this.name = name; this.emailId = emailId; this.city = city; this.country = country; this.friendlist=new Profile[MAX]; this.pendingRequests = new Profile[MAX]; this.friendCount=0; this.pendingCount=0; } public void addFriends(Profile profile) { profile.receiveFriendRequest(this, null); } public void addFriends(Profile profile, String message) { profile.receiveFriendRequest(this, message); } public void receiveFriendRequest(Profile profile, String message) { this.pendingRequests[this.pendingCount++] = profile; } public Profile[] getPendingRequests() { return this.pendingRequests; } public void acceptFriendRequest(Profile profile) { int index = this.pendingRequests.length; for(int i=0; i<this.pendingCount; i++) { if(profile.equals(this.pendingRequests[i])) { index = i; } } profile.addToFriendList(this); addToFriendList(profile); for(int i=index; i<this.pendingRequests.length-1; i++) this.pendingRequests[i] = this.pendingRequests[i+1]; this.pendingRequests[this.pendingRequests.length-1]=null; this.pendingCount--; } private void addToFriendList(Profile profile) { this.friendlist[this.friendCount++] = profile; } public void removeFriend(Profile profile) { int index=this.friendlist.length; for(int i=0; i<this.friendlist.length; i++) { if(this.friendlist[i] == null) break; if(profile.equals(this.friendlist[i])) { index = i; } } for(int i=index; i<this.friendlist.length-1; i++) this.friendlist[i] = this.friendlist[i+1]; this.friendlist[this.friendlist.length-1] = null; this.friendCount--; profile.removeFriend(this); } public void showFriends() { for(int i=0; i<this.friendlist.length; i++) { if(this.friendlist[i] == null) break; System.out.println(this.friendlist[i]); } } @Override public String toString() { return "Name: " + this.name + "\nEmail: " + this.emailId + "\nCity: " + this.city + "\nCountry: " + this.country; } @Override public boolean equals(Object obj) { if(obj == null) return false; if(!(obj instanceof Profile)) return false; Profile profile = (Profile)obj; if(this == profile) return true; return(profile.name.equals(this.name) && profile.emailId.equals(this.emailId) && profile.city.equals(this.city) && profile.country.equals(this.country)); } public boolean search(Profile profile) { for(int i=0; i<this.friendlist.length; i++) { if(this.friendlist[i] == null) break; if(profile.equals(this.friendlist[i])) return true; } return false; } public Profile[] checkMutualFriends(Profile profile) { int count = 0; for(int i=0; i<this.friendlist.length; i++) { if(this.friendlist[i] == null) break; if(profile.search(this.friendlist[i])) count++; } Profile mutual[] = new Profile[count]; int k = -1; for(int i=0; i<this.friendlist.length; i++) { if(this.friendlist[i] == null) break; if(profile.search(this.friendlist[i])) mutual[++k] = this.friendlist[i]; } return mutual; } } class ProfileTest { private static final int MAX = 1000; public static void main(String args[]) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); Profile profiles[] = new Profile[MAX]; int counter=0; while(true) { System.out.print("\nMENU\n----\n1. Create new profile\n2. View Profiles\n3. Send friend request\n4. Accept requests\n5. View Friend List\n6. View mutual friends\n7. Exit\n\nEnter choice: "); int choice = Integer.parseInt(br.readLine()); switch(choice) { case 1: System.out.print("CREATE PROFILE\n-----------------\nName: "); String name = br.readLine(); System.out.print("Email: "); String email = br.readLine(); System.out.print("City: "); String city = br.readLine(); System.out.print("Country: "); String country = br.readLine(); profiles[counter] = new Profile(); profiles[counter++].createProfile(name, email, city, country); System.out.println("Profile successfully created!"); break; case 2: System.out.println("VIEW PROFILES\n--------------"); for(int i=0; i<counter; i++) { System.out.println("ID: " + (i+1)); System.out.println(profiles[i]); } break; case 3: System.out.println("SEND FRIEND REQUEST\n--------------"); System.out.print("Sender Profile ID: "); int senderID = Integer.parseInt(br.readLine()) - 1; System.out.print("Receiver Profile ID: "); int receiverID = Integer.parseInt(br.readLine()) - 1; if(senderID < 0 || senderID >= counter || receiverID < 0 || receiverID >= counter) { System.out.println("Invalid IDs"); } else { System.out.print("Enter personalized message: "); String msg = br.readLine(); profiles[senderID].addFriends(profiles[receiverID], msg); System.out.println("Friend request sent"); } break; case 4: System.out.print("Enter profile ID: "); int pid = Integer.parseInt(br.readLine()) - 1; System.out.println("PENDING REQUESTS\n--------------"); Profile pending[] = profiles[pid].getPendingRequests(); for(int i=0; i<pending.length; i++) { if(pending[i]==null) break; System.out.println(pending[i]); System.out.print("ACCEPT (Y/N) ? "); char x = br.readLine().trim().toUpperCase().charAt(0); if(x=='Y') profiles[pid].acceptFriendRequest(pending[i]); } break; case 5: System.out.print("Enter profile ID: "); int pidf = Integer.parseInt(br.readLine()) - 1; System.out.println("FRIEND LIST\n--------------"); profiles[pidf].showFriends(); break; case 6: System.out.print("Enter profile ID 1: "); int pid1 = Integer.parseInt(br.readLine()) - 1; System.out.print("Enter profile ID 2: "); int pid2 = Integer.parseInt(br.readLine()) - 1; Profile mutual[] = profiles[pid1].checkMutualFriends(profiles[pid2]); System.out.println("MUTUAL FRIENDS\n--------------"); for(Profile p:mutual) System.out.println(p); break; case 7: return; default: System.out.println("Invalid choice"); } } } }