-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMoney.java
More file actions
138 lines (105 loc) · 3.69 KB
/
Money.java
File metadata and controls
138 lines (105 loc) · 3.69 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import java.io.Serializable;
import java.math.BigDecimal;
import org.apache.commons.lang.ObjectUtils;
import com.google.common.base.Objects;
/**
* 通用的金额类
*
* 不可变类 ThreadSafe
*
* @author liyebing created on 15/9/2.
* @version $Id$
*/
public class Money implements Serializable, Comparable<Money> {
private static final long serialVersionUID = 8902774465289570464L;
private static final int DEF_SCALE = 2;
/** 为何这里不用BigDecimal,因为Mdies不支持BigDecimal的序列化与反序列化,坑!! */
private float money;
private Money() {
money = BigDecimal.ZERO.floatValue();
}
private Money(float money) {
this.money = money;
}
public static Money create() {
return new Money();
}
public static Money create(String amount) {
return new Money(new BigDecimal(amount).floatValue());
}
public static Money create(float amount) {
return new Money(amount);
}
public static Money create(BigDecimal money) {
return new Money(money.floatValue());
}
public static Money create(Money money) {
return new Money(money.getMoney().floatValue());
}
public Money plus(BigDecimal money) {
float amount = (new BigDecimal(Float.toString(this.money)).add(money)).floatValue();
return Money.create(amount);
}
public Money subtract(BigDecimal money) {
float amount = (new BigDecimal(Float.toString(this.money)).subtract(money)).floatValue();
return Money.create(amount);
}
public Money plus(Money money) {
float amount = (new BigDecimal(Float.toString(this.money)).add(money.getMoney())).floatValue();
return Money.create(amount);
}
public Money subtract(Money money) {
float amount = (new BigDecimal(Float.toString(this.money)).subtract(money.getMoney())).floatValue();
return Money.create(amount);
}
public Money divide(Money money) {
BigDecimal b2 = money.getMoney();
float amount= Money.create(Float.toString(getMoney().divide(b2, DEF_SCALE, BigDecimal.ROUND_HALF_UP).floatValue())).getMoney().floatValue();
return Money.create(amount);
}
public Money multi(Money money) {
float amount = Money.create(Float.toString(this.getMoney().multiply(money.getMoney()).setScale(DEF_SCALE, BigDecimal.ROUND_HALF_UP).floatValue()))
.getMoney().floatValue();
return Money.create(amount);
}
public BigDecimal getMoney() {
String amount = Float.toString(money);
return new BigDecimal(amount);
}
public float fetchMoneyFloatValue() {
return money;
}
public boolean same(Money money) {
return compareTo(money) == 0;
}
public boolean lessThan(Money o) {
return compareTo(o) < 0;
}
public boolean moreThan(Money o) {
return compareTo(o) > 0;
}
public int compareTo(Money o) {
return new BigDecimal(Float.toString(this.money)).compareTo(o.getMoney());
}
public static void main(String[] args) {
Money plus= Money.create("10.5").plus(Money.create("0.02"));
Money sub= plus.subtract(Money.create("0.5"));
System.out.println(plus.getMoney());
System.out.println(sub.getMoney());
}
@Override
public boolean equals(Object o) {
if (o == null || !(o instanceof Money)) {
return false;
}
Money that = (Money) o;
return this.getMoney().compareTo(that.getMoney()) == 0;
}
@Override
public int hashCode() {
return Objects.hashCode(this.getMoney());
}
public String toString() {
return ObjectUtils.toString(getMoney());
}
}