我有3节课。我正在学习接口和接口类TravelCost必须具有公共摘要以及方法类型和名称,以便它通过所有三个类保持一致。这三个班级(AirTravelCost,TrainTravelCost,CarTravelCost)将实施TravelCost。我已完成所有设置和测试工作。但是,我假设的测试页面是您通过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.272727272727272725小时,费用为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 :(得分: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
可能是比双重更好的返回类型。