-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBankManger.java
More file actions
167 lines (151 loc) · 6.32 KB
/
Copy pathBankManger.java
File metadata and controls
167 lines (151 loc) · 6.32 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
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
import java.util.ArrayList;
import java.util.Random;
import java.io.*;
public class BankManger {
private ArrayList<String> accountNumbers = new ArrayList<>();
private String accountNumber;
private AccountManger[] customerData;
private int nextEmpty;
private AccountManger currentAccount;
private static final String DATA_FILE = "customerData.ser"; // File to store customer data
public BankManger(){
// AccountManger[] customerData is an array that stores the customers first name, lat name, password, and account Number
customerData = new AccountManger[20];
loadCustomerData();
}
public void addAccount(AccountManger account){
if(nextEmpty < customerData.length) {
customerData[nextEmpty] = account;
nextEmpty++;
}
//If we run out of space we have to resize our customer Data array
else{
customerData = arrayResize(customerData);
}
}
//When User is trying to log in Make sure the pin exits within the Data
public boolean searchCustomer(String enteredPin){
//Returns true if account is found and false if array is empty or customer isn't found
if(customerData[0] == null){
return false;
}
else{
for(int i =0; i< customerData.length; i++){
// if we reach the end of the customer data then pin was not found within database
if(customerData[i] == null){
return false;
}
String accountNum = customerData[i].getAccountNumber();
String pin = getAccountPin(accountNum);
if(pin.equals(enteredPin)){
setcurrentAccount(customerData[i]);
return true;
}
}
}
return false;
}
//Getter for account PIn
public String getAccountPin(String accountNumber){
String[] number = accountNumber.split(" ");
// last four digits of the account
return number[3];
}
//Return current Account
public void setcurrentAccount(AccountManger account){
this.currentAccount = account;
}
//get Current Account
public AccountManger getCurrentAccount(){
return currentAccount;
}
public double getBalance(){
return getCurrentAccount().getBalance();
}
public void setBalance(double amount){
getCurrentAccount().setBalance(amount);
}
public void withdrawal(double amount){
getCurrentAccount().withdraw(amount);
}
// Method to resize the customer data once it's full
public AccountManger[] arrayResize(AccountManger[] data){
// Double the previous size of the customer Data
AccountManger[] resizedCustomerData = new AccountManger[customerData.length*2];
System.arraycopy(data,0,resizedCustomerData,0,data.length);
return resizedCustomerData;
}
public String setAccountNumber(){
// If arraylist is empty, populate arraylist with new account numbers
if(accountNumbers.isEmpty()){
populateAccountNumbers();
//Once account is populated then assign the first available account number
accountNumber = accountNumbers.get(0);
accountNumbers.remove(0);
}
// Gets the first available accountNumber in ArrayList then
else{
accountNumber = accountNumbers.get(0);
accountNumbers.remove(0);
}
return accountNumber;
}
// Method to generate a random account number
public static String generateRandomAccountNumber() {
Random random = new Random();
StringBuilder accountNumber = new StringBuilder();
// Generate 16 random digits
for (int i = 0; i < 16; i++) {
int digit = random.nextInt(10); // Random number between 0-9
accountNumber.append(digit);
// Add a space after every 4 digits (except the last group)
if ((i + 1) % 4 == 0 && i < 15) {
accountNumber.append(" ");
}
}
return accountNumber.toString();
}
// Method to populate account numbers once we run out of random account numbers
public void populateAccountNumbers(){
// Add 10 new account Numbers
for (int i = 0; i < 10; i++) {
accountNumbers.add(generateRandomAccountNumber());
}
}
// Save customer data to a file
public void saveCustomerData() {
/*
A FileOutputStream is created to write data to a file
An ObjectOutputStream is wrapped around the FileOutputStream to write objects in a serialized/byte format.
*/
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(DATA_FILE))) {
//The writeObject() method of ObjectOutputStream serializes the customerData (an ArrayList of AccountManger objects) and saves it to the file.
oos.writeObject(customerData);
oos.writeInt(nextEmpty); // Save the nextEmpty index
} catch (IOException e) {
// If any issues arise during file writing (e.g., file not found or access denied), they are caught and logged.
System.err.println("Error saving customer data: " + e.getMessage());
}
}
@SuppressWarnings("unchecked")
public void loadCustomerData() {
File file = new File(DATA_FILE);
// checks if the file exists before attempting to load data.
if (file.exists()) {
// ObjectInputStream: Reads the serialized data from the file.
// While ois.readObject(): Deserializes the data into an array of AccountManger objects.
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file))) {
customerData = (AccountManger[]) ois.readObject();
//ois.readInt(): Reads an additional integer (nextEmpty) that presumably tracks the next empty index in the customerData array.
nextEmpty = ois.readInt();
} catch (IOException | ClassNotFoundException e) {
// When issues occur like reading into the file
System.err.println("Error loading customer data: " + e.getMessage());
}
}
}
// Call this method when the program ends
public void shutdown() {
saveCustomerData();
}
}