-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathPoint.java
More file actions
63 lines (51 loc) · 1.82 KB
/
Point.java
File metadata and controls
63 lines (51 loc) · 1.82 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
package com.loopers.domain.point;
import com.loopers.domain.BaseEntity;
import com.loopers.support.error.CoreException;
import com.loopers.support.error.ErrorType;
import jakarta.persistence.*;
import lombok.Getter;
@Entity
@Table(name = "point")
@Getter
public class Point extends BaseEntity {
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Id
private Long id;
private String userId;
private Long balance;
protected Point() {}
private Point(String userId, Long balance) {
this.userId = requireValidUserId(userId);
this.balance = balance;
}
public static Point create(String userId, Long balance) {
return new Point(userId, balance);
}
String requireValidUserId(String userId) {
if(userId == null || userId.isEmpty()) {
throw new CoreException(ErrorType.BAD_REQUEST, "사용자 ID가 비어있을 수 없습니다.");
}
return userId;
}
public void charge(Long chargeAmount) {
if (chargeAmount == null || chargeAmount <= 0) {
throw new CoreException(ErrorType.BAD_REQUEST, "0원 이하로 포인트를 충전 할수 없습니다.");
}
this.balance += chargeAmount;
}
public void use(Long useAmount) {
if (useAmount == null || useAmount <= 0) {
throw new CoreException(ErrorType.BAD_REQUEST, "0원 이하로 사용할 수 없습니다.");
}
if (this.balance < useAmount) {
throw new CoreException(ErrorType.BAD_REQUEST, "포인트가 부족합니다.");
}
this.balance -= useAmount;
}
public void refund(Long amount) {
if (amount == null || amount <= 0) {
throw new CoreException(ErrorType.BAD_REQUEST, "0원 이하로 환불할 수 없습니다.");
}
this.balance += amount;
}
}