1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.shiro.samples.aspectj.bank; |
20 | |
|
21 | |
|
22 | |
import org.apache.commons.lang.builder.ToStringBuilder; |
23 | |
import org.apache.commons.lang.builder.ToStringStyle; |
24 | |
|
25 | |
import java.sql.Timestamp; |
26 | |
import java.util.ArrayList; |
27 | |
import java.util.Date; |
28 | |
import java.util.List; |
29 | |
|
30 | |
public class Account { |
31 | |
|
32 | |
private static long _SEQUENCE; |
33 | |
|
34 | |
private long _id; |
35 | |
|
36 | |
private String _ownerName; |
37 | |
|
38 | |
private volatile boolean _isActive; |
39 | |
|
40 | |
private double _balance; |
41 | |
|
42 | |
private final List<AccountTransaction> _transactions; |
43 | |
|
44 | |
private String _createdBy; |
45 | |
|
46 | |
private Date _creationDate; |
47 | |
|
48 | 24 | public Account(String anOwnerName) { |
49 | 24 | _id = ++_SEQUENCE; |
50 | 24 | _ownerName = anOwnerName; |
51 | 24 | _isActive = true; |
52 | 24 | _balance = 0.0d; |
53 | 24 | _transactions = new ArrayList<AccountTransaction>(); |
54 | 24 | _createdBy = "unknown"; |
55 | 24 | _creationDate = new Date(); |
56 | 24 | } |
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
public long getId() { |
64 | 88 | return _id; |
65 | |
} |
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
public String getOwnerName() { |
73 | 66 | return _ownerName; |
74 | |
} |
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
public boolean isActive() { |
82 | 74 | return _isActive; |
83 | |
} |
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
public void setActive(boolean aIsActive) { |
91 | 6 | _isActive = aIsActive; |
92 | 6 | } |
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
public void setOwnerName(String aOwnerName) { |
100 | 0 | _ownerName = aOwnerName; |
101 | 0 | } |
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
public double getBalance() { |
109 | 156 | return _balance; |
110 | |
} |
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
public List<AccountTransaction> getTransactions() { |
118 | 188 | return _transactions; |
119 | |
} |
120 | |
|
121 | |
protected void applyTransaction(AccountTransaction aTransaction) throws NotEnoughFundsException, InactiveAccountException { |
122 | 38 | if (!_isActive) { |
123 | 0 | throw new InactiveAccountException("Unable to apply " + aTransaction.getType() + " of amount " + aTransaction.getAmount() + " to account " + _id); |
124 | |
} |
125 | |
|
126 | 72 | synchronized (_transactions) { |
127 | 38 | if (AccountTransaction.TransactionType.DEPOSIT == aTransaction.getType()) { |
128 | 18 | _transactions.add(aTransaction); |
129 | 18 | _balance += aTransaction.getAmount(); |
130 | |
|
131 | 18 | } else if (AccountTransaction.TransactionType.WITHDRAWAL == aTransaction.getType()) { |
132 | 20 | if (_balance < aTransaction.getAmount()) { |
133 | 4 | throw new NotEnoughFundsException("Unable to withdraw " + aTransaction.getAmount() + "$ from account " + _id + " - current balance is " + _balance); |
134 | |
} |
135 | 16 | _transactions.add(aTransaction); |
136 | 16 | _balance -= aTransaction.getAmount(); |
137 | |
|
138 | 16 | } else { |
139 | 0 | throw new IllegalArgumentException("The transaction passed in has an invalid type: " + aTransaction.getType()); |
140 | |
} |
141 | |
} |
142 | 34 | } |
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
protected void setCreatedBy(String aCreatedBy) { |
150 | 24 | _createdBy = aCreatedBy; |
151 | 24 | } |
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
public String getCreatedBy() { |
159 | 0 | return _createdBy; |
160 | |
} |
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
public Date getCreationDate() { |
168 | 0 | return _creationDate; |
169 | |
} |
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
public String toString() { |
176 | 768 | return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE). |
177 | 384 | append("id", _id). |
178 | 384 | append("ownerName", _ownerName). |
179 | 384 | append("isActive", _isActive). |
180 | 384 | append("balance", _balance). |
181 | 384 | append("tx.count", _transactions.size()). |
182 | 384 | append("createdBy", _createdBy). |
183 | 384 | append("creationDate", new Timestamp(_creationDate.getTime())). |
184 | 384 | toString(); |
185 | |
} |
186 | |
} |