tags:

views:

142

answers:

5

I am trying to create a new instance of Couple with the given name and gender and add to the collections of couples, in the addCouple method. I have another class called Couple. In that class I have getters and Setters one for name and the gender. I tried to use the bulk operation for the list:

List<Dating> list1 = new ArrayList<Dating>(this.addCouple);

but I got an error "nonstatic variable this cannot be referenced from a static context". I then tried to use Collection.sort then printout the list. I got the same error message. So I believe I do not know how to use this.addList. Could someone tell me how to use it. Should I be using this.addList? Thanks in advance.

public class Dating
{

  private List<Male> maleList;
  private List<Female> femaleList; 


  public Dating()
  {

    super();
    maleList = new ArrayList<Single>();
    femaleList = new ArrayList<Single>();
  }


  public void lists()
  { 
   this.addList("Jack","Male",'m');
   this.addList("Mike","Male",'m'); 

   this.addList("Lynda","Female",'f'); 
   this.addList("Katie","Female",'f'); 
  }


   public static void addCouple (String aName, char aGender)
   {
      Collections.sort(this.addList);
      for (Couple group : this.addList)
      {
         System.out.println(" " + group.getName() + " " + group.getGender());
      }

   }
+1  A: 

You have to make your members static or you must remove the static flag from your function. You can not mix static with non static stuff, roughly said.

InsertNickHere
InsertNickHere - how do I do this then? Do I need to declare local variable for this.addList?
Which part of *you must remove the static flag from your function* is unclear?
Willi
addCouple method header. I am not sure the type of method header that needs to be declared. One thing is for sure the methoder header must have (String aName, char aGender) arguments. The method suppose to create a new instance of Couple (which I don't know how to do?) with the given name and gender . It then adds to the collection of couples (again I don't what it means). So I cannot alter public void lists() including this.addList. Does my comments make any sense?
+2  A: 

Your addCouple is declared as a static method. There is no this for static methods, because they don't belong to an instance. They belong to a class.

Enno Shioji
Zwei - how do I do this then?
+7  A: 

I think you have much bigger problems than that. Classes Male, Female, Single? Those are attributes of a Person class, not classes in and of themselves. Your design needs rework.

You need Person and Couple classes. You also need a model that reflects the real world. Like it or not, you can have Male-Male and Female-Female couples.

I'd write it like this. As you can see, List works perfectly:

Gender.java:

public enum Gender
{
    MALE, FEMALE;
}

MaritalStatus.java:

public enum MaritalStatus
{
    SINGLE, MARRIED, DIVORCED, WIDOWED, SEPARATED;
}

Person.java:

import java.text.DateFormat;
import java.util.Date;

public class Person
{
    private String name;
    private Date birthdate;
    private Gender gender;
    private MaritalStatus maritalStatus;

    public Person(String name)
    {
        this(name, new Date(), Gender.MALE, MaritalStatus.SINGLE);
    }

    public Person(String name, Gender gender)
    {
        this(name, new Date(), gender, MaritalStatus.SINGLE);
    }

    public Person(String name, Date birthdate)
    {
        this(name, birthdate, Gender.MALE, MaritalStatus.SINGLE);
    }

    public Person(String name, Date birthdate, Gender gender)
    {
        this(name, birthdate, gender, MaritalStatus.SINGLE);
    }

    public Person(String name, Date birthdate, Gender gender, MaritalStatus maritalStatus)
    {
        if ((name == null) || (name.trim().length() == 0))
            throw new IllegalArgumentException("name cannot be blank or null");

        this.name = name;
        this.birthdate = ((birthdate == null) ? new Date() : new Date(birthdate.getTime()));
        this.gender = gender;
        this.maritalStatus = maritalStatus;
    }

    public String getName()
    {
        return name;
    }

    public Date getBirthdate()
    {
        return new Date(birthdate.getTime());
    }

    public Gender getGender()
    {
        return gender;
    }

    public MaritalStatus getMaritalStatus()
    {
        return maritalStatus;
    }

