Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Devoloping Java applications using the JCF(Java Collections Framework) Design a

ID: 3548634 • Letter: D

Question

Devoloping Java applications using the JCF(Java Collections Framework)


Design a java application that maintains the data fo a simple social network. Each person in the network should have a profile that contains at least the persons name, 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, search for other profiles, and add friends.


Please note that the social network and the list of friends for each person are to be collections from the JCF, and that the entire application is to be written in a single .java file. Testing should show at least 6 people joining the network, and demonstrate each of the user options above.

Explanation / Answer

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.concurrent.CopyOnWriteArrayList;
// create a profile
// Modify a profile
// Delete a Profile
// Add friend to a Profile. A friend profile should already be created.
// The method search for the friend profile, if exist it will add.

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);
// create a profile
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;
}
// Modify a profile

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;

}
// Delete a Profile

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;

}
// Add friend to a Profile. A friend profile should already be created.
// The method search for the friend profile, if exist it will add.

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;
}

}


Output:


Creating a Profile in the network
Please enter the Profile name
Ramanujam
Please enter the status: (Married/Unmarried)
Married
Person Profie succesfully created
Do you wish to create another profile (y/n)
Y
Please enter the Profile name
Hellen Keller
Please enter the status: (Married/Unmarried)
Married
Person Profie succesfully created
Do you wish to create another profile (y/n)
Y
Please enter the Profile name
Albert Einstein
Please enter the status: (Married/Unmarried)
Married
Person Profie succesfully created
Do you wish to create another profile (y/n)
Y
Please enter the Profile name
Tesla
Please enter the status: (Married/Unmarried)
Married
Person Profie succesfully created
Do you wish to create another profile (y/n)
Y
Please enter the Profile name
Hawkins
Please enter the status: (Married/Unmarried)
Married
Person Profie succesfully created
Do you wish to create another profile (y/n)
Y
Please enter the Profile name
Newton
Please enter the status: (Married/Unmarried)
Married
Person Profie succesfully created
Do you wish to create another profile (y/n)
N
Do you wish to Modify a profile(y/n)
Y
Enter the name
Newton
Do you wish to modify name (y/n)
Y
Please enter the new name
Issac Newton
Profile Modified Details:
Name: Issac Newton
Status: Married
Freinds details
No friends linked to the profile
Do you wish to modify status (y/n)
Y
Please enter the new status
Unmarried
Profile Modified Details:
Name: Issac Newton
Status: Unmarried
Freinds details
No friends linked to the profile
Do you wish to Modify a profile(y/n)
N
Do you wish to Delete a profile(y/n)
Y
Enter the name
Albert Einstein
Profile Deleted Successfully
Do you wish to Delete a profile(y/n)
Y
Enter the name
Albert Einstein
No Such Profile found
Do you wish to Delete a profile(y/n)
N
Do you wish to add a friend to a Profile(y/n)
Y
Enter the profile name you want to add friends
Ramanujam
Enter the Friend name
Hellen Keller
Friend profile added succesfully
Do you wish to add another friend for this profile (y/n)
Y
Enter the Friend name
Tesla
Friend profile added succesfully
Do you wish to add another friend for this profile (y/n)
Y
Enter the Friend name
Hawkins
Friend profile added succesfully
Do you wish to add another friend for this profile (y/n)
Y
Enter the Friend name
Issac Newton
Friend profile added succesfully
Do you wish to add another friend for this profile (y/n)
Y
Enter the Friend name
Albeert Einstein
No such freind profile exist
Do you wish to add another friend for this profile (y/n)
N
Do you wish to add a friend to a Profile(y/n)
Y
Enter the profile name you want to add friends
Hellen Keller
Enter the Friend name
Ramanujam
Friend profile added succesfully
Do you wish to add another friend for this profile (y/n)
N
Do you wish to add a friend to a Profile(y/n)
N


Thanks,

Sri

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote