-1

Im trying to create a very simple banking system with swing. I have three classes so far, they are Gui, Main, and Validate.

My problem seem to be when i try to log in. The user/pass i created and stored in the Validate class. I'm getting a wrong password or username, why? What goes wrong?

To get the login popup(JOptionPane), you have to click the "LOG IN" button.

Here is the code for all three classes:

THE MAIN CLASS:

public class Main{

    public static void main(String[] args){

        Validate validate = new Validate();
        Gui gui = new Gui(validate);
        //create Accounts. Send accounts to gui+validate.
    }       

}

THE VALIDATE CLASS:

public class Validate {

Map<String,String> userMap;

public Validate(){

    userMap = new HashMap<String,String>();

    //Skapa ett inloggning för mig
    //accounts.createUser();
    userMap.put("john","smith");

}

public boolean validateUser(String username, String password){

    if(userMap.isEmpty() != true){
        if(userMap.containsKey(username)){
            String passTest = userMap.get(username);
            if(password.equals(passTest)){
                return true;
                //accounts.logInUser();
            }else{
                return false;
            }   
        }else{
            return false;
        }
    }else{
        return false;
    }

}


}

THE GUI CLASS:

public class Gui extends JFrame implements ActionListener{

JPanel loggning, center, botten;
JButton loggaIn, loggaUt;
JTextField text1, text2;
JLabel lb1, lb2;
JMenuBar mbar;
JMenu about,system;
JMenuItem aboutBank, exit;
Font font;
Validate validate;

public Gui(Validate validate){

    super("BANK");
    this.validate = validate;

    //Skapa menun.
    mbar = new JMenuBar();
    system = new JMenu("System");
    mbar.add(system);
    about = new JMenu("About");
    mbar.add(about);
    aboutBank = new JMenuItem("Bank Info");
    about.add(aboutBank);
    aboutBank.addActionListener(this);
    exit = new JMenuItem("Exit");
    exit.addActionListener(this);
    system.add(exit);
    add(mbar, BorderLayout.NORTH);

    //skapa vänstra panelen. Här loggar man in/ut.
    loggning = new JPanel();
    loggning.setLayout(new GridLayout(20,1,0,5));
    text1 = new JTextField("");
    text2 = new JTextField("");
    lb1 = new JLabel("  ANGE ANVÄNDARNAMN:   ");
    lb2 = new JLabel("       ANGE LÖSENORD:  ");
    Border border = BorderFactory.createLineBorder(Color.gray, 1);
    lb1.setBorder(border);
    lb2.setBorder(border);
    //loggning.add(lb1);
    //loggning.add(text1);
    //loggning.add(lb2);
    //loggning.add(text2);
    loggaIn = new JButton("Logga in");
    loggaUt = new JButton("Logga ut");
    loggaUt.setEnabled(false);
    loggaIn.setPreferredSize(new Dimension(100,10));
    loggaUt.setPreferredSize(new Dimension(100,10));
    loggaIn.addActionListener(this);
    loggaUt.addActionListener(this);
    loggning.add(loggaIn);
    loggning.add(loggaUt);
    add(loggning, BorderLayout.WEST);

    //Skapa mittpanelen. Här finns information om dig och dina konton.
    center = new JPanel();

    add(center, BorderLayout.CENTER);

    //skapa botten panelen. Här kan man ändra i sina konton. Skicka pengar, Dra ut pengar, ändra info? osv.


    //JFrame fixeringar
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setVisible(true);
    setSize(900,900);
}






@Override
public void actionPerformed(ActionEvent e) {
    if(e.getSource() == loggaIn){
        JTextField user = new JTextField();
        JTextField pass = new JTextField();
        Object[] message = {"Enter your username:", pass, "Enter your password:", user};  
        int option = JOptionPane.showConfirmDialog(this, message, "Enter all your values", JOptionPane.OK_CANCEL_OPTION);  
        if (option == JOptionPane.OK_OPTION)  
        {  
            String pass1 = pass.getText();
            String user1 = user.getText();
            Boolean valid = validate.validateUser(user1,pass1);
            if(valid == true){
                loggaIn.setEnabled(false);
                loggaUt.setEnabled(true);
                showLogInInfo(user1);
                //accounts.getLoggedInInfo();
                //gui.showLoggedInInfo();
                //Logged in info showed in center jpanel.
            }else{
                JOptionPane.showMessageDialog(this,"Username or password wrong", "Error", JOptionPane.INFORMATION_MESSAGE);
            }
        }

    }else if(e.getSource() == loggaUt){
        loggaIn.setEnabled(true);
        loggaUt.setEnabled(false);
        //accounts.gotOutUser();
        //text1.setEditable(true);  
        //text2.setEditable(true);
    }else if(e.getSource() == exit){
        this.dispose();
    }else if(e.getSource() == aboutBank){
        String mssg = "Ali Bank.\nSince 2014.\nIf you have questions,\nthere is no customer service.\nSorry we cant help you.";
        JOptionPane.showMessageDialog(this, mssg, "Bank Information",JOptionPane.INFORMATION_MESSAGE);
    }else{}

}

public void showLogInInfo(String user){
    String messs = ("User " + user + " is now logged in.");
    JOptionPane.showMessageDialog(this, messs, "Log In Info", JOptionPane.INFORMATION_MESSAGE);
}

}

Some comments may be in Swedish, sorry. Mostly I think they are irrelevant, but if you have a question, please ask.

CJR
  • 3,174
  • 6
  • 34
  • 78
  • 2
    See: http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – kiheru Aug 25 '14 at 23:07
  • After changing the String comparing method, it still does not work. What should I do? @kiheru – CJR Aug 25 '14 at 23:15
  • You should ask a new question with a small example that illustrates what you are trying to do. Also be sure to include any error messages or output that you get and explain how it differs from what you want. – Code-Apprentice Aug 25 '14 at 23:19

2 Answers2

4

You have the username and password fields swapped in your popup box:

Object[] message = {"Enter your username:", pass, "Enter your password:", user};

This should be:

Object[] message = {"Enter your username:", user, "Enter your password:", pass};

After fixing this, I was successfully able to login.

therealrootuser
  • 10,215
  • 7
  • 31
  • 46
  • Thank you so much dude, but you were 7 seconds too late (he he). I figured it out after a while. But thanks, have a nice one. @mattingly980 – CJR Aug 26 '14 at 02:02
0

You pass inverse password and username. See change

 Boolean valid = validate.validateUser(user1,pass1);

to

 Boolean valid = validate.validateUser(pass1,user1);
LunaVulpo
  • 3,043
  • 5
  • 30
  • 58