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.lang3.builder.ToStringBuilder;
23 import org.apache.commons.lang3.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 public Account(String anOwnerName) {
49 _id = ++_SEQUENCE;
50 _ownerName = anOwnerName;
51 _isActive = true;
52 _balance = 0.0d;
53 _transactions = new ArrayList<AccountTransaction>();
54 _createdBy = "unknown";
55 _creationDate = new Date();
56 }
57
58
59
60
61
62
63 public long getId() {
64 return _id;
65 }
66
67
68
69
70
71
72 public String getOwnerName() {
73 return _ownerName;
74 }
75
76
77
78
79
80
81 public boolean isActive() {
82 return _isActive;
83 }
84
85
86
87
88
89
90 public void setActive(boolean aIsActive) {
91 _isActive = aIsActive;
92 }
93
94
95
96
97
98
99 public void setOwnerName(String aOwnerName) {
100 _ownerName = aOwnerName;
101 }
102
103
104
105
106
107
108 public double getBalance() {
109 return _balance;
110 }
111
112
113
114
115
116
117 public List<AccountTransaction> getTransactions() {
118 return _transactions;
119 }
120
121 protected void applyTransaction(AccountTransaction aTransaction) throws NotEnoughFundsException, InactiveAccountException {
122 if (!_isActive) {
123 throw new InactiveAccountException("Unable to apply " + aTransaction.getType() + " of amount " + aTransaction.getAmount() + " to account " + _id);
124 }
125
126 synchronized (_transactions) {
127 if (AccountTransaction.TransactionType.DEPOSIT == aTransaction.getType()) {
128 _transactions.add(aTransaction);
129 _balance += aTransaction.getAmount();
130
131 } else if (AccountTransaction.TransactionType.WITHDRAWAL == aTransaction.getType()) {
132 if (_balance < aTransaction.getAmount()) {
133 throw new NotEnoughFundsException("Unable to withdraw " + aTransaction.getAmount() + "$ from account " + _id + " - current balance is " + _balance);
134 }
135 _transactions.add(aTransaction);
136 _balance -= aTransaction.getAmount();
137
138 } else {
139 throw new IllegalArgumentException("The transaction passed in has an invalid type: " + aTransaction.getType());
140 }
141 }
142 }
143
144
145
146
147
148
149 protected void setCreatedBy(String aCreatedBy) {
150 _createdBy = aCreatedBy;
151 }
152
153
154
155
156
157
158 public String getCreatedBy() {
159 return _createdBy;
160 }
161
162
163
164
165
166
167 public Date getCreationDate() {
168 return _creationDate;
169 }
170
171
172
173
174
175 public String toString() {
176 return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).
177 append("id", _id).
178 append("ownerName", _ownerName).
179 append("isActive", _isActive).
180 append("balance", _balance).
181 append("tx.count", _transactions.size()).
182 append("createdBy", _createdBy).
183 append("creationDate", new Timestamp(_creationDate.getTime())).
184 toString();
185 }
186 }