-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCarServiceMemoryImpl.java
More file actions
60 lines (50 loc) · 1.72 KB
/
CarServiceMemoryImpl.java
File metadata and controls
60 lines (50 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package ro.cristi.curs.service;
import java.math.BigDecimal;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;
import ro.cristi.curs.exception.CarServiceException;
import ro.cristi.curs.model.Car;
import ro.cristi.curs.repository.CarRepository;
@Service
@Profile("local")
public class CarServiceMemoryImpl implements CarServiceMemory {
@Autowired
CarRepository carRepository;
@Override
public List<Car> getExpensiveCars(Integer percent) throws CarServiceException {
List<Car> cars = carRepository.getAllCars();
if(percent > 100){
throw new CarServiceException(40001, "Percent must be less");
}
if (cars == null || cars.isEmpty()) {
throw new CarServiceException(50001, "No cars found");
} else if (cars.size()>1000) {
throw new CarServiceException(50002, "Too many cars found");
}
//after error checks
for (Car car : cars) {
car.setPrice(car.getPrice().multiply(BigDecimal.valueOf((100 + percent) / 100)));
}
return cars;
}
@Override
public List<Car> getOlderCars(Integer years){
//todo: exceptions
List<Car> cars = carRepository.getAllCars();
for(Car car : cars){
car.setYear(car.getYear() - years);
}
return cars;
}
@Override
public List<Car> getEfficientCars(Integer units){
//todo: exceptions
List<Car> cars = carRepository.getAllCars();
for(Car car : cars){
car.setConsumption(car.getConsumption() - units);
}
return cars;
}
}