Class TourGuideService
java.lang.Object
com.openclassrooms.tourguide.service.TourGuideService
Main service that manages the TourGuide app's features:
- Tracks user locations
- Assigns reward points
- Provides recommendations for nearby attractions
- Generates personalized travel offers
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final ExecutorService
private final gpsUtil.GpsUtil
private final com.github.benmanes.caffeine.cache.Cache<UUID,
gpsUtil.location.VisitedLocation> private final RewardsService
private final boolean
private final boolean
final Tracker
private static final String
private final tripPricer.TripPricer
-
Constructor Summary
ConstructorsConstructorDescriptionTourGuideService
(gpsUtil.GpsUtil gpsUtil, RewardsService rewardsService, ExecutorService executorService, boolean startTracker) Builder of the main TourGuide service. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Adds a user to the internal list if it does not already exist.private double
Generates a random latitude between -85.05 and 85.05 degrees.private double
Generates a random longitude between -180 and 180 degrees.private void
Generates a random location history for a user.Returns the full list of registered users.List<gpsUtil.location.Attraction>
getNearByAttractions
(gpsUtil.location.VisitedLocation visitedLocation) Returns the 5 closest attractions to the given position.private Date
Creates a random date within the last 30 days.List<tripPricer.Provider>
getTripDeals
(User user) Generate a list of personalized travel offers for a user.Search for a user by username.gpsUtil.location.VisitedLocation
getUserLocation
(User user) Returns the user's current location.getUserRewards
(User user) Returns the list of rewards associated with a given user.private void
Initializes a list of internal test users.void
setAllUsers
(List<User> allUsers) Defines a new user list for the service.void
shutdown()
Stops user tracking if it is active.void
trackAllUsersLocations
(List<User> users) Starts location tracking for all provided users.CompletableFuture<gpsUtil.location.VisitedLocation>
trackUserLocation
(User user) Starts location tracking for a given user.
-
Field Details
-
executor
-
allUsers
-
internalUserMap
-
gpsUtil
private final gpsUtil.GpsUtil gpsUtil -
rewardsService
-
tripPricer
private final tripPricer.TripPricer tripPricer -
tracker
-
startTracker
private final boolean startTracker -
locationCache
private final com.github.benmanes.caffeine.cache.Cache<UUID,gpsUtil.location.VisitedLocation> locationCache -
TRIP_PRICER_API_KEY
- See Also:
-
testMode
private final boolean testMode- See Also:
-
-
Constructor Details
-
TourGuideService
public TourGuideService(gpsUtil.GpsUtil gpsUtil, RewardsService rewardsService, ExecutorService executorService, @Value("${tourguide.startTracker:true}") boolean startTracker) Builder of the main TourGuide service. Initializes the necessary components- Parameters:
gpsUtil
- User geolocation servicerewardsService
- Rewards Management ServiceexecutorService
- Thread pool for asynchronous processingstartTracker
- Indicates whether to enable automatic user tracking
-
-
Method Details
-
setAllUsers
Defines a new user list for the service. This method replaces the current users with those provided in parameters The list must not be null or empty, otherwise an error is thrown.- Parameters:
allUsers
- Liste des utilisateurs à enregistrer- Throws:
IllegalArgumentException
- if the list is nullIllegalStateException
- if the list is empty
-
getUserRewards
Returns the list of rewards associated with a given user.- Parameters:
user
- User for whom you want to get the rewards- Returns:
- List of reward points earned by this user
-
getUserLocation
Returns the user's current location. If the user already has a saved location, it is returned. Otherwise, a new location is obtained in real time.- Parameters:
user
- Concerned user- Returns:
- Last known position or updated position
-
trackUserLocation
Starts location tracking for a given user. If a recent position is already available in the cache, it is used directly. Otherwise, a new position is retrieved from the GPS service, added to the user's history, and rewards are calculated in the background.- Parameters:
user
- The user to locate- Returns:
- A CompletableFuture containing the user's new (or old) position
-
trackAllUsersLocations
Starts location tracking for all provided users. Each user is located in parallel via asynchronous calls, and execution waits for all operations to complete before continuing.- Parameters:
users
- List of users to follow
-
getNearByAttractions
public List<gpsUtil.location.Attraction> getNearByAttractions(gpsUtil.location.VisitedLocation visitedLocation) Returns the 5 closest attractions to the given position. This method sorts all known attractions by distance from the user's current location, then returns the top 5.- Parameters:
visitedLocation
- Current position of the user- Returns:
- List of the 5 nearest attractions
-
getUser
Search for a user by username. If the user exists in the internal list, it is returned. Otherwise, an error is thrown.- Parameters:
userName
- Username to search for- Returns:
- The user corresponding to the given name
- Throws:
IllegalArgumentException
- if no user is found
-
getAllUsers
Returns the full list of registered users. This method returns a new list containing all users currently stored in internal memory.- Returns:
- List of all known users
-
addUser
Adds a user to the internal list if it does not already exist. If a user with the same name is already registered, they will not be replaced.- Parameters:
user
- The user to add
-
getTripDeals
Generate a list of personalized travel offers for a user. The number of reward points is taken into account to calculate offers tailored to the user's profile (adults, children, duration). The list is then saved to the user's account.- Parameters:
user
- The user for whom we want to get offers- Returns:
- List of suppliers with their travel offers
-
initializeInternalUsers
private void initializeInternalUsers()Initializes a list of internal test users. The number of users to be created is determined by a configuration value. Each user receives a username, a unique name, and a history of simulated locations. These users are added to the service's internal map. -
generateUserLocationHistory
Generates a random location history for a user. This method adds three simulated locations with randomly generated coordinates and dates to create a movement history.- Parameters:
user
- The user to add the positions to
-
generateRandomLongitude
private double generateRandomLongitude()Generates a random longitude between -180 and 180 degrees. This method is used to simulate geographic coordinates.- Returns:
- A random longitude value
-
generateRandomLatitude
private double generateRandomLatitude()Generates a random latitude between -85.05 and 85.05 degrees.- Returns:
- A random latitude value
-
getRandomTime
Creates a random date within the last 30 days.- Returns:
- A recent random date
-
shutdown
public void shutdown()Stops user tracking if it is active.
-