简体   繁体   中英

How to use LocalTime correctly in a method?

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;
    }
}

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.

@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

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");
}

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM