if one condition is satisfied it is not checking any other condition in if else
After one condition satisfied it is not checking other condition . I want that it shouls check for premium condition also
public Car getCar(String CarType , String InsuranceType) //, //String InsuranceType)
{
// comparing string with method argument
if(CarType.equalsIgnoreCase("Hatchback"))
{
return new Hatchback();
}
else if(CarType.equalsIgnoreCase("Sedan"))
{
return new Sedan();
}
else if(CarType.equalsIgnoreCase("SUV"))
{
return new SUV();
}
if(InsuranceType.equalsIgnoreCase("Premium"))
{
return new Premium();
}
return null;
}
That's because you return when the if
is true
, so you don't get to the Premium check.
The return
keyword finished the execution of a method, and can be used to return a value from a method.
If you want that the Premium check will be executed, move it to be the 1st check.