-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAdminR.java
242 lines (213 loc) · 6.61 KB
/
AdminR.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
//AdminR.java
import java.io.*;
import java.util.*;
public class AdminR
{
public static void main(String[] args)
{
ArrayList<Users> arrayOfUsers = new ArrayList<>();
File file = new File("Accounts.dat");
AdminR admin = new AdminR();
Scanner input = new Scanner(System.in);
try
{
//File file = new File("Accounts.dat");
if(!file.exists())
{
file.createNewFile();
FileOutputStream oFile = new FileOutputStream("Accounts.dat");
ObjectOutputStream oObject = new ObjectOutputStream(oFile);
oObject.writeObject(arrayOfUsers);
}
FileInputStream iFile = new FileInputStream(file);
ObjectInputStream iObject = new ObjectInputStream(iFile);
boolean selecting = true;
while(selecting)
{
System.out.println("Welcome to RETH Bank. What would you like to do today?");
System.out.println("1)Add User");
System.out.println("2)Delete User");
System.out.println("3)List all users");
System.out.println("4)exit");
int selection = input.nextInt();
switch (selection)
{
case 1:
admin.addUser();
break;
case 2:
admin.deleteUser();
break;
case 3:
admin.listAllUsers();
break;
case 4:
selecting = false;
}//end switch
}
}
catch(FileNotFoundException fnfe){System.out.println("File not found");}
catch(IOException ioe){System.out.println("Input not found");}
/*catch(FileNotFoundException fnfe){System.out.println("File not found");}*/
}//end of main
void addUser()
{
File file = new File("Accounts.dat");
Scanner input = new Scanner(System.in);
ArrayList<Users> arrayOfUsers = new ArrayList<Users>();
Users user = new Users();
System.out.println("Enter the account number: ");
try
{
String accountNumberEntered = input.nextLine();
if(!file.exists())
{
file.createNewFile();
FileOutputStream oFile = new FileOutputStream(file);
ObjectOutputStream oObject = new ObjectOutputStream(oFile);
oObject.writeObject(arrayOfUsers);
oObject.close();
oFile.close();
}//end if statement
FileInputStream iFile = new FileInputStream(file);
ObjectInputStream iObject = new ObjectInputStream(iFile);
user.setAccountNumber(accountNumberEntered);
arrayOfUsers.add(user);
FileOutputStream oFile = new FileOutputStream(file);
ObjectOutputStream oObject = new ObjectOutputStream(oFile);
oObject.writeObject(arrayOfUsers);
System.out.println("Account number "+accountNumberEntered+" successfully added");
}
catch(IOException ioe){System.out.println("Input not found");}
/*catch(FileNotFoundException fnfe){System.out.println("File not found");}*/
/*catch(ObjectNotFoundException onfe){System.out.println("Input not found");}*/
}//end add user
void deleteUser()
{
File file = new File("Accounts.dat");
ArrayList<Users> arrayOfUsers = new ArrayList<Users>();
Users user = new Users();
Scanner input = new Scanner(System.in);
System.out.println("Which user would you like to delete?");
try
{
String accountNumberForDeletion = input.nextLine();
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) iObject.readObject();
if(arrayOfUsers.size() != 0)
{
boolean accountNumberNotFound = true;
int counter = 0;
do
{
user = arrayOfUsers.get(counter);
if(accountNumberForDeletion.equals(user.getAccountNumber()))
{
arrayOfUsers.remove(counter);
FileOutputStream oFile = new FileOutputStream(file);
ObjectOutputStream oObject = new ObjectOutputStream(oFile);
oObject.writeObject(arrayOfUsers);
oObject.close();
oFile.close();
}//end of if
counter++;
}while(accountNumberNotFound);//end of do-while
iObject.close();
iFile.close();
}//end of if
else
{
System.out.println("There are no account numbers in existence yet.");
}//end of else
}//end of try
catch(IOException ioe){System.out.println("Input not found or given");}
catch(ClassNotFoundException cnfe){System.out.println("Class not found");}
}//end of deleteUser
void listAllUsers()
{
Scanner input = new Scanner(System.in);
ArrayList<Users> arrayOfUsers = new ArrayList<Users>();
Users user = new Users();
File file = new File("Accounts.dat");
try
{
FileInputStream iFile = new FileInputStream(file);
ObjectInputStream iObject = new ObjectInputStream(iFile);
arrayOfUsers = (ArrayList) iObject.readObject();
System.out.println("The current number of users is "+arrayOfUsers.size()+". This is the list:");
String stop = input.nextLine();
boolean accountNumbersInFile = true;
int counter = 1;
do
{
if(arrayOfUsers.size() != 0)
{
if(counter == arrayOfUsers.size())
{
accountNumbersInFile = false;
}else
{
user = arrayOfUsers.get(counter-1);
System.out.println(user.getAccountNumber());
}
}
counter++;
}while(accountNumbersInFile);
FileOutputStream oFile = new FileOutputStream(file);
ObjectOutputStream oObject = new ObjectOutputStream(oFile);
oObject.writeObject(arrayOfUsers);
}
catch(IOException ioe){System.out.println("Input not found");}
catch(ClassNotFoundException cnfe){System.out.println("Class not found");}
}//end of listAllUsers
void applyInterestToSavingsAccounts()
{
ArrayList<Users> arrayOfUsers = new ArrayList<>();
Users user = new Users();
File file = new File("Accounts.dat");
try
{
if(!file.exists())
{
file.createNewFile()
}
if(arrayOfUsers.size() != 0)
{
FileInputStream iFile = new FileInputStream(file);
ObjectInputStream iObject = new ObjectInputStream(iFile);
arrayOfUsers = (ArrayList) iObject.readObject();
for(int i = 0;i <= arrayOfUsers.size();i++)
{
user = arrayOfUsers.get(i);
System.out.println(user.getPrinciple());
user.calculateInterest(1);
System.out.println(user.getPrinciple());
}//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();
}
catch(IOException ioe){System.out.println("Input not found");}
catch(ClassNotFoundException cnfe){System.out.println("Class not found");}
System.out.println("Interest has been applied to all accounts!");
}
else
{
System.out.println("There are no accounts stored);
}
}//end of applyInterestToAllSavingsAccounts
}//end of class