首页 > 解决方案 > 编写代码以确定航班的总成本

问题描述

总成本可以通过将去程航班和回程航班的成本相加来计算。然而,离开飞行和返回飞行都是飞行类的对象。有没有办法将这些添加到值中以获得总成本?

这是我尝试过的:

public Double getTotalCost() {
    return departingFlight + returningFlight;
}//End of method

飞行类:公共类飞行{

private String code;
private String origin;
private String destination;
private String departureTime;
private String arrivalTime;
private Double cost;

public Flight(String cde, String orig , String dest, String dtime,String atime,Double cst) {
    code = cde;
    origin = orig;
    destination = dest;
    departureTime = dtime;
    arrivalTime = atime;
    cost = cst;

}//End of constructor flight

public String getCode() {
    return code;

}//End of method

public String getDepartureTime() {
    return departureTime;
}//End of method

public Double getCost() {
    return cost;
}//End of method

public String toString() {
    return "" + cost + "\t " + origin + " " + departureTime + "\t" + destination + " " + arrivalTime; 
    }//end of toString method

}//课程结束

任何帮助将不胜感激。:)

标签: java

解决方案


这是你需要做的。将两个航班添加到参数,然后:

public Double getTotalCost(Flight a,Flight b) {
    return a.getCost() + b.getCost();
}//End of method

推荐阅读