tags:

views:

72

answers:

5

I have two classes. One(Person) for getters and setters, and another(People) for compute the data. What is my situation is, I get the data from the DB using ResultSet, then created a person Object to store the row data. Then i created people Object to store all persons.

Each Object created as SET.

while(rs.next())
{
    Set<People> people = new HashSet<people>();
    Person person = new Person();
    String name = rs.getString(2);
    person.setName(name);
    int id = rs.getInt(1);
    person.setId(id);
    String dept = rs.getString(4);
    person.setDept(dept);
    int age = rs.getInt(3);
    person.setAge(age);
    people.add(person);
}
return people;

Now the problem is the last line in the While Loop people.add(person);

It says

The method add(People) in the type Set is not applicable for the arguments (Person)

How can i overcome this problem?

Thanks.

+1  A: 

Based on what you are trying to do I feel, you should convert your Person to People class before adding to the set. Your People class may have a constructor which takes a Person as an argument and copies the required fields from Person to People. Here your code to add to set will look like people.add(new People(person));

When you declare Set<People> people = new HashSet<People>(); what it means is that this set is supposed to contain objects of 'type' People i.e. instances of People or instances of subclasses of People. If People is an interface, then the set may contain any object that implements the interface.

Gopi
+4  A: 

My understandig from your design is that you have a People has-many Person relation, so the People class holds a collection of Person objects. Then I'd expect something like this:

public class Person {
  private String name;
  private Date dateOfBirth;
  // .. more attributes

  // getters and setters

  // overrides of equals, hashcode and toString
}

public class People implements Set<Person> {
  private Set<Person> persons = new HashSet<Person>();

  public boolean add(Person person) {
    return persons.add(person);
  }

  // more methods for remove, contains, ...
}

So in your database related code you wouldn't need to create another set, because People already has the one you need:

People people = new People();  // or get it, if it's already created
while(rs.next())
{
    Person person = new Person();
    String name = rs.getString(2);
    person.setName(name);
    int id = rs.getInt(1);
    person.setId(id);
    String dept = rs.getString(4);
    person.setDept(dept);
    int age = rs.getInt(3);
    person.setAge(age);
    people.add(person);
}
return people;
Andreas_D
+2  A: 

I don't understand why you would want 2 classes in the first place. You cand have Person implement the computational part as well. But, nevertheless, what you could do:

class People implements Set<Person> {

private HashSet<Person> hashset = new HashSet<Person>();

// ... your computational code goes here
// delegate all Set methods to hashset
}

and then:

People people = new People();
while(rs.next())
{
    Person person = new Person();
    String name = rs.getString(2);
    person.setName(name);
    int id = rs.getInt(1);
    person.setId(id);
    String dept = rs.getString(4);
    person.setDept(dept);
    int age = rs.getInt(3);
    person.setAge(age);
    people.add(person);
}
return people;
drstupid
Agree. If People delegates HashMap instead of inherits would be better:)
卢声远 Shengyuan Lu
Correct. You could have reasoned, though :)
drstupid
A: 

I don't think that Set<People> people = new HashSet<people>(); should be written in the loop.

卢声远 Shengyuan Lu
The big problem is with the line "people.add(person);". The declaration is just a typo
drstupid
Ya, i just mentioned it in there... Its not actually declared there.
NooBDevelopeR
+1  A: 

I understand Person is a data structure (bean-like, with getters and setters), and People should contain all Person objects from the database and perform calculations on them.

If that is true, first of all, you cannot declare people within the loop (because a new People object will be created for each Person, and you don't want that, from what I understand).

Second, People needs to be able to contain Person objects. So it should at least be composed of a Set of Person objects. You can add more functionality as you please. So, try something like this:

public class People {

    Set<Person> persons = new HashSet<Person>();

    Set<Person> getPersons() {
        return persons;
    }

    int computeSomethingAboutPeople() { 
        // return as you please
    }

}

And use it like this, as the previous poster suggested:

People people = new People();
while(rs.next())
{
    Person person = new Person();
    String name = rs.getString(2);
    person.setName(name);
    int id = rs.getInt(1);
    person.setId(id);
    String dept = rs.getString(4);
    person.setDept(dept);
    int age = rs.getInt(3);
    person.setAge(age);
    people.getPersons().add(person);
}
int answer = people.computeSomethingAboutPeople();
double-m
Have `People` implementing the `Set` interface. Then you can use it as a Set and don't have to force the user to do something like `people.getPersons().add(person);`. And if provide a getter for the set, don't return the internal set, return a `Collections.unmodifiableSet(persons)`, so nobody can change the internals.
Andreas_D
Thanks for your answer. It helped me. But i have an another question. Do i have to declare a set for Person Class. Like, Set<Person> persons = new HashSet<Person>(); .
NooBDevelopeR