Repository navigation
Expand file tree
/
Copy pathAccount.java
More file actions
194 lines (176 loc) · 4.92 KB
/
Copy pathAccount.java
File metadata and controls
194 lines (176 loc) · 4.92 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/**
* represents a bank account
*
*/
public class Account
{
// the customer
private Customer customer;
// the account number
private String accountNumber;
// the account balance
private double balance;
// the maximum number of transactions
private final int MAX_TRANSACTIONS = 10;
// array to hold transactions
private Transaction[] transactions;
// the number of Transactions in the array
private int numberOfTransactions;
/**
* assigns values of instance variables
* @param customer is an object reference to the class Customer
* @param accountNumber is the customer's account number
*/
public Account(Customer customer, String accountNumber)
{
transactions = new Transaction[MAX_TRANSACTIONS];
numberOfTransactions = 0;
this.customer = customer;
this.accountNumber = accountNumber;
balance = 0.0f;
}
/**
* returns the account number
* @return accountNumber returns the account number
*/
public String getAccountNumber()
{
return accountNumber;
}
/**
* returns the customer name
* @return FirstName and LastName returns the customerName
*/
public String getCustomerName()
{
return (customer.getFirstName() + " " + customer.getLastName());
}
/**
* returns the account balance
* @return balance
*/
public double getBalance()
{
return balance;
}
/**
* returns the number of transactions
* @return numberOfTransactions
*/
public int getNumberOfTransactions()
{
return numberOfTransactions;
}
/**
* prints details of the account and its current transactions
*/
public void displayTransactions()
{
System.out.format("Account: %s\nCustomer: %s\n",
accountNumber,this.getCustomerName());
for(int i=0;i<numberOfTransactions;i++)
{
Transaction trans = transactions[i];
System.out.format("%4.2f %s %s %s\n", trans.getAmount(),
trans.getType(),
trans.getReference(),
trans.getDateString());
}
}
/**
* adds a new Transaction object to the array of contained objects
* @param newTransaction - shows all the new transactions
*/
public void addTransaction(Transaction newTransaction)
{
if(numberOfTransactions < MAX_TRANSACTIONS)
{
transactions[numberOfTransactions] = newTransaction;
numberOfTransactions++;
}
updateBalance();
}
/**
* assigns the array to get transactions
* @return transactions
*/
public Transaction[] getTransactions()
{
return transactions;
}
/**
* gets the array of contained Transaction objects
* @param reference shows the reference
* @return target - exact transaction referenced
*/
public Transaction getTransaction(String reference)
{
Transaction target = null;
int i = 0;
while(i<numberOfTransactions)
{
if(transactions[i].getReference().equals(reference))
{
target = transactions[i];
break;
}
i++;
}
return target;
}
/**
* removes a contained Transaction object specified by its reference property.
* @param reference the values
*/
public void removeTransaction(String reference)
{
int i = 0;
boolean found = false;
while(i<numberOfTransactions)
{
if(transactions[i].getReference().equals(reference))
{
found = true;
break;
}
i++;
}
if(found)
{
for(int j=i;j<numberOfTransactions;j++)
{
transactions[j] = transactions[j+1];
}
transactions[numberOfTransactions]=null;
numberOfTransactions--;
}
updateBalance();
}
//updates a contained Balance
private void updateBalance()
{
double total = 0.0; // total value of all transactions
for(int i=0;i<numberOfTransactions;i++)
{
Transaction trans = transactions[i];
if (trans.getType().equals("CREDIT"))
{
total = total + trans.getAmount();
}
else if(trans.getType().equals("DEBIT"))
{
total = total - trans.getAmount();
}
}
this.balance = total; // assign total to account balance field
}
/**
* returns a string representation of this object
* @return accountNumber
*/
public String toString()
{
return String.format("Account number: %s, Customer: %s",
accountNumber, this.getCustomerName());
}
}