This repository was archived by the owner on Apr 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCart.java
More file actions
115 lines (101 loc) · 3.42 KB
/
Cart.java
File metadata and controls
115 lines (101 loc) · 3.42 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package group.rohlik.entity;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.util.Assert;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashSet;
import java.util.Set;
@Entity
@Getter
@NoArgsConstructor
@Table(name = "carts")
public class Cart {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@OneToMany(mappedBy = "cart", orphanRemoval = true, cascade = CascadeType.ALL)
@EqualsAndHashCode.Exclude
private Set<CartLine> lines = new HashSet<>();
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(
name = "cart_discounts",
joinColumns = @JoinColumn(name = "cart_id"),
inverseJoinColumns = @JoinColumn(name = "discount_id")
)
@EqualsAndHashCode.Exclude
private Set<Discount> discounts = new HashSet<>();
public void addLine(Product product, int quantity) {
Assert.isTrue(quantity >= 0, "Line quantity must be positive");
CartLine cartLine = lines
.stream()
.filter(currentCartLine -> currentCartLine.getProduct().equals(product))
.findFirst()
.orElse(null);
if (cartLine == null) {
if (quantity > 0) {
cartLine = new CartLine();
cartLine.setProduct(product);
cartLine.setQuantity(quantity);
cartLine.setCart(this);
lines.add(cartLine);
}
} else {
if (quantity == 0) {
lines.remove(cartLine);
} else {
cartLine.setQuantity(quantity);
}
}
}
private double totalLinesPrice() {
return lines
.stream()
.mapToDouble(currentCartLine -> currentCartLine.getQuantity() * currentCartLine.getProduct().getPrice())
.sum();
}
private double totalDiscountsPercentage() {
return discounts
.stream()
.mapToDouble(Discount::getValue)
.sum();
}
public double totalPrice() {
double totalLinesPrice = totalLinesPrice();
return Math.max(
0,
BigDecimal
.valueOf(totalLinesPrice - (totalLinesPrice * totalDiscountsPercentage() / 100))
.setScale(2, RoundingMode.CEILING)
.doubleValue()
);
}
public Integer quantityOfProduct(String sku) {
return lines
.stream()
.filter(line -> line.getProduct().getSku().equals(sku))
.findFirst()
.map(CartLine::getQuantity)
.orElse(0);
}
public boolean hasDiscount(String name) {
return discounts
.stream()
.anyMatch(line -> line.getName().equals(name));
}
public void applyDiscount(Discount discount) {
Assert.isTrue(!discounts.contains(discount), "Discount already applied");
discounts.add(discount);
}
}