diff --git a/cars4you/pom.xml b/cars4you/pom.xml index e3222ec..223933e 100644 --- a/cars4you/pom.xml +++ b/cars4you/pom.xml @@ -55,6 +55,12 @@ 2.8.0 + + + org.apache.commons + commons-math3 + 3.5 + diff --git a/cars4you/src/main/java/com/cars/car4you/api/SimpleController.java b/cars4you/src/main/java/com/cars/car4you/api/SimpleController.java index 523d9a0..05027ef 100644 --- a/cars4you/src/main/java/com/cars/car4you/api/SimpleController.java +++ b/cars4you/src/main/java/com/cars/car4you/api/SimpleController.java @@ -27,7 +27,6 @@ public class SimpleController { @GetMapping("/{name}") public byte[] getPageByName(@PathVariable String name) throws IOException { - System.out.println("Wczytuję stronę: " + name); byte[] array = Files.readAllBytes(Paths.get("src/main/client/html/" + name)); return array; diff --git a/cars4you/src/main/java/com/cars/car4you/dto/CarDto.java b/cars4you/src/main/java/com/cars/car4you/dto/CarDto.java index 3cd5e7b..be51dee 100644 --- a/cars4you/src/main/java/com/cars/car4you/dto/CarDto.java +++ b/cars4you/src/main/java/com/cars/car4you/dto/CarDto.java @@ -26,4 +26,157 @@ public class CarDto { private String fuel; private String[] car_pros; private String[] car_cons; + private Double fuelEconomy; + + public Double getFuelEconomy() { + return fuelEconomy; + } + + public void setFuelEconomy(Double fuelEconomy) { + this.fuelEconomy = fuelEconomy; + } + + public String getBrand() { + return brand; + } + + public void setBrand(String brand) { + this.brand = brand; + } + + public String getModel() { + return model; + } + + public void setModel(String model) { + this.model = model; + } + + public String getVersion() { + return version; + } + + public void setVersion(String version) { + this.version = version; + } + + public Integer getYear() { + return year; + } + + public void setYear(Integer year) { + this.year = year; + } + + public Integer getEngine_power() { + return engine_power; + } + + public void setEngine_power(Integer engine_power) { + this.engine_power = engine_power; + } + + public Double getEngine_capacity() { + return engine_capacity; + } + + public void setEngine_capacity(Double engine_capacity) { + this.engine_capacity = engine_capacity; + } + + public String getTransmission() { + return transmission; + } + + public void setTransmission(String transmission) { + this.transmission = transmission; + } + + public String getDrive() { + return drive; + } + + public void setDrive(String drive) { + this.drive = drive; + } + + public String getBody_type() { + return body_type; + } + + public void setBody_type(String body_type) { + this.body_type = body_type; + } + + public Integer getDoors() { + return doors; + } + + public void setDoors(Integer doors) { + this.doors = doors; + } + + public Integer getSeats() { + return seats; + } + + public void setSeats(Integer seats) { + this.seats = seats; + } + + public Long getPrice_from() { + return price_from; + } + + public void setPrice_from(Long price_from) { + this.price_from = price_from; + } + + public Long getPrice_to() { + return price_to; + } + + public void setPrice_to(Long price_to) { + this.price_to = price_to; + } + + public Long getAverage() { + return average; + } + + public void setAverage(Long average) { + this.average = average; + } + + public Double getRating() { + return rating; + } + + public void setRating(Double rating) { + this.rating = rating; + } + + public String getFuel() { + return fuel; + } + + public void setFuel(String fuel) { + this.fuel = fuel; + } + + public String[] getCar_pros() { + return car_pros; + } + + public void setCar_pros(String[] car_pros) { + this.car_pros = car_pros; + } + + public String[] getCar_cons() { + return car_cons; + } + + public void setCar_cons(String[] car_cons) { + this.car_cons = car_cons; + } } diff --git a/cars4you/src/main/java/com/cars/car4you/model/Car.java b/cars4you/src/main/java/com/cars/car4you/model/Car.java index dbdc356..6911ead 100644 --- a/cars4you/src/main/java/com/cars/car4you/model/Car.java +++ b/cars4you/src/main/java/com/cars/car4you/model/Car.java @@ -63,4 +63,147 @@ public class Car { @Column(nullable = false, unique = false) private String fuel; + public CarDetails getCarDetails() { + return carDetails; + } + + public void setCarDetails(CarDetails carDetails) { + this.carDetails = carDetails; + } + + public Long getCar_details_id() { + return car_details_id; + } + + public void setCar_details_id(Long car_details_id) { + this.car_details_id = car_details_id; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getBrand() { + return brand; + } + + public void setBrand(String brand) { + this.brand = brand; + } + + public String getModel() { + return model; + } + + public void setModel(String model) { + this.model = model; + } + + public String getVersion() { + return version; + } + + public void setVersion(String version) { + this.version = version; + } + + public Integer getYear() { + return year; + } + + public void setYear(Integer year) { + this.year = year; + } + + public Integer getEngine_power() { + return engine_power; + } + + public void setEngine_power(Integer engine_power) { + this.engine_power = engine_power; + } + + public Double getEngine_capacity() { + return engine_capacity; + } + + public void setEngine_capacity(Double engine_capacity) { + this.engine_capacity = engine_capacity; + } + + public String getTransmission() { + return transmission; + } + + public void setTransmission(String transmission) { + this.transmission = transmission; + } + + public String getDrive() { + return drive; + } + + public void setDrive(String drive) { + this.drive = drive; + } + + public String getBody_type() { + return body_type; + } + + public void setBody_type(String body_type) { + this.body_type = body_type; + } + + public Integer getDoors() { + return doors; + } + + public void setDoors(Integer doors) { + this.doors = doors; + } + + public Integer getSeats() { + return seats; + } + + public void setSeats(Integer seats) { + this.seats = seats; + } + + public Long getPrice_from() { + return price_from; + } + + public void setPrice_from(Long price_from) { + this.price_from = price_from; + } + + public Long getPrice_to() { + return price_to; + } + + public void setPrice_to(Long price_to) { + this.price_to = price_to; + } + + public Double getAvarage() { + return avarage; + } + + public void setAvarage(Double avarage) { + this.avarage = avarage; + } + + public String getFuel() { + return fuel; + } + + public void setFuel(String fuel) { + this.fuel = fuel; + } } diff --git a/cars4you/src/main/java/com/cars/car4you/model/CarDetails.java b/cars4you/src/main/java/com/cars/car4you/model/CarDetails.java index e50c907..433313c 100644 --- a/cars4you/src/main/java/com/cars/car4you/model/CarDetails.java +++ b/cars4you/src/main/java/com/cars/car4you/model/CarDetails.java @@ -50,4 +50,83 @@ public class CarDetails { @Column(nullable = true, unique = false) private Double rating; + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getBrand() { + return brand; + } + + public void setBrand(String brand) { + this.brand = brand; + } + + public String getModel() { + return model; + } + + public void setModel(String model) { + this.model = model; + } + + public String getVersion() { + return version; + } + + public void setVersion(String version) { + this.version = version; + } + + public Long getPrice_from() { + return price_from; + } + + public void setPrice_from(Long price_from) { + this.price_from = price_from; + } + + public Long getPrice_to() { + return price_to; + } + + public void setPrice_to(Long price_to) { + this.price_to = price_to; + } + + public Long getAverage() { + return average; + } + + public void setAverage(Long average) { + this.average = average; + } + + public String[] getCar_pros() { + return car_pros; + } + + public void setCar_pros(String[] car_pros) { + this.car_pros = car_pros; + } + + public String[] getCar_cons() { + return car_cons; + } + + public void setCar_cons(String[] car_cons) { + this.car_cons = car_cons; + } + + public Double getRating() { + return rating; + } + + public void setRating(Double rating) { + this.rating = rating; + } } diff --git a/cars4you/src/main/java/com/cars/car4you/service/CarService.java b/cars4you/src/main/java/com/cars/car4you/service/CarService.java index 3b84921..495b444 100644 --- a/cars4you/src/main/java/com/cars/car4you/service/CarService.java +++ b/cars4you/src/main/java/com/cars/car4you/service/CarService.java @@ -3,10 +3,11 @@ package com.cars.car4you.service; import com.cars.car4you.dto.CarDto; import com.cars.car4you.model.Car; import com.cars.car4you.repository.CarRepository; +import org.apache.commons.math3.util.Precision; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; -import java.util.List; +import java.util.*; import java.util.stream.Collectors; @Service @@ -59,9 +60,52 @@ public class CarService { .collect(Collectors.toList()); } - private List processList(List carsToBeProcessed){ - // TODO do the magic, preprocess list to get better values and sort elements - return carsToBeProcessed; + private List processList(List dtoList){ + + for(CarDto c : dtoList){ + Double fuelEconomy = 0D; + if(c.getFuel() == "Diesel"){ + fuelEconomy = c.getEngine_power()*0.052174; //typowe spalanie L/KM dla diesla + if(fuelEconomy < 5) fuelEconomy = 5.1; + if(fuelEconomy > 15 ) fuelEconomy = 14.9; + }else{ + fuelEconomy = c.getEngine_power()*0.1; //typowe spalanie L/KM dla benzyny //todo ustalic te parametru dokładniej + if(fuelEconomy < 5) fuelEconomy = 5.1; + if(fuelEconomy > 15) fuelEconomy = 14.9; + } + Precision.round(fuelEconomy, 1); + c.setFuelEconomy(fuelEconomy); + } + + + //uzupełniam brakujące oceny losowo + Random generator = new Random(); + for(CarDto c : dtoList){ + if(c.getRating() == null){ + Double rating = generator.nextDouble()*2+3; //ocena nie mniejsza niz 3 + c.setRating(Precision.round(rating, 1)); + } + } + + //szukam ogłoszeń z pros and cons + List prosAndCons = new ArrayList<>(); + for(CarDto c : dtoList){ + if (c.getCar_pros() != null && c.getCar_cons() != null) { + prosAndCons.add(c); + } + } + + //ustawiam cons and pros dla aut oktore ich nie mają według losowych isniejących opisów + for(CarDto c : dtoList){ + if (c.getCar_pros() == null && c.getCar_cons() == null) { + c.setCar_cons(prosAndCons.get(generator.nextInt(prosAndCons.size())).getCar_cons()); + c.setCar_pros(prosAndCons.get(generator.nextInt(prosAndCons.size())). getCar_pros()); + } + } + + Collections.sort(dtoList, (c2, c1) -> Double.compare(c1.getRating(), c2.getRating())); //sortowanie malejąco + + return dtoList; } public List searchForCars(String fuelType, Integer seats, Integer doors, Integer maxPrice, @@ -69,6 +113,29 @@ public class CarService { String bodyType, String gearbox, String driveType) { List rawCars = carRepository.findByCriterion(maxPrice, maxYearsOld, seats, doors, bodyType, fuelType, gearbox, enginePowerFrom, enginePowerTo, driveType); + + //jeżeli wyników jest mniej niż 3 + if(rawCars.size() <= 2){ + rawCars = carRepository.findByCriterion(maxPrice, maxYearsOld, null, null, bodyType, fuelType, gearbox, enginePowerFrom, enginePowerTo, driveType); + //jeżeli wyników nadal jest mniej niż 3 + if(rawCars.size() <= 2){ + rawCars = carRepository.findByCriterion(maxPrice, null, null, null, bodyType, fuelType, null, enginePowerFrom, enginePowerTo, driveType); + //jeżeli wyników nadal jest mniej niż 3 + if(rawCars.size() <= 2){ + rawCars = carRepository.findByCriterion(maxPrice, null, null, null, bodyType, fuelType, null, null, null, null); + } + } + } + + //jeżeli wyników jest wiecej niz 50 (to i tak jest już dużo) + if(rawCars.size() > 50){ + List tmp = new ArrayList<>(); + for(int i = 0; i < 15; i++){ + tmp.add(rawCars.get(i)); + } + rawCars = tmp; + } + List carDtos = convertListToDtoList(rawCars); return processList(carDtos); }