-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser.java
More file actions
157 lines (147 loc) · 5.4 KB
/
user.java
File metadata and controls
157 lines (147 loc) · 5.4 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
package PROJECT.Banking;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class user {
private Connection connection;
private Scanner scanner;
public user(Connection connection, Scanner scanner){
this.connection = connection;
this.scanner = scanner;
}
public void register(){
scanner.nextLine();
System.out.print("Full Name: ");
String full_name = scanner.nextLine();
if(!isValidName(full_name)) {
System.out.println("Invalid Name!");
return;
}
System.out.print("Email: ");
String email = scanner.nextLine();
if(!isValidEmail(email)) {
System.out.println("Invalid Email!");
return;
}
System.out.print("Password: ");
String password = scanner.nextLine();
if(!isValidPassword(password)) {
System.out.println("Invalid Password!");
return;
}
if(user_exist(email)) {
System.out.println("User Already Exists for this Email Address!!");
return;
}
String register_query = "INSERT INTO User(full_name, email, password) VALUES(?, ?, ?)";
try {
PreparedStatement preparedStatement = connection.prepareStatement(register_query);
preparedStatement.setString(1, full_name);
preparedStatement.setString(2, email);
preparedStatement.setString(3, password);
int affectedRows = preparedStatement.executeUpdate();
if (affectedRows > 0) {
System.out.println("Registration Successfull!");
} else {
System.out.println("Registration Failed!");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public String login(){
scanner.nextLine();
System.out.print("Email: ");
String email = scanner.nextLine();
System.out.print("Password: ");
String password = scanner.nextLine();
String login_query = "SELECT * FROM User WHERE email = ? AND password = ?";
try{
PreparedStatement preparedStatement = connection.prepareStatement(login_query);
preparedStatement.setString(1, email);
preparedStatement.setString(2, password);
ResultSet resultSet = preparedStatement.executeQuery();
if(resultSet.next()){
return email;
}else{
return null;
}
}catch (SQLException e){
e.printStackTrace();
}
return null;
}
public boolean user_exist(String email){
String query = "SELECT * FROM user 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 boolean close_account(){
scanner.nextLine();
System.out.print("Enter Email: ");
String email=scanner.nextLine();
// System.out.print("Enter Security Pin: ");
// String password=scanner.nextLine();
// String query1="SELECT account_number from Accounts WHERE email = ? and security_pin = ?";
String query = "DELETE acc, us " +
"FROM accounts AS acc " +
"INNER JOIN user AS us " +
"ON acc.email = us.email " +
"WHERE acc.email = ?";
try {
PreparedStatement preparedStatement = connection.prepareStatement(query);
preparedStatement.setString(1, email);
// preparedStatement.setString(2, password);
// ResultSet resultSet = preparedStatement.executeQuery();
int rowsAffected = preparedStatement.executeUpdate();
if (rowsAffected > 0) {
// PreparedStatement preparedStatement1 = connection.prepareStatement(query);
// preparedStatement1.setString(1, email);
// preparedStatement1.setString(2, password);
// preparedStatement1.executeUpdate();
return true;
}
else {
return false;
}
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
public static boolean isValidName(String name) {
String regex = "^[a-zA-Z ]+$";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(name);
return m.matches();
}
public boolean isValidEmail(String email) {
String emailRegex= "^[a-zA-Z0-9]+@[a-zA-Z0-9]+\\.[a-zA-Z]{2,}$";
Pattern p = Pattern.compile(emailRegex);
Matcher m = p.matcher(email);
return m.matches();
}
public boolean isValidPassword( String password) {
String pw= "^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{8,}$";
Pattern p= Pattern.compile(pw);
Matcher m= p.matcher(password);
boolean b= m.matches();
return b;
}
}