-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.java
195 lines (160 loc) · 5.76 KB
/
Main.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
package CodSoft;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
class ATM {
private UserAccount account;
public ATM(UserAccount account) {
this.account = account;
}
public boolean withdraw(double amount) {
return account.withdraw(amount);
}
public boolean deposit(double amount) {
return account.deposit(amount);
}
public double checkBalance() {
return account.getBalance();
}
public boolean hasInitialDeposit() {
return account.getBalance() > 0;
}
}
public class ATMInterface {
private ATM atm;
private JFrame frame;
private JTextField amountField;
private JLabel balanceLabel;
public ATMInterface(ATM atm) {
this.atm = atm;
createUI();
}
private void createUI() {
frame = new JFrame("ATM Machine");
frame.setSize(400, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(null);
JLabel amountLabel = new JLabel("Amount");
amountLabel.setBounds(50, 50, 100, 30);
frame.add(amountLabel);
amountField = new JTextField();
amountField.setBounds(150, 50, 150, 30);
frame.add(amountField);
JButton withdrawButton = new JButton("Withdraw");
withdrawButton.setBounds(50, 100, 100, 30);
frame.add(withdrawButton);
JButton depositButton = new JButton("Deposit");
depositButton.setBounds(200, 100, 100, 30);
frame.add(depositButton);
JButton balanceButton = new JButton("Check Balance");
balanceButton.setBounds(50, 150, 250, 30);
frame.add(balanceButton);
balanceLabel = new JLabel("Balance: ₹" + atm.checkBalance());
balanceLabel.setBounds(50, 200, 250, 30);
frame.add(balanceLabel);
withdrawButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (atm.hasInitialDeposit()) {
performWithdraw();
} else {
JOptionPane.showMessageDialog(frame, "Please make an initial deposit before withdrawing.");
}
}
});
depositButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
performDeposit();
}
});
balanceButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (atm.hasInitialDeposit()) {
checkBalance();
} else {
JOptionPane.showMessageDialog(frame, "Please make an initial deposit before checking balance.");
}
}
});
frame.setVisible(true);
}
private void performWithdraw() {
try {
double amount = Double.parseDouble(amountField.getText());
if (atm.withdraw(amount)) {
JOptionPane.showMessageDialog(frame, "Withdrawal successful!");
} else {
JOptionPane.showMessageDialog(frame, "Insufficient funds or invalid amount.");
}
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(frame, "Please enter a valid amount.");
}
updateBalance();
}
private void performDeposit() {
try {
double amount = Double.parseDouble(amountField.getText());
if (atm.deposit(amount)) {
JOptionPane.showMessageDialog(frame, "Deposit successful!");
} else {
JOptionPane.showMessageDialog(frame, "Invalid amount.");
}
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(frame, "Please enter a valid amount.");
}
updateBalance();
}
private void checkBalance() {
updateBalance();
JOptionPane.showMessageDialog(frame, "Your balance is ₹" + atm.checkBalance());
}
private void updateBalance() {
balanceLabel.setText("Balance: ₹" + atm.checkBalance());
}
public static void main(String[] args) {
double initialBalance = 0;
// Loop until a valid initial balance is entered
while (true) {
String initialBalanceStr = JOptionPane.showInputDialog("Enter the initial balance (must be ₹100 or greater):");
if (initialBalanceStr == null) {
JOptionPane.showMessageDialog(null, "Input canceled. Exiting.");
System.exit(0);
}
try {
initialBalance = Double.parseDouble(initialBalanceStr);
if (initialBalance >= 100) {
break; // Exit loop if valid input
} else {
JOptionPane.showMessageDialog(null, "Initial balance must be ₹100 or greater than ₹100. Please enter a valid amount.");
}
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null, "Invalid input. Please enter a valid number.");
}
}
UserAccount account = new UserAccount(initialBalance);
ATM atm = new ATM(account);
new ATMInterface(atm);
}
}
class UserAccount {
private double balance;
public UserAccount(double initialBalance) {
this.balance = initialBalance;
}
public double getBalance() {
return balance;
}
public boolean deposit(double amount) {
if (amount > 0) {
balance += amount;
return true;
}
return false;
}
public boolean withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
return true;
}
return false;
}
}