Call variable from one class to another java POO

2

Good I'm doing a POO project, and I want to get a data from a class Employee, to make a comparison in another class Salary, can this be done? Or should I make the comparison in the main method directly? 'Import javax.swing.JOptionPane;

/**
 *
 * @author Luis Eduardo
 */
public class test {


    public static void main(String[] args) {

       //creacion del objeto P, segmento que craga datos del empleado por pantalla y los muestra por pantalla

      Empleado p = new Empleado ();

      p.setDatos(null, null);
      JOptionPane.showConfirmDialog(null, "NOMBRE: "+p.getDatos());
      JOptionPane.showConfirmDialog(null, "CARGO: "+p.getCargo());
      // Promedio de comision maxima que es el 15% de su sueldo maximo
      p.setComision();
      JOptionPane.showConfirmDialog(null, "SALARIO: "+p.getSalrio());
      JOptionPane.showConfirmDialog(null, "COMISION: "+p.getComisionmax());

      // creacion del objeto cuenta ademas de operar los dosdescuenotos disponibles en el programa (descuento prestamo y descuento ahorro)
      Salario liquidacion = new Salario ();

      liquidacion.SetDesceuntos();
      JOptionPane.showConfirmDialog(null, "DESCUENTO: "+ liquidacion.getDescuento());

      //Comparacion de valores de comision del mes 
      liquidacion.setComisionReal();

       if (liquidacion.ValorTotalComi > p.valorMaxComision)
       {
           JOptionPane.showConfirmDialog(null,"La comision asignada Es mayor a la legal ");
       }
       else
       {
           JOptionPane.showConfirmDialog(null, "La comision de este mes es:"+liquidacion.ValorTotalComi);
       }

       liquidacion.setParaficales();
       JOptionPane.showConfirmDialog(null, "DESCUENTO PARAFISCALES: "+ liquidacion.GetParafiscales());




    }

}

Next class Employee:

import javax.swing.JOptionPane;



/**
 *
 * @author Luis Eduardo
 */
public class Empleado 
{
    //variable nativas.
    String nombreEmpleado;
    String cargoEmpleado;
    int salarioMensual;
    double valorMaxComision;

    //constructores 
    public Empleado ()
    {
      this.salarioMensual = 0;
      this.valorMaxComision = 0.0;
      this.nombreEmpleado = "";
      this.cargoEmpleado = "";
    }

    public Empleado (String nombreEmpleado,String cargoEmpleado, int salarioMensual,double valorMaxComision)
    {
       this.nombreEmpleado = nombreEmpleado;
       this.cargoEmpleado = cargoEmpleado;
       this.salarioMensual = salarioMensual;
       this.valorMaxComision = valorMaxComision;

    }
    //Metodos

    public void setDatos (String nombreEmpleado,String cargoEmpleado) 
    {
       this.nombreEmpleado = JOptionPane.showInputDialog("Por Favor ingrese el nombre del empleado:");
       this.cargoEmpleado = JOptionPane.showInputDialog("Por Favor ingrese el cargo del empleado:");
       this.salarioMensual=Integer.parseInt(JOptionPane.showInputDialog("Por Favor ingrese el salario del empleado:"));

    }
    public void setComision ()
    {
        this.valorMaxComision = (salarioMensual * 15)/100;
    }
    public String getDatos () 
    {
        return nombreEmpleado;
    }

    public String getCargo ()
    {
        return cargoEmpleado;
    }

    public double getSalrio()
    {
        return salarioMensual;
    }
    public double getComisionmax ()
    {
        return valorMaxComision;
    }


}

Salary Class:

import javax.swing.JOptionPane;


/**
 *
 * @author Luis Eduardo
 */
public class Salario
{
    //variable nativas.

    int ValorTotalDes;
    int ValorTotalComi;
    int DesParafiscal;
    int prestamoEmpresa;
    int DescuentoAhorro;
    int SalarioMensual;
    int valorParafiscal;
    int salarioNeto ;


    //constructores 

