This repository has been archived by the owner on Oct 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBank.java
192 lines (163 loc) · 4.91 KB
/
Bank.java
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
import javax.swing.JOptionPane;
import java.util.Scanner;
public class Bank {
public static void main(String [] args)
{
BankAccount [] accountArray = new BankAccount[1];
Object[] options =
{"Add a new account",
"Deposit to an account",
"Withdraw from an account",
"Sort all and print report",
"Search for a Bank Account",
"Exit"
};
int answer;
int index;
do
{
answer = JOptionPane.showOptionDialog(null,
"What would you like to do?",
"Bank Account", JOptionPane.DEFAULT_OPTION,
JOptionPane.QUESTION_MESSAGE, null, options, options[5]);
if (answer == 0) //add a new account
{
index = 0;
String stud = JOptionPane.showInputDialog(null, "Enter account number, balance, and name");
Scanner keyboard = new Scanner(stud);
String number = keyboard.next();
double balance = keyboard.nextDouble();
String name = keyboard.next();
if (BankAccount.getNumberOfAccounts() >= accountArray.length)
{
BankAccount [] secondArray = new BankAccount[(accountArray.length) * 2];
for (int i = 0; i < accountArray.length; i++)
{
secondArray[i] = accountArray[i];
}
accountArray = secondArray;
}
if ((number.equals("") || (name.equals(""))))
{
System.out.println("Error: Account number and name must be entered.");
}
else if (balance > 0.0)
{
BankAccount newAccount = new BankAccount(number, balance, name);
accountArray[index] = newAccount;
}
else if (balance <= 0.0)
{
BankAccount newAccount = new BankAccount(number, name);
accountArray[index] = newAccount;
}
index++;
}
if (answer == 1) //deposit to an amount
{
String acct = JOptionPane.showInputDialog("Please enter account number and deposit amount");
Scanner keyboard = new Scanner(acct);
String searchNumber1 = keyboard.next();
double depositAmount = keyboard.nextDouble();
double arraySlot = -1;
int actualSize = 0;
for (int i = 0; i < accountArray.length - 1; i++)
{
if (accountArray[i] == null)
{
actualSize = i;
}
}
for (int i = 0; i < actualSize; i++)
{
if (accountArray[i].getAccountNumber().equals(searchNumber1)){
accountArray[i].deposit(depositAmount);
arraySlot = i;}
}
if (arraySlot < 0)
{
System.out.println("Error: There is no account with this number in our records");
}
}
if (answer == 2) // withdraw from an amount
{
String acct = JOptionPane.showInputDialog("Please enter account number and withdraw amount");
Scanner keyboard = new Scanner(acct);
String searchNumber2 = keyboard.next();
double withdrawAmount = keyboard.nextDouble();
double arraySlot = -1;
int actualSize = 0;
for (int i = 0; i < accountArray.length - 1; i++)
{
if (accountArray[i] == null)
{
actualSize = i;
}
}
for (int i = 0; i < actualSize; i++)
{
if (accountArray[i].getAccountNumber().equals(searchNumber2))
accountArray[i].withdraw(withdrawAmount);
arraySlot = i;
}
if (arraySlot < 0)
{
System.out.println("Error: There is no account with this number in our records");
}
}
if (answer == 3) // sort all based on balance in descending, and print report of all accounts as well as total # of accts and total and average balance
{
sortArray (accountArray);
}
if (answer == 4) // search for a bank account
{
String name = JOptionPane.showInputDialog("Please enter name");
int actualSize = 0;
int count = 0;
for (int i = 0; i < accountArray.length - 1; i++)
{
if (accountArray[i] == null)
{
actualSize = i;
}
}
for(int i = 0; i < actualSize; i++){
String temp = accountArray[i].getAccountHolderName();
int arraySlot = temp.toLowerCase().indexOf(name.toLowerCase());
if(arraySlot != -1){
accountArray[i].printReport();
count++;
}
}
}
}
while (answer != 5);
}
public static void sortArray(BankAccount[] accountArray){
int total = 0;
int actualSize = 0;
for(int i = accountArray.length - 1; i >= 0; i--){
if(accountArray[i] == null){
actualSize = i;
}
}
for(int i = 0; i < actualSize - 1; i++){
int arraySlot = i;
for(int j = i; j < actualSize; j++){
if(accountArray[j].getBalance() > accountArray[arraySlot].getBalance()){
arraySlot = j;
}
}
BankAccount larger = accountArray[arraySlot];
accountArray[arraySlot] = accountArray[i];
accountArray[i] = larger;
}
for(int i = 0; i < actualSize; i++){
total += accountArray[i].getBalance();
accountArray[i].printReport();
}
double average = (double) total / BankAccount.getNumberOfAccounts();
System.out.println("Total balance is: " + total);
System.out.println("Average balance is: " + average);
}
}