-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBank.java
More file actions
101 lines (89 loc) · 2.62 KB
/
Bank.java
File metadata and controls
101 lines (89 loc) · 2.62 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
import java.util.ArrayList;
public class Bank
{
ArrayList<BankAccount> list = new ArrayList<>();
// helper method to find the index of a specified account
private int search(String accountNumberIn)
{
for(int i = 0; i <= list.size() - 1; i++)
{
BankAccount tempAccount = list.get(i); // find the account at index i
String tempNumber = tempAccount.getAccountNumber(); // get account number
if(tempNumber.equals(accountNumberIn)) // if this is the account we are looking for
{
return i; // return the index
}
}
return -999;
}
// return the total number of items
public int getTotal()
{
return list.size();
}
// return an account with a particular account number
public BankAccount getItem(String accountNumberIn)
{
int index = search(accountNumberIn);
if(index != -999) // check that account exists
{
return list.get(index);
}
else
{
return null; // no such account
}
}
// add an item to the list
public boolean addAccount(String accountNumberIn, String nameIn)
{
if(search(accountNumberIn) == -999) // check that account does not already exist
{
list.add(new BankAccount(accountNumberIn, nameIn)); // add new account
return true;
}
return false;
}
// deposit money in a specified account
public boolean depositMoney(String accountNumberIn, double amountIn)
{
BankAccount acc = getItem(accountNumberIn);
if(acc != null)
{
acc.deposit(amountIn);
return true; // indicate success
}
else
{
return false; // indicate failure
}
}
// withdraw money from a specified account
public boolean withdrawMoney(String accountNumberIn, double amountIn)
{
BankAccount acc = getItem(accountNumberIn);
if(acc != null && acc.getBalance() >= amountIn)
{
acc.withdraw(amountIn);
return true; // indicate success
}
else
{
return false; // indicate failure
}
}
// remove an account
public boolean removeAccount(String accountNumberIn)
{
int index = search(accountNumberIn); // find index of account
if(index != -999) // if account exists account
{
list.remove(index);
return true; // remove was successful
}
else
{
return false; // remove was unsuccessful
}
}
}