-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHealthTrackerGUI.java
More file actions
263 lines (225 loc) · 9.7 KB
/
HealthTrackerGUI.java
File metadata and controls
263 lines (225 loc) · 9.7 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
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
package HealthTracker;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.sql.*;
import java.util.HashMap;
import static HealthTracker.DatabaseConnection.getConnection;
public class HealthTrackerGUI {
public static final HashMap<String, String> userCredentials = new HashMap<>();
static {
checkUserCredentials();
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setTitle("Personal Health Tracker Login");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
JLabel titleLabel = new JLabel("Personal Health Tracker", SwingConstants.CENTER);
titleLabel.setFont(new Font("Arial", Font.BOLD, 24));
JPanel northPanel = new JPanel(new BorderLayout());
JPanel titlePanel = new JPanel(new BorderLayout());
titlePanel.add(titleLabel, BorderLayout.CENTER);
northPanel.add(titlePanel, BorderLayout.CENTER);
frame.getContentPane().add(northPanel, BorderLayout.NORTH);
JPanel centerPanel = new JPanel(new GridBagLayout());
frame.add(centerPanel, BorderLayout.CENTER);
loginComponents(centerPanel);
frame.setVisible(true);
}
public static void loginComponents(JPanel panel) {
GridBagConstraints gbc = new GridBagConstraints();
JLabel userLabel = new JLabel("User ID:");
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.EAST;
panel.add(userLabel, gbc);
JTextField userText = new JTextField(20);
gbc.gridx = 1;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.WEST;
panel.add(userText, gbc);
JLabel password = new JLabel("Password:");
gbc.gridx = 0;
gbc.gridy = 1;
gbc.anchor = GridBagConstraints.EAST;
panel.add(password, gbc);
JPasswordField passwordText = new JPasswordField(20);
gbc.gridx = 1;
gbc.gridy = 1;
gbc.anchor = GridBagConstraints.WEST;
panel.add(passwordText, gbc);
JButton loginButton = new JButton("Login");
gbc.gridx = 0;
gbc.gridy = 2;
gbc.gridwidth = 2;
gbc.insets = new Insets(20, 0, 10, 0);
gbc.anchor = GridBagConstraints.CENTER;
panel.add(loginButton, gbc);
JButton signUpButton = new JButton("Sign Up");
gbc.gridx = 0;
gbc.gridy = 3;
gbc.gridwidth = 2;
gbc.insets = new Insets(20, 0, 10, 0);
gbc.anchor = GridBagConstraints.CENTER;
panel.add(signUpButton, gbc);
loginButton.addActionListener(e -> {
String user = userText.getText();
String pw = new String(passwordText.getPassword());
if (user.equals("admin") && pw.equals("admin123")) {
JOptionPane.showMessageDialog(panel, "Admin Login Successful");
AdminScreen.showAdminScreen();
return;
}
try {
if (userCredentials.get(user).equals(pw)) {
JOptionPane.showMessageDialog(panel, "Login successful");
MainScreen.showMainScreen(user);
} else {
JOptionPane.showMessageDialog(panel, "User Does Not Exist Please Sign Up");
}
} catch (NullPointerException ex) {
JOptionPane.showMessageDialog(panel, "User Does Not Exist Please Sign Up");
}
});
signUpButton.addActionListener(e -> {
try {
showNewUserDialog(panel);
} catch (Exception ex1) {
ex1.printStackTrace();
}
});
}
public static void showNewUserDialog(JPanel panel) throws Exception {
File f = new File("D:\\code\\user.txt");
BufferedWriter bw = new BufferedWriter(new FileWriter(f, true));
JDialog dialog = new JDialog((Frame) null, "New User Sign Up", true);
dialog.setSize(400, 300);
dialog.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
JLabel newUserLabel = new JLabel("User ID:");
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.EAST;
dialog.add(newUserLabel, gbc);
JTextField newUserText = new JTextField(25);
gbc.gridx = 1;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.WEST;
gbc.insets = new Insets(5, 5, 5, 5);
dialog.add(newUserText, gbc);
JLabel name = new JLabel("Full Name:");
gbc.gridx = 0;
gbc.gridy = 1;
gbc.anchor = GridBagConstraints.EAST;
dialog.add(name, gbc);
JTextField nameText = new JTextField(25);
gbc.gridx = 1;
gbc.gridy = 1;
gbc.anchor = GridBagConstraints.WEST;
gbc.insets = new Insets(5, 5, 5, 5);
dialog.add(nameText, gbc);
JLabel newpassword = new JLabel("Set Password:");
gbc.gridx = 0;
gbc.gridy = 2;
gbc.anchor = GridBagConstraints.EAST;
dialog.add(newpassword, gbc);
JPasswordField newPasswordText = new JPasswordField(25);
gbc.gridx = 1;
gbc.gridy = 2;
gbc.anchor = GridBagConstraints.WEST;
gbc.insets = new Insets(5, 5, 5, 5);
dialog.add(newPasswordText, gbc);
JButton signUpButton = new JButton("Sign Up");
gbc.gridx = 0;
gbc.gridy = 3;
gbc.gridwidth = 2;
gbc.insets = new Insets(20, 0, 10, 0);
gbc.anchor = GridBagConstraints.CENTER;
dialog.add(signUpButton, gbc);
signUpButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String newUser = newUserText.getText();
String name = nameText.getText();
String newPwd = new String(newPasswordText.getPassword());
if (name.matches("[a-zA-Z ]+")){
try (BufferedReader br = new BufferedReader(new FileReader(f))) {
String line;
while ((line = br.readLine()) != null) {
if (!line.startsWith("User ID:")) continue;
String user_id = line.split(":")[1].trim();
line = br.readLine();
if (line == null || !line.startsWith("Password:")) continue;
String password = line.split(":")[1].trim();
if (user_id.equals(newUser) && password.equals(newPwd)) {
JOptionPane.showMessageDialog(dialog, "User ID already exists.", "Error",
JOptionPane.ERROR_MESSAGE);
return;
}
}
} catch (Exception e1) {
e1.printStackTrace();
}
if (!newUser.isEmpty() && !newPwd.isEmpty()) {
try {
bw.write("User ID: " + newUser);
bw.newLine();
bw.write("Password: " + newPwd);
bw.newLine();
userCredentials.put(newUser, newPwd);
String sql = "{call insert_user(?, ?, ?)}";
try (Connection con = getConnection();
CallableStatement cstmt = con.prepareCall(sql)) {
cstmt.setString(1, newUser);
cstmt.setString(2, name);
cstmt.setString(3, newPwd);
cstmt.executeUpdate();
} catch (Exception e2) {
e2.printStackTrace();
}
} catch (IOException e1) {
e1.printStackTrace();
}
JOptionPane.showMessageDialog(panel, "Sign Up successful! You can now log in.");
dialog.dispose();
} else {
JOptionPane.showMessageDialog(dialog, "User ID or password are Empty.", "Error",
JOptionPane.ERROR_MESSAGE);
}
}
}
});
dialog.setLocationRelativeTo(panel);
dialog.setVisible(true);
bw.flush();
bw.close();
}
public static void checkUserCredentials() {
File f = new File("D:\\code\\user.txt");//stores the user id and pass
try {
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
String line;
String user = null;
String password = null;
while ((line = br.readLine()) != null) {
if (line.startsWith("User ID: ")) {
user = line.split(": ")[1];
} else if (line.startsWith("Password: ")) {
password = line.split(": ")[1];
}
if (user != null && password != null) {
userCredentials.put(user, password);
user = null;
password = null;
}
}
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}