package inheritance;

import java.util.ArrayList;
import java.util.Collection;
import java.util.stream.Collectors;

public class Train {
  private Collection<TrainCar> cars = new ArrayList<>();

  public void addTrainCar(TrainCar car) {
    this.cars.add(car);
  }

  public boolean contains(TrainCar car) {
    return this.cars.contains(car);
  }

  public int getTotalWeight() {
    return 
      this.cars
        .stream()
        .map(car -> car.getTotalWeight())
        .reduce(0, (a, b) -> a+b);
  }

  public int getPassengerCount() {
    return
      this.cars
        .stream()
        .filter(car -> car instanceof PassengerCar)
        .map(car -> (PassengerCar) car)
        .map(pcar -> pcar.getPassengerCount())
        .reduce(0, (a, b) -> a+b);
  }

  public int getCargoWeight() {
    return
      this.cars
        .stream()
        .filter(car -> car instanceof CargoCar)
        .map(car -> (CargoCar) car)
        .map(ccar -> ccar.getCargoWeight())
        .reduce(0, (a, b) -> a+b);
  }

  @Override
  public String toString() {
    return
      "\uD83D\uDE82 - "
      + this.cars
        .stream()
        .map(car -> car.toString())
        .collect(Collectors.joining(" - "));
  }
}
