I got this code:
public Class Car;
{
private String name;
public int number;
public Car(String name, int number)
{
this.name = name;
this.number = number;
}
// this class also got getters and setters
Then I got another class:
public class CarList
{
private Collection<Car> cars;
public CarList()
{
cars = new HashSet<>();
}
public boolean insertCar(Car car)
{
return cars.add(car);
}
Then code continues with some other methods but they are not problem.
If I create two instances of class Car
, for example car1(mazda,1)
and car2(porsche,2)
and then I call method insertCar
, everything is OK, method returns true
because there are two cars with different name and number.
But if create for example car1(mazda,1)
and then car2(porsche,1)
method returns also true
despite the fact that numbers of the cars are the same.
EDIT:So my question is how to force method to return false
when I try to ''add'' cars with the same number AND same name ( so how would @Override equals() method change )?