-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFastCash.java
More file actions
84 lines (69 loc) · 2.58 KB
/
Copy pathFastCash.java
File metadata and controls
84 lines (69 loc) · 2.58 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
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
public class FastCash extends JFrame implements ActionListener {
JButton b100, b500, b1000, b2000, b5000, b10000, back;
String cardNumber;
FastCash(String cardNumber) {
this.cardNumber = cardNumber;
setTitle("Fast Cash");
setLayout(null);
JLabel label = new JLabel("Select Amount");
label.setBounds(140, 20, 150, 30);
add(label);
b100 = new JButton("Rs 100");
b500 = new JButton("Rs 500");
b1000 = new JButton("Rs 1000");
b2000 = new JButton("Rs 2000");
b5000 = new JButton("Rs 5000");
b10000 = new JButton("Rs 10000");
back = new JButton("Back");
JButton[] buttons = {b100, b500, b1000, b2000, b5000, b10000};
int y = 70;
for (JButton btn : buttons) {
btn.setBounds(100, y, 180, 30);
btn.addActionListener(this);
add(btn);
y += 40;
}
back.setBounds(100, y + 10, 180, 30);
back.addActionListener(this);
add(back);
setSize(400, 400);
setLocation(450, 200);
setVisible(true);
}
public void actionPerformed(ActionEvent ae) {
String amt = ae.getActionCommand().substring(3).trim(); // Rs 100 -> 100
if (ae.getSource() == back) {
setVisible(false);
new Transaction(cardNumber);
return;
}
try {
Conn conn = new Conn();
ResultSet rs = conn.s.executeQuery("SELECT * FROM bank WHERE card_number = '" + cardNumber + "'");
int balance = 0;
while (rs.next()) {
String type = rs.getString("type");
int amount = Integer.parseInt(rs.getString("amount"));
balance += type.equals("Deposit") ? amount : -amount;
}
int withdrawAmt = Integer.parseInt(amt);
if (withdrawAmt > balance) {
JOptionPane.showMessageDialog(null, "Insufficient Balance.");
return;
}
String query = "INSERT INTO bank (card_number, date, type, amount) VALUES ('" + cardNumber + "', NOW(), 'Withdraw', '" + amt + "')";
conn.s.executeUpdate(query);
JOptionPane.showMessageDialog(null, "Rs. " + amt + " Withdrawn Successfully");
setVisible(false);
new Transaction(cardNumber);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new FastCash("1234567890123456");
}
}