@@ -9,7 +9,7 @@ public class BankAccount {
99
1010 private BankAccountType accountType ;
1111 private int accountNumber ;
12- private static int nextAccoutNumber = 1 ;
12+ private static int nextAccountNumber = 1 ;
1313 private double balance ;
1414 private String accountHoldersName ;
1515 private double interestRate ;
@@ -20,7 +20,7 @@ public class BankAccount {
2020 public BankAccount (BankAccountType accountType , double balance , String accountHoldersName , double interestRate ,
2121 BankAccountStatus accountStatus , OverdraftProtection overdraftProtection ) {
2222 this .accountType = accountType ;
23- accountNumber = nextAccoutNumber ++;
23+ accountNumber = nextAccountNumber ++;
2424 this .balance = balance ;
2525 this .accountHoldersName = accountHoldersName ;
2626 this .interestRate = interestRate ;
@@ -31,30 +31,174 @@ public BankAccount(BankAccountType accountType, double balance, String accountHo
3131
3232 public Double getBalance () {
3333 if ( accountStatus .equals (BankAccountStatus .OFAC_FROZEN ) ) {
34- // do nothing probably throw an expection at some point
3534 return null ;
3635 }
3736 else {
3837 return balance ;
3938 }
4039 }
4140
41+ public String getAccountHoldersName () {
42+ return accountHoldersName ;
43+ }
44+
45+ public void setAccountHoldersName (String newName ) {
46+ if ( accountStatus .equals (BankAccountStatus .OPEN ) ) {
47+ accountHoldersName = newName ;
48+
49+ transactionRecord .add (new BankAccountTransaction (TransactionType .NAME_CHANGE , 0.0 ,
50+ accountStatus , this .accountHoldersName ));
51+
52+ return ;
53+ }
54+
55+ public BankAccountStatus getAccountStatus () {
56+ return accountStatus ;
57+ }
58+
59+ public void setAccountStatus (BankAccountStatus accountStatus ) {
60+ if ( isAccountOpenOrFrozen () ) {
61+ // check balance if trying to close
62+ if ( isNewAccountStatusClose (accountStatus ) ) {
63+ if ( this .balance == 0.0 ) {
64+ this .accountStatus = accountStatus ;
65+
66+ transactionRecord .add (new BankAccountTransaction (TransactionType .STATUS_CHANGE , 0.0 ,
67+ this .accountStatus , accountHoldersName ));
68+ }
69+ }
70+ else {
71+ this .accountStatus = accountStatus ;
72+
73+ transactionRecord .add (new BankAccountTransaction (TransactionType .STATUS_CHANGE , 0.0 ,
74+ this .accountStatus , accountHoldersName ));
75+ }
76+ }
77+
78+ return ;
79+ }
80+
81+ private boolean isAccountOpenOrFrozen () {
82+ return !accountStatus .equals (BankAccountStatus .CLOSED );
83+ }
84+
85+ private boolean isNewAccountStatusClose (BankAccountStatus newStatus ) {
86+ return newStatus .equals (BankAccountStatus .CLOSED );
87+ }
88+
4289 public ApprovalStatus updateBalanceWithCreditOrDebit (double amount ) {
90+
4391 if ( accountStatus .equals (BankAccountStatus .OPEN ) ) {
92+
93+ if ( overdraftProtection .equals (OverdraftProtection .ENABLED ) ) {
94+ if ( isTransactionAnOverDraft (amount ) ) {
95+ return ApprovalStatus .NOT_APPROVED ;
96+ }
97+ }
98+
4499 if ( amount > 0.0 ) {
45- credit (amount );
100+ return credit (amount );
101+ }
102+ else if ( amount < 0.0 ) {
103+ return debit (amount );
46104 }
47105 else {
48- debit (amount );
106+ return ApprovalStatus .ZERO_TRANSACTION ;
107+ }
108+ }
109+ else {
110+ return ApprovalStatus .CANT_COMPLETE_DUE_TO_ACCT_STATUS ;
111+ }
112+ }
113+
114+ private ApprovalStatus credit (double amountToCredit ) {
115+ Double previousBalance = getBalance ();
116+ balance += amountToCredit ;
117+
118+ // check if balance updated
119+ if ( (previousBalance + amountToCredit ) == getBalance () ) {
120+ // create new transaction record and add to list
121+ transactionRecord .add (new BankAccountTransaction (TransactionType .DEPOSIT ,
122+ amountToCredit , accountStatus , accountHoldersName ));
123+ return ApprovalStatus .APPROVED ;
124+ }
125+ else {
126+ return ApprovalStatus .NOT_APPROVED ;
127+ }
128+ }
129+
130+ private ApprovalStatus debit (double amountToDebit ) {
131+ Double previousBalance = getBalance ();
132+ balance += amountToDebit ;
133+
134+ if ( (previousBalance - amountToDebit ) == getBalance () ) {
135+ // create new transaction record
136+ transactionRecord .add (new BankAccountTransaction (TransactionType .WITHDRAWL , amountToDebit ,
137+ accountStatus , accountHoldersName ));
138+ return ApprovalStatus .APPROVED ;
139+ }
140+ else {
141+ return ApprovalStatus .NOT_APPROVED ;
142+ }
143+ }
144+
145+
146+ private boolean isTransactionAnOverDraft (double amount ) {
147+ return (Math .abs (amount ) > this .getBalance ());
148+ }
149+
150+ // refactor this and transferBalanceTo, gotta be some reapeat code
151+ public ApprovalStatus transferBalanceFrom (BankAccount transferFrom , double amountToTransfer ) {
152+ if ( bothAccountsHaveSameOwner (this , transferFrom ) ) {
153+
154+ if ( doesAccountHaveSufficientBalance (transferFrom , amountToTransfer ) ) {
155+ ApprovalStatus debitApproval = transferFrom .updateBalanceWithCreditOrDebit (-amountToTransfer );
156+ ApprovalStatus creditApproval = this .updateBalanceWithCreditOrDebit (amountToTransfer );
157+
158+ transactionRecord .add (new BankAccountTransaction (TransactionType .TRANSFER , amountToTransfer ,
159+ accountStatus , accountHoldersName ));
160+
161+ // pull out into check approvals method
162+ if ( (debitApproval .equals (ApprovalStatus .APPROVED ))
163+ && creditApproval .equals (ApprovalStatus .APPROVED ) ) {
164+ return ApprovalStatus .APPROVED ;
165+ }
49166 }
50167 }
168+
169+ return ApprovalStatus .NOT_APPROVED ;
51170 }
52171
53- private ApprovalStatus credit (double amount ) {
172+ public ApprovalStatus transferBalanceTo (BankAccount transferTo , double amountToTransfer ) {
173+ // check if both accts have the same owener
174+ if ( bothAccountsHaveSameOwner (this , transferTo ) ) {
175+
176+ // check if from has enough money
177+ if (doesAccountHaveSufficientBalance (this , amountToTransfer )) {
54178
179+ // do the transfer
180+ ApprovalStatus debitApproval = this .updateBalanceWithCreditOrDebit (-amountToTransfer );
181+ ApprovalStatus creditApproval = transferTo .updateBalanceWithCreditOrDebit (amountToTransfer );
182+
183+ transactionRecord .add (new BankAccountTransaction (TransactionType .TRANSFER , amountToTransfer ,
184+ accountStatus , accountHoldersName ));
185+
186+
187+ if ((debitApproval .equals (ApprovalStatus .APPROVED ))
188+ && creditApproval .equals (ApprovalStatus .APPROVED )) {
189+ return ApprovalStatus .APPROVED ;
190+ }
191+ }
192+ }
193+
194+ return ApprovalStatus .NOT_APPROVED ;
55195 }
56196
57- private ApprovalStatus debit (double amount ) {
197+ private boolean bothAccountsHaveSameOwner (BankAccount transferFrom , BankAccount transferTo ) {
198+ return transferFrom .getAccountHoldersName ().equals (transferTo .accountHoldersName );
199+ }
58200
201+ private boolean doesAccountHaveSufficientBalance (BankAccount acct , double amount ) {
202+ return acct .getBalance () >= amount ;
59203 }
60204}
0 commit comments