Let's assume I have got a List of Flight
Objects
public class Flight {
private int passengers;
private int price
...
//getters and Setters
}
List<Flight> flightList = new ArrayList<Flight>();
Now i need to accumulate price per passenger and the Price, because I have to be able to proceed both informations later on. so I would create two methods:
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;
}
I have got like 4-5 methods of the same type. I am not sure whether there is too much redundancy, because I call the for loop 4-5 Times and I could easily do all the operations in one for loop, but that would have the effect of multiple return values. So is this the appropriate way ? (just a dummy example)