title: Login system using ArrayList
body: <p>I am trying to create a login system using a user class I created and storing multiple users in an ArrayList. Then in order to make sure what the user enters into the various fields is the same as what the specific "user" is, there is a loop at the bottom that compares the two. The method at the bottom either returns true or false. It's rather late where I am so if I am not explaining in the most clear way possible, I apologize.</p>
  
  <p>My problem is my loginAuthorization() method only returns true if and only if the first element in the ArrayList is entered and if it is entered correctly. It ignores all other users that are added to the ArrayList. This question obviously has a "well duh" answer to it, or so I assume, and I don't understand why this is happening. If someone can show me what I am doing wrong, it would be greatly appreciated. :)</p>
  
  <p>My loginWindow class:</p>
  
  <pre><code>    /**
       *  @author OperatorX© 2013
       */
  
      package dossierIB;
  
      import java.awt.BorderLayout;
      import java.awt.EventQueue;
      import java.awt.Toolkit;
  
      import javax.swing.JFrame;
      import javax.swing.JPanel;
      import javax.swing.border.EmptyBorder;
      import net.miginfocom.swing.MigLayout;
      import javax.swing.JLabel;
      import java.awt.Component;
      import javax.swing.Box;
      import java.awt.Dimension;
  
      import javax.swing.ImageIcon;
      import javax.swing.JOptionPane;
      import javax.swing.JTextField;
      import javax.swing.JPasswordField;
      import javax.swing.JButton;
      import javax.swing.UIManager;
  
      import java.awt.event.ActionListener;
      import java.awt.event.ActionEvent;
      import java.util.ArrayList;
      import java.util.Arrays;
  
      public class LoginWindow extends JFrame 
      {
          private JPanel contentPane;
          private JFrame errorFrame;
          private JTextField usernameField;
          private JPasswordField passwordField;
  
  
          /**
           * Launch the application.
           */
          public static void main(String[] args) {
              EventQueue.invokeLater(new Runnable() {
                  public void run() {
                      try {
                          UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                          LoginWindow frame = new LoginWindow();
                          frame.setLocationRelativeTo(null);
                          frame.setVisible(true);
                      } catch (Exception e) {
                          e.printStackTrace();
                      }
                  }
              });
          }
  
          /**
           * Create the frame.
           */
          public LoginWindow() {
              setResizable(false);
              setTitle("Employee Authorization");
              setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              setBounds(100, 100, 450, 300);
              contentPane = new JPanel();
              contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
              setContentPane(contentPane);
              contentPane.setLayout(new MigLayout("", "[][][][grow][]", "[][][][][][][]"));
  
              JPanel bgImagePanel = new JPanel();
              ImagePanel backgroundPanel = new ImagePanel(new ImageIcon("C:/Users/OpX/Documents/workspace/Dossier (WBP)/images/testtoptrans.png").getImage());
              bgImagePanel.add(backgroundPanel);
              contentPane.add(bgImagePanel, "cell 0 0 5 1");
  
              Component rigidArea_4 = Box.createRigidArea(new Dimension(20, 20));
              contentPane.add(rigidArea_4, "cell 4 1 1 6");
  
              JLabel lblUsername = new JLabel("Username");
              contentPane.add(lblUsername, "cell 1 2");
  
              Component rigidArea_1 = Box.createRigidArea(new Dimension(20, 20));
              contentPane.add(rigidArea_1, "cell 0 1 1 6");
  
              Component rigidArea_3 = Box.createRigidArea(new Dimension(20, 20));
              contentPane.add(rigidArea_3, "cell 2 2 1 3");
  
              usernameField = new JTextField();
              contentPane.add(usernameField, "cell 3 2,growx");
              usernameField.setColumns(10);
  
              Component rigidArea_2 = Box.createRigidArea(new Dimension(20, 20));
              contentPane.add(rigidArea_2, "cell 1 3");
  
              JLabel lblPassword = new JLabel("Password");
              contentPane.add(lblPassword, "cell 1 4");
  
              passwordField = new JPasswordField();
              contentPane.add(passwordField, "cell 3 4,growx");
  
              Component rigidArea_5 = Box.createRigidArea(new Dimension(20, 20));
              contentPane.add(rigidArea_5, "cell 1 5 3 1");
  
  
              JButton btnLogin = new JButton("Login");
              btnLogin.addActionListener(new ActionListener() 
              {
                  public void actionPerformed(ActionEvent e) 
                  {
                      if(loginAuthorization() == true)
                      {
                          ManageWindow manifest = new ManageWindow();
                          manifest.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                          manifest.setSize(697,500);
                          manifest.setLocationRelativeTo(null);
                          manifest.setVisible(true);
  
                          setVisible(false);
                          dispose();
                      }
                      else
                          return;
                  }
              });
              contentPane.add(btnLogin, "flowx,cell 3 6,alignx right");
  
              JButton btnCreateAccount = new JButton("Create Account");
              btnCreateAccount.setEnabled(false);
              btnCreateAccount.addActionListener(new ActionListener() 
              {
                  public void actionPerformed(ActionEvent e) 
                  {
  
                  }
              });
              contentPane.add(btnCreateAccount, "cell 3 6");
          }
  
          public Boolean loginAuthorization()
          {
              errorFrame = new JFrame();
              String username = usernameField.getText();
              String password = new String(passwordField.getPassword());
  
              ArrayList&lt;User&gt; users = new ArrayList&lt;User&gt;();
              User manager = new User("manager", "gocompany");
              User shiftManager = new User("shiftmanager", "goshifts");
              User stockBoy = new User("stockboy", "istockshelves");
              User cashier = new User("cashier", "trustedwithmoney");
              users.add(manager);
              users.add(shiftManager);
              users.add(stockBoy);
              users.add(cashier);
  
              for(int i = 0; i &lt; users.size(); i++)
              {
                  if(users.get(i).getUsername().compareTo(username) == 0 &amp;&amp; users.get(i).getPassword().compareTo(password) == 0)
                  {
                      return true;
                  }
  
                  else
                  {
                      passwordField.setText("");
                      Toolkit.getDefaultToolkit().beep();
                      JOptionPane.showMessageDialog(errorFrame,
                              "Could not authorize employee with given username and password.\n\n" +
                              "Please reenter or contact your system administrator.\n\n",
                              "Employee Authorization Failed",
                              JOptionPane.ERROR_MESSAGE);
                      return false;
                  }
              }
  
              return false;
          }
      }
  </code></pre>
  
  <p>My User class:</p>
  
  <pre><code>/**
   *  @author OperatorX© 2013
   */
  
  package dossierIB;
  
  public class User 
  {
      private String user;
      private String pass;
  
      public User(String u, String p)
      {
          user = u;
          pass = p;
      }
  
      public String getUsername()
      {
          return user;
      }
  
      public String getPassword()
      {
          return pass;
      }
  
      public void setUsername(String username)
      {
          this.user = username;
      }
  
      public void setPassword(String password)
      {
          this.pass = password;
      }
  
  }
  </code></pre>