-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBank.java
More file actions
284 lines (269 loc) · 9.18 KB
/
Copy pathBank.java
File metadata and controls
284 lines (269 loc) · 9.18 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
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
import java.awt.*;
import java.awt.event.*;
import java.awt.FlowLayout;
import java.sql.*;
import javax.swing.*;
public class Bank extends JFrame implements ActionListener
{
static JLabel label1,label2,label3,sep;
static JButton sign,create,deposit,withdraw,trans,balance,close,logout;
static JTextField txtA1;
static JTextField txtA2;
static Connection con;
static Statement st1,st2,st3;
static ResultSet rs1;
int numd; //to be deposited
int numf; //actual amount in account(balance)
int nume; //to be withdrawn
public Bank()
{
super(" MY Bank ");
setLayout(new FlowLayout());
try {
con = DriverManager.getConnection("jdbc:ucanaccess://C:/Users/Rohit/Desktop/Java Project/bank.mdb");
st1=con.createStatement();
rs1=st1.executeQuery("select * from tab1");
}
catch(Exception e)
{
System.out.println("there was some error in establishing connection : "+e);
}
sep = new JLabel("Existing User Signin : ");
add(sep);
label1 = new JLabel(" Account Number : ");
add(label1);
txtA1 = new JTextField(15);
add(txtA1);
label2 = new JLabel(" Name : ");
add(label2);
txtA2 = new JTextField(15);
add(txtA2);
sign=new JButton("SIGN IN");
add(sign);
sep = new JLabel(" ");
add(sep);
//sep = new JLabel(" ");
//add(sep);
sep = new JLabel("New User Signup : ");
add(sep);
create=new JButton("CREATE NEW ACCOUNT");
add(create);
sep = new JLabel(" ");
add(sep);
sep = new JLabel(" ");
add(sep);
deposit=new JButton("DEPOSIT");
add(deposit);
withdraw=new JButton("WITHDRAW");
add(withdraw);
trans=new JButton("TRANSACTION");
add(trans);
balance=new JButton("BALANCE");
add(balance);
logout=new JButton("LOGOUT");
add(logout);
sep = new JLabel(" ");
add(sep);
close=new JButton("CLOSE");
add(close);
create.addActionListener(this);
deposit.addActionListener(this);
withdraw.addActionListener(this);
trans.addActionListener(this);
balance.addActionListener(this);
logout.addActionListener(this);
close.addActionListener(this);
sign.addActionListener(this);
deposit.setEnabled(false);
withdraw.setEnabled(false);
trans.setEnabled(false);
balance.setEnabled(false);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==create)
{
String stra = JOptionPane.showInputDialog(null,"Please enter the NAME to open a NEW ACCOUNT");
String strb = JOptionPane.showInputDialog(null,"Please enter the ACCOUNT NUMBER");
if(stra==null || strb==null)
{
JOptionPane.showMessageDialog(null," Either any one or both the values were left blank");
deposit.setEnabled(false);
withdraw.setEnabled(false);
trans.setEnabled(false);
balance.setEnabled(false);
create.setEnabled(true);
sign.setEnabled(true);
}
else
{
try
{
st2=con.createStatement(); //st2 is a statement object
String rr="insert into tab1 values('"+stra+"','"+strb+"',0,0,0)"; //url for values into the database
st2.executeUpdate(rr);
st2.close(); //close the statement object
JOptionPane.showMessageDialog(null,"ACCOUNT Created Successfully");
txtA1.setText(strb); //once created successfully, keep the acc no and name on the text fields for ease of login
txtA2.setText(stra);
}
catch(Exception e2)
{
JOptionPane.showMessageDialog(null," There is already a user with same ACCOUNT NUMBER ,Give another ACCOUNT NUMBER ");
}
deposit.setEnabled(false);
withdraw.setEnabled(false);
trans.setEnabled(false);
balance.setEnabled(false);
create.setEnabled(false);
sign.setEnabled(true);
}
}
else if(e.getSource()==sign)
{
String num=txtA1.getText(); //get the number value from the acc number string
try
{
st1.close();
st1=con.createStatement();
rs1=st1.executeQuery("Select * from tab1 where AccNum Like '"+num+"'"); //search for the acount number in the table
rs1.next();
txtA1.setText(rs1.getString("AccNum"));
txtA2.setText(rs1.getString("UserName"));
}
catch(Exception e2)
{
JOptionPane.showMessageDialog(null,"Invalid Account Number");
}
deposit.setEnabled(true);
withdraw.setEnabled(true);
trans.setEnabled(true);
balance.setEnabled(true);
create.setEnabled(false);
}
else if(e.getSource()==deposit)
{
String strc = JOptionPane.showInputDialog(null,"Please enter the amount to be DEPOSITED"); //open a new window to take user input
numd=Integer.parseInt(strc);
int numb=Integer.parseInt(txtA1.getText());
if (numd<=0)
{
JOptionPane.showMessageDialog(null," NO Amount Deposited");
}
else
{
try
{
int num,num1;
st3=con.createStatement();
rs1=st3.executeQuery("select * from tab1 where Accnum like '"+txtA1.getText()+"'");
rs1.next();
num1=Integer.parseInt(rs1.getString("Deposit")); //take in the values from the database
num=Integer.parseInt(rs1.getString("Balance")); //take in value from the database
JOptionPane.showMessageDialog(null,"Amount Deposited Successfully , Amount is "+numd);
numf=num+numd; //new balance
num1=num1+numd; //to show total deposited
st2=con.createStatement();
String ss="update tab1 set Deposit="+num1+" where AccNum="+numb+""; //url to set new deposit amount
String aa="update tab1 set Balance="+numf+" where AccNum="+numb+""; //url to set new balance
st2.executeUpdate(ss);
st2.executeUpdate(aa);
st2.close();
st3.close();
}
catch(Exception e2)
{
JOptionPane.showMessageDialog(null,"Amount can not be DEPOSITED");
}
}
}
else if(e.getSource()==withdraw)
{
String strd = JOptionPane.showInputDialog(null,"Please enter the amount to be WITHDRAWN"); //show dialog box to withdraw amount
nume=Integer.parseInt(strd);
int numc=Integer.parseInt(txtA1.getText());
if(numf<=nume) //if amount withdrawn is > amount in account then error
{
JOptionPane.showMessageDialog(null,"Sorry can not Withdraw, no sufficient Balance");
}
else
{
try
{
int num1;
st3=con.createStatement();
rs1=st3.executeQuery("select * from tab1 where Accnum like '"+txtA1.getText()+"'"); //get data of that acc numbr in result set
rs1.next();
num1=Integer.parseInt(rs1.getString("Withdraw"));
numf=Integer.parseInt(rs1.getString("Balance"));
JOptionPane.showMessageDialog(null,"Amount Withdrawn is "+nume);
numf=numf-nume; //update balance in account
num1=num1+nume; //total withdrawn
st2=con.createStatement();
String pp="update tab1 set Withdraw="+num1+" where AccNum="+numc+"";
String pp1="update tab1 set Balance="+numf+" where AccNum="+numc+"";
st2.executeUpdate(pp);
st2.executeUpdate(pp1);
st2.close();
}
catch(Exception e2)
{
JOptionPane.showMessageDialog(null," Can't WITHDRAW ");
}
}
}
else if(e.getSource()==balance)
{
String num=txtA1.getText();
try
{
st3=con.createStatement();
rs1=st3.executeQuery("select * from tab1 where Accnum like '"+num+"'");
rs1.next();
numf=Integer.parseInt(rs1.getString("Balance")); //get balance data from the table
JOptionPane.showMessageDialog(null,"Balance is "+numf);
}
catch(Exception e2)
{
JOptionPane.showMessageDialog(null," Balance null ");
}
}
else if(e.getSource()==trans)
{
String strq = JOptionPane.showInputDialog(null,"Enter the ACCOUNT NUMBER to view the TRANSACTION DONE");
int numk=Integer.parseInt(strq); //get the acc number from the user to view trannsactions
int numa=Integer.parseInt(txtA1.getText());
if(numk==numa)
{
JOptionPane.showMessageDialog(null,"Amount Deposited is "+numd);
JOptionPane.showMessageDialog(null,"Amount Withdrawn is "+nume);
}
else
{
JOptionPane.showMessageDialog(null,"The Account Number is not the same as in the TEXTFIELD");
}
}
else if(e.getSource()==logout)
{
txtA1.setText(""); //clear acc num test field
txtA2.setText(""); //clear name text field
deposit.setEnabled(false); //visibility off of all buttons
withdraw.setEnabled(false);
trans.setEnabled(false);
balance.setEnabled(false);
create.setEnabled(true);
sign.setEnabled(true);
}
else if(e.getSource()==close)
{
System.exit(0);
}
}
public static void main(String args[])
{
Bank obj = new Bank();
obj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
obj.setSize(250,500);
obj.setVisible(true);
}
}