My code is fine until the last part in the find most social person where : List
ID: 3813982 • Letter: M
Question
My code is fine until the last part in the find most social person where : List mostSocial = new ArrayList<MostSocial>();
It comes up with an error cannot find symbol class list.
/modified Person, AddressBook java files
//created 2 more supporting java files , SocialMedia and MostSocial
//TestPerson.java - Test methods of this project
public class TestPerson {
public static void main(String[] args) {
//create person object 1
Person p = new Person("Diana", "Prince");
p.setMobile("0409670123");
p.setEmail("diana.prince@inscom.mil");
p.addSocialMediaAccount("@dianap", "Twitter", "http://twitter.com/", 50);
p.addSocialMediaAccount("diana.prince", "Facebook", "http://facebook.com/", 90);
//print person object 1
p.printContactDetails();
System.out.println(" ");
//create person object 2
Person p1 = new Person("John", "James");
p1.setMobile("0135297881");
p1.setEmail("john.james@subcom.tes");
p1.addSocialMediaAccount("@johjam", "Twitter", "http://twitter.com/", 40);
p1.addSocialMediaAccount("john.james", "Facebook", "http://facebook.com/", 80);
p1.printContactDetails();
//create AddressBook object
AddressBook ab= new AddressBook();
//add 2 person objects created above
ab.addPerson(p1);
ab.addPerson(p);
System.out.println(" ");
//find the person with first and surname in the address book contacts
Person p2=ab.findPerson("Diana", "Prince");
p2.printContactDetails();
System.out.println(" ");
Person p3=ab.findPerson("John", "James");
p3.printContactDetails();
//create person object 3
Person p5= new Person("Christy", "Peter");
p5.setMobile("4561238901");
p5.setEmail("chrt.pert@tuscs.jul");
p5.addSocialMediaAccount("@chrtpert", "Twitter", "http://twitter.com/", 90);
p5.addSocialMediaAccount("chrt.pert", "Facebook", "http://facebook.com/", 80);
//add to the address book
ab.addPerson(p5);
//print the highest total social media activity level person object
System.out.println(" person with the highest total social media activity level");
Person p4=ab.findMostSocial();
p4.printContactDetails();
}
}
public class SocialMedia {
String userID;
String websiteName;
String websiteURL;
int activityLevel;
SocialMedia(String userID, String websiteName, String websiteURL, int activityLevel)
{
this.userID=userID;
this.websiteName=websiteName;
this.websiteURL=websiteURL;
this.activityLevel=activityLevel;
}
public String getUserID() {
return userID;
}
public void setUserID(String userID) {
this.userID = userID;
}
public String getWebsiteName() {
return websiteName;
}
public void setWebsiteName(String websiteName) {
this.websiteName = websiteName;
}
public String getWebsiteURL() {
return websiteURL;
}
public void setWebsiteURL(String websiteURL) {
this.websiteURL = websiteURL;
}
public int getActivityLevel() {
return activityLevel;
}
public void setActivityLevel(int activityLevel) {
this.activityLevel = activityLevel;
}
}
public class MostSocial implements Comparable<MostSocial>{
private String firstName;
private String surname;
int totalActivityLevel;
MostSocial(String firstName,String surname,int totalActivityLevel )
{
this.firstName=firstName;
this.surname=surname;
this.totalActivityLevel=totalActivityLevel;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public int getTotalActivityLevel() {
return totalActivityLevel;
}
public void setTotalActivityLevel(int totalActivityLevel) {
this.totalActivityLevel = totalActivityLevel;
}
@Override
public int compareTo(MostSocial ms) {
// TODO Auto-generated method stub
int activity = ms.getTotalActivityLevel();
return activity-this.totalActivityLevel;
}
}
import java.util.ArrayList;
/**
* Person details including contacts and social media accounts
*
* @author (your name and student number)
* @version (a version number or a date)
*/
public class Person {
private String firstName;
private String surname;
private String mobile;
private String email;
private ArrayList socialMediaAccounts;
public ArrayList getSocialMediaAccounts() {
return socialMediaAccounts;
}
public void setSocialMediaAccounts(ArrayList socialMediaAccounts) {
this.socialMediaAccounts = socialMediaAccounts;
}
/**
* Create a Person with the first name and surname given by
* the parameters.
*/
public Person(String firstName, String surname) {
this.firstName = firstName;
this.surname = surname;
mobile = null;
email = null;
this.socialMediaAccounts = new ArrayList();
}
/**
* @return the person's first name
*/
public String getFirstName() {
return firstName;
}
/**
* Set the person's first name
* unless the parameter is an empty string.
*/
public void setFirstName(String firstName) {
if(firstName !=null & firstName.length()>0)
{
this.firstName = firstName;
}
}
/**
* Return the person's surname
*/
public String getSurname() {
return surname;
}
/**
* Set the person's surname
* unless the parameter is an empty string.
*/
public void setSurname(String surname) {
if(surname !=null & surname.length()>0)
{
this.surname = surname;
}
}
/**
* Return the person's mobile phone number
*/
public String getMobile() {
return mobile;
}
/**
* Set the person's mobile phone number
*/
public void setMobile(String mobile) {
//check if mobile is not null and has some characters
if(mobile !=null & mobile.length()>0)
{
//check if mobile phone number is of only digits from 0 to 9
//convert to char array
char[] cArray=mobile.toCharArray();
//loop for array length times
for (int i = 0; i < cArray.length; i++) {
if(!Character.isDigit(cArray[i])){
return;
}
} //end of for loop
} //end of if
else
{
return;
}
this.mobile = mobile;
}
/**
* Return the person's email address
*/
public String getEmail() {
return email;
}
/**
* Set the person's email address
*/
public void setEmail(String email) {
if(email !=null & email.length()>0)
{
this.email = email;
}
}
/**
* Create a new SocialMediaAccount object, and add it to the socialMediaAccounts list.
*/
public void addSocialMediaAccount(String userID, String websiteName, String websiteURL, int activityLevel) {
//create SocialMedia object
SocialMedia sm = new SocialMedia(userID,websiteName,websiteURL,activityLevel);
//add to the arraylist
socialMediaAccounts.add(sm);
}
/**
* Search the socialMediaAccounts list for an account on the website specified by the websiteName
* parameter, and return the userID for that account. If no such account can be found, return
* null.
*/
public String getSocialMediaID(String websiteName) {
//iterate the socialMediaAccounts list
for (int i = 0; i < socialMediaAccounts.size(); i++) {
//get Social Media object
SocialMedia sm = (SocialMedia) socialMediaAccounts.get(i);
//check if user account exists in the website passed
if(sm.getWebsiteName().equals(websiteName))
{
return sm.getUserID();
}
}
return null;
}
/** Print the person's contact details in the format given in the
* project specifications.
*/
public void printContactDetails() {
System.out.println("name : " + this.getFirstName() + " " + this.getSurname());
System.out.println("mobile : " +this.getMobile());
System.out.println("email : " +this.getEmail());
//iterate and print the social media details of the Person
//iterate the socialMediaAccounts list
for (int i = 0; i < socialMediaAccounts.size(); i++) {
//get Social Media object
SocialMedia sm = (SocialMedia) socialMediaAccounts.get(i);
System.out.println(sm.getWebsiteName()+","+sm.getUserID()+","+sm.getWebsiteURL());
} //end of for loop
}
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* Manage an AddressBook of Person contacts
*
* @author (your name and student number here)
* @version (a version number or a date)
*/
public class AddressBook {
private ArrayList contacts;
AddressBook()
{
//intialize
contacts=new ArrayList<AddressBook>();
}
/** Add the person p to the "contacts" list, unless they have the same
* surname and first name as a person already in the list, in which case
* do not add them, but instead print the error message "could not add person".
*
*/
public void addPerson(Person p) {
// Iterate the contact list
// add if the Person p is not already in the contact list
for (int i = 0; i < contacts.size(); i++)
{
Person person = (Person) contacts.get(i);
if(person.getFirstName().equals(p.getFirstName())
&& person.getSurname().equals(p.getSurname()))
{
System.out.println("could not add person");
}
}//end of for loop
//add to the contacts list
contacts.add(p);
}
/** Search for a person in the "contacts" list by first name and surname,
* and return the relevant Person object if one matches, or otherwise return null.
*/
public Person findPerson(String firstName, String surname) {
// Iterate the contact list
for (int i = 0; i < contacts.size(); i++)
{
Person person = (Person) contacts.get(i);
if(person.getFirstName().equals(firstName)
&& person.getSurname().equals(surname))
{
return person;
}
}//end of for loop
return null;
}
/**
* Find the most social person in the address book.
*/
public Person findMostSocial() {
// Iterate the contact list
List mostSocial = new ArrayList<MostSocial>();
for (int i = 0; i < contacts.size(); i++)
{
Person person = (Person) contacts.get(i);
//get all social media accounts and add the activity level of each person
int totactivity=0;
for (int j = 0; j < person.getSocialMediaAccounts().size(); j++) {
SocialMedia sm=(SocialMedia) person.getSocialMediaAccounts().get(j);
totactivity=totactivity+sm.getActivityLevel();
} //end of inner for
//create MostSocial object
MostSocial ms = new MostSocial(person.getFirstName(),person.getSurname(),totactivity);
mostSocial.add(ms);
}//end of for loop
//sort the MostSocial list
Collections.sort(mostSocial);
//get the first object
MostSocial ms = (MostSocial) mostSocial.get(0);
return findPerson(ms.getFirstName(), ms.getSurname());
}
Explanation / Answer
Hey,
I compiled and ran your code and there seems to be no compile errors.
But based on what you had said, this error will come if in your AddressBook class, you miss out the the following import:
import java.util.List;
Please make sure that it is there and there are no
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.