View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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       * Returns the id attribute.
60       *
61       * @return The id value.
62       */
63      public long getId() {
64          return _id;
65      }
66  
67      /**
68       * Returns the ownerName attribute.
69       *
70       * @return The ownerName value.
71       */
72      public String getOwnerName() {
73          return _ownerName;
74      }
75  
76      /**
77       * Returns the isActive attribute.
78       *
79       * @return The isActive value.
80       */
81      public boolean isActive() {
82          return _isActive;
83      }
84  
85      /**
86       * Changes the value of the attributes isActive.
87       *
88       * @param aIsActive The new value of the isActive attribute.
89       */
90      public void setActive(boolean aIsActive) {
91          _isActive = aIsActive;
92      }
93  
94      /**
95       * Changes the value of the attributes ownerName.
96       *
97       * @param aOwnerName The new value of the ownerName attribute.
98       */
99      public void setOwnerName(String aOwnerName) {
100         _ownerName = aOwnerName;
101     }
102 
103     /**
104      * Returns the balance attribute.
105      *
106      * @return The balance value.
107      */
108     public double getBalance() {
109         return _balance;
110     }
111 
112     /**
113      * Returns the transactions attribute.
114      *
115      * @return The transactions value.
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      * Changes the value of the attributes createdBy.
146      *
147      * @param aCreatedBy The new value of the createdBy attribute.
148      */
149     protected void setCreatedBy(String aCreatedBy) {
150         _createdBy = aCreatedBy;
151     }
152 
153     /**
154      * Returns the createdBy attribute.
155      *
156      * @return The createdBy value.
157      */
158     public String getCreatedBy() {
159         return _createdBy;
160     }
161 
162     /**
163      * Returns the creationDate attribute.
164      *
165      * @return The creationDate value.
166      */
167     public Date getCreationDate() {
168         return _creationDate;
169     }
170 
171     /* (non-Javadoc)
172     * @see java.lang.Object#toString()
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 }