Search in sources :

Example 1 with FareType

use of org.opentripplanner.routing.core.Fare.FareType in project OpenTripPlanner by opentripplanner.

the class DefaultFareServiceImpl method getCost.

@Override
public Fare getCost(GraphPath path) {
    List<Ride> rides = createRides(path);
    // If there are no rides, there's no fare.
    if (rides.size() == 0) {
        return null;
    }
    Fare fare = new Fare();
    boolean hasFare = false;
    for (Map.Entry<FareType, Collection<FareRuleSet>> kv : fareRulesPerType.entrySet()) {
        FareType fareType = kv.getKey();
        Collection<FareRuleSet> fareRules = kv.getValue();
        // pick up a random currency from fareAttributes,
        // we assume that all tickets use the same currency
        Currency currency = null;
        WrappedCurrency wrappedCurrency = null;
        if (fareRules.size() > 0) {
            currency = Currency.getInstance(fareRules.iterator().next().getFareAttribute().getCurrencyType());
            wrappedCurrency = new WrappedCurrency(currency);
        }
        hasFare = populateFare(fare, currency, fareType, rides, fareRules);
    }
    return hasFare ? fare : null;
}
Also used : FareType(org.opentripplanner.routing.core.Fare.FareType) Currency(java.util.Currency) WrappedCurrency(org.opentripplanner.routing.core.WrappedCurrency) Collection(java.util.Collection) WrappedCurrency(org.opentripplanner.routing.core.WrappedCurrency) FareRuleSet(org.opentripplanner.routing.core.FareRuleSet) HashMap(java.util.HashMap) Map(java.util.Map) Fare(org.opentripplanner.routing.core.Fare)

Example 2 with FareType

use of org.opentripplanner.routing.core.Fare.FareType in project OpenTripPlanner by opentripplanner.

the class AddingMultipleFareService method getCost.

@Override
public Fare getCost(GraphPath path) {
    Fare fare = null;
    for (FareService subService : subServices) {
        Fare subFare = subService.getCost(path);
        if (subFare == null) {
            // No fare, next one please
            continue;
        }
        if (fare == null) {
            // Pick first defined fare
            fare = new Fare(subFare);
        } else {
            // Merge subFare with existing fare
            // Must use a temporary as we need to keep fare clean during the loop on FareType
            Fare newFare = new Fare(fare);
            for (FareType fareType : FareType.values()) {
                Money cost = fare.getFare(fareType);
                Money subCost = subFare.getFare(fareType);
                if (cost == null && subCost == null) {
                    continue;
                }
                if (cost != null && subCost == null) {
                    /*
                         * If for a given fare type we have partial data, we try to pickup the
                         * default "regular" cost to fill-in the missing information. For example,
                         * adding a bike fare which define only a "regular" cost, with some transit
                         * fare defining both "regular" and "student" costs. In that case, we
                         * probably want the "regular" bike fare to be added to the "student"
                         * transit fare too. Here we assume "regular" as a sane default value.
                         */
                    subCost = subFare.getFare(FareType.regular);
                } else if (cost == null && subCost != null) {
                    /* Same, but the other way around. */
                    cost = fare.getFare(FareType.regular);
                }
                if (cost != null && subCost != null) {
                    // Add sub cost to cost
                    newFare.addFare(fareType, cost.getCurrency(), cost.getCents() + subCost.getCents());
                } else if (cost == null && subCost != null) {
                    // Add new cost
                    // Note: this should not happen often: only if a fare
                    // did not compute a "regular" fare.
                    newFare.addFare(fareType, subCost.getCurrency(), subCost.getCents());
                }
            }
            fare = newFare;
        }
    }
    // Can be null here if no sub-service has returned fare
    return fare;
}
Also used : Money(org.opentripplanner.routing.core.Money) FareType(org.opentripplanner.routing.core.Fare.FareType) FareService(org.opentripplanner.routing.services.FareService) Fare(org.opentripplanner.routing.core.Fare)

Aggregations

Fare (org.opentripplanner.routing.core.Fare)2 FareType (org.opentripplanner.routing.core.Fare.FareType)2 Collection (java.util.Collection)1 Currency (java.util.Currency)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 FareRuleSet (org.opentripplanner.routing.core.FareRuleSet)1 Money (org.opentripplanner.routing.core.Money)1 WrappedCurrency (org.opentripplanner.routing.core.WrappedCurrency)1 FareService (org.opentripplanner.routing.services.FareService)1