-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathATM_Replica.java
360 lines (344 loc) · 10.1 KB
/
ATM_Replica.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
import javax.swing.*;
import java.awt.event.*;
import java.awt.Color;
import java.io.FileWriter;
import java.util.*;
import java.io.*;
public class ATM_Replica extends JFrame
{
JLabel name,password,sh_bal,sh_dep_st,sh_wit_st;
JTextField en_name,dep_amt,wit_amt;
JPasswordField en_pass;
JButton Cr_Acc,Dep,Bal_Inq,done_dep,done_wit,With,cls,strt;
public ATM_Replica(){ }
public ATM_Replica(String s){super(s);}
public void setComponents()
{
name=new JLabel("Enter Your Name");
password=new JLabel("Enter Your Password");
en_pass=new JPasswordField();
Cr_Acc=new JButton("Create Account");
Dep=new JButton("Deposit");
Bal_Inq=new JButton("Balance Inquiry");
sh_bal=new JLabel();
dep_amt=new JTextField();
wit_amt=new JTextField();
en_name=new JTextField();
sh_dep_st=new JLabel();
sh_wit_st=new JLabel();
done_dep=new JButton("Done");
done_wit=new JButton("Done");
With=new JButton("Withdraw");
cls=new JButton("Clear");
strt=new JButton("Welcome to MyBank ATM");
Cr_Acc.setBackground(Color.yellow);
Dep.setBackground(Color.yellow);
Bal_Inq.setBackground(Color.yellow);
With.setBackground(Color.yellow);
done_dep.setBackground(Color.green);
done_wit.setBackground(Color.green);
strt.setBackground(Color.green);
cls.setBackground(Color.red);
setLayout(null);
name.setBounds(100,100,100,20);
en_name.setBounds(300,100,50,20);
password.setBounds(100,150,150,20);
en_pass.setBounds(300,150,50,20);
Cr_Acc.setBounds(10,200,150,20); //button
Dep.setBounds(180,200,100,20); //button
Bal_Inq.setBounds(300,200,150,20); //button
With.setBounds(470,200,150,20); //button
cls.setBounds(640,200,100,20); //button
strt.setBounds(250,250,200,100); //button
setLayout(null);
add(wit_amt);
add(done_wit);
add(sh_wit_st);
add(sh_bal);
add(done_dep);
add(dep_amt);
add(sh_dep_st);
add(name);
add(password);
add(en_name);
add(en_pass);
add(Cr_Acc);
add(Dep);
add(Bal_Inq);
add(With);
add(cls);
add(strt);
Cr_Acc.addActionListener(new create());
Dep.addActionListener(new deposit());
Bal_Inq.addActionListener(new inqurie());
With.addActionListener(new withdraw());
done_dep.addActionListener(new final_deposit());
done_wit.addActionListener(new final_withdraw());
cls.addActionListener(new clear());
strt.addActionListener(new start());
name.setVisible(false);
en_name.setVisible(false);
en_pass.setVisible(false);
password.setVisible(false);
Cr_Acc.setVisible(false);
Dep.setVisible(false);
Bal_Inq.setVisible(false);
With.setVisible(false);
cls.setVisible(false);
}
/////////////////////////////////////////////////////////////
class create implements ActionListener
{
public void actionPerformed(ActionEvent e1)
{
try{
String amt ="0";
String getname = en_name.getText();
File f1=new File(getname);
if(!f1.exists())
{
FileOutputStream fout=new FileOutputStream(getname,true);
String getpass = en_pass.getText();
FileWriter fw=new FileWriter(getname,true);
fw.write(getpass);
fw.write(" ");
fw.write(amt);
fw.close();
fout.close();
sh_bal.setVisible(false);
}
else
{
sh_bal.setBounds(10,220,150,20);
sh_bal.setVisible(true);
sh_bal.setText("Account Already Exists");
}}catch(IOException ioe){ }
}
}
//////////////////////////////////////////////////////////////
class deposit implements ActionListener
{
public void actionPerformed(ActionEvent e2)
{try
{
wit_amt.setVisible(false);
done_wit.setVisible(false);
sh_wit_st.setVisible(false);
String getname = en_name.getText();
String getpass = en_pass.getText();
Scanner sd = new Scanner(new File(getname));
String check_pass;
check_pass = sd.next();
if(getpass.equals(check_pass))
{
done_dep.setVisible(true);
dep_amt.setVisible(true);
sh_dep_st.setVisible(true);
sh_bal.setText("Current Balance: "+sd.next());
sh_bal.setBounds(10,220,150,20);
sh_dep_st.setText("Amount to deposit");
sh_dep_st.setBounds(50,250,150,20);
dep_amt.setBounds(200,250,50,20);
done_dep.setBounds(300,250,80,20);
}
else{sh_bal.setBounds(10,220,150,20);
sh_bal.setText("Incorrect password");}
sd.close();
}
catch(FileNotFoundException fnf){sh_bal.setBounds(10,220,150,20);
sh_bal.setText("Account not created");}
}
}
////////////////////////////////////////////////////////////////////////////
class final_deposit implements ActionListener
{
public void actionPerformed(ActionEvent e3)
{try
{
String getname = en_name.getText();
String getpass = en_pass.getText();
Scanner sd = new Scanner(new File(getname));
String amt_deposited;
amt_deposited = dep_amt.getText();
String pre_amt;
sd.next();
pre_amt=sd.next();
FileWriter fw=new FileWriter(getname,false);
fw.write(getpass);
fw.write(" ");
Integer amt1=Integer.parseInt(amt_deposited);
Integer amt2=Integer.parseInt(pre_amt);
Integer amt=amt1+amt2;
amt_deposited = String.valueOf(amt);
fw.write(amt_deposited);
sh_bal.setBounds(10,220,150,20);
sh_bal.setText("Current Balance: "+amt);
fw.close();
sd.close();
dep_amt.setText("");
done_dep.setVisible(false);
dep_amt.setVisible(false);
sh_dep_st.setVisible(false);
}catch(IOException ioe){}
}
}
////////////////////////////////////////////////////////////////////////////
class inqurie implements ActionListener
{
public void actionPerformed(ActionEvent e3)
{
try{
done_dep.setVisible(false);
dep_amt.setVisible(false);
sh_dep_st.setVisible(false);
wit_amt.setVisible(false);
done_wit.setVisible(false);
sh_wit_st.setVisible(false);
String getname = en_name.getText();
String getpass = en_pass.getText();
Scanner sd = new Scanner(new File(getname));
String check_pass;
check_pass = sd.next();
if(getpass.equals(check_pass))
{
sh_bal.setBounds(10,220,150,20);
sh_bal.setText("Current Balance: "+sd.next());
}
else{
sh_bal.setBounds(10,220,150,20);
sh_bal.setText("Incorrect password");}
sd.close();
}catch(FileNotFoundException fnf){sh_bal.setBounds(10,220,150,20);
sh_bal.setText("Account not created");}
}
}
////////////////////////////////////////////////////////////////////////////
class withdraw implements ActionListener
{
public void actionPerformed(ActionEvent e3)
{
try
{
done_dep.setVisible(false);
dep_amt.setVisible(false);
sh_dep_st.setVisible(false);
String getname = en_name.getText();
String getpass = en_pass.getText();
Scanner sd = new Scanner(new File(getname));
String check_pass;
check_pass = sd.next();
if(getpass.equals(check_pass))
{
wit_amt.setVisible(true);
done_wit.setVisible(true);
sh_wit_st.setVisible(true);
sh_bal.setText("Current Balance: "+sd.next());
sh_bal.setBounds(10,220,150,20);
sh_wit_st.setText("Amount to withdraw");
sh_wit_st.setBounds(50,250,150,20);
wit_amt.setBounds(200,250,50,20);
done_wit.setBounds(300,250,80,20);
}
else{sh_bal.setBounds(10,220,150,20);
sh_bal.setText("Incorrect password");}
sd.close();
}catch(FileNotFoundException fnf){sh_bal.setBounds(10,220,150,20);
sh_bal.setText("Account not created");}
}
}
////////////////////////////////////////////////////////////////////////////
class final_withdraw implements ActionListener
{
public void actionPerformed(ActionEvent e3)
{
try
{
String getname = en_name.getText();
String getpass = en_pass.getText();
Scanner sd = new Scanner(new File(getname));
String amt_withdrawn;
amt_withdrawn = wit_amt.getText();
String pre_amt;
sd.next();
pre_amt=sd.next();
FileWriter fw=new FileWriter(getname,false);
fw.write(getpass);
fw.write(" ");
Integer amt1=Integer.parseInt(amt_withdrawn);
Integer amt2=Integer.parseInt(pre_amt);
if(amt1 < amt2)
{
Integer amt=amt2-amt1;
amt_withdrawn = String.valueOf(amt);
fw.write(amt_withdrawn);
sh_bal.setBounds(10,220,150,20);
sh_bal.setText("Current Balance: "+amt);
fw.close();
}
else
{
sh_bal.setBounds(10,220,150,20);
sh_bal.setText("Insufficient Balance");
fw.write(" ");
fw.write(pre_amt);
}
wit_amt.setText("");
wit_amt.setVisible(false);
done_wit.setVisible(false);
sh_wit_st.setVisible(false);
fw.close();sd.close();
}catch(IOException ioe){}
}
}
////////////////////////////////////////////////////////////////////////////
class clear implements ActionListener
{
public void actionPerformed(ActionEvent e1)
{
en_pass.setText("");
en_name.setText("");
sh_bal.setText("");
name.setVisible(false);
en_name.setVisible(false);
en_pass.setVisible(false);
password.setVisible(false);
Cr_Acc.setVisible(false);
Dep.setVisible(false);
Bal_Inq.setVisible(false);
With.setVisible(false);
cls.setVisible(false);
strt.setVisible(true);
done_dep.setVisible(false);
dep_amt.setVisible(false);
sh_dep_st.setVisible(false);
wit_amt.setVisible(false);
done_wit.setVisible(false);
sh_wit_st.setVisible(false);
}
}
////////////////////////////////////////////////////////////////////////////
class start implements ActionListener
{
public void actionPerformed(ActionEvent e1)
{
strt.setVisible(false);
name.setVisible(true);
en_name.setVisible(true);
en_pass.setVisible(true);
password.setVisible(true);
Cr_Acc.setVisible(true);
Dep.setVisible(true);
Bal_Inq.setVisible(true);
With.setVisible(true);
cls.setVisible(true);
}
}
public static void main(String []args)
{
ATM_Replica jf=new ATM_Replica("MyBank");
jf.setComponents();
jf.setSize(800,800);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}