-
Notifications
You must be signed in to change notification settings - Fork 0
/
Methods.java
463 lines (405 loc) · 12.1 KB
/
Methods.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
import java.util.*;
import java.io.*;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
/*
Main Method of the system. Which controls everything.
By :- Bernabas Tekklign;
Gitub User Name:- ----- "Barnaan2"
Email Address ------ "BarnaanTekalign@gmail.com"
" CopyRight--- feel free to use it for any purpose "
*/
public class Methods {
static int[] idNumbers;
// generate file that object will be seralized to;
static void generateId( int identification,int type){
Random Id = new Random();
int number =1 + Id.nextInt(1000000);
checkId(number,identification,type);
}
// check uniqueness of the file name generated and add tags
static void checkId(int idNumber,int idetification, int type){
String fileName = "Null.txt";
String Type = "";
if(type == 1){
Type += "A";
fileName = "Admin.txt";
}
else if(type == 2){
Type += "B";
fileName = "Banker.txt";
}
else if(type == 3){
Type += "C";
fileName = "Customer.txt";
}
else{
System.out.println("the type is not correct.");
}
try{
String Id = Type + idNumber;
BufferedReader ids = new BufferedReader(new FileReader(fileName));
String data = "";
while((data = ids.readLine()) != null){
if(data == Id){
generateId(type,idetification);
}
}
ids.close();
saveId(Id,fileName,idetification);
}
catch(Exception e){
System.out.println("there is an error within your code");
System.out.println(e.getMessage());
}
}
// will save the generated file name and create relation
static void saveId(String Id, String fileName,int idetification){
try{
PrintWriter addId = new PrintWriter(new FileOutputStream(fileName,true));
addId.println(Id);
addId.close();
}
catch(Exception e){
System.out.println("there is an error within your code");
System.out.println(e.getMessage());
}
String Filename = fileNamer(Id);
setRelation(idetification,Filename);
}
// create text file that the object will be seralized on;
static String fileNamer(String Id){
String filename = Id + ".txt";
return filename;
}
//Serialize any class type of object
public static <V> void seralize(String fileName, V object){
try{ File files = new File(fileName);
FileOutputStream os = new FileOutputStream(files);
ObjectOutputStream dos = new ObjectOutputStream(os);
dos.writeObject(object);
dos.close();
}
catch(Exception e){
System.out.println(e.getMessage());
}
}
//create file to save the relation of ID and File name
void create_Relation(){
try{
String filename = "Relation.txt";
File files = new File(filename);
FileOutputStream hash = new FileOutputStream(files);
ObjectOutputStream hashmap = new ObjectOutputStream(hash);
HashMap<Integer,String> data = new HashMap<Integer,String>();
//one key can only have one value and can exist only once;
data.put(000000,"me");
hashmap.writeObject(data);
hashmap.close();
}
catch(Exception e){
System.out.println(e.getMessage());
}
}
// set relation of Id and File name
static void setRelation(Integer identifaction, String fileName){
String filename = "Relation.txt";
HashMap<Integer,String> data;
//valueOf();
data = (HashMap<Integer,String>) deserialize(filename);
data.put(identifaction,fileName);
seralize(filename,data);
}
// deseralize hashmap object to help us search for our objects
@SuppressWarnings({ "unchecked" })
public static HashMap<Integer,String> deserialize(String fileName){
HashMap<Integer,String> object = new HashMap<Integer,String>();
try{ File files = new File(fileName);
FileInputStream output;
output = new FileInputStream(files);
ObjectInputStream input;
input = new ObjectInputStream(output);
object = (HashMap<Integer,String>) input.readObject();
input.close();
}
catch(Exception e) {
System.out.println(e.getMessage()); }
return object; }
// it return file name for user we passed in
static String getFileName(Integer identifaction){
String filename = "Relation.txt";
HashMap<Integer,String> data;
data = (HashMap<Integer,String>) deserialize(filename);
String fileName =data.get(identifaction);
return fileName;
}
// this delete the relation
static void delete(Integer identifaction){
String filename = "Relation.txt";
HashMap<Integer,String> data;
data = (HashMap<Integer,String>) deserialize(filename);
data.remove(identifaction);
data.keySet();
seralize(filename,data);
System.out.println("the operation is done");
}
// generate account number for the customer
static int accountNumberGenerator(){
int accountNumber = 10000;
try{ String fileName = "accountNumber.txt";
BufferedReader ids = new BufferedReader(new FileReader(fileName));
int numberOfAccountNumber = 0;
while(ids.readLine() != null){
numberOfAccountNumber +=1;
}
accountNumber += numberOfAccountNumber;
ids.close();
PrintWriter account = new PrintWriter(new FileOutputStream(fileName,true));
account.println(accountNumber);
account.close();
}
catch(Exception e){
System.out.println(e.getMessage());
}
return accountNumber;
}
//generate ID number for admin users
static int AdminIdGenerate() {
int Idnumber = 20000;
try{
String fileName = "AdminId.txt";
BufferedReader ids = new BufferedReader(new FileReader(fileName));
int Admins = 0;
while(ids.readLine() != null){
Admins +=1;
}
Idnumber += Admins;
ids.close();
PrintWriter account = new PrintWriter(new FileOutputStream(fileName,true));
account.println(Idnumber);
account.close();
}
catch(Exception e){
System.out.println(e.getMessage());
}
return Idnumber;
}
//generate Id number for employee users
static int EmpIdGenerate(){
int Idnumber = 30000;
try{
String fileName = "EmployeeId.txt";
BufferedReader ids = new BufferedReader(new FileReader(fileName));
int numberOfAccountNumber = 0;
while( ids.readLine() != null){
numberOfAccountNumber +=1;}
Idnumber += numberOfAccountNumber;
ids.close();
PrintWriter account = new PrintWriter(new FileOutputStream(fileName,true));
account.println(Idnumber);
account.close(); }
catch(Exception e){
System.out.println(e.getMessage());}
return Idnumber;
}
// uses to create file in case we had an error related it
public void create_files(String Filename)
{
try {
File file = new File(Filename);
file.createNewFile(); }
catch(Exception e) {
System.out.println(e.getMessage()); }
}
// date for input
public static String date() {
LocalDate date = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
String dateNow = date.format(formatter);
return dateNow;
}
// create array of object that will store our data
public static void createObjectArray(int type,int number) {
Scanner input = new Scanner(System.in);
int num = number;
switch(type) {
case 1:
Admin[] obj = new Admin[num];
createObject(obj,number);
takeInput(obj,number,input);
break;
case 2:
Banker[] banker = new Banker[num];
// banker array that have length of 4 holds five elements
createObject(banker,number);
takeInput(banker,number,input);
break;
case 3:
Customer[] customer = new Customer[num];
createObject(customer,number);
takeInput(customer,number,input);
break;
default:
System.out.println("there is an error with your code");
break;
}
}
//create customer object
public static void createObject(Customer[] obj, int index){
int type = 3;
int num =index;
idNumbers = new int[num];
for(int i = 0; i<index; i++)
{
obj[i] = new Customer();
int Acc_no =accountNumberGenerator();
idNumbers[i]=Acc_no;
generateId(Acc_no, type);
}}
//create Admin object
public static void createObject(Admin[] obj, int index){
int type = 1;
int num =index;
idNumbers = new int[num];
for(int i = 0 ; i<index; i++)
{
obj[i] = new Admin();
int AdmId = AdminIdGenerate();
idNumbers[i]=AdmId;
generateId( AdmId, type);
}}
//create Employee object
public static void createObject(Banker[] obj, int index){
int type = 2;
int num =index;
idNumbers = new int[num];
for(int i = 0; i<index; i++)
{
obj[i] = new Banker();
int empId = EmpIdGenerate();
idNumbers[i]=empId;
generateId( empId, type);
}
}
//take input form users to set our fields
public static void takeInput(Admin[] user,int index,Scanner input)
{
System.out.println("Please fillout the Information");
for(int i = 0; i<index; i++){
System.out.print("Name: ");
String name = input.nextLine();
user[i].set_name(name);
int id = idNumbers[i];
user[i].set_id(id);
// here i need to set id number and for that i need that array;
System.out.println("Age:");
int age = input.nextInt();
user[i].set_age(age);
input.nextLine();
System.out.println("Sex:");
String gender = input.nextLine();
user[i].set_gender(gender);
System.out.println("date set by the system");
user[i].set_date_of_regstration(date());
System.out.println("Branch: ");
String Branch = input.nextLine();
user[i].set_branch(Branch);
System.out.println("password: ");
String password = input.nextLine();
user[i].set_password(password);
if(i < index-1){
System.out.println("insert the Next Admin");
}
String filename = getFileName(id);
seralize(filename,user[i]);
}}
// take input to store
public static void takeInput(Banker[] user,int index, Scanner input)
{
System.out.println("Please fillout the Information");
for(int i = 0; i<index; i++){
System.out.print("Name: ");
String name = input.nextLine();
user[i].set_name(name);
int id = idNumbers[i];
user[i].set_id(id);
// here i need to set id number and for that i need that array;
System.out.println("Age:");
int age = input.nextInt();
user[i].set_age(age);
input.nextLine();
System.out.println("Sex:");
String gender = input.nextLine();
user[i].set_gender(gender);
System.out.print("Enter Qualification :");
String qual = input.nextLine();
user[i].set_qualification(qual);
System.out.print("Enter Position :");
String position = input.nextLine();
user[i].set_position(position);
System.out.println("Date is set by the system");
user[i].set_date_of_regstration(date());
System.out.println("Branch: ");
String Branch = input.nextLine();
user[i].set_branch(Branch);
System.out.println("Salary: ");
double salary = input.nextDouble();
user[i].set_salary(salary);
input.nextLine();
System.out.println("password: ");
String password = input.nextLine();
user[i].set_password(password);
if(i <index-1){
System.out.println("insert the Next Employee");}
String filename = getFileName(id);
seralize(filename,user[i]);
}
}
//take input to store
public static void takeInput(Customer[] user,int index, Scanner input)
{
System.out.println("Please fillout the Information");
for(int i = 0; i<index; i++){
System.out.print("Name: ");
String name = input.nextLine();
user[i].set_name(name);
int id = idNumbers[i];
user[i].set_id(id);
// here i need to set id number and for that i need that array;
System.out.println("Age:");
int age = input.nextInt();
user[i].set_age(age);
input.nextLine();
System.out.println("Sex:");
String gender = input.nextLine();
user[i].set_gender(gender);
System.out.println("date is set by the system");
user[i].set_date_of_regstration(date());
System.out.println("Branch: ");
String Branch = input.nextLine();
user[i].set_branch(Branch);
System.out.println("Balance :");
double balance = input.nextDouble();
user[i].set_balance(balance);
input.nextLine();
System.out.println("your Id number: ");
int creator = input.nextInt();
user[i].set_account_creator(creator);
if(i <index-1){
System.out.println("insert the Next Customer");
}
String filename = getFileName(id);
seralize(filename,user[i]);
}
}
public static void printIdnumbers() {
System.out.println("The account numbers / id numbers are :");
for(int i:idNumbers) {
System.out.println(i);
}
}
public static <V>void saveChanges(int id, V object ) {
String filename = getFileName(id);
seralize(filename,object);
}
}