Skip to content

Commit cfdf57f

Browse files
author
rustavil
committed
[refactor] tests
1 parent f7a6293 commit cfdf57f

30 files changed

+522
-570
lines changed
Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,28 @@
11
package ru.rustavil.fuel_consumption.domain;
22

3+
import ru.rustavil.fuel_consumption.domain.exceptions.InvalidException;
4+
35
import java.util.UUID;
46

57
public class Driver {
68

7-
private UUID id;
8-
private Long identifier;
9-
10-
public Driver() {
11-
this.id = UUID.randomUUID();
12-
}
13-
14-
public Driver(UUID id) {
15-
this.id = id;
16-
}
9+
private final UUID id;
10+
private final Long identifier;
1711

1812
public Driver(Long identifier) {
1913
this.id = UUID.randomUUID();
14+
if (identifier <= 0) {
15+
throw new InvalidException("Driver identifier must be positive");
16+
}
2017
this.identifier = identifier;
2118
}
2219

2320
public UUID getId() {
2421
return id;
2522
}
2623

27-
public void setId(UUID id) {
28-
this.id = id;
29-
}
30-
3124
public Long getIdentifier() {
3225
return identifier;
3326
}
3427

35-
public void setIdentifier(Long identifier) {
36-
this.identifier = identifier;
37-
}
3828
}
Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,77 @@
11
package ru.rustavil.fuel_consumption.domain;
22

3+
import ru.rustavil.fuel_consumption.domain.exceptions.InvalidException;
4+
35
import java.math.BigDecimal;
6+
import java.util.Objects;
47
import java.util.UUID;
58

