O'Reilly logo

Java 9 Cookbook by Nick Samoylov, Mohamed Sanaulla

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

There's more...

So far, we did not leave the API user a choice of the output format. Currently, it is implemented as the method printResult()

void printResult(TrafficUnit tu, double timeSec, double speedMph) {  System.out.println("Road " + tu.getRoadCondition() + ", tires "                      + tu.getTireCondition() + ": "                      + tu.getVehicleType().getType() + " speedMph ("                      + timeSec + " sec)=" + speedMph + " mph");}

To make it more flexible, we can add another parameter to our API:

Traffic api = new TrafficImpl(Month.APRIL, DayOfWeek.FRIDAY, 17,                              "USA", "Denver", "Main103S");double timeSec = 10.0;int trafficUnitsNumber = 10;BiConsumer<TrafficUnit, Double> output = (tm, sp) ->  System.out.println("Road " + tm.getRoadCondition() + ", tires "  + tm.getTireCondition() ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required