-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransfer.java
More file actions
188 lines (147 loc) · 5.97 KB
/
Copy pathTransfer.java
File metadata and controls
188 lines (147 loc) · 5.97 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
import javax.swing.*;
import java.awt.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
class Transfer extends JFrame {
Transfer(String username) {
Font f = new Font("Futura", Font.BOLD, 30);
Font f2 = new Font("Calibri", Font.PLAIN, 18);
JLabel title = new JLabel("Transfer Funds", JLabel.CENTER);
JLabel l1 = new JLabel("Receiver:");
JTextField t1 = new JTextField(10);
JLabel l2 = new JLabel("Amount:");
JTextField t2 = new JTextField(10);
JButton b1 = new JButton("Transfer");
JButton b2 = new JButton("Back");
title.setFont(f);
l1.setFont(f2);
t1.setFont(f2);
l2.setFont(f2);
t2.setFont(f2);
b1.setFont(f2);
b2.setFont(f2);
Container c = getContentPane();
c.setLayout(null);
int labelX = 200, fieldX = 400, yStart = 80, width = 150, height = 30, gap = 40;
title.setBounds(250, 20, 300, 40);
l1.setBounds(labelX, yStart, width, height);
t1.setBounds(fieldX, yStart, width, height);
l2.setBounds(labelX, yStart + gap, width, height);
t2.setBounds(fieldX, yStart + gap, width, height);
b1.setBounds(250, yStart + 2 * gap, 120, 40);
b2.setBounds(400, yStart + 2 * gap, 120, 40);
c.add(title);
c.add(l1);
c.add(t1);
c.add(l2);
c.add(t2);
c.add(b1);
c.add(b2);
b2.addActionListener(
a->
{
new Home(username);
dispose();
}
);
b1.addActionListener(
a->
{
String reciever = t1.getText();
String s1 = t2.getText();
if(reciever.isEmpty() || s1.isEmpty())
{
JOptionPane.showMessageDialog(null,"Cannot be empty");
return;
}
double amount= Double.parseDouble(s1);
double balance = fetchBalance(username);
if(amount>balance)
{
JOptionPane.showMessageDialog(null,"Insufficient Balance");
return;
}
String url = "jdbc:mysql://localhost:3306/3dec";
try (Connection con = DriverManager.getConnection(url, "root", "Mehta@17")) {
String sql = "update users set balance=? where username=?";
try (PreparedStatement pst = con.prepareStatement(sql)) {
pst.setDouble(1, balance-amount);
pst.setString(2, username);
pst.executeUpdate();
updatePassbook(username,"Transfer to "+reciever,-amount,balance-amount);
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e.getMessage());
}
balance = fetchBalance(reciever);
try (Connection con = DriverManager.getConnection(url, "root", "Mehta@17")) {
String sql = "update users set balance=? where username=?";
try (PreparedStatement pst = con.prepareStatement(sql)) {
pst.setDouble(1, balance+amount);
pst.setString(2, reciever);
pst.executeUpdate();
JOptionPane.showMessageDialog(null, "Transfer Completed");
t1.setText("");
t2.setText("");
updatePassbook(reciever," Transfer from "+username,amount,balance+amount);
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e.getMessage());
}
}
);
setVisible(true);
setSize(800, 550);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Transfer Funds");
}
void updatePassbook(String username,String desc,double amount,double balance)
{
String url = "jdbc:mysql://localhost:3306/3dec";
try(Connection con = DriverManager.getConnection(url,"root","Mehta@17"))
{
String sql = "insert into transactions(username,description,amount,balance) values(?,?,?,?)";
try(PreparedStatement pst = con.prepareStatement(sql))
{
pst.setString(1,username);
pst.setString(2,desc);
pst.setDouble(3,amount);
pst.setDouble(4,balance);
pst.executeUpdate();
}
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,e.getMessage());
}
}
double fetchBalance(String username)
{
double balance = 0.0;
String url = "jdbc:mysql://localhost:3306/3dec";
try(Connection con = DriverManager.getConnection(url,"root","Mehta@17"))
{
String sql = "select balance from users where username = ?";
try(PreparedStatement pst = con.prepareStatement(sql))
{
pst.setString(1,username);
ResultSet rs = pst.executeQuery();
if(rs.next())
{
balance = rs.getDouble("balance");
}
}
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,e.getMessage());
}
return balance;
}
public static void main(String[] args) {
new Transfer("Swayam");
}
}