69
public class FuelConsumption {
710

8-
private UUID id;
9-
private Driver driver;
10-
private FuelType fuelType;
11-
private Double fuelVolume;
12-
private BigDecimal fuelPrice;
11+
private final UUID id;
12+
private final Driver driver;
13+
private final FuelType fuelType;
14+
private final Double fuelVolume;
15+
private final BigDecimal fuelPrice;
1316

14-
public FuelConsumption() {
15-
this.id = UUID.randomUUID();
17+
public FuelConsumption(UUID id,
18+
Driver driver,
19+
FuelType fuelType,
20+
Double fuelVolume,
21+
BigDecimal fuelPrice) {
22+
this.id = id;
23+
if (Objects.isNull(driver)) {
24+
throw new InvalidException("Driver required");
25+
}
26+
this.driver = driver;
27+
this.fuelType = fuelType;
28+
if (fuelVolume <= 0) {
29+
throw new InvalidException("Fuel volume must be great than 0");
30+
}
31+
this.fuelVolume = fuelVolume;
32+
if (fuelPrice.compareTo(BigDecimal.ZERO) <= 0) {
33+
throw new InvalidException("Fuel price must be great than 0");
34+
}
35+
this.fuelPrice = fuelPrice;
1636
}
1737

18-
public FuelConsumption(Driver driver, FuelType fuelType, Double fuelVolume, BigDecimal fuelPrice) {
38+
public FuelConsumption(Driver driver,
39+
FuelType fuelType,
40+
Double fuelVolume,
41+
BigDecimal fuelPrice) {
1942
this.id = UUID.randomUUID();
43+
if (Objects.isNull(driver)) {
44+
throw new InvalidException("Driver required");
45+
}
2046
this.driver = driver;
2147
this.fuelType = fuelType;
48+
if (fuelVolume <= 0) {
49+
throw new InvalidException("Fuel volume must be great than 0");
50+
}
2251
this.fuelVolume = fuelVolume;
52+
if (fuelPrice.compareTo(BigDecimal.ZERO) <= 0) {
53+
throw new InvalidException("Fuel price must be great than 0");
54+
}
2355
this.fuelPrice = fuelPrice;
2456
}
2557

2658
public UUID getId() {
2759
return id;
2860
}
2961

30-
public void setId(UUID id) {
31-
this.id = id;
32-
}
33-
3462
public Driver getDriver() {
3563
return driver;
3664
}
3765

38-
public void setDriver(Driver driver) {
39-
this.driver = driver;
40-
}
41-
4266
public FuelType getFuelType() {
4367
return fuelType;
4468
}
4569

46-
public void setFuelType(FuelType fuelType) {
47-
this.fuelType = fuelType;
48-
}
49-
5070
public Double getFuelVolume() {
5171
return fuelVolume;
5272
}
5373

54-
public void setFuelVolume(Double fuelVolume) {
55-
this.fuelVolume = fuelVolume;
56-
}
57-
5874
public BigDecimal getFuelPrice() {
5975
return fuelPrice;
6076
}
61-
62-
public void setFuelPrice(BigDecimal fuelPrice) {
63-
this.fuelPrice = fuelPrice;
64-
}
6577
}
Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
package ru.rustavil.fuel_consumption.domain;
22

33

4-
import ru.rustavil.fuel_consumption.domain.exceptions.EmptyFuelConsumptionException;
5-
import ru.rustavil.fuel_consumption.domain.exceptions.MinimalFuelVolumeException;
6-
import ru.rustavil.fuel_consumption.domain.exceptions.RequiredDriverException;
4+
import ru.rustavil.fuel_consumption.domain.exceptions.InvalidException;
75

86
import java.math.BigDecimal;
97
import java.util.*;
108
import java.util.stream.Collectors;
119

1210
public class FuelPurchase {
1311

14-
private static final long MIN_REGISTRATION_FUEL_VOLUME = 1;
15-
private List<FuelConsumption> fuelConsumptionList;
16-
17-
public FuelPurchase() {
18-
}
12+
private final List<FuelConsumption> fuelConsumptionList;
1913

2014
public FuelPurchase(FuelConsumption fuelConsumption) {
15+
if (Objects.isNull(fuelConsumption)) {
16+
throw new InvalidException("Fuel consumption empty");
17+
}
2118
this.fuelConsumptionList = Collections.singletonList(fuelConsumption);
2219
}
2320

2421
public FuelPurchase(List<FuelConsumption> fuelConsumptionList) {
22+
if (Objects.isNull(fuelConsumptionList) || fuelConsumptionList.isEmpty()) {
23+
throw new InvalidException("Fuel consumption empty");
24+
}
2525
this.fuelConsumptionList = fuelConsumptionList;
2626
}
2727

@@ -41,18 +41,4 @@ public BigDecimal getTotalPrice() {
4141
return this.fuelConsumptionList.stream().map(FuelConsumption::getFuelPrice).reduce(BigDecimal.ZERO, BigDecimal::add);
4242
}
4343

44-
public void IsCanRegister() {
45-
if (Objects.isNull(this.fuelConsumptionList) || this.fuelConsumptionList.isEmpty()) {
46-
throw new EmptyFuelConsumptionException();
47-
}
48-
for (FuelConsumption fuelConsumption: this.fuelConsumptionList) {
49-
if (Objects.isNull(fuelConsumption.getDriver())) {
50-
throw new RequiredDriverException();
51-
}
52-
if (fuelConsumption.getFuelVolume() < MIN_REGISTRATION_FUEL_VOLUME) {
53-
throw new MinimalFuelVolumeException(String.format("Minimal registration volume %d", MIN_REGISTRATION_FUEL_VOLUME));
54-
}
55-
}
56-
}
57-
5844
}

src/main/java/ru/rustavil/fuel_consumption/domain/FuelPurchaseNotification.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package ru.rustavil.fuel_consumption.domain;
22

33
import com.google.common.base.Joiner;
4+
import ru.rustavil.fuel_consumption.domain.exceptions.InvalidException;
45

56
import java.util.Map;
67

@@ -9,13 +10,21 @@ public class FuelPurchaseNotification implements Notification {
910
private static final String TITLE = "New fuel purchase!";
1011
private static final String MSG_TMPL = "Total fuel volume: %.2fL (%s). Total price: %.2fEUR";
1112

12-
private Double totalFuelVolume;
13-
private Map<FuelType, Double> fuelVolumeMap;
14-
private Double totalPrice;
13+
private final Double totalFuelVolume;
14+
private final Map<FuelType, Double> fuelVolumeMap;
15+
private final Double totalPrice;
1516

16-
public FuelPurchaseNotification(Double totalFuelVolume, Map<FuelType, Double> fuelVolumeMap, Double totalPrice) {
17+
public FuelPurchaseNotification(Double totalFuelVolume,
18+
Map<FuelType, Double> fuelVolumeMap,
19+
Double totalPrice) {
20+
if (totalFuelVolume < 0) {
21+
throw new InvalidException("Total fuel price must not be negative");
22+
}
1723
this.totalFuelVolume = totalFuelVolume;
1824
this.fuelVolumeMap = fuelVolumeMap;
25+
if (totalPrice < 0) {
26+
throw new InvalidException("Total fuel price must not be negative");
27+
}
1928
this.totalPrice = totalPrice;
2029
}
2130

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,55 @@
11
package ru.rustavil.fuel_consumption.domain;
22

3+
import ru.rustavil.fuel_consumption.domain.exceptions.InvalidException;
4+
5+
import java.math.BigDecimal;
36
import java.time.LocalDate;
47
import java.util.Objects;
58

69
public class MonthDriverFuelConsumption {
710

8-
private LocalDate date;
9-
private Long driverIdentifier;
10-
private FuelType fuelType;
11-
private Double fuelVolume;
12-
private Double fuelPrice;
13-
14-
public MonthDriverFuelConsumption() {
15-
}
11+
private final LocalDate date;
12+
private final Long driverIdentifier;
13+
private final FuelType fuelType;
14+
private final Double fuelVolume;
15+
private final BigDecimal fuelPrice;
1616

17-
public MonthDriverFuelConsumption(int year, int mont, Long driverIdentifier, FuelType fuelType, Double fuelVolume, Double fuelPrice) {
17+
public MonthDriverFuelConsumption(
18+
int year, int mont, Long driverIdentifier, FuelType fuelType,
19+
Double fuelVolume, BigDecimal fuelPrice) {
1820
this.date = LocalDate.of(year, mont, 1);
1921
this.driverIdentifier = driverIdentifier;
2022
this.fuelType = fuelType;
23+
if (fuelVolume < 0) {
24+
throw new InvalidException("Total fuel volume must not be negative");
25+
}
2126
this.fuelVolume = fuelVolume;
27+
if (fuelPrice.compareTo(BigDecimal.ZERO) < 0 ) {
28+
throw new InvalidException("Total fuel price must not be negative");
29+
}
2230
this.fuelPrice = fuelPrice;
2331
}
2432

2533
public LocalDate getDate() {
2634
return date;
2735
}
2836

29-
public void setDate(LocalDate date) {
30-
this.date = date;
31-
}
32-
3337
public Long getDriverIdentifier() {
3438
return driverIdentifier;
3539
}
3640

37-
public void setDriverIdentifier(Long driverIdentifier) {
38-
this.driverIdentifier = driverIdentifier;
39-
}
40-
4141
public FuelType getFuelType() {
4242
return fuelType;
4343
}
4444

45-
public void setFuelType(FuelType fuelType) {
46-
this.fuelType = fuelType;
47-
}
48-
4945
public Double getFuelVolume() {
5046
return fuelVolume;
5147
}
5248

53-
public void setFuelVolume(Double fuelVolume) {
54-
this.fuelVolume = fuelVolume;
55-
}
56-
57-
public Double getFuelPrice() {
49+
public BigDecimal getFuelPrice() {
5850
return fuelPrice;
5951
}
6052

61-
public void setFuelPrice(Double fuelPrice) {
62-
this.fuelPrice = fuelPrice;
63-
}
64-
6553
@Override
6654
public boolean equals(Object o) {
6755
if (this == o) return true;
@@ -76,6 +64,7 @@ public boolean equals(Object o) {
7664

7765
@Override
7866
public int hashCode() {
79-
return Objects.hash(date, driverIdentifier, fuelType, fuelVolume, fuelPrice);
67+
return Objects.hash(date, driverIdentifier,
68+
fuelType, fuelVolume, fuelPrice);
8069
}
8170
}

0 commit comments

Comments
 (0)