Nantes Université

TripException isn't thrown in createTrip

Selon l'interface fr.univnantes.trainreservation.TicketReservationSystem la methode createTrip() doit respecter la signature et le fonctionnement suivant :

    /**
     * Creates and registers a new trip in the system.
     * This new trip must take place *after* the last trip of the chosen train (based on the real arrival time).
     * In addition, a trip can only be created if the following constraints are satisfied:
     * - The last destination of the train must be in the same city as the origin of the new trip.
     * - The last arrival of the train must be at least 10 minutes from the departure of the new trip (using real departure and arrival times).
     * - The arrival must come after the departure.
     * - The origin must be different from the destination.
     * @return The created registered trip.
     * @throws TripException If one of the above constraints was not satisfied.
     */
    Trip createTrip(City origin, City destination, Train train, Instant departure, Instant arrival) throws TripException;

Hors dans l'implementation fr.univnantes.trainreservation.impl.TicketReservationSystemImpl on constate que les conditions d'invalidités du trajet ne sont vérifiées que si le train spécifié pour le nouveau trajet a au moins un trajet renseigné. Ainsi des conditions sans rapport avec les trajets antérieurs du train ne sont pas vérifiées on peux donner en exemple les conditions suivantes :

  • L'heure d'arrivée doit être après l'heure de départ
  • La ville d'origine doit être différente de la ville de destination.

On pourrait donc corriger cela de deux manières :

  • Soit en altérant la spécification de cette façon par exemple :
    /**
     * Creates and registers a new trip in the system.
     * This new trip must take place *after* the last trip of the chosen train (based on the real arrival time).
     * In addition, a trip can only be created:
     * - If the following constraints are satisfied :
     *   - The last destination of the train must be in the same city as the origin of the new trip.
     *   - The last arrival of the train must be at least 10 minutes from the departure of the new trip (using real departure and arrival times).
     *   - The arrival must come after the departure.
     *   - The origin must be different from the destination.
     * - Or if this trip is first trip of given train.
     * @return The created registered trip.
     * @throws TripException If one of the above constraints was not satisfied.
     */
    Trip createTrip(City origin, City destination, Train train, Instant departure, Instant arrival) throws TripException;
  • Soit en altérant l'implémentation, en vérifiant les conditions qui n'ont pas besoin des anciens trajet du train associé même si celui-ci n'a pas de trajet. En ce basant sur l'implementation actuelle on pourrait la modifier de cette façon par exemple :
    @Override
    public Trip createTrip(City origin, City destination, Train train, Instant departure, Instant arrival) throws TripException {
        List<Trip> trainTrips = this.findOrderedTripsOfTrain(train);

        boolean durationTripIsValid = arrival.isAfter(departure);

        boolean originIsDifferentFromDestination = origin != destination;

        if (!(durationTripIsValid && originIsDifferentFromDestination))
            throw new TripException();

        if (!trainTrips.isEmpty()) {

            Trip lastTrainTrip = trainTrips.get(trainTrips.size() - 1);

            boolean comesAfter = lastTrainTrip.findRealArrivalTime().isBefore(departure);

            boolean c1 = lastTrainTrip.getDestination() == origin;

            boolean c2 = Duration.between(lastTrainTrip.findRealArrivalTime(), departure)
                    .compareTo(Duration.ofMinutes(10)) == 1;

            if (!(comesAfter && c1 && c2))
                throw new TripException();
        }
        Trip trip = new TripImpl(origin, destination, train, departure, arrival);
        trips.add(trip);
        return trip;
    }

P.S. : Désolé par avance pour la mise en page, gitlab depuis mobile ce n'est pas l'idéal.

Modification effectuée par Mathieu Féry