source: trip-planner/src/main/java/finki/diplomska/tripplanner/service/impl/LocationServiceImpl.java@ 6a3a178

Last change on this file since 6a3a178 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 3.4 KB
Line 
1package finki.diplomska.tripplanner.service.impl;
2
3import finki.diplomska.tripplanner.models.Location;
4import finki.diplomska.tripplanner.models.exceptions.LocationNotFoundException;
5import finki.diplomska.tripplanner.repository.jpa.JpaLocationRepository;
6import finki.diplomska.tripplanner.service.LocationService;
7import org.springframework.stereotype.Service;
8
9import java.util.*;
10
11
12@Service
13public class LocationServiceImpl implements LocationService {
14
15 private final JpaLocationRepository locationRepository;
16
17 public LocationServiceImpl(JpaLocationRepository locationRepository) {
18 this.locationRepository = locationRepository;
19 }
20
21 @Override
22 public List<Location> findLocationsFromCity(String locName, String companion, List<String> categories) {
23 return this.locationRepository.findLocationsFromCity(locName, companion, categories);
24 }
25
26 @Override
27 public List<Location> findLocationsFromCountry(String locName, String companion, String region, List<String> categories) {
28 return this.locationRepository.findLocationsFromCountry(locName, companion, region, categories);
29 }
30
31 @Override
32 public List<Location> findAll() {
33 return this.locationRepository.findAll();
34 }
35
36 @Override
37 public Location getById(Long id) {
38 Optional<Location> location = this.locationRepository.findById(id);
39 if (!location.isPresent()) {
40 throw new LocationNotFoundException(id);
41 }
42 return location.get();
43 }
44
45 @Override
46 public Optional<Location> findById(Long id) {
47 return this.locationRepository.findById(id);
48 }
49
50 @Override
51 public List<Location> scheduleLocations(String locName, String companion, String region, List<String> categories, int numberOfDays) {
52 int maxMinutesPerDay = numberOfDays *6 * 60;
53 int minutesPerDay = 0;
54 List<Location> locations = this.locationRepository.findLocationsFromCity(locName, companion, categories);
55 List<Location> countryLocations = this.locationRepository.findLocationsFromCountry(locName, companion, region, categories);
56 List<Location> newList = new ArrayList<>();
57 int listSize = locations.size();
58 int listCountrySize = countryLocations.size();
59
60 if(locName.equals("Macedonia")){
61 while(minutesPerDay < maxMinutesPerDay){
62 for(Location l: countryLocations) {
63 if (minutesPerDay < maxMinutesPerDay && l.getDuration() + minutesPerDay <= maxMinutesPerDay && listCountrySize != 0) {
64 newList.add(l);
65 listCountrySize --;
66 }
67 minutesPerDay += l.getDuration();
68 if (minutesPerDay > maxMinutesPerDay) {
69 break;
70 }
71 }
72 }
73 }else{
74 while(minutesPerDay < maxMinutesPerDay ){
75 for(Location l: locations) {
76 if (minutesPerDay < maxMinutesPerDay && l.getDuration() + minutesPerDay <= maxMinutesPerDay && listSize != 0) {
77 newList.add(l);
78 listSize --;
79 }
80 minutesPerDay += l.getDuration();
81 if (minutesPerDay > maxMinutesPerDay) {
82 break;
83 }
84 }
85 }
86 }
87
88 return newList;
89 }
90
91}
Note: See TracBrowser for help on using the repository browser.