I am very new to Java and am stuck on the following assignment. I've been asked to create a method in the AddressBook class that returns the Person object with the highest total ActivityLevel.
I have created the getSocialMediaActivityLevel(), maxValue() and findMostSocial() methods.
The findMostSocial method does return the value that I am after, however the description for the assignment includes the statement: "requires you to design a simple algorithm and integrate it into the existing classes." It seems like I've used a lot of unnecessary code, but I'm very unsure how to simplify what I've done. Any help is greatly appreciated.
public class SocialMediaAccount {
private String userID;
private String websiteName;
private String websiteURL;
private int activityLevel;
public SocialMediaAccount(String userID, String websiteName, String websiteURL, int activityLevel) {
this.userID = userID;
this.websiteName = websiteName;
this.websiteURL = websiteURL;
this.activityLevel = activityLevel;
}
public void addSocialMediaAccount(String userID, String websiteName, String websiteURL, int activityLevel) {
SocialMediaAccount account1;
account1 = new SocialMediaAccount(userID, websiteName, websiteURL, activityLevel);
socialMediaAccounts.add(account1);
}
import java.util.ArrayList;
public class Person {
private String firstName;
private String surname;
private String mobile;
private String email;
private ArrayList<SocialMediaAccount> socialMediaAccounts;
//returns the combined ActivityLevel for all the Person's SocialMediaAccounts.
public int getSocialMediaActivityLevel(){
int total = 0;
for(SocialMediaAccount e : socialMediaAccounts){
total += e.getActivityLevel();
}
return total;
}
import java.util.ArrayList;
import java.util.Collections;
public class AddressBook {
private ArrayList<Person> contacts;
public AddressBook(){
contacts = new ArrayList<>();
}
//returns the highest combined ActivityLevel in the ArrayList contacts
public int maxValue(){
ArrayList<Integer> maxActivityLevel = new ArrayList<>();
for(Person e : contacts){
maxActivityLevel.add(e.getSocialMediaActivityLevel());
}
int maxValue = Collections.max(maxActivityLevel);
return maxValue;
}
//returns the Person object in the contacts ArrayList with the highest combined ActivityLevel
public Person findMostSocial(){
for(Person p: contacts){
if(maxValue() == p.getSocialMediaActivityLevel()){
return p;
}
}
return null;
}