    public Salario ()
    {
      this.ValorTotalDes = 0;
      this.ValorTotalComi = 0;
      this.DesParafiscal =  0;
      this.prestamoEmpresa =0;
      this.DescuentoAhorro =0;
      this.SalarioMensual =0;
      this.valorParafiscal =0;
      this.salarioNeto =0 ;

    }
    public void Salario (int valorTotalDes,int valorTotalComi, int DesParafiscal,int SalarioMensual, int ValorParafiscal, int salarioNeto)
    {
      this.ValorTotalDes = valorTotalDes ;
      this.ValorTotalComi = valorTotalComi;
      this.DesParafiscal = DesParafiscal;
      this.SalarioMensual = SalarioMensual;
      this.valorParafiscal = ValorParafiscal;
      this.salarioNeto = salarioNeto;
    }
    //metodos

    public void SetDesceuntos ()
    {
       this.prestamoEmpresa =  Integer.parseInt(JOptionPane.showInputDialog("Por favor indique el valor del prestamo"));
       this.DescuentoAhorro =  Integer.parseInt(JOptionPane.showInputDialog("Por favor indique el valor para el fonde de ahorro"));

       this.ValorTotalDes = prestamoEmpresa + DescuentoAhorro;
    }

    public int getDescuento ()
    {
        return ValorTotalDes;
    }

    public void setComisionReal ()
    {
        this.ValorTotalComi = Integer.parseInt(JOptionPane.showInputDialog("Digite el valor de la comision de este mes"));
    }

    public int getComisionReal()
    {
        return ValorTotalComi;
    }
    public void setParaficales ()
    {
        this.SalarioMensual= Integer.parseInt(JOptionPane.showInputDialog("Digite el valor del salario de este mes"));

        valorParafiscal = (SalarioMensual*8)/100;

    }

    public int GetParafiscales ()
    {
        return valorParafiscal;
    }

    public void SetSalarioNeto ()
    {
        this.salarioNeto = SalarioMensual + ValorTotalComi - valorParafiscal - ValorTotalDes;
    }

    public int getSalarioNeto ()
    {
        return salarioNeto;
    }

   }
    
asked by Luis Eduardo Rojas Becerra 26.04.2017 в 22:44
source

3 answers

1

To access the properties of the object, you should only declare it as static so you can access it from another class without having the instance.

    public class Empleado {
String nombreEmpleado; 
static String variableAComparar;
}

public class OtraClase{
    public void compararAlgo(){
        if(Empleado.variableAcomparar=="algun valor"){
            //true
        }
    }
} 

Keep in mind that the variable to buy must already be with value before making the comparison, if you do it in the constructor there is no problem.

Empleados emp=new Empleados();
emp.elMetodoQueCargaLasVariables();
Otraclase oc=new OtraClase();
oc.compararAlgo();
    
answered by 27.04.2017 / 00:04
source
0

Yes it is possible, but the variable salamisMensualdouble, valueMaxComision should be in Salary, it is a conceptual problem that belong to Employee. As you are doing you have to create a property / variable of type Employee within Salary. Both classes must be public and all variables have constructor and getters and setters.

public class Salario
{
    Empleado empleado1;//el nombre puede ser otro

    //constructors

    public Salario()
    {
    }

    public Salario(Empleado empleado1)
    {
        this.empleado1 = empleado1;
    }

    //get and set

    public Empleado getEmpleado1()
    {
        return this.empleado1;
    }

    public void setEmpleado1(Empleado empleado1)
    {
        this.empleado1 = empleado1;
    }

    /*  método personalizado
     *  X viene de la clase Empleado
     *  */

    public void comparar()
    {
        int y=1;
        if (getEmpleado1().getX()==y)
        {
            //acción
        }
    }
}

public class Empleado
{
    private int x;

    //constructors
    public Empleado()
    {
    }

    public Empleado(int x)
    {
        this.x = x;
    }

    //get and set
    public int getX()
    {
        return this.x;
    }

    public void setX(int x)
    {
        this.x = x;
    }
}
    
answered by 27.04.2017 в 08:21
0

add public class Salario , modify it to be a Employee subclass

public class Salario extends Empleado{...}

already with that any variable of Employee can be transferred transparently to Salary

    
answered by 08.10.2018 в 18:25