forked from shivi13102/FinWise
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSettingsPanel.java
156 lines (132 loc) · 7.29 KB
/
SettingsPanel.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
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class SettingsPanel extends JPanel {
private Main mainFrame;
public SettingsPanel(Main mainFrame) {
this.mainFrame = mainFrame;
setLayout(new BorderLayout());
// Load and resize the image
ImageIcon originalIcon = new ImageIcon("E:/SEMESTER SUBJECTS/3rd SEMESTER/ADVANCE OOPS/MINI PROJECT/budget_app/Photos/Settings.jpg"); // Replace with your image path
Image originalImage = originalIcon.getImage();
Image resizedImage = originalImage.getScaledInstance(1300, 640, Image.SCALE_SMOOTH); // Adjust size as needed
ImageIcon resizedIcon = new ImageIcon(resizedImage);
// Create a panel for the image
JPanel imagePanel = new JPanel();
imagePanel.add(new JLabel(resizedIcon));
add(imagePanel, BorderLayout.NORTH);
// Create a panel for buttons
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(3, 1, 10, 8));
// Create buttons
JButton signUpButton = new JButton("Sign Up");
JButton logInButton = new JButton("Log In");
JButton logOutButton = new JButton("Log Out");
// Add buttons to the button panel
buttonPanel.add(signUpButton);
buttonPanel.add(logInButton);
buttonPanel.add(logOutButton);
// Add button panel to the settings panel
add(buttonPanel, BorderLayout.CENTER);
// Add action listeners for buttons
signUpButton.addActionListener(e -> handleSignUp());
logInButton.addActionListener(e -> handleLogIn());
logOutButton.addActionListener(e -> handleLogOut());
}
private void handleSignUp() {
// Collect user details
String name = JOptionPane.showInputDialog(SettingsPanel.this, "Enter your name:");
String username = JOptionPane.showInputDialog(SettingsPanel.this, "Enter your username:");
// Create a password field to get masked password input
JPasswordField passwordField = new JPasswordField();
int option = JOptionPane.showConfirmDialog(SettingsPanel.this, passwordField, "Enter your password:", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
if (option == JOptionPane.OK_OPTION) {
String password = new String(passwordField.getPassword());
if (name != null && username != null && password != null && !password.isEmpty()) {
try {
// Check if user already exists
Connection conn = connect();
String checkQuery = "SELECT * FROM user WHERE username = ?";
PreparedStatement checkStmt = conn.prepareStatement(checkQuery);
checkStmt.setString(1, username);
ResultSet rs = checkStmt.executeQuery();
if (rs.next()) {
JOptionPane.showMessageDialog(SettingsPanel.this, "User already has an account. Please login.");
} else {
// Insert new user
String insertQuery = "INSERT INTO user (full_name, username, password) VALUES (?, ?, ?)";
PreparedStatement insertStmt = conn.prepareStatement(insertQuery);
insertStmt.setString(1, name);
insertStmt.setString(2, username);
insertStmt.setString(3, password); // Consider hashing the password for security
insertStmt.executeUpdate();
JOptionPane.showMessageDialog(SettingsPanel.this, "Sign Up successful!");
}
conn.close();
} catch (Exception ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(SettingsPanel.this, "An error occurred. Please try again.");
}
} else {
JOptionPane.showMessageDialog(SettingsPanel.this, "All fields are required.");
}
}
}
private void handleLogIn() {
// Collect login details
String username = JOptionPane.showInputDialog(SettingsPanel.this, "Enter your username:");
// Create a password field to get masked password input
JPasswordField passwordField = new JPasswordField();
int option = JOptionPane.showConfirmDialog(SettingsPanel.this, passwordField, "Enter your password:", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
if (option == JOptionPane.OK_OPTION) {
String password = new String(passwordField.getPassword());
if (username != null && password != null && !username.isEmpty() && !password.isEmpty()) {
try {
// Check user credentials
Connection conn = connect();
String loginQuery = "SELECT * FROM user WHERE username = ? AND password = ?";
PreparedStatement loginStmt = conn.prepareStatement(loginQuery);
loginStmt.setString(1, username);
loginStmt.setString(2, password);
ResultSet rs = loginStmt.executeQuery();
if (rs.next()) {
JOptionPane.showMessageDialog(SettingsPanel.this, "You are logged in!");
// Update the sidebar with user details
String name = rs.getString("full_name");
String userID = rs.getString("username"); // or other relevant field
mainFrame.updateUserName(name);
mainFrame.updateUserID(userID);
// Redirect to the dashboard
mainFrame.getCardLayout().show(mainFrame.getContentPanel(), "Dashboard");
} else {
JOptionPane.showMessageDialog(SettingsPanel.this, "You do not have an account. Please sign up.");
}
conn.close();
} catch (Exception ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(SettingsPanel.this, "An error occurred. Please try again.");
}
} else {
JOptionPane.showMessageDialog(SettingsPanel.this, "Both fields are required.");
}
}
}
private void handleLogOut() {
// Switch to the initial panel
mainFrame.getCardLayout().show(mainFrame.getContentPanel(), "Dashboard"); // Change this to the desired initial panel if needed
mainFrame.updateUserName("Name"); // Reset to default values if needed
mainFrame.updateUserID("Username");
}
private Connection connect() throws Exception {
// Database connection details
String url = "jdbc:mysql://localhost:3306/finwise";
String user = "root"; // Replace with your database username
String password = "My#music135"; // Replace with your database password
return DriverManager.getConnection(url, user, password);
}
}