-
Notifications
You must be signed in to change notification settings - Fork 0
/
AccountManager.java
171 lines (161 loc) · 7.12 KB
/
AccountManager.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
package BankingManagementSystem;
import java.math.BigDecimal;
import java.sql.*;
import java.util.Scanner;
public class AccountManager {
private Connection con;
private Scanner sc;
AccountManager(Connection con,Scanner sc) {
this.con = con;
this.sc = sc;
}
public void credit_money(long account_number)throws SQLException{
sc.nextLine();
System.out.println("Enter account number");
double amount = sc.nextDouble();
sc.nextLine();
System.out.println("Enter Security pin:");
String Security_pin = sc.nextLine();
try{
con.setAutoCommit(false);
if(account_number != 0){
PreparedStatement pstmt = con.prepareStatement("Select * from account where account_number = ? AND security_pin = ?");
pstmt.setLong(1, account_number);
pstmt.setString(2, Security_pin);
ResultSet rs = pstmt.executeQuery();
if(rs.next()){
String credit_query = "Update account set balance = balance + ? where account_number = ?";
PreparedStatement pstmt1 = con.prepareStatement(credit_query);
pstmt1.setDouble(1, amount);
pstmt1.setLong(2, account_number);
int rowsaffected = pstmt1.executeUpdate();
if(rowsaffected>0){
System.out.println("Rs" +amount+ " Amount credited successfully");
con.commit();
con.setAutoCommit(true);
return;
}else {
System.out.println("Transaction failed");
con.rollback();
con.setAutoCommit(true);
}
}else{
System.out.println("Invalid account number");
}
}
}catch(SQLException e){
e.printStackTrace();
}
con.setAutoCommit(true);
}
public void debit_money(long account_number)throws SQLException{
sc.nextLine();
System.out.println("Enter account number");
double amount = sc.nextDouble();
sc.nextLine();
System.out.println("Enter Security pin:");
String Security_pin = sc.nextLine();
try{
con.setAutoCommit(false);
if(account_number != 0){}
PreparedStatement pstmt = con.prepareStatement("Select * from account where account_number = ? AND security_pin = ?");
pstmt.setLong(1, account_number);
pstmt.setString(2, Security_pin);
ResultSet rs = pstmt.executeQuery();
if(rs.next()){
String debit_query = "Update account set balance = balance - ? where account_number = ?";
PreparedStatement pstmt1 = con.prepareStatement(debit_query);
pstmt1.setDouble(1, amount);
pstmt1.setLong(2, account_number);
int rowsaffected = pstmt1.executeUpdate();
if(rowsaffected>0){
System.out.println("Rs" +amount+ " Amount credited successfully");
con.commit();
con.setAutoCommit(true);
return;
}else {
System.out.println("Transaction failed");
con.rollback();
con.setAutoCommit(true);
}
}else{
System.out.println("Invalid account number");
}
}
catch(SQLException e){
e.printStackTrace();
}
con.setAutoCommit(true);
}
public void transfer_money(long Sender_account_number)throws SQLException {
sc.nextLine();
System.out.println("Enter Receiver account number");
long Receiver_account_number = sc.nextLong();
System.out.println("Enter amount:");
double amount = sc.nextDouble();
sc.nextLine();
System.out.println("Enter Security pin:");
String Security_pin = sc.nextLine();
try {
con.setAutoCommit(false);
if (Sender_account_number != 0 && Receiver_account_number != 0) {
PreparedStatement pstmt = con.prepareStatement("select * from account where account_number = ? AND security_pin = ?");
pstmt.setLong(1, Sender_account_number);
pstmt.setString(2, Security_pin);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
double current_balance = rs.getDouble("balance");
if (amount <= current_balance) {
String debit_query = "Update accounts set balance = balance-? where account_number = ?";
String credit_query = "Update accounts set balance = balance+? where account_number = ?";
PreparedStatement creditpstmt = con.prepareStatement(credit_query);
PreparedStatement debitpstmt = con.prepareStatement(debit_query);
creditpstmt.setDouble(1, amount);
creditpstmt.setLong(2, Receiver_account_number);
debitpstmt.setDouble(1, amount);
debitpstmt.setLong(2, Receiver_account_number);
int rowsaffected1 = debitpstmt.executeUpdate();
int rowsaffected2 = creditpstmt.executeUpdate();
if (rowsaffected1 > 0 && rowsaffected2 > 0) {
System.out.println("transaction successful");
System.out.println("Rs." + amount + "transfered successfully");
con.commit();
con.setAutoCommit(true);
return;
} else {
System.out.println("transaction failed");
con.rollback();
con.setAutoCommit(true);
}
} else {
System.out.println("insufficient Balance");
}
} else {
System.out.println("invalid security pin");
}
}
}catch(SQLException e){
e.printStackTrace();
}
con.setAutoCommit(true);
}
public void get_balance(long account_number){
sc.nextLine();
System.out.println("enter security pin");
String security_pin = sc.nextLine();
try{
PreparedStatement pstmt= con.prepareStatement("Select balance from accounts where account_number= ? and security_pin = ?");
pstmt.setLong(1,account_number);
pstmt.setString(2,security_pin);
ResultSet rs=pstmt.executeQuery();
if(rs.next()){
double balance = rs.getDouble("balance");
System.out.println("balance" +balance);
}else{
System.out.println("invalid pin!!");
}
}catch(SQLException e){
e.printStackTrace();
}
}
}