-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBankingService.java
More file actions
107 lines (89 loc) · 3.65 KB
/
Copy pathBankingService.java
File metadata and controls
107 lines (89 loc) · 3.65 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
package banking;
import java.sql.*;
public class BankingService {
private final Connection con;
public BankingService(Connection con) {
this.con = con;
}
/**
* Record transaction in database
*/
private void recordTransaction(int sender, int receiver, int amount, String type) {
try {
String sql = "INSERT INTO transactions(sender_ac, receiver_ac, amount, trans_type) VALUES (?,?,?,?)";
PreparedStatement ps = con.prepareStatement(sql);
ps.setInt(1, sender);
ps.setInt(2, receiver);
ps.setInt(3, amount);
ps.setString(4, type);
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* Get transaction history for an account
*/
public ResultSet getTransactionHistory(int acNo) throws SQLException {
String sql = "SELECT t.*, " +
"CASE " +
" WHEN t.sender_ac = ? THEN 'DEBIT' " +
" ELSE 'CREDIT' " +
"END as type, " +
"CASE " +
" WHEN t.sender_ac = ? THEN t.receiver_ac " +
" ELSE t.sender_ac " +
"END as other_account " +
"FROM transactions t " +
"WHERE t.sender_ac = ? OR t.receiver_ac = ? " +
"ORDER BY t.trans_date DESC";
PreparedStatement ps = con.prepareStatement(sql);
ps.setInt(1, acNo);
ps.setInt(2, acNo);
ps.setInt(3, acNo);
ps.setInt(4, acNo);
return ps.executeQuery();
}
public boolean createAccount(String name, int pass) throws SQLException {
String sql = "INSERT INTO customer(cname,balance,pass_code) VALUES (?,1000,?)";
PreparedStatement ps = con.prepareStatement(sql);
ps.setString(1, name);
ps.setInt(2, pass);
return ps.executeUpdate() == 1;
}
public ResultSet login(String name, int pass) throws SQLException {
String sql = "SELECT * FROM customer WHERE cname=? AND pass_code=?";
PreparedStatement ps = con.prepareStatement(sql);
ps.setString(1, name);
ps.setInt(2, pass);
return ps.executeQuery();
}
public ResultSet getAccount(int acNo) throws SQLException {
PreparedStatement ps = con.prepareStatement("SELECT * FROM customer WHERE ac_no=?");
ps.setInt(1, acNo);
return ps.executeQuery();
}
public boolean transfer(int sender, int receiver, int amt) throws Exception {
con.setAutoCommit(false);
PreparedStatement chk = con.prepareStatement("SELECT ac_no FROM customer WHERE ac_no=?");
chk.setInt(1, receiver);
if (!chk.executeQuery().next()) return false;
PreparedStatement bal = con.prepareStatement("SELECT balance FROM customer WHERE ac_no=?");
bal.setInt(1, sender);
ResultSet rs = bal.executeQuery();
if (rs.next() && rs.getInt(1) < amt) return false;
PreparedStatement debit = con.prepareStatement("UPDATE customer SET balance=balance-? WHERE ac_no=?");
debit.setInt(1, amt);
debit.setInt(2, sender);
debit.executeUpdate();
PreparedStatement credit = con.prepareStatement("UPDATE customer SET balance=balance+? WHERE ac_no=?");
credit.setInt(1, amt);
credit.setInt(2, receiver);
credit.executeUpdate();
// Record the transaction
recordTransaction(sender, receiver, amt, "TRANSFER");
con.commit();
con.setAutoCommit(true);
return true;
}
}