-2

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;
     }
Pang
  • 9,564
  • 146
  • 81
  • 122
stacey
  • 7
  • Rather than calculating the max and then searching for the person with the matching level, consider looping through the "contacts" list just once. You can keep track of both the maximum activity level you've seen so far, and the person who has that level, and update them only if the next contact in the list "beats" the current best score. – CAW Apr 06 '17 at 13:04
  • As an aside, there's also a simple improvement which you could make to your current solution without really changing the algorithm. Currently you calculate maxLevel() multiple times inside the findMostSocial() method. You only need to do this once, store the value in a variable and then compare each person's score to it. – CAW Apr 06 '17 at 13:07

1 Answers1

0

I think what you need here is Quick Sort algorithm here to determine max value of social media activity in list of Persons. So, I would suggest to replace your maxValue() and findMostSocial() methods with method implementing QuickSort. Here is one such implementation for reference. Cheers

mp77
  • 428
  • 3
  • 7