    @Override
    public boolean equals(Object o)
    {
        if (this == o)
        {
            return true;
        }
        if (o == null || getClass() != o.getClass())
        {
            return false;
        }

        Person person = (Person) o;

        if (!birthdate.equals(person.birthdate))
        {
            return false;
        }
        if (gender != person.gender)
        {
            return false;
        }
        if (maritalStatus != person.maritalStatus)
        {
            return false;
        }
        if (!name.equals(person.name))
        {
            return false;
        }

        return true;
    }

    @Override
    public int hashCode()
    {
        int result = name.hashCode();
        result = 31 * result + birthdate.hashCode();
        result = 31 * result + gender.hashCode();
        result = 31 * result + maritalStatus.hashCode();
        return result;
    }

    @Override
    public String toString()
    {
        return "Person{" +
               "name='" + name + '\'' +
               ", birthdate=" + DateFormat.getDateInstance(DateFormat.MEDIUM).format(birthdate) +
               ", gender=" + gender +
               ", maritalStatus=" + maritalStatus +
               '}';
    }
}

Couple.java:

public class Couple
{
    private Person ying;
    private Person yang;

    public Couple(Person ying, Person yang)
    {
        this.ying = ying;
        this.yang = yang;
    }

    public Person getYing()
    {
        return ying;
    }

    public Person getYang()
    {
        return yang;
    }

    @Override
    public String toString()
    {
        return "Couple{" +
               "ying=" + ying +
               ", yang=" + yang +
               '}';
    }
}

Dating.java:

import java.util.ArrayList;
import java.util.List;


public class Dating
{
    private List<Person> individuals = new ArrayList<Person>();
    private List<Couple> couples     = new ArrayList<Couple>();

    public static void main(String[] args)
    {
        Dating dating = new Dating();

        dating.addIndividual(new Person("Jack"));
        dating.addIndividual(new Person("Mike"));
        dating.addIndividual(new Person("Lydia", Gender.FEMALE));
        dating.addIndividual(new Person("Kate", Gender.FEMALE));

        System.out.println(dating);
    }

    public void addIndividual(Person p)
    {
        this.individuals.add(p);
    }

    public void removeIndividual(Person p)
    {
        this.individuals.remove(p);
    }

    public Person findIndividual(String name)
    {
        Person found = null;

        if (name != null)
        {
            for (Person p : this.individuals)
            {
                if (name.equals(p.getName()))
                {
                    found = p;
                    break;
                }
            }
        }

        return found;
    }

    @Override
    public String toString()
    {
        return "Dating{" +
               "individuals=" + individuals +
               ", couples=" + couples +
               '}';
    }
}
duffymo
Wow this is a great answer, good code, and probably 10X more useful than the original (plus it compiles!). I would complain about the "{" not being on the same line as, but I'm trying to stop doing that, :-)
Java Drinker
Compiles - and runs. Don't complain about brace placement. You suffer for eternity if you do it the other way. 8) Accept the answer if you find it really helpful.
duffymo
+1 and +1 for everybody who places `{` on the same line as the method header.
Willi
+1  A: 

You'd better be off with creating a "Person" class which might roughly look like this:

public class Person
{
    private String name;
    private boolean male; // or use an enum GENDER if you plan to do more than male/female

    public Person(String name, boolean male)
    {
        this.name = name;
        this.male = male;
    }

    // [...] getters and setters

    public String toString()
    {
            return name + " " + (male ? "male" : "female");
    }
}

and, depending on what you actually want to do... (a little bit more information would be good)

public class Dating
{
    private List<Person> persons;
    //[...] constructor

    public void addWhateverYouMeanWithCouple(String name, boolean male)
    {
        this.persons.add(new Person(name, male));
        System.out.println(persons);
    }
}

I really don't know what you are looking for, this is the only bit of code I can provide you, though I still think it's not exactly what you are looking for.

If you want to sort the List of Persons you will have implement Comparable<Person> (see Comparable JavaDoc) in the Person class and provide some logic to compare two Persons (e.g. using name.compareTo(otherName)).

If you actually want to have "Couples" or Pairs, have a look at HashMap.

Tedil
A: 

attually just two lines of code was required. I was difficult to explain in first instance. Thanks for all the relies.