-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAccountManager.java
More file actions
182 lines (170 loc) · 8.44 KB
/
AccountManager.java
File metadata and controls
182 lines (170 loc) · 8.44 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
package PROJECT.Banking;
import java.sql.*;
import java.util.Scanner;
public class AccountManager {
private Connection connection;
private Scanner scanner;
AccountManager(Connection connection, Scanner scanner){
this.connection = connection;
this.scanner = scanner;
}
public void credit_money(long account_number)throws SQLException {
scanner.nextLine();
System.out.print("Enter Amount: ");
double amount = scanner.nextDouble();
scanner.nextLine();
System.out.print("Enter Security Pin: ");
String security_pin = scanner.nextLine();
try {
connection.setAutoCommit(false);
if(account_number != 0) {
PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM Accounts WHERE account_number = ? and security_pin = ? ");
preparedStatement.setLong(1, account_number);
preparedStatement.setString(2, security_pin);
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
String credit_query = "UPDATE Accounts SET balance = balance + ? WHERE account_number = ?";
PreparedStatement preparedStatement1 = connection.prepareStatement(credit_query);
preparedStatement1.setDouble(1, amount);
preparedStatement1.setLong(2, account_number);
int rowsAffected = preparedStatement1.executeUpdate();
if (rowsAffected > 0) {
System.out.println("Rs."+amount+" credited Successfully");
connection.commit();
connection.setAutoCommit(true);
return;
} else {
System.out.println("Transaction Failed!");
connection.rollback();
connection.setAutoCommit(true);
}
}else{
System.out.println("Invalid Security Pin!");
}
}
}catch (SQLException e){
e.printStackTrace();
}
connection.setAutoCommit(true);
}
public void debit_money(long account_number) throws SQLException {
scanner.nextLine();
System.out.print("Enter Amount: ");
double amount = scanner.nextDouble();
scanner.nextLine();
System.out.print("Enter Security Pin: ");
String security_pin = scanner.nextLine();
try {
connection.setAutoCommit(false);
if(account_number!=0) {
PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM Accounts WHERE account_number = ? and security_pin = ? ");
preparedStatement.setLong(1, account_number);
preparedStatement.setString(2, security_pin);
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
double current_balance = resultSet.getDouble("balance");
if (amount<=current_balance){
String debit_query = "UPDATE Accounts SET balance = balance - ? WHERE account_number = ?";
PreparedStatement preparedStatement1 = connection.prepareStatement(debit_query);
preparedStatement1.setDouble(1, amount);
preparedStatement1.setLong(2, account_number);
int rowsAffected = preparedStatement1.executeUpdate();
if (rowsAffected > 0) {
System.out.println("Rs."+amount+" debited Successfully");
connection.commit();
connection.setAutoCommit(true);
return;
} else {
System.out.println("Transaction Failed!");
connection.rollback();
connection.setAutoCommit(true);
}
}else{
System.out.println("Insufficient Balance!");
}
}else{
System.out.println("Invalid Pin!");
}
}
}catch (SQLException e){
e.printStackTrace();
}
connection.setAutoCommit(true);
}
public void transfer_money(long sender_account_number) throws SQLException {
scanner.nextLine();
System.out.print("Enter Receiver Account Number: ");
long receiver_account_number = scanner.nextLong();
System.out.print("Enter Amount: ");
double amount = scanner.nextDouble();
scanner.nextLine();
System.out.print("Enter Security Pin: ");
String security_pin = scanner.nextLine();
try{
connection.setAutoCommit(false);
if(sender_account_number!=0 && receiver_account_number!=0){
PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM Accounts WHERE account_number = ? AND security_pin = ? ");
preparedStatement.setLong(1, sender_account_number);
preparedStatement.setString(2, security_pin);
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
double current_balance = resultSet.getDouble("balance");
if (amount<=current_balance){
// Write debit and credit queries
String debit_query = "UPDATE Accounts SET balance = balance - ? WHERE account_number = ?";
String credit_query = "UPDATE Accounts SET balance = balance + ? WHERE account_number = ?";
// Debit and Credit prepared Statements
PreparedStatement creditPreparedStatement = connection.prepareStatement(credit_query);
PreparedStatement debitPreparedStatement = connection.prepareStatement(debit_query);
// Set Values for debit and credit prepared statements
creditPreparedStatement.setDouble(1, amount);
creditPreparedStatement.setLong(2, receiver_account_number);
debitPreparedStatement.setDouble(1, amount);
debitPreparedStatement.setLong(2, sender_account_number);
int rowsAffected1 = debitPreparedStatement.executeUpdate();
int rowsAffected2 = creditPreparedStatement.executeUpdate();
if (rowsAffected1 > 0 && rowsAffected2 > 0) {
System.out.println("Transaction Successful!");
System.out.println("Rs."+amount+" Transferred Successfully");
connection.commit();
connection.setAutoCommit(true);
return;
} else {
System.out.println("Transaction Failed");
connection.rollback();
connection.setAutoCommit(true);
}
}else{
System.out.println("Insufficient Balance!");
}
}else{
System.out.println("Invalid Security Pin!");
}
}else{
System.out.println("Invalid account number");
}
}catch (SQLException e){
e.printStackTrace();
}
connection.setAutoCommit(true);
}
public void getBalance(long account_number){
scanner.nextLine();
System.out.print("Enter Security Pin: ");
String security_pin = scanner.nextLine();
try{
PreparedStatement preparedStatement = connection.prepareStatement("SELECT balance FROM Accounts WHERE account_number = ? AND security_pin = ?");
preparedStatement.setLong(1, account_number);
preparedStatement.setString(2, security_pin);
ResultSet resultSet = preparedStatement.executeQuery();
if(resultSet.next()){
double balance = resultSet.getDouble("balance");
System.out.println("Balance: "+balance);
}else{
System.out.println("Invalid Pin!");
}
}catch (SQLException e){
e.printStackTrace();
}
}
}