Java- How to allow access to a different GUI menu for Learner and Admin

In summary, the conversation discusses difficulties with accessing and determining whether a User is a learner or an admin. It mentions attempting to use the method hasSpecialAccess, but unable to reach it. The idea of overriding this method in child classes is suggested, but the issue of inputting the name and password from the view arises. Tests were created and found that the method testClassHierarchy() is giving null values for names and passwords. The conversation also includes code for an Admin and a Test Harness class.
  • #1
Smiles1
8
0
I am trying to get two different menus to show up depending on whether the User is a learner or an admin. However, I'm having some difficulty with the program and the execution. I would appreciate any help or suggestions.

I don't know how to access a user to figure out if it's a learner or an admin. I have been trying to gain access to the method hasSpecialAccess to determine this but can't figure out how to reach it.

Would it be easier if I override this method in my children classes.. but then how would I take in the name and password from the view? :(

I tried to create some tests to maybe figure out what I'm doing wrong and found out that the method testClassHierarchy() in my testClassHierarchy() is giving me null values for the names and passwords.

I had to leave out some classes because I reached the limit on text.ADMIN

Code:
import java.awt.Image;

public class Admin extends User {

    private String adminName;
    private String adminPassword;
    private boolean hasSpecialAccess;
    private Avatar avatarAdmin;
    private Image supremeRuler = null;
    private String rank;

    public Admin(String adminName, String adminPassword, boolean hasSpecialAccess, Avatar avatarAdmin, String rank) {
        super(adminName, adminPassword, avatarAdmin, hasSpecialAccess, rank);
    }

    public String getAdminName() {
        return adminName;
    }

    public void setAdminName(String adminName) {
        this.adminName = adminName;
    }

    public String getAdminPassword() {
        return adminPassword;
    }

    public void setAdminPassword(String adminPassword) {
        this.adminPassword = adminPassword;
    }

    @Override
    public Boolean getHasSpecialAccess() {
        return true;
    }

    public void setHasSpecialAccess(boolean hasSpecialAccess) {
        this.hasSpecialAccess = hasSpecialAccess;
    }

    public Avatar getAvatarAdmin() {
        return avatarAdmin;
    }

    public void setAvatarAdmin(Avatar avatarAdmin) {
        this.avatarAdmin = avatarAdmin;

    }

    public Image getSupremeRuler() {
        return supremeRuler;
    }

    public void setSupremeRuler(Image supremeRuler) {
        this.supremeRuler = supremeRuler;
    }

    @Override
    public String getRank() {
        return "SupremeRuler";
    }

    @Override
    public String toString() {
        return "Admin{" + "adminName=" + adminName + ", adminPassword=" + adminPassword + ", hasSpecialAccess=" + hasSpecialAccess + ", avatarAdmin=" + avatarAdmin + ", supremeRuler=" + supremeRuler + ", rank=" + rank + '}';
    }

  
}

TEST HARNESS

Code:
public class TestHarness {
    //Test the Learner and FlashCard classes

    public TestHarness() {
        Image image1 = null;
        Avatar avatarInstance = new Avatar("TiredTony", "3333", image1);
        Learner learnerInstance = new Learner("CaptainCrunch", "donutBaker123", avatarInstance, true, "Tadpole");

        if (learnerInstance != null) {
            System.out.println("Test Learner created successfully");
            System.out.println("Learner Name = " + learnerInstance.getLearnerName() + "Learner Password " + learnerInstance.getLearnerPassword());
            //Setting Learner Name and Password
            learnerInstance.setLearnerName("banana");
            learnerInstance.setLearnerPassword("lordOfTheBananas");
            //Testing object change
            System.out.println("Learner Name = " + learnerInstance.getLearnerName() + "Learner Password " + learnerInstance.getLearnerPassword());

        }
        
        testClassHierarchy();
       
    }

    public void testClassHierarchy() {
        ArrayList<User> userArray = new ArrayList<User>();
        Image lemonImage = null;
        Avatar avatar1 = new Avatar("LemonHead", "Yellow", lemonImage);
        Learner learner1 = new Learner("LarryLarengetis", "Learnafed", avatar1, false, "Grasshopper");
        Admin admin1 = new Admin("Terrance", "Turtle", true, avatar1, "SupremeRuler");

        userArray.add(learner1);
        userArray.add(admin1);

        for (int i = 0; i < userArray.size(); i++) {
            System.out.println(userArray.get(i).getHasSpecialAccess());
 System.out.println(userArray.get(i).getRank());
        }

    }
}
USER CODE CONTAINS LOGIN PROBLEMS

Code:
public class User {

    private String userName;
    private String userPassword;
    private Avatar avatar;
    private Boolean hasSpecialAccess;
    private String rank;

    public User(String userName, String userPassword, Avatar avatar, Boolean hasSpecialAccess, String rank) {
        this.userName = userName;
        this.userPassword = userPassword;
        this.avatar = avatar;
        this.hasSpecialAccess = hasSpecialAccess;
        this.rank = rank;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getUserPassword() {
        return userPassword;
    }

    public void setUserPassword(String userPassword) {
        this.userPassword = userPassword;
    }

    public Avatar getAvatar() {
        return avatar;
    }

    public void setAvatar(Avatar avatar) {
        this.avatar = avatar;
    }

    public Boolean getHasSpecialAccess() {
        return hasSpecialAccess;
    }

    public void setHasSpecialAccess(Boolean hasSpecialAccess) {
        this.hasSpecialAccess = hasSpecialAccess;
    }

    public String getRank() {
        return rank;
    }
//LOGIN PROBLEM
    public void Login(String viewTextName, String viewTextPassword) {
        if (this.userName.equals(viewTextName) && this.userPassword.equals(viewTextPassword)) {
            System.out.println("Access Granted to the Learner view");
        } else if (this.userName.equals(viewTextName) && this.userPassword.equals(viewTextPassword)) {
            System.out.println("Access Granted to the Admin view");
        } else {
            System.out.println("NO ACCESS FOR YOU");
        }
    }
}

LOGINMENUVIEW

Code:
import java.awt.Color;
import java.awt.Font;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class LoginMenuView extends JFrame {
    
    //Will display login menu which will ask for learner name and password, verify it's correct and then allow access to the homescreen.
    private String displayMsg = " ";
    private JLabel userNameLabel;
    private JLabel passwordLabel;
    private JTextField jTxtUsername;
    private JTextField jTxtPassword;
    private JButton loginButton;
    private JButton cancelButton;
    private JLabel welcomeBanner;
    private Image img = null;
    private Avatar avatar = new Avatar("Avataronian", "11", img);
    private User user = new User("Noster-waffle", "banana33", avatar, false, "");
       
    
    public LoginMenuView() {
        
        JFrame frame = new JFrame();
        welcomeBanner = new JLabel("Welcome to a Mandarin flashcard App");
        welcomeBanner.setForeground(Color.yellow);
        welcomeBanner.setFont(new Font("Algerian", Font.ITALIC, 30));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = (JPanel) frame.getContentPane();
        panel.setLayout(null);

        panel.setBackground(Color.yellow);
        userNameLabel = new JLabel("Username:");
        userNameLabel.setForeground(Color.black);
        passwordLabel = new JLabel("Password:");
        passwordLabel.setForeground(Color.black);
        jTxtUsername = new JTextField(30);
        jTxtPassword = new JTextField(30);
        loginButton = new JButton("Login");
        loginButton.setBackground(Color.darkGray);
        loginButton.setForeground(Color.WHITE);
        loginButton.addActionListener(new LoginButtonListener());
        cancelButton = new JButton("Cancel");
        cancelButton.setBackground(Color.darkGray);
        cancelButton.setForeground(Color.WHITE);
        
        welcomeBanner.setBounds(10, 0, 1000, 100);
        userNameLabel.setBounds(100, 100, 100, 100);
        jTxtUsername.setBounds(180, 138, 130, 25);
        passwordLabel.setBounds(400, 100, 100, 100);
        jTxtPassword.setBounds(480, 138, 130, 25);
        loginButton.setBounds(178, 178, 130, 25);
        cancelButton.setBounds(480, 178, 130, 25);

        panel.add(welcomeBanner);
        panel.add(userNameLabel);
        panel.add(jTxtUsername);
        panel.add(passwordLabel);
        panel.add(jTxtPassword);
        panel.add(loginButton);
        panel.add(cancelButton);

        frame.setTitle("Mandarin Flashcards");
        frame.setSize(800, 500);
        frame.setVisible(true);

    }

    public class LoginButtonListener implements ActionListener {

        public void actionPerformed(ActionEvent event) {
            
            String viewTextName = jTxtUsername.getText();
            String viewTextPassword = jTxtPassword.getText();
            
            user.Login(viewTextName, viewTextPassword);

        }
    }

}

View attachment 8423
 

Attachments

  • null.JPG
    null.JPG
    25.1 KB · Views: 96
Last edited by a moderator:
Technology news on Phys.org
  • #2
I think the main issue with your code is that you are not actually passing any parameters to the Login() method. In order to access the user to figure out whether it's a learner or an admin, you need to pass in the username and password from the view so they can be checked against the userName and userPassword properties of the User class. You can do this by adding parameters to the Login() method and then using the parameters in the if statement to check the userName and userPassword. public void Login(String viewTextName, String viewTextPassword) { if (this.userName.equals(viewTextName) && this.userPassword.equals(viewTextPassword)) { System.out.println("Access Granted to the Learner view"); } else if (this.userName.equals(viewTextName) && this.userPassword.equals(viewTextPassword)) { System.out.println("Access Granted to the Admin view"); } else { System.out.println("NO ACCESS FOR YOU"); } }In the LoginMenuView class, you can call the Login() method with the viewTextName and viewTextPassword parameters and pass in the values of the text fields jTxtUsername and jTxtPassword. public class LoginButtonListener implements ActionListener { public void actionPerformed(ActionEvent event) { String viewTextName = jTxtUsername.getText(); String viewTextPassword = jTxtPassword.getText(); user.Login(viewTextName, viewTextPassword); } }
 

FAQ: Java- How to allow access to a different GUI menu for Learner and Admin

How can I allow access to a different GUI menu for Learner and Admin in Java?

To allow access to a different GUI menu for Learner and Admin in Java, you will need to use conditional statements in your code. This means that you will need to check the user's role (Learner or Admin) and then display the appropriate menu based on their role.

Can I use Java Swing to create a different GUI menu for Learner and Admin?

Yes, you can use Java Swing to create a different GUI menu for Learner and Admin. You will need to create separate classes for each menu and then use conditional statements to display the appropriate menu based on the user's role.

How do I restrict access to certain features in the GUI menu for Learner and Admin?

To restrict access to certain features in the GUI menu for Learner and Admin, you can use access modifiers such as private, public, or protected in your code. This will limit the access of certain features to only specific roles.

Is it possible to change the GUI menu for Learner and Admin dynamically?

Yes, it is possible to change the GUI menu for Learner and Admin dynamically. You can use event listeners to detect when the user's role changes and then update the GUI menu accordingly.

Can I use a database to store the user's role for accessing the different GUI menus in Java?

Yes, you can use a database to store the user's role for accessing the different GUI menus in Java. You can retrieve the user's role from the database and use it to determine which GUI menu to display for that user.

Similar threads

Replies
1
Views
2K
Replies
3
Views
3K
Replies
1
Views
3K
Replies
5
Views
2K
Replies
2
Views
1K
Replies
1
Views
2K
Back
Top