2013-03-19 56 views
-2

我有3个班。我正在学习接口,接口类TravelCost必须具有公共抽象和方法类型和名称,以便它们在所有三个类中保持一致。三类(AirTravelCost,TrainTravelCost,CarTravelCost)将实施TravelCost。我拥有所有的设置和测试工作。但是,我假设的测试页面是您通过arrayList输入搜索的最低成本和最短持续时间。我不知道该怎么做,因为我从来没有在ArrayList中做过这件事。在这里,在测试类中的示例代码:如何通过ArrayList搜索最低数量和最短持续时间?

import java.util.*; 

public class TestTravelCost 
{ 
    public static void main(String [] args) 
    { 
    /*Scanner scn = new Scanner(System.in); //scanner object 

    System.out.println("Number of Miles: "); 
    double numOfMiles = scn.nextDouble(); 

    System.out.println("Hotel cost per night: "); 
    double cost = scn.nextDouble(); 

    System.out.println("Description: "); 
    String description = scn.nextLine();*/ 

    TravelCost c = new CarTravelCost(400, 200, "Boston");//instantiate object for car travel 
    TravelCost t = new TrainTravelCost(6, 60.0, "Boston"); //instantiate object for train travel 
    TravelCost a = new AirTravelCost(224, "20110103", "0743" , "20110103", "1153", "Boston");//instantiate object for air travel 

    ArrayList<TravelCost> AL = new ArrayList<TravelCost>();//array list for car travel 
    AL.add(c); 
    AL.add(t); 
    AL.add(a); 

    for(TravelCost tc : AL) 
    { 
     System.out.println(tc.toString()); 
    } 
    } 
} 

输出: 汽车旅行波士顿将采取7.2727272727272725小时,耗资210.0
火车前往波士顿将采取6.0小时,耗资70.0
航空旅行到波士顿会采取1.0166666666666666和成本243.48888888888888 //这是不正确的计算,我不知道我错在哪里,但它假设是最短的持续时间相同。我想我不擅长数学。

下面是我用于航空旅行

public double getDuration() 
{ 
    //---DEPARTURE---// 
    int Dyear = Integer.parseInt(departureDate.substring(0,3)); //2011 
    int Dmonth = Integer.parseInt(departureDate.substring(4,5));//01 
    int Dday = Integer.parseInt(departureDate.substring(6,7));//03 

    int Dhour = Integer.parseInt(departureTime.substring(0,1));//0743 
    int Dminute = Integer.parseInt(departureTime.substring(2,3));//1153 
    //---ARRIVAL---// 
    int Ayear = Integer.parseInt(arrivalDate.substring(0,3)); //2011 
    int Amonth = Integer.parseInt(arrivalDate.substring(4,5));//01 
    int Aday = Integer.parseInt(arrivalDate.substring(6,7));//03 

    int Ahour = Integer.parseInt(arrivalTime.substring(0,1));//0743 
    int Aminute = Integer.parseInt(arrivalTime.substring(2,3));//1153 

    GregorianCalendar date = new GregorianCalendar(Dyear, Dmonth, Dday, Dhour, Dminute);//departure date & time 
    GregorianCalendar time = new GregorianCalendar(Ayear, Amonth, Aday, Ahour, Aminute);//arrival date & time 

    //date = arrivalDate - departureDate;//2011-01-03 - 2011-01-03 = 0 
    //time = arrivalTime - departureTime;//0734 - 1153 = 410 

    double duration = (Math.abs(date.getTimeInMillis() - time.getTimeInMillis())/60000.0)/60.0; 
    return duration; 
    `enter code here` } 

的计算方法如何得到这样的结果在我的代码?

最低的成本:火车旅行波士顿将采取11.0小时,耗资70.0
时间最短:航空旅行波士顿将采取4.166666666666667小时耗资234.0

回答

0

你不显示TravelCost接口,但要实现什么你希望它至少应该有一个getDuration和getCost方法。

public interface TravelCost { 
    ... // what you already have in the interface definition 
    public double getDuration(); 
    public double getCost(); 
} 

武装这一说法,我想创建一个小的虚拟类实现的基础是对这些性质相媲美:

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 
} 

这将让你找到你要找的内容:

// instantiate 2 DummyTC's with impossibly high cost &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() < cheapest.getCost()) { 
     cheapest = tc; 
    } 
    // if present tc is shorter than shortest, swap 
    if (tc.getDuration() < 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()); 

另一件事,你的日期处理代码是可怕的复杂。看看SimpleDateFormat

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 
} 

截止日期也有routine to get the epoch-millis你可以用你已有的代码继续,虽然long可能是一个更好的回报型比在这种情况下的两倍。