Java如何处理多个返回值?

时间:2018-02-07 17:26:23

标签: java oop arraylist methods

我们假设我有一个Flight对象列表

public class Flight {
    private int passengers;
    private int price
    ...

    //getters and Setters
}

List<Flight> flightList = new ArrayList<Flight>();

现在我需要累积每位乘客的价格和价格,因为我必须能够在以后进行这两种信息。所以我会创建两个方法:

public int calculatePrice(List<Flight> flightList) {
    int price = 0;
    for (Flight flight : flightList) {
        price = price + flight.getPrice();
    }
    return price;
}

public int calculatePricePerPassengers(List<Flight> flightList) {
    int pricePerPassenger = 0;
    for (Flight flight : flightList) {
        pricePerPassenger = (pricePerPassenger) + (flight.getPrice() / flight.getPassgers());
    }
    return pricePerPassenger;
}

我有4-5种相同类型的方法。我不确定是否有太多冗余,因为我调用for循环4-5次,我可以轻松地在一个for循环中执行所有操作,但这会产生多个返回值的效果。这是适当的方式吗? (只是一个虚拟的例子)

3 个答案:

答案 0 :(得分:5)

您可以使用map或者也可以扭曲要在类中返回的所有字段。在这里,我将使用Map进行示例。

public static Map<String, Integer> calculatePrice(List<Flight> flightList) {
    Map<String, Integer> priceMap = new HashMap<>();
    int price = 0;
    int pricePerPassenger = 0;
    for (Flight flight : flightList) {
        price = price + flight.getPrice();
        pricePerPassenger = (pricePerPassenger) + (flight.getPrice() / flight.getPassgers());
    }
    priceMap.put("price", price);
    priceMap.put("pricePerPassenger", pricePerPassenger);
    return priceMap;
}

修改 使用包装类(DTO)的示例。

    public static FligtPriceDTO calculatePrice(List<Flight> flightList) {
        FligtPriceDTO dto = new FligtPriceDTO();
        int price = 0;
        int pricePerPassenger = 0;
        for (Flight flight : flightList) {
            price = price + flight.getPrice();
            pricePerPassenger = (pricePerPassenger) + (flight.getPrice() / flight.getPassgers());
        }
        dto.setPrice(price);
        dto.setPricePerPassenger(pricePerPassenger);
        return dto;
    }
}

class FligtPriceDTO {
    private int price;
    private int pricePerPassenger;

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public int getPricePerPassenger() {
        return pricePerPassenger;
    }

    public void setPricePerPassenger(int pricePerPassenger) {
        this.pricePerPassenger = pricePerPassenger;
    }

}

答案 1 :(得分:1)

您可以将这两个值包装到一个新数组中:

public int[] calculatePrices(List<Flight> flightList) {

    int totalPrice = 0;
    int pricePerCustomer = 0;

    for (Flight flight : flightList) {
        totalPrice += flight.getPrice();
        pricePerCustomer += (flight.getPrice() / flight.getPassgers());
    }

    return new int[] { totalPrice, pricePerCustomer };
}

答案 2 :(得分:0)

这一切都归结为您希望为用户提供的功能。因此,如果用户希望能够单独计算两种价格,那么我看不到任何其他方式。但是,如果您可以同时提供两种价格,那么您可以消除其中一种方法,只在您的航班列表中循环一次并返回两种价格的组合。例如:

public static Pair[] calculatePrices(List<Flight> flightList)
{
     int pricePerFlight= 0;
     int pricePerPassenger = 0;
     for(Flight flight : flightList)
    {
       pricePerFlight = pricePerFlight + flight.getPrice();
       pricePerPassenger =   pricePerPassenger  +(flight.getPrice()/flight.getPassgers());

     }
    return new Pair[] {new Pair<>("pricePerFlight", pricePerFlight), new Pair<>("pricePerPassenger", pricePerPassenger )};
}