Please using Java to do the following project This is related to topic: A List I
ID: 3904759 • Letter: P
Question
Please using Java to do the following project
This is related to topic: A List Implementation That Uses an Array
This is related to topic: Graph Implementations
Feel free to use any data structure as needed.
Once finished, you can either paste the code here or send me a link on github. Thanks:)
Design and implement an application that maintains the data for a simple social network. Eaclh person in the network should have a profile that contains the person's name, optional image, current status, and a list of friends. Your application should allow a user to join the network, leave the network, create a profile, modify the profile, scarch for other profiles, and add friends.Explanation / Answer
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.concurrent.CopyOnWriteArrayList;
public class Network {
List<PersonProfile> profile = new ArrayList<PersonProfile>();
public List<PersonProfile> getProfile() {
return profile;
}
public void setProfile(List<PersonProfile> profile) {
this.profile = profile;
}
public static void main(String[] args) {
Network network = new Network();
Scanner sc = new Scanner(System.in);
System.out.println("Creating a Profile in the network");
while (true) {
System.out.println("Please enter the Profile name");
String name = sc.nextLine();
System.out.println("Please enter the status:
(Married/Unmarried)");
String status = sc.nextLine();
PersonProfile profile = network.createProfile(name, status);
network.getProfile().add(profile);
System.out.println("Person Profie succesfully created");
System.out.println("Do you wish to create another profile
(y/n)");
if (sc.nextLine().equalsIgnoreCase("n"))
break;
}
while (true) {
System.out.println("Do you wish to Modify a profile(y/n)");
if (sc.nextLine().equalsIgnoreCase("y")) {
System.out.println("Enter the name");
network.modifyProfile(sc.nextLine());
} else
break;
}
while (true) {
System.out.println("Do you wish to Delete a profile(y/n)");
if (sc.nextLine().equalsIgnoreCase("y")) {
System.out.println("Enter the name");
network.deleteProfile(sc.nextLine());
} else
break;
}
while (true) {
System.out.println("Do you wish to add a friend to a
Profile(y/n)");
if (sc.nextLine().equalsIgnoreCase("y")) {
System.out
.println("Enter the profile name you want to add
friends");
PersonProfile p = network.searchProfile(sc.nextLine());
if (p == null)
System.out
.println("You can't add friends to a non
existing profile");
else {
while (true) {
System.out.println("Enter the Friend name");
PersonProfile friend = network.searchProfile(sc
.nextLine());
if (friend == null)
System.out.println("No such freind profile
exist");
else
network.addFriends(friend);
System.out
.println("Do you wish to add another
friend for this profile (y/n)");
if (sc.nextLine().equalsIgnoreCase("n"))
break;
}
}
} else
break;
}
}
PersonProfile createProfile(String name, String status) {
PersonProfile profile = new PersonProfile();
profile.setPersonName(name);
profile.setStatus(status);
return profile;
}
void modifyProfile(String name) {
Scanner scanner = new Scanner(System.in);
boolean b = false;
for (PersonProfile profile : this.getProfile()) {
if (profile.getPersonName().equalsIgnoreCase(name)) {
b = true;
System.out.println("Do you wish to modify name (y/n)");
if (scanner.nextLine().equalsIgnoreCase("y")) {
System.out.println("Please enter the new name");
profile.setPersonName(scanner.nextLine());
System.out.println("Profile Modified Details:
Name: "
+ profile.getPersonName() + " Status: "
+ profile.getStatus());
System.out.println("Freinds details");
if (profile.getFriend().isEmpty())
System.out.println("No friends linked to the
profile");
else {
for (PersonProfile friend : profile.getFriend())
{
System.out.println("Name: "
+ friend.getPersonName() + " Status:
"
+ friend.getStatus());
}
}
}
System.out.println("Do you wish to modify status (y/n)");
if (scanner.nextLine().equalsIgnoreCase("y")) {
System.out.println("Please enter the new status");
profile.setStatus(scanner.nextLine());
System.out.println("Profile Modified Details:
Name: "
+ profile.getPersonName() + " Status: "
+ profile.getStatus());
System.out.println("Freinds details");
if (profile.getFriend().isEmpty())
System.out.println("No friends linked to the
profile");
else {
for (PersonProfile friend : profile.getFriend())
{
System.out.println("Name: "
+ friend.getPersonName() + " Status:
"
+ friend.getStatus());
}
}
}
}
}
if (!b)
System.out.println("No such profile exists");
}
void deleteProfile(String name) {
CopyOnWriteArrayList<PersonProfile> oldProfile = new
CopyOnWriteArrayList<PersonProfile>();
oldProfile.addAll(this.getProfile());
boolean b = false;
for (PersonProfile profile : oldProfile) {
if (profile.getPersonName().equalsIgnoreCase(name)) {
oldProfile.remove(profile);
b = true;
}
}
this.setProfile(oldProfile);
if (b)
System.out.println("Profile Deleted Successfully");
else
System.out.println("No Such Profile found");
}
void addFriends(PersonProfile person) {
List<PersonProfile> friendsList = new ArrayList<PersonProfile>();
friendsList.add(person);
for (PersonProfile profile : this.getProfile()) {
profile.setFriend(friendsList);
}
System.out.println("Friend profile added succesfully");
}
PersonProfile searchProfile(String name) {
for (PersonProfile profile : this.getProfile()) {
if (profile.getPersonName().equalsIgnoreCase(name)) {
return profile;
}
}
return null;
}
}
class PersonProfile {
String personName;
String status;
List<PersonProfile> friend = new ArrayList<PersonProfile>();
public String getPersonName() {
return personName;
}
public void setPersonName(String personName) {
this.personName = personName;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public List<PersonProfile> getFriend() {
return friend;
}
public void setFriend(List<PersonProfile> friend) {
this.friend = friend;
}
}