Repository navigation
Expand file tree
/
Copy pathATM.java
More file actions
145 lines (114 loc) · 4.07 KB
/
Copy pathATM.java
File metadata and controls
145 lines (114 loc) · 4.07 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
import java.awt.*;
import java.awt.event.*;
class Account {
private double balance;
public Account(double balance) {
this.balance = balance;
}
public void deposit(double amount) {
if (amount > 0) balance += amount;
}
public void withdraw(double amount) {
if (amount > 0 && amount <= balance) balance -= amount;
}
public double getBalance() {
return balance;
}
}
public class ATM extends Frame implements ActionListener {
Label title, balanceLabel;
TextField amountField;
Button depositBtn, withdrawBtn, balanceBtn, clearBtn;
TextArea output;
Account acc = new Account(45000);
ATM() {
setTitle("Digital ATM");
setSize(450,500);
setLayout(null);
setBackground(new Color(28,28,30));
// Title
title = new Label("DIGITAL ATM");
title.setBounds(150,50,200,30);
title.setFont(new Font("Arial",Font.BOLD,22));
title.setForeground(Color.WHITE);
add(title);
// Balance Card
balanceLabel = new Label("Account Balance : ₹45000");
balanceLabel.setBounds(110,100,250,30);
balanceLabel.setFont(new Font("Arial",Font.BOLD,16));
balanceLabel.setForeground(new Color(0,200,255));
add(balanceLabel);
// Amount input
Label amount = new Label("Enter Amount");
amount.setBounds(70,160,120,25);
amount.setForeground(Color.WHITE);
add(amount);
amountField = new TextField();
amountField.setBounds(200,160,150,25);
add(amountField);
// Buttons
depositBtn = new Button("Deposit");
depositBtn.setBounds(70,220,120,40);
depositBtn.setBackground(new Color(0,120,255));
depositBtn.setForeground(Color.WHITE);
withdrawBtn = new Button("Withdraw");
withdrawBtn.setBounds(230,220,120,40);
withdrawBtn.setBackground(new Color(0,120,255));
withdrawBtn.setForeground(Color.WHITE);
balanceBtn = new Button("Check Balance");
balanceBtn.setBounds(70,280,120,40);
balanceBtn.setBackground(new Color(60,60,60));
balanceBtn.setForeground(Color.WHITE);
clearBtn = new Button("Clear");
clearBtn.setBounds(230,280,120,40);
clearBtn.setBackground(new Color(60,60,60));
clearBtn.setForeground(Color.WHITE);
add(depositBtn);
add(withdrawBtn);
add(balanceBtn);
add(clearBtn);
// Output console
output = new TextArea();
output.setBounds(70,350,280,80);
output.setBackground(Color.BLACK);
output.setForeground(new Color(0,255,150));
output.setFont(new Font("Monospaced",Font.BOLD,14));
add(output);
// Button events
depositBtn.addActionListener(this);
withdrawBtn.addActionListener(this);
balanceBtn.addActionListener(this);
clearBtn.addActionListener(this);
// Close window
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
dispose();
}
});
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
double amount = 0;
try {
amount = Double.parseDouble(amountField.getText());
} catch(Exception ex) {}
if(e.getSource()==depositBtn){
acc.deposit(amount);
output.setText("Deposit Successful\nBalance: ₹"+acc.getBalance());
}
if(e.getSource()==withdrawBtn){
acc.withdraw(amount);
output.setText("Withdraw Successful\nBalance: ₹"+acc.getBalance());
}
if(e.getSource()==balanceBtn){
output.setText("Current Balance : ₹"+acc.getBalance());
}
if(e.getSource()==clearBtn){
amountField.setText("");
output.setText("");
}
}
public static void main(String[] args) {
new ATM();
}
}