-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAdmin.java
251 lines (222 loc) · 6.54 KB
/
Admin.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
//Admin Java
import java.io.*;
import java.util.*;
public class Admin /*extends Users */implements Serializable
{
static final String PATH = "/home/pi/JavaPrograms/BankOnIt/Accounts.txt";
public static void main(String args[])
{
File file = new File("Accounts.dat");
Scanner input = new Scanner(System.in);
Users user = new Users();
ArrayList<Users> arrayOfUsers = new ArrayList<Users>();
Admin newAdmin = new Admin();
boolean condition = true;
int selection = 0;
while(condition)
{
System.out.println("Welcome, enter a selection:");
System.out.println("1) Add user");
System.out.println("2) Delete user");
System.out.println("3) List All Users");
System.out.println("4) Apply Interest");
System.out.println("5) Exit");
// System.out.println("Current Number of Users is: "+ newAdmin.getNOU());
selection = input.nextInt();
switch (selection)
{
case 1:
newAdmin.addUser();
break;
case 2:
newAdmin.deleteUser();
break;
case 3:
newAdmin.listUsers();
break;
case 4:
newAdmin.applyInterest();
break;
case 5:
condition = false;
break;
}
}
}
protected int numberOfUsers;
//protected ArrayList<Users> arrayOfUsers = new ArrayList<Users>();
int getNOU()
{
return numberOfUsers;
}
public void addUser()
{
File file = new File("Accounts.dat");
Scanner input = new Scanner(System.in);
Users newUser = new Users();
ArrayList<Users> arrayOfUsers = new ArrayList<Users>();
System.out.println("Please enter a 5-digit account number: ");
String accountNumberStr = input.nextLine();
//countNUmberInt = Integer.parseInt();
newUser.setAccountNumber(accountNumberStr);
System.out.println("Please now enter a 5-digit pin for the account: ");
String PINStr = input.nextLine();
newUser.setPIN(PINStr);
try{
if(!file.exists())
{
file.createNewFile();
FileOutputStream oFile = new FileOutputStream(file);
ObjectOutputStream oObject = new ObjectOutputStream(oFile);
oObject.writeObject(arrayOfUsers);
oObject.close();
oFile.close();
}
FileInputStream iFile = new FileInputStream(file);
ObjectInputStream iObject = new ObjectInputStream(iFile);
arrayOfUsers = (ArrayList<Users>)iObject.readObject();
arrayOfUsers.add(newUser);
FileOutputStream oFile = new FileOutputStream(file);
ObjectOutputStream oObject = new ObjectOutputStream(oFile);
oObject.writeObject(arrayOfUsers);
oObject.close();
oFile.close();
iFile.close();
iObject.close();
}
catch(FileNotFoundException fnfe)
{
System.out.println("File not found fnfe");
}
catch(IOException ioe)
{
System.out.println("File not found ioe");
}
catch(ClassNotFoundException cnfe)
{
System.out.println("Class not found");
}
}
public void deleteUser()
{
File file = new File("Accounts.dat");
ArrayList<Users> arrayOfUsers = new ArrayList<Users>();
Users currentUser = new Users();
Scanner input = new Scanner(System.in);
System.out.println("Enter the Account Number that you would like to delete:");
try{
int accountNumberforDeletion = input.nextInt();
FileInputStream iFile = new FileInputStream(file);
ObjectInputStream iObject = new ObjectInputStream(iFile);
arrayOfUsers = (ArrayList<Users>) iObject.readObject();
boolean condition = true;
int i = 0;
do
{
if(i == arrayOfUsers.size())
{
System.out.println("That account number does not exist");
condition = false;
}else
{
currentUser = arrayOfUsers.get(i);
if(currentUser.getAccountNumber() == accountNumberforDeletion && currentUser.checkCheckingsBalance() == 0.00 && currentUser.checkSavingsBalance() == 0.00)
{
arrayOfUsers.remove(i);
System.out.println("Account "+accountNumberforDeletion +" was succesfully removed from the system");
condition = false;
}
else
{
System.out.println("The account "+accountNumberforDeletion+" still has funds and can't be erased");
}
}
i++;
}
while(condition);
FileOutputStream oFile = new FileOutputStream(file);
ObjectOutputStream oObject = new ObjectOutputStream(oFile);
oObject.writeObject(arrayOfUsers);
iObject.close();
iFile.close();
oObject.close();
oFile.close();
}
catch(IOException ioe){System.out.println("IO Exception");}
catch(ClassNotFoundException ioe){System.out.println("Class not found exception Exception");}
//numberOfUsers--;
}
public void listUsers()
{
ArrayList<Users> arrayOfUsers = new ArrayList<Users>();
Users currentUser = new Users();
//Scanner input = new Scanner(System.in);
System.out.println("These are the existing accounts: ");
try{
//int accountNumberforDeletion = input.nextInt();
FileInputStream iFile = new FileInputStream("Accounts.dat");
ObjectInputStream iObject = new ObjectInputStream(iFile);
arrayOfUsers = (ArrayList<Users>) iObject.readObject();
//System.out.println("These are the current accounts: ");
System.out.println("Current number of accounts: "+arrayOfUsers.size());
boolean condition = true;
int i = 0;
do
{
if(i == arrayOfUsers.size())
{
condition = false;
}else
{
currentUser = arrayOfUsers.get(i);
System.out.println("\n"+currentUser.getAccountNumber());
i++;
}
}while(condition);
iObject.close();
iFile.close();
}
catch(IOException ioe){System.out.println("IO Exception");}
catch(ClassNotFoundException ioe){System.out.println("IO Exception");}
}
public void applyInterest()
{
ArrayList<Users> arrayOfUsers = new ArrayList<>();
Users user = new Users();
File file = new File("Accounts.dat");
try
{
if(!file.exists())
{
file.createNewFile();
}
FileInputStream iFile = new FileInputStream(file);
ObjectInputStream iObject = new ObjectInputStream(iFile);
arrayOfUsers = (ArrayList) iObject.readObject();
if(arrayOfUsers.size() != 0)
{
for(int i = 0;i < arrayOfUsers.size();i++)
{
user = arrayOfUsers.get(i);
System.out.println(user.checkSavingsBalance());
user.makeSavingsDeposit(user.calculateInterest(user.checkSavingsBalance()));
System.out.println(user.checkSavingsBalance());
}//end of for loop
FileOutputStream oFile = new FileOutputStream(file);
ObjectOutputStream oObject = new ObjectOutputStream(oFile);
oObject.writeObject(arrayOfUsers);
iObject.close();
iFile.close();
oObject.close();
oFile.close();
System.out.println("Interest has been applied to all accounts!");
}
else
{
System.out.println("There are no accounts stored");
}
}
catch(IOException ioe){System.out.println("Input not found");}
catch(ClassNotFoundException cnfe){System.out.println("Class not found");}
}
}//class end