View difference between Paste ID: AWDcNQBH and h9n6QCft
SHOW: | | - or go back to the newest paste.
1
/*
2
 * Commit:
3
 *  -------------------------------------------------------------------
4
 * | Author       |  Commit   |         Message       | Date           |
5
 * | Oscar Nivor  |  01ddbab  | feat: new class       | 45 seconds ago |
6
 *  ------------------------------------------------------------------- 
7
 */
8
9
/*
10
 * *******************************************************************************
11
 * ******************************** MAIN CODE ************************************
12
 * *******************************************************************************
13
 */
14
15
import java.util.*;
16
import com.rentalServiceRegistry.*;
17
18
@Component
19
public class Service {
20
    public final static String apiKey = "fez67fezyb223";
21
    private long timeToExecute = 0;
22
    @Autowired
23
    private RentalServiceRegistry rentalServiceRegistry;
24
25
    // Renvoie les noms des services de locations
26
    public ArrayList<String> ListRentalServices(String country, Integer mp, Integer limit, String city, Boolean airport, String typeVoiture) throws NoServiceFoundException {
27
        timeToExecute = System.currentTimeMillis();
28
        ArrayList<String> servicesToReturn = new ArrayList<>();
29
        rentalServiceRegistry.setAPIKey(apiKey);
30
        RentalServiceCriteria rentalServiceCriteria = new RentalServiceCriteria();
31
        rentalServiceCriteria.setCountry(country);
32
        rentalServiceCriteria.setCity(city);
33
        if (airport == true)
34
            rentalServiceCriteria.setAirport(true);
35
            
36
        ArrayList<RentalServices> services;
37
        try {
38
            services = rentalServiceRegistry.getRentalServices(rentalServiceCriteria);
39
        } catch (NoServiceFoundException e) {
40
            System.out.println("No service match the criteria");
41
            throw e;
42
        } catch (Exception e) {
43
            System.out.println(e.getMessage());
44
            throw e;
45
        }
46
47
        for (RentalService service : services) {
48
            if (service.getPrice() < mp ) {
49
                if (service.getCarType() == typeVoiture) {
50
                    if (servicesToReturn.size() < limit)
51
                        servicesToReturn.add(service.getName());
52
                }
53
            }
54
        }
55
        timeToExecute = System.currentTimeMillis() - timeToExecute;
56
        System.out.println("Time to execute : " + timeToExecute);
57
        // Return the services
58
        return servicesToReturn;
59
    }
60
}
61
62
63
/*
64
 * *******************************************************************************
65
 * ********************************** TESTS **************************************
66
 * *******************************************************************************
67
 */
68
69
import static org.junit.jupiter.api.Assertions.assertEquals;
70
import org.junit.jupiter.api.Test;
71
72
import java.util.ArrayList;
73
74
public class ServiceTest {
75
76
    private final Service serviceToTest;
77
78
    @Test
79
    public void test1() {
80
        serviceToTest = new Service();
81
        ArrayList<String> services = serviceToTest.ListRentalServices("france", 500, 20, "nice", true, "economique");
82
        assertTrue(services.size() > 0);
83
    }
84
85
    @Test
86
    public void test2() {
87
        ArrayList<String> services = serviceToTest.ListRentalServices("france", 500, 20, "nice", false, "economique");
88
        assertTrue(services.size() > 0);
89
    }
90
91
    @Test
92
    public void test3() {
93
        ArrayList<String> services = serviceToTest.ListRentalServices("france", 500, 20, "nice", false, "mini");
94
        assertTrue(services.size() > 0);
95
    }
96
97
    @Test
98
    public void test4() {
99
        ArrayList<String> services = serviceToTest.ListRentalServices("allemagne", 500, 20, "nice", false, "mini");
100
        assertTrue(services.size() > 0);
101
    }
102
}