-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWithdraw.java
46 lines (39 loc) · 1.82 KB
/
Withdraw.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
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JOptionPane;
public class Withdraw {
static void withdraw(int accountNumber){
String withdrawValueString = JOptionPane.showInputDialog(null, "Type the withdraw's value");
int withdrawValue = Integer.parseInt(withdrawValueString);
try {
String sql = "SELECT * from account";
Statement stt = Connect.connection.createStatement();
ResultSet res = stt.executeQuery(sql);
while (res.next()) {
int account_number = res.getInt(1);
double currentBalance = res.getDouble("balance");
if(accountNumber == account_number){
if(withdrawValue <= currentBalance){
double newBalanceValue = currentBalance - withdrawValue;
String updateBalance = "UPDATE account SET balance = ? WHERE id = ?";
PreparedStatement preparedStatement = Connect.connection.prepareStatement(updateBalance);
preparedStatement.setDouble(1, newBalanceValue);
preparedStatement.setInt(2, account_number);
int response = preparedStatement.executeUpdate();
if(response > 0){
Main.accounBalance = newBalanceValue;
JOptionPane.showMessageDialog(null, "Withdraw successfully made!");
}
} else {
JOptionPane.showMessageDialog(null, "Withdraw value greater than balance :(");
}
}
}
} catch (SQLException e) {
e.printStackTrace();
}
Main.isLogged();
}
}