Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue#9 : Complex conditional statement #9 #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/DB.java
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,25 @@ public static void updateProduct(String id,int quan)
}

}

public static String getUserRole(String username, String password) {
String sql = "SELECT Role FROM users WHERE Username = '"+username+" AND Password = '"+password+"'";
try (Connection conn = DBConnection();
PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, username);
stmt.setString(2, password);
try (ResultSet rs = stmt.executeQuery()) {
if (rs.next()) {
return rs.getString("Role");
} else {
return null;
}
}
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
public static void main(String args[])
{

Expand Down
84 changes: 47 additions & 37 deletions src/Login.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,44 +107,12 @@ public void actionPerformed(ActionEvent e) {
});

btnLogin = new JButton("Login");
btnLogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
performLogin();
}
});

btnLogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
password=passwordField.getText().toString().toLowerCase();
username=usernameField.getText().toString().toLowerCase();
passwordField.setText("");
usernameField.setText("");
if(password.equals("")||username.equals(""))
error.setText(errorText);
else
{
error.setText("");
if(username.equals("admin"))
{
if(DB.varifyLogin(username,password))
{
error.setText("");
AdminPanel p=new AdminPanel();
p.setVisible(true);
}
else
error.setText(errorText);
}
else
{
if(DB.varifyLogin(username,password))
{
error.setText("");
generateInvoice g=new generateInvoice();
g.setVisible(true);
}
else
error.setText(errorText);
}

}
}
});
btnLogin.setBounds(282, 227, 89, 23);
contentPane.add(btnLogin);

Expand All @@ -165,6 +133,48 @@ public void actionPerformed(ActionEvent arg0) {


}
private void performLogin() {
String username = usernameField.getText().trim();
String password = new String(passwordField.getPassword());

if (username.isEmpty() || password.isEmpty()) {
error.setText(errorText);
} else {
String userRole = DB.getUserRole(username, password);
if (userRole != null) {

error.setText("Login successful!");

switch (userRole) {
case "admin":
openAdminPanel();
break;
case "user":
generateInvoice();
break;
default:
error.setText("Unknown user role");
break;
}
} else {
error.setText(errorText);
}
}
}

private void openAdminPanel() {
AdminPanel adminPanel = new AdminPanel();
adminPanel.setVisible(true);
dispose();
}

private void generateInvoice() {
generateInvoice g = new generateInvoice();
g.setVisible(true);
dispose();
}


public static String getMac()
{
InetAddress ip;
Expand Down