-
Notifications
You must be signed in to change notification settings - Fork 0
/
User.java
108 lines (95 loc) · 4.29 KB
/
User.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
import java.io.Console;
import java.sql.*;
import java.util.Scanner;
public class User extends AirlinesReservationSystem {
public static String username;
public static String userFirstName;
public static String userLastName;
public static String userContactNumber;
public static String password;
public String role;
public int numberOfSeats;
public static int userId;
private static Scanner scanner = new Scanner(System.in);
// function to handle user login
public boolean userLogin(String role) throws Exception {
Console console = System.console();
// Loop until a valid username is entered
do {
showAppTitle();
showDisplayMessage();
System.out.print("\n\t\t\t\tEnter your username: ");
username = scanner.nextLine();
// If the username is not valid, display an error message
if (!checkUsername(username)) {
setDisplayMessage(red + "\t!!OOPS! The username is not correct!!" + reset);
}
} while (!checkUsername(username));
System.out.print("\t\t\t\tEnter your password: ");
char[] passwordArray = console.readPassword();
password = new String(passwordArray);
java.util.Arrays.fill(passwordArray, ' ');
// If the user is authenticated successfully
if (authenticateUser(username, password, role)) {
// Query the database for the user's details
ResultSet user = Database.databaseQuery("select * from users where username = ?;", username);
user.next();
// Store the user's details in local variables
userId = user.getInt("id");
userFirstName = user.getString("firstname");
userLastName = user.getString("lastname");
user.close();
// Return true to indicate successful login
return true;
} else {
// If the authentication failed, display an error message and return false
setDisplayMessage(red + "\tERROR! The username or password is incorrect" + reset);
return false;
}
}
// function to register a new user
public static void registerUser(String role) throws Exception {
// Loop until a unique username is entered
String username;
do {
showAppTitle();
showDisplayMessage();
System.out.print("\n\t\t\t\tEnter a username: ");
username = scanner.nextLine();
// If the username is already taken, display an error message
if (checkUsername(username)) {
setDisplayMessage(red + "\t!!OOPS! The username is already taken!!" + reset);
}
} while (checkUsername(username));
System.out.print("\t\t\t\tEnter your password: ");
String password = scanner.nextLine();
System.out.print("\t\t\t\tEnter your first name: ");
String userFirstName = scanner.nextLine();
System.out.print("\t\t\t\tEnter your last name: ");
String userLastName = scanner.nextLine();
System.out.print("\t\t\t\tEnter your contact number: ");
String userContactNumber = scanner.nextLine();
// random 7 digit user id
String userId = String.valueOf((int) (Math.random() * 10000000));
// Insert the new user's details into the database
Database.databaseQuery(
"insert into users ( id ,username, password, firstname, lastname, role,phone_no) values (?,?,?,?,?,?,?);",
userId, username, password, userFirstName, userLastName, role, userContactNumber);
// Display a success message
setDisplayMessage(green + "\tUser registered successfully" + reset);
}
public boolean authenticateUser(String username, String password, String role) throws Exception {
if (Database.databaseQuery("SELECT * FROM users WHERE username = ? AND password = ? AND role = ?;", username,
password, role) != null) {
return true;
}
return false;
}
public static boolean checkUsername(String username) throws Exception {
if (Database.databaseQuery("SELECT * FROM users WHERE username = ?;", username) != null) {
return true;
} else {
return false;
}
}
}