Apply to a variable of class A the value of an attribute of class B

2

I am with a problem in the development of a Java program, I need to set in a varibale of a class Menu the value set to an attribute of the Logica class.

Logica class

public class Logica {

    public static boolean administrador = false;
    public int usIngresado = LogIn.cedula;
    public String pwdIngresado = LogIn.ci;

    public void checkUser (int usIngresado, String pwdIngresado) {//determinación de Admin
        int usuario = usIngresado , usAdmin = 123456, usRec = 456789;
        String pass = pwdIngresado, pwdAd = "admin", pwdRec = "user";
        if (usuario == usAdmin && pass.equals(pwdAd)) {
            System.out.println("Ingresa en administrador");
            administrador = true;
        }
    }
}

The method works, I've tried it with impressions, and it sets the value to administrator according to the user. The issue is that I have not managed from the Menu class, get that value ( true or false ) to validate buttons and windows. Achieving that Menu read correctly is to accommodate it for the rest of the windows I'm working.

Resulting query: how do I get the administrator value and save it in the validation variable of the Menu class? Or how do I read the value directly, without needing to duplicate the data in another variable?

As a clarification, with my teacher I can not count, since it does not explain it says "investigate" ... and that's what I'm doing now.

Thank you very much

    
asked by Vizz3rdriX 12.10.2018 в 23:20
source

2 answers

1

According to your code you should change the void of checkUser by boolean and return the variable administrador with return :

public class Logica {

    public static boolean administrador = false;
    public int usIngresado = LogIn.cedula;
    public String pwdIngresado = LogIn.ci;

    public boolean checkUser (int usIngresado, String pwdIngresado) {//determinación de Admin
        int usuario = usIngresado , usAdmin = 123456, usRec = 456789;
        String pass = pwdIngresado, pwdAd = "admin", pwdRec = "user";
        if (usuario == usAdmin && pass.equals(pwdAd)) {
            System.out.println("Ingresa en administrador");
            administrador = true;
        }
        return administrador;
    }
}
    
answered by 13.10.2018 / 00:48
source
0

I changed your code, leaving checUser as boolean and returning administrador . Thank you very much!

public class Logica {    
    public static boolean administrador = false;

    public boolean checkUser (int usIngresado, String pwdIngresado) {//determinación de Admin
        int usuario = usIngresado , usAdmin = 39830638, usRec = 16866983;
        String pass = pwdIngresado, pwdAd = "admin", pwdRec = "user";
        if (usuario == usAdmin && pass.equals(pwdAd)) {
            System.out.println("Ingresa en administrador");
            administrador = true;
        }
        System.out.println("estado administrador:   " + administrador);
        return administrador;
    }

    public boolean getAdministrador () {
        return administrador;
    }

    public void setAdministrador(boolean administrador) {
        this.administrador = administrador;
    }
}

What I had in MenuPpal was:

  public class MenuPpal extends JFrame {


        //saqué todo lo que 
        //corresponde a la importación
        //de clases gráficas

        if (admin.getAdministrador()) {
                btnGestUsrs.setEnabled(true);//habilito el botón
            }
        else {
                btnGestUsrs.setEnabled(false);//no habilito el botón
            }
}

THE SOLUTION: Although there are people who may seem intuitive or very easy, after unraveling ideas, all I had to do was invoke that attribute directly, plain and simple. Here is my change to if above.

  public class MenuPpal extends JFrame {

        Logica admin = new Logica();

        //saqué todo lo que 
        //corresponde a la importación
        //de clases gráficas

        if (Logica.administrador) {//si quiero saber si es true el boolean de la condición no es necesario hacerla explicita, por eso no pongo el (== true), ahora si lo que me interesa es saber si es false, si debo poner la condición completa

                btnGestUsrs.setEnabled(true);//habilito el botón
            }
        else {
                btnGestUsrs.setEnabled(false);//no habilito el botón
            }
}

And the most important thing about my problem was when I instantiated the method that saved the value in administrador , it was below the window opening where I requested the value of administrador .

the code that affected me is this, that I was in a third class (a jframe) that I never noticed.

                log.setUsIngresado(Integer.parseInt(ci));
                log.setPwdIngresado(pass);

                boolean validar = ValCed.esCIValida(ci);


                if (validar == true && log.Loggeo(log.getUsIngresado(), log.getPwdIngresado())) {
                    MenuPpal abrir = new MenuPpal();
                    abrir.setVisible(true);
                    LogIn.this.dispose();
                }
                else {
                    lblCedValida.setText("Datos No Válidos");
                }
                usr.setUsIngresado(Integer.parseInt(ci));
                usr.setPwdIngresado(pass);
                usr.checkUser(usr.getUsIngresado(), usr.getPwdIngresado());//así nunca paso los parámetros ni logro ejecutar el método para darle el valor a administrador
            }
        });
        btnIngresar.setBounds(201, 144, 89, 23);
        contentPane.add(btnIngresar);

This is the correction:

                log.setUsIngresado(Integer.parseInt(ci));
                log.setPwdIngresado(pass);

                boolean validar = ValCed.esCIValida(ci);

                usr.setUsIngresado(Integer.parseInt(ci));
                usr.setPwdIngresado(pass);
                usr.checkUser(usr.getUsIngresado(), usr.getPwdIngresado());//aquí es donde paso los parámetros para darle valor a administrador


                if (validar == true && log.Loggeo(log.getUsIngresado(), log.getPwdIngresado())) {
                    MenuPpal abrir = new MenuPpal();
                    abrir.setVisible(true);
                    LogIn.this.dispose();
                }
                else {
                    lblCedValida.setText("Datos No Válidos");
                }
            }
        });
        btnIngresar.setBounds(201, 144, 89, 23);
        contentPane.add(btnIngresar);

Basic Concept of Programming: set of ORDINED ORDERS !!! Never forget that, in what order are your orders! Thanks to those who helped me!

    
answered by 14.10.2018 в 03:52