I have to create a class CarFactory, which draws a car as per seasons (this one I think I got right) and also switches on lights of the given car from 20pm till 6am. Please igonore @Beans, it is just for testing purposes. My question is how the implementation of hasHeadlightsTurnedOn() should look like? And am a bit lost as to how implement the rule of the lights being switched on from 20pm till 6am.
Interface:
public interface Car {
boolean hasHeadlightsTurnedOn();
String getCarType();
}
Then 3 types of car, as an example one of them:
public class Sedan implements Car {
@Override
public String getCarType(){
return "Sedan";
}
@Override
public boolean hasHeadlightsTurnedOn() {
return true ;
}
}
Class CarFactory:
@Configuration
public class CarFactory {
@Bean
public Car randomCar(){
Car car;
String[] strArr= {"Spring", "Autumn", "Summer","Winter"};
Random generator = new Random();
int chosen = generator.nextInt(strArr.length);
if (chosen == 3) {
car = new SUV();
} else if (chosen == 2) {
car = new Cabrio();
} else {
car = new Sedan();
}
return car;
}
}