-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBankAccount.java
More file actions
209 lines (182 loc) · 6.48 KB
/
BankAccount.java
File metadata and controls
209 lines (182 loc) · 6.48 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
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
import java.util.*;
public class BankAccount {
static final String Bank_Name="PK BANK";
private static int accountCounter=20260001;
private String name;
private int age;
private final int accountNumber;
private double balance;
private String password;
private ArrayList<Transaction> transactions=new ArrayList<>();
// constructor 1
public BankAccount(String name,int age,String password){
if(age<18){
throw new IllegalArgumentException("=================================\nAgeLimit : 18\nWait for "+(18-age)+" years...\n=================================");
}
this.age=age;
this.name=name;
this.password=password;
this.accountNumber=accountCounter++;
}
// constructor 2
public BankAccount(String name,int age,String password,double balance){
if(age<18){
throw new IllegalArgumentException("=================================\nAgeLimit : 18\nWait for "+(18-age)+" years...\n=================================");
}
this.age=age;
this.name=name;
this.password=password;
this.accountNumber=accountCounter++;
this.balance=balance;
}
// constructor for loading
public BankAccount(int accNo,String name,int age,String password,double balance){
this.accountNumber = accNo;
this.name = name;
this.age = age;
this.password = password;
this.balance = balance;
}
public static void setCounter(int value){
accountCounter = value;
}
public String getName(){
return name;
}
public int getAccountNumber(){
return accountNumber;
}
public double getBalance(){
return balance;
}
// deposit
public void deposit(double amount,boolean log){
if(amount <= 0){
throw new IllegalArgumentException("##### Invalid Amount! #####");
}
balance+=amount;
if(log){
transactions.add(new Transaction("Credited ",amount,"Cash"));
}
}
//default deposit
public void deposit(double amount){
deposit(amount,true);
}
// withdraw
public void withdraw(double amount, boolean log) throws InsufficientBalanceException{
if(amount <= 0){
throw new IllegalArgumentException("##### Invalid Amount! #####");
}
if(amount>balance){
throw new InsufficientBalanceException("##### Insufficient Bank Balance! #####");
}
balance-=amount;
if (log){
transactions.add(new Transaction("Debited ",amount,"Cash"));
}
}
// default withdraw
public void withdraw(double amount) throws InsufficientBalanceException {
withdraw(amount,true);
}
public void display(){
System.out.println("=================================\nBank : "+Bank_Name+"\nAccount Holder : "+name+"\nAge : "+age+"\nAccount Number : "+accountNumber+"\nTotal Balance = Rs."+balance+"\n=================================");
}
// transfer
public void transfer(BankAccount rec,double amount) throws InsufficientBalanceException{
if(rec == null){
System.out.println("##### Receiver's account not found! #####");
return;
}
if(this.accountNumber == rec.accountNumber){
System.out.println("##### Can not transfer to same account! #####");
return;
}
this.withdraw(amount,false);
rec.deposit(amount,false);
transactions.add(new Transaction("Debited ",amount,"to "+rec.accountNumber));
rec.transactions.add(new Transaction("Credited ",amount,"from "+this.accountNumber));
System.out.println("Successfully Transferred.......");
}
public boolean checkPass(String pass){
return this.password.equals(pass);
}
public void changePass(String oldPass,String newPass){
if(this.password.equals(oldPass)){
this.password=newPass;
System.out.println("Password Changed Successfully.......");
}else{
System.out.println("##### Incorrect Password! #####");
}
}
//transaction history
public void showTransactions(){
System.out.println("========= TRANSACTION HISTORY =========");
if(transactions.isEmpty()){
System.out.println("No transactions yet.");
} else {
for(Transaction t : transactions){
System.out.println(t);
}
}
System.out.println("=======================================");
}
// CSV save
public String fileToString(){
String trans="";
for(Transaction t:transactions){
trans+=t.getType()+":"+t.getAmount()+":"+t.getDetail()+"|";
}
return accountNumber + "," + name + "," + age + "," + password + "," + balance + "," + trans;
}
// CSV load
public static BankAccount stringToFile(String line){
String[] data=line.split(",",6);
BankAccount acc = new BankAccount(
Integer.parseInt(data[0]),
data[1],
Integer.parseInt(data[2]),
data[3],
Double.parseDouble(data[4])
);
if(data.length == 6 && !data[5].isEmpty() && data[5]!=null){
String[] tArr = data[5].split("\\|");
for(String t:tArr){
if(t==null || t.trim().isEmpty()){
continue;
}
String[] parts=t.split(":");
try{
acc.transactions.add(new Transaction(parts[0],Double.parseDouble(parts[1]),parts[2] ));
}catch(Exception e){
System.out.println("corrupted transaction"+t);
}
}
}
return acc;
}
// filer by transaction
public void filterTransactionByType(String type){
if(type.equals("D")){
type="Debited ";
}else if(type.equals("C")){
type="Credited ";
}else{
System.out.println("invalid input");
return;
}
System.out.println("========= Filtered TRANSACTION =========");
boolean found=false;
for(Transaction t:transactions){
if(t.getType().equalsIgnoreCase(type)){
System.out.println(t);
found=true;
}
}
if(!found){
System.out.println("No Transactions");
}
System.out.println("=======================================");
}
}