Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You're not showing the <code>TravelCost</code> interface, but to achieve what you want it should at least have a getDuration and getCost method.</p> <pre><code>public interface TravelCost { ... // what you already have in the interface definition public double getDuration(); public double getCost(); } </code></pre> <p>Armed with that, I'd create a small dummy class implementing the basics to be comparable on these properties:</p> <pre><code>public DummyTC implements TravelCost { private double cost; private double duration; public DummyTC(double cost, double duration) { this.cost = cost; this.duration = duration; } public double getDuration() { return duration; } public double getCost() { return cost; } // and other methods/properties imposed by TravelCost } </code></pre> <p>This will allow you to find what you're looking for:</p> <pre><code>// instantiate 2 DummyTC's with impossibly high cost &amp;durations TravelCost cheapest = new DummyTC(99999999999.99, 99999999999.99); TravelCost shortest = new DummyTC(99999999999.99, 99999999999.99); // iterate over the List for(TravelCost tc : AL) { // if present tc is cheaper than cheapest, swap if ( tc.getCost() &lt; cheapest.getCost() ) { cheapest = tc; } // if present tc is shorter than shortest, swap if ( tc.getDuration() &lt; shortest.getDuration() ) { shortest = tc; } } // at this point cheapest and shortest will contain the cheapest and shortest // ways of transportation, so we print them out. System.out.println(cheapest.toString()); System.out.println(shortest.toString()); </code></pre> <p>Another thing, your date handling code is horribly convoluted. Have a look at <a href="http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html" rel="nofollow">SimpleDateFormat</a></p> <pre><code>Date date = null; Date time = null; // create a SimpleDateFormat instance for your time/date format SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd"); try { // parse it date = format.parse(departureDate); // done } catch (ParseException e) { // departureDate could not be parsed, you should handle that case here } try { // parse it time = format.parse(arrivalTime); // done } catch (ParseException e) { // arrivalTime could not be parsed, you should handle that case here } </code></pre> <p>As Date also has a <a href="http://docs.oracle.com/javase/6/docs/api/java/util/Date.html#getTime%28%29" rel="nofollow">routine to get the epoch-millis</a> you can continue with the code you already have, although a <code>long</code> is probably a better return-type than a double in this case.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload