-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuthentication.java
More file actions
199 lines (150 loc) · 5.88 KB
/
Authentication.java
File metadata and controls
199 lines (150 loc) · 5.88 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import java.util.*;
import javax.swing.*;
public class Authentication{
public static void main(String[] args){
Scanner console = new Scanner(System.in);
Admin admin = new Admin();
Account student = new Account();
String userInput = "";
String passInput = "";
String firstname = "";
String middlename = "";
String lastname = "";
String course = "";
while(true){
try{
int choice = Integer.parseInt(JOptionPane.showInputDialog("Online Enrollment\n[1]Login\n[2]Create Account\n[3]Admin\n[4]Exit"));
if(choice == 1){
userInput = JOptionPane.showInputDialog("Enter username");
passInput = JOptionPane.showInputDialog("Enter password");
student.access(userInput,passInput);
userInput = "";
passInput = "";
}
else if(choice == 2){
//Account Creation
firstname = JOptionPane.showInputDialog("Enter firstname");
middlename = JOptionPane.showInputDialog("Enter middlename");
lastname = JOptionPane.showInputDialog("Enter lastname");
course = JOptionPane.showInputDialog("Enter course");
userInput = JOptionPane.showInputDialog("Enter username");
if(student.verifier(userInput))
JOptionPane.showMessageDialog(null,"Username already existed!");
else{
passInput = JOptionPane.showInputDialog("Enter password");
new Account(userInput, passInput);
new Account(firstname, middlename, lastname, course);
userInput = "";
passInput = "";
firstname = "";
middlename = "";
lastname = "";
course = "";
JOptionPane.showMessageDialog(null,"Successful!");
}
}
else if(choice == 3){
while(userInput.isEmpty() && passInput.isEmpty() && true){
userInput = JOptionPane.showInputDialog("Enter admin username");
passInput = JOptionPane.showInputDialog("Enter admin password");
if(userInput.compareTo(admin.getAdmin()) == 0 && passInput.compareTo(admin.getPass()) == 0){
userInput = "";
passInput = "";
break;
}
else{
JOptionPane.showMessageDialog(null,"Error, Please try again!");
userInput = "";
passInput = "";
}
}
JOptionPane.showMessageDialog(null,"Login");
while(true){
int choices = Integer.parseInt(JOptionPane.showInputDialog("[1] View Data\n[2]Exit"));
if(choices == 1){
admin.accessData();
}
else if(choices == 2)
break;
else
JOptionPane.showMessageDialog(null,"Error, Please try again!");
}
}
else if(choice == 4)
break;
else
JOptionPane.showMessageDialog(null,"Error, Please try again!");
}catch(Exception e){
JOptionPane.showMessageDialog(null,"Error, Please try again!");
}
}
}
}
class Account{
private static String registrationId = "";
private static String studentUser = "";
private static String studentPass = "";
private static String firstname = "";
private static String middlename = "";
private static String lastname = "";
private static String idNum = "";
private static String course = "";
private static String information = "";
Random rand = new Random();
private static HashMap <String, String> studentAccount = new HashMap<String, String>();
private static HashMap <String, String> studentInformation = new HashMap<String, String>();
Account(){
}
Account(String firstname, String middlename, String lastname,String course){
this.firstname = firstname;
this.middlename = middlename;
this.lastname = lastname;
this.course = course;
setInformation();
}
Account(String studentUser, String studentPass){
this.studentUser = studentUser;
this.studentPass = studentPass;
}
public void setInformation(){
this.information = "First Name: " + this.firstname +"\nMiddle Name: " + this.middlename + "\nLast Name: "+ this.lastname +"\nID Number: #"+getIdNum()+"\nCourse: "+course+"\nStatus: Enrolled";
studentInformation.put(this.studentUser, this.information);
studentAccount.put(this.studentUser, this.studentPass);
}
public void access(String username , String password){
if(this.studentAccount.get(username) != null && this.studentAccount.get(username).compareTo(password) == 0)
JOptionPane.showMessageDialog(null,studentInformation.get(username));
else
JOptionPane.showMessageDialog(null,"Invalid Username or Password!");
}
public boolean verifier(String username){
return studentAccount.containsKey(username);
}
public void viewStudentData(){
String display = "";
for(String x : studentInformation.values())
display += x+"\n\n";
JOptionPane.showMessageDialog(null,display);
}
private String getIdNum(){
String num = "123456789";
String ref = "";
for(int i = 0; i < 8; i++)
ref += num.charAt(rand.nextInt(num.length()));
return ref;
}
}
class Admin{
Account student = new Account();
private String adminUser = "admin";
private String adminPass = "admin";
public String getAdmin(){
return this.adminUser;
}
public String getPass(){
return this.adminPass;
}
public void accessData(){
student.viewStudentData();
}
}