I am working on a small project where I have Classes Country, Population and Continent.
Both of the Classes Population and Continent use Enums to define possible values. Where in Country I want to use "factory methods" to create new Countries. Code follows:
public enum Continent {
EUROPE,
ASIA,
AMERICA;
}
public enum Population {
LOW(1000000),
AVERAGE(2000000),
HIGH(5000000);
}
public class Country{
private Continent=null;
private Population=null;
Country(Continent continent, Population population){
this.continent=continent;
this.population=population;
}
Ok so my problem is that I tried to override my equals() function, but it doesn't give me the expected results.
public boolean equals(Country other){
if(this.population == other.population && this.continent==other.continent)
return true;
else
return false;
}
But the assertions tests are giving me the following results back
java.lang.AssertionError: [new Country(Europe,HIGH)] Expecting:
<"Romania (Country@464a7b61)"> to be equal to: <"Romania (Country@488d8dd8)">
but was not.
I was looking online, and what I have found was that when same parameters are given it should know it is the same one, e.g. there should not be two different objects for same parameters. I am sure if I have understood it correctly, but it seemed relevant.
I still don`t know if that is the case and how to approach it. Ideas?
UPDATE:
After some suggestions appeared I tried changing the equals function to
public boolean equals(Country other){
if(this.population.equals(other.population) && this.continent.equals(other.continent))
return true;
else
return false;
}
UPDATE 2:
public boolean equals(Object o){
if(this.population.equals(o.population) && this.continent.equals(o.continent))
return true;
else
return false;
}
It is not allowing me to do .population and .continent