-
Notifications
You must be signed in to change notification settings - Fork 0
/
Registration.java
292 lines (245 loc) · 10 KB
/
Registration.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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import java.lang.*;
import java.io.*;
import java.nio.file.*;
import java.time.*;
import java.time.format.*;
@SuppressWarnings("deprecation")
public class Registration extends JFrame {
private Container c;
private ImageIcon icon, logo;
private JLabel label1, imgLabel;
private Font f1, f2, f3, f4, f5, f6;
private JTextField tf1, tf2, tf4, tf5;
private JComboBox securityQsn;
private JButton btn1, btn2, btn3, btn4, nBtn;
private JPasswordField tf3;
private Cursor cursor;
private int a, b;
@SuppressWarnings("unchecked")
Registration() {
// Frame Layout
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("Rent Management");
this.setSize(900, 450);
this.setLocationRelativeTo(null);
this.setResizable(false);
c = this.getContentPane();
c.setLayout(null);
c.setBackground(Color.decode("#F2F2F2"));
// Icon
icon = new ImageIcon(getClass().getResource("/images/icon.png"));
this.setIconImage(icon.getImage());
// Logo
logo = new ImageIcon(getClass().getResource("/images/Logo.jpg"));
imgLabel = new JLabel(logo);
imgLabel.setBounds(10, 10, logo.getIconWidth(), logo.getIconHeight());
c.add(imgLabel);
// Fonts
f1 = new Font("Segoe UI Black", Font.PLAIN, 35);
f2 = new Font("Segoe UI Black", Font.PLAIN, 25);
f3 = new Font("Segoe UI Semibold", Font.PLAIN, 35);
f4 = new Font("Segoe UI", Font.PLAIN, 25);
f5 = new Font("Segoe UI", Font.PLAIN, 19);
f6 = new Font("Segoe UI", Font.PLAIN, 25);
// Title
label1 = new JLabel();
label1.setText("Enter Your Information");
label1.setBounds(430, 25, 500, 50);
label1.setFont(f1);
c.add(label1);
// User Name
label1 = new JLabel();
label1.setText("User Name");
label1.setBounds(430, 75, 500, 50);
label1.setFont(f4);
c.add(label1);
tf1 = new JTextField();
tf1.setBounds(570, 85, 260, 30);
tf1.setFont(f5);
c.add(tf1);
// Email
label1 = new JLabel();
label1.setText("Email");
label1.setBounds(430, 110, 500, 50);
label1.setFont(f4);
c.add(label1);
tf2 = new JTextField();
tf2.setBounds(570, 120, 260, 30);
tf2.setFont(f5);
c.add(tf2);
// Password
label1 = new JLabel();
label1.setText("Password");
label1.setBounds(430, 145, 500, 50);
label1.setFont(f4);
c.add(label1);
tf3 = new JPasswordField();
tf3.setBounds(570, 155, 260, 30);
tf3.setFont(f2);
tf3.setEchoChar('*');
c.add(tf3);
// Question
label1 = new JLabel();
label1.setText("Question");
label1.setBounds(430, 180, 500, 50);
label1.setFont(f4);
c.add(label1);
String[] secQsn = { "Choose a Security Question...", "Your dream job?", "Your favorite song?",
"First pet's name?", "Your favorite hobby?" };
securityQsn = new JComboBox(secQsn);
securityQsn.setBounds(570, 190, 259, 30);
securityQsn.setSelectedIndex(0);
securityQsn.setFont(f5);
securityQsn.setBackground(Color.white);
c.add(securityQsn);
// Answer
label1 = new JLabel();
label1.setText("Answer");
label1.setBounds(430, 215, 500, 50);
label1.setFont(f4);
c.add(label1);
tf4 = new JTextField();
tf4.setBounds(570, 225, 260, 30);
tf4.setFont(f5);
c.add(tf4);
// Captcha Label and Text Field
label1 = new JLabel();
label1.setText("Captcha");
label1.setBounds(430, 250, 500, 50);
label1.setFont(f4);
c.add(label1);
tf5 = new JTextField();
tf5.setBounds(615, 260, 215, 30);
tf5.setFont(f5);
c.add(tf5);
// To get a random number for captcha
Random rand = new Random();
int a = rand.nextInt(10);
int b = rand.nextInt(10);
// Captcha
label1 = new JLabel();
label1.setText(" " + a + " + " + b + " ");
label1.setBounds(530, 260, 75, 30);
label1.setFont(f4);
label1.setForeground(Color.red);
label1.setBackground(Color.decode("#FFD3D3"));
label1.setOpaque(true);
c.add(label1);
// Cursor for JButtons
cursor = new Cursor(Cursor.HAND_CURSOR);
// JButtons
btn1 = new JButton("Exit");
btn1.setBounds(53, 325, 183, 50);
btn1.setFont(f2);
btn1.setCursor(cursor);
btn1.setForeground(Color.WHITE);
btn1.setBackground(Color.decode("#C00000"));
c.add(btn1);
btn2 = new JButton("Back");
btn2.setBounds(251, 325, 183, 50);
btn2.setFont(f2);
btn2.setCursor(cursor);
btn2.setForeground(Color.WHITE);
btn2.setBackground(Color.decode("#226135"));
c.add(btn2);
btn3 = new JButton("Reset");
btn3.setBounds(450, 325, 183, 50);
btn3.setFont(f2);
btn3.setCursor(cursor);
btn3.setForeground(Color.WHITE);
btn3.setBackground(Color.decode("#226135"));
c.add(btn3);
btn4 = new JButton("Register");
btn4.setBounds(649, 325, 183, 50);
btn4.setFont(f2);
btn4.setCursor(cursor);
btn4.setForeground(Color.WHITE);
btn4.setBackground(Color.decode("#226135"));
c.add(btn4);
nBtn = new JButton("");
nBtn.setBounds(0, 0, 0, 0);
c.add(nBtn);
// Exit Button
btn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
System.exit(0);
}
});
// Back Button
btn2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
setVisible(false);
Home frame = new Home();
frame.setVisible(true);
}
});
// Reset Button
btn3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
setVisible(false);
Registration frame = new Registration();
frame.setVisible(true);
}
});
// Register Button
btn4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
String textField1 = tf1.getText().toLowerCase(); // User Name
String textField2 = tf2.getText(); // Email
String textField3 = tf3.getText(); // Password
String textField4 = tf4.getText(); // Security Question Answer
String textField5 = tf5.getText(); // Captcha
String secQsn = String.valueOf(securityQsn.getSelectedItem()); // Security Question
int result = 0;
if (textField5.isEmpty()) {
JOptionPane.showMessageDialog(null, "Please fill all of the fields.", "Warning!",
JOptionPane.WARNING_MESSAGE);
} else {
result = Integer.parseInt(tf5.getText());
if (textField1.isEmpty() || textField2.isEmpty() || textField3.isEmpty() || textField4.isEmpty() || textField5.isEmpty() || ((securityQsn.getSelectedIndex()) == 0)) {
JOptionPane.showMessageDialog(null, "Please fill all of the fields.", "Warning!",
JOptionPane.WARNING_MESSAGE);
} else if (result != (a + b)) {
JOptionPane.showMessageDialog(null, "Wrong Captcha.", "Warning!", JOptionPane.WARNING_MESSAGE);
} else {
try {
File file = new File(".\\Data\\userData.txt");
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file, true);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter pw = new PrintWriter(bw);
LocalDateTime myDateObj = LocalDateTime.now();
DateTimeFormatter myFormatObj = DateTimeFormatter.ofPattern("HH:mm a, dd/MM/yyyy");
String timeAndDate = myDateObj.format(myFormatObj);
pw.println("User Name : " + textField1);
pw.println("Password : " + textField3);
pw.println("Email : " + textField2);
pw.println("Security Question : " + secQsn);
pw.println("Answer : " + textField4);
pw.println("Time & Date : " + timeAndDate);
pw.println("===============================================");
pw.close();
} catch (Exception ex) {
System.out.print(ex);
}
JOptionPane.showMessageDialog(null, "Registration Successfully Completed.",
"Registration Complete", JOptionPane.WARNING_MESSAGE);
setVisible(false);
Home frame = new Home();
frame.setVisible(true);
}
}
}
});
}
public static void main(String[] args) {
Registration frame = new Registration();
frame.setVisible(true);
}
}