Results 1 to 4 of 4
  1. #1
    slitka is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default New to GUI. Logging in with Password and username

    I have tried and tried, but I am getting nowhere by myself. Basically There is three different types to login to the program; an admin, student or faculty. As of right now I am trying to get the easiest out of the way, which I would think would be admin (ID is admin Password is admin). Student and faculty are in an array with their own passwords and IDs. This is what I have as of right now. Please excuse the mess, since this is my first ever time to mess with GUIs.

    Java Code:
     public Application()
        {
            super("Log on");
            this.setSize(300, 300);
            setLayout(new FlowLayout());
            this.setVisible(true);
            currentUserObject = null;
            userDatabase    = new  Person[20];
            courseDatabase  = new  Course[10];
            numberOfUsers   = 0;
            numberOfCourses = 0;
            keyboard = new Scanner(System.in);
            addRecords();
    
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JPanel namePanel = new JPanel();
            JPanel passwordPanel = new JPanel();
            namePanel.setLayout(new GridLayout(4,4));  
            namePanel.setLayout(new BorderLayout());
            namePanel.setBackground(Color.WHITE);
            passwordPanel.setLayout(new GridLayout(4,4));
            passwordPanel.setLayout(new BorderLayout());
           passwordPanel.setBackground(Color.WHITE);
    
            endButton = new JButton("Exit");
            endButton.addActionListener(this);
    
    
            login = new JButton ("Login");
    
            clear = new JButton("Clear");
            clear.addActionListener(this);
    
            name = new JTextField (20);
            name.addActionListener(this);
            JLabel nameLabel = new JLabel("Enter your User Name here:");
            namePanel.add(nameLabel);
    
    
            password = new JPasswordField (20);
            password.addActionListener(this);
            JLabel passwordLabel =  new JLabel("Enter your Password here:");
            passwordPanel.add(passwordLabel);
    
    
            add(namePanel);
            add(name);
            add(passwordPanel);
            add(password);
            add(login);
            add(endButton);
            add(clear);
    
    
        
           
    
    
    
        }
    
    
        public void actionPerformed(ActionEvent e)
        {
    
            if(e.getSource() == clear)
            {
                 name.setText("");
                 password.setText("");
            }
    
            if (e.getSource()== endButton)
            {
                System.exit(0);
            }
    
            if (e.getSource()==login)
                
            {
    
            char [] ID, pw;
     pw = password.getPassword();
     
        
     
            
    
            if (?????)
            {
                    JOptionPane.showMessageDialog(controllingFrame,
                        "Success! You typed the right password.");
                }
    
    
            else {
                    JOptionPane.showMessageDialog(controllingFrame,
                        "Invalid password. Try again.",
                        "Error Message",
                        JOptionPane.ERROR_MESSAGE);
                }
    
    
            } 
    
    
            // please go over the presentations and example programs that are posted on webct.
        
        }
    
        private static boolean isPasswordCorrect(char[] input)
        {
        boolean isCorrect = true;
        char[] correctPassword = {'b', 'u', 'g', 'a', 'b', 'o', 'o'};
    
        if (input.length != correctPassword.length) {
            isCorrect = false;
        }
        else {
            isCorrect = Arrays.equals (input, correctPassword);
        }
        return isCorrect;
    }
    I took parts form a tutorial and it confused me even more. when I try
    My question is then this. How would I go about logging in as an admin?

  2. #2
    FlyNn is offline Senior Member
    Join Date
    Feb 2010
    Posts
    128
    Rep Power
    0

    Default

    The way I would do, is to assign letter to the id. Admin could be: a1234, Student: s1234, Faculty: f1234. When logging, check the first character and depending of which char it is check matching array for password.

    Hope this helps
    Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Quote Originally Posted by slitka View Post
    How would I go about logging in as an admin?
    By typing in the correct user id and password.

    If you want a more specific answer then ask a more specific question. Speaking of questions, why have you added a Listener to the password field and not the login button?

  4. #4
    slitka is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    By typing in the correct user id and password.

    If you want a more specific answer then ask a more specific question. Speaking of questions, why have you added a Listener to the password field and not the login button?
    OOPS. That is an excellent question! I feel like a really stupid man. That could be the exact reason. I'll report back here in a little bit.

    Okay, I got it to actually respond to the tutorial password. I don't think I worded the whole login ID/Password problem thing very well, so I will do it again. In this program we already have set logins. For instance, to login into an admin you must type the ID and Password as admin. If you wanted to log into a student you pick from this list:
    Java Code:
    private void addRecords()
        {
            // Create Users
            userDatabase[numberOfUsers++] = new Student("[B]92038201[/B]",  "Ilyas",   "Uyanik", "Computer Science", "[U]1956[/U]",       2, 1);//0
            userDatabase[numberOfUsers++] = new Student("[B]20483002[/B]", "Joseph",   "Mathew", "Computer Science", "[U]1234[/U]",       3, 1);//1
            userDatabase[numberOfUsers++] = new Student("[B]26023923[/B]",  "Suzan",     "Pink", "Computer Science", "[U]2342[/U]",       3, 1);//2
            userDatabase[numberOfUsers++] = new Student("[B]34532343[/B]",    "Amy",   "Yellow", "Computer Science", "[U]4533[/U]",       1, 1);//3
            userDatabase[numberOfUsers++] = new Student("[B]78643546[/B]", "George",    "Black", "Computer Science", "[U]0495[/U]",       4, 1);//4
            userDatabase[numberOfUsers++] = new Student("[B]45345753[/B]",  "Fatih", "Haberdar", "Computer Science", "[U]5478[/U]",       1, 1);//5
            userDatabase[numberOfUsers++] = new Student("[B]23748967[/B]",  "Murat",  "Erdogan", "Computer Science", "[U]9043[/U]",       2, 1);//6
            userDatabase[numberOfUsers++] = new Student("[B]32946262[/B]",    "Sam",     "Blue", "Computer Science", "[U]3241[/U]",       4, 1);//7
            
    
            userDatabase[numberOfUsers++] = new Faculty("[B]52037101[/B]",  "Hakan", "Haberdar", "Computer Science", "[U]1267[/U]",  70000.0, 2);//8
            userDatabase[numberOfUsers++] = new Faculty("[B]30293102[/B]",   "Bill",    "Brown", "Computer Science", "[U]7810[/U]", 100000.0, 2);//9
            userDatabase[numberOfUsers++] = new Faculty("[B]10132103[/B]",   "John",    "White", "Computer Science", "[U]3724[/U]",  60000.0, 2);//10
            userDatabase[numberOfUsers++] = new Faculty("[B]32546104[/B]",   "Ayse",      "Red", "Computer Science", "[U]2938[/U]",  80000.0, 2);//11
            userDatabase[numberOfUsers++] = new Faculty("[B]98232105[/B]",   "Emre",    "Charu", "Computer Science", "[U]2302[/U]",  83000.0, 2);//12
    The first number (which is bolded) is the ID. The last number (which is underlined) is the Password. A login screen will appear. When you type in your ID and password you should be sent to a menu that has different options. For example:
    Java Code:
    System.out.println("------------+------------");
                System.out.println("Please make a selection:");
                System.out.println("A - Add Course");
                System.out.println("S - Add Student");
                System.out.println("F - Add Faculty");
                System.out.println("L - List Users");
                System.out.println("C - List Courses");
    (I'll convert that into GUI later).



    This was given to us at the bottom, and I am not sure if we are supposed to use this for the GUI.
    Java Code:
     private void login()
        {
            boolean userVerified=false;
            int attemptNumber =0;
            String ID, password;
            do
            {
                System.out.print("ID       :");
                ID = keyboard.next();
                System.out.print("Password :");
                password = keyboard.next();
                
                attemptNumber++;
                if( ID.equals("admin") && password.equals("admin"))
                {
                    showAdminMenu();
                    userVerified = true;
                }
                else
                {
                    // Faculty/Student
                    for(int i=0; i<numberOfUsers;i++)
                    {
                        if( userDatabase[i].validation(ID, password) )
                        {
                            currentUserObject =  userDatabase[i];
                            if(userDatabase[i].getUserType()==1 )
                            {
                                showStudentMenu();
                                userVerified = true;
                            }
                            else if(userDatabase[i].getUserType()==2 )
                            {
                                showFacultyMenu();
                                userVerified = true;
                            }
                        }
                    }
                    if( !userVerified && attemptNumber<3)
                    {
                         System.out.println("User not found, please try again.");
                    }
    
                }
            } while (!userVerified && attemptNumber<3 );
            if( !userVerified )
            {
                System.out.println("None of the IDs/passwords you entered matches our records.");
                System.exit(attemptNumber);
            }
        }
    Would I use the login() in my GUI, or do I make separate isPasswordcorrect functions? If I were to make separate isPasswordCorrect functions how would I access the student and faculty array?
    Last edited by slitka; 04-12-2011 at 12:58 AM.

Similar Threads

  1. how to link url with username and password
    By gb.rashu in forum JavaServer Pages (JSP) and JSTL
    Replies: 13
    Last Post: 08-12-2010, 04:12 PM
  2. password username and databases
    By chalo in forum JCreator
    Replies: 0
    Last Post: 12-02-2008, 09:11 AM
  3. username password verification
    By bheezee in forum JDBC
    Replies: 0
    Last Post: 11-25-2008, 07:55 PM
  4. Help, created a username and password box
    By cachi in forum AWT / Swing
    Replies: 1
    Last Post: 08-07-2007, 05:21 AM
  5. JTextFields with username & password.
    By Eric in forum AWT / Swing
    Replies: 2
    Last Post: 07-02-2007, 12:41 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •