-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAccounts.java
More file actions
116 lines (102 loc) · 4.08 KB
/
Accounts.java
File metadata and controls
116 lines (102 loc) · 4.08 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
package PROJECT.Banking;
import java.sql.*;
import java.util.Random;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Accounts {
private Connection connection;
private Scanner scanner;
public Accounts(Connection connection, Scanner scanner) {
this.connection = connection;
this.scanner = scanner;
}
public long open_account(String email) {
if (!account_exist(email)) {
String open_account_query = "INSERT INTO Accounts(account_number, full_name, email, balance, security_pin) VALUES(?, ?, ?, ?, ?)";
scanner.nextLine();
System.out.print("Enter Full Name: ");
String full_name = scanner.nextLine();
if (!user.isValidName(full_name)) {
throw new RuntimeException("Invalid Name");
}
System.out.print("Enter Initial Amount: ");
double balance = scanner.nextDouble();
scanner.nextLine();
System.out.print("Enter Security Pin: ");
String security_pin = scanner.nextLine();
try {
long account_number = generateAccountNumber();
PreparedStatement preparedStatement = connection.prepareStatement(open_account_query);
preparedStatement.setLong(1, account_number);
preparedStatement.setString(2, full_name);
preparedStatement.setString(3, email);
preparedStatement.setDouble(4, balance);
preparedStatement.setString(5, security_pin);
int rowsAffected = preparedStatement.executeUpdate();
if (rowsAffected > 0) {
return account_number;
} else {
throw new RuntimeException("Account Creation failed!!");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
throw new RuntimeException("Account Already Exist");
}
public long getAccount_number(String email) {
String query = "SELECT account_number from Accounts WHERE email = ?";
try {
PreparedStatement preparedStatement = connection.prepareStatement(query);
preparedStatement.setString(1, email);
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
return resultSet.getLong("account_number");
}
} catch (SQLException e) {
e.printStackTrace();
}
throw new RuntimeException("Account Number Doesn't Exist!");
}
private long getRandomNumberUsingNextLong() {
// try {
// Statement statement = connection.createStatement();
// ResultSet resultSet = statement.executeQuery("SELECT account_number from Accounts ORDER BY account_number DESC LIMIT 1");
// if (resultSet.next()) {
// long last_account_number = resultSet.getLong("account_number");
// return last_account_number + 1;
// } else {
// return 10000100;
// }
// } catch (SQLException e) {
// e.printStackTrace();
// }
// return 10000100;
Random generator = new Random(System.currentTimeMillis());
return generator.nextLong() % 1000000000000L;
}
public boolean account_exist(String email) {
String query = "SELECT account_number from Accounts WHERE email = ?";
try {
PreparedStatement preparedStatement = connection.prepareStatement(query);
preparedStatement.setString(1, email);
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
return true;
} else {
return false;
}
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
public long generateAccountNumber(){
long Acc=getRandomNumberUsingNextLong();
if(Acc<=0){
return (Acc*(-1));
}else
return Acc;
}
}