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());
}
}
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:
MaritalStatus.java:
Person.java:
Couple.java:
Dating.java: