1

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;
    }
}
Veronique
  • 23
  • 7

4 Answers4

2

This would be your function for hasHeadlightsTurnedOn.

boolean hasHeadlightsTurnedOn(){
    int hour = LocalTime.now().getHour();
    return hour>=20 || hour<6;
}

For the season part, you could use the month and the day to determine what season it is instead of a randomly generated number.

2
@Override
public boolean hasHeadlightsTurnedOn() {
    LocalTime lt = LocalTime.now();

    // long way
    /*int hour = lt.getHour();
    if (hour < 6 || hour >= 20) {
        return true;
    } else {
        return false;
    }*/

    // short way
    return lt.getHour() < 6 || lt.getHour() >= 20;
}

Also, I would recommend using either an abstract class or a default method in the interface. This way, you don't have to create the same method for each class.

Abstract class:

import java.time.LocalTime;

public abstract class AbstractCar implements Car {

    @Override
    public boolean hasHeadlightsTurnedOn() {
        LocalTime lt = LocalTime.now();
        return lt.getHour() < 6 || lt.getHour() >= 20;
    }
}

and then the car classes:

public class Sedan extends AbstractCar {
    @Override
    public String getCarType(){
        return "Sedan";
    }
}

OR

Default method:

import java.time.LocalTime;

public interface Car {
    default boolean hasHeadlightsTurnedOn() {
        LocalTime lt = LocalTime.now();
        return lt.getHour() < 6 || lt.getHour() >= 20;
    }
    String getCarType();
}

and your car classes, just without the hasHeadlightsTurnedOn function

anonymus1994.1
  • 308
  • 1
  • 6
1

Define a private Boolean variable on each car type. Check the current time and if it falls under the time bracket in which light is suppose to be on then set it to true. Some thing like this

Date date = new Date() ;

SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm") ;
dateFormat.format(date);
System.out.println(dateFormat.format(date));

if(dateFormat.parse(dateFormat.format(date)).after(dateFormat.parse("12:07")))
{
    System.out.println("Current time is greater than 12.07");
}else{`enter code here`
    System.out.println("Current time is less than 12.07");
}
Amit
  • 633
  • 6
  • 13
  • You used terrible date-time classes that were years ago supplanted by the modern *java.time* classes. Suggesting these legacy classes in 2021 is poor advice. – Basil Bourque Feb 15 '21 at 18:26
  • I kept my example as simple for as possible so it can be digested easily. One can any time repurpose the code however they want to – Amit Feb 15 '21 at 18:41
0

LocalDateTime has a getHour() method that you can use like this:

LocalDateTime time = LocalDateTime.now();
if (time.getHour() < 6 || time.getHour() >= 20) {
    // headlights on
} else {
    // headlights off
}
Erik
  • 578
  • 2
  • 9
  • Calling `LocalDateTime.now` is almost never the right thing to do. That class cannot represent a moment, a specific point on the timeline. To represent a moment as seen through the wall-clock time used by the people of a particular region, use `ZonedDateTime`. Your code happens to work here, but is a poor example. Furthermore, for this Question we don’t need a moment, we only need `LocalTime`. – Basil Bourque Feb 15 '21 at 18:30