Nantes Université

Skip to content
Extraits de code Groupes Projets
Valider 0034ed8f rédigé par Erwan BOUSSE's avatar Erwan BOUSSE
Parcourir les fichiers

improve specification and code for some methods

parent d51aab80
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
......@@ -97,6 +97,9 @@ public interface TicketReservationSystem {
* - The origin must be different from the destination.
* @return The created registered trip.
* @throws TripException If one of the above constraints was not satisfied.
* @throws NullPointerException if one of the parameters is null
* @throws IllegalArgumentException if the provided City or Train objets are not part of the TicketReservationSystem
*
*/
Trip createTrip(City origin, City destination, Train train, Instant departure, Instant arrival) throws TripException;
......@@ -109,7 +112,7 @@ public interface TicketReservationSystem {
/**
* Adds a departure delay to a trip.
* Also automatically adds an arrival delay of the same amount.
* Also automatically adds an arrival delay of the same amount (see 'delayTripArrival').
* @param delay The amount of delay to add.
*/
void delayTripDeparture(Trip trip, Duration delay);
......@@ -122,22 +125,24 @@ public interface TicketReservationSystem {
void delayTripArrival(Trip trip, Duration delay);
/**
* Finds the trip that follows an existing trip of a train.
* Finds the trip that follows an existing trip of a train, if it exists.
* @param train The train.
* @param trip The trip.
* @return The trip following the given trip, for the train.
* @throws TripException if the trip's train is not the same
* @param trip The given trip.
* @return The train trip following the given trip, or an empty result if the given trip is the last.
* @throws IllegalArgumentException if the given train or trip are part of the TicketReservationSystem, or if the given trip is not linked with the given train.
* @throws NullPointerException if train or trip is null
*/
Optional<Trip> findNextTripOfTrain(Train train, Trip trip) throws TripException;
Optional<Trip> findNextTripOfTrain(Train train, Trip trip) throws IllegalArgumentException, NullPointerException;
/**
* Finds the trip that precedes an existing trip of a train.
* Finds the trip that precedes an existing trip of a train, if it exists.
* @param train The train.
* @param trip The trip.
* @return The trip preceding the given trip, for the train.
* @throws TripException if the trip's train is not the same
* @return The train trip preceding the given trip, or an empty result if the given trip is the first.
* @throws IllegalArgumentException if the given train or trip are not part of the TicketReservationSystem, or if the given trip is not linked with the given train.
* @throws NullPointerException if train or trip is null
*/
Optional<Trip> findPreviousTripOfTrain(Train train, Trip trip) throws TripException;
Optional<Trip> findPreviousTripOfTrain(Train train, Trip trip) throws IllegalArgumentException, NullPointerException;
/**
* Finds and orders all the trips of a given train.
......
......@@ -86,8 +86,22 @@ public class TicketReservationSystemImpl implements TicketReservationSystem {
@Override
public Trip createTrip(City origin, City destination, Train train, Instant departure, Instant arrival) throws TripException {
List<Trip> trainTrips = this.findOrderedTripsOfTrain(train);
Objects.requireNonNull(origin);
Objects.requireNonNull(destination);
Objects.requireNonNull(train);
Objects.requireNonNull(departure);
Objects.requireNonNull(arrival);
if (!(this.getCities().contains(origin) && this.getCities().contains(destination) )) {
throw new IllegalArgumentException("Provided cities must be part of the system");
}
if (!this.getAllTrains().contains(train)) {
throw new IllegalArgumentException("Provided train must be part of the system");
}
List<Trip> trainTrips = this.findOrderedTripsOfTrain(train);
if (!trainTrips.isEmpty()) {
......@@ -128,20 +142,24 @@ public class TicketReservationSystemImpl implements TicketReservationSystem {
@Override
public void delayTripArrival(Trip trip, Duration delay) {
trip.addArrivalDelay(delay);
try {
Optional<Trip> nextTrip = findNextTripOfTrain(trip.getTrain(), trip);
if (nextTrip.isPresent()) {
delayTripDeparture(nextTrip.get(), delay);
}
} catch (TripException e) {
e.printStackTrace();
}
}
@Override
public Optional<Trip> findNextTripOfTrain(Train train, Trip trip) throws TripException {
public Optional<Trip> findNextTripOfTrain(Train train, Trip trip) throws IllegalArgumentException, NullPointerException {
Objects.requireNonNull(train);
Objects.requireNonNull(trip);
if (!this.getAllTrains().contains(train)) {
throw new IllegalArgumentException("The provided train is not part of the system.");
}
if (!this.getAllTrips().contains(trip)) {
throw new IllegalArgumentException("The provided trip is not part of the system.");
}
if (trip.getTrain() != train) {
throw new TripException();
throw new IllegalArgumentException("The provided trip is not linked with the provided train.");
}
List<Trip> orderedTrips = findOrderedTripsOfTrain(train);
int nextTripIndex = orderedTrips.indexOf(trip) + 1;
......@@ -150,9 +168,17 @@ public class TicketReservationSystemImpl implements TicketReservationSystem {
}
@Override
public Optional<Trip> findPreviousTripOfTrain(Train train, Trip trip) throws TripException {
public Optional<Trip> findPreviousTripOfTrain(Train train, Trip trip) throws IllegalArgumentException, NullPointerException {
Objects.requireNonNull(train);
Objects.requireNonNull(trip);
if (!this.getAllTrains().contains(train)) {
throw new IllegalArgumentException("The provided train is not part of the system.");
}
if (!this.getAllTrips().contains(trip)) {
throw new IllegalArgumentException("The provided trip is not part of the system.");
}
if (trip.getTrain() != train) {
throw new TripException();
throw new IllegalArgumentException("The provided trip is not linked with the provided train.");
}
List<Trip> orderedTrips = findOrderedTripsOfTrain(train);
int previousTripIndex = orderedTrips.indexOf(trip) - 1;
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter