Error in JAVA creation of arrayList incompatible types: String can not be converted to branch ----

0

What does the error mean and how can I solve it? It is a database of a bank in which I have to use a type "branch" but I can not use it add that type To My arrayList

ArrayList Array_Asesores = new ArrayList();

Here is the code porfavorrr ...

public class asesor {
    private int IDASESOR;
    private sucursal Sucursal;
    private String Nombre_asesor;
    private String Direccion;
    private String email;
    private int num_cel;
    private int edad;

    //Constructor
    public asesor(int IDASESOR_, String Nombre_asesor_, String Direccion_, sucursal Sucursal_, String email_, int num_cel_, int edad_) {
        IDASESOR = IDASESOR_;
        Nombre_asesor = Nombre_asesor_;
        Direccion = Direccion_;
        Sucursal = Sucursal_;
        email=email_;
        num_cel= num_cel_;
        edad = edad_;
    }
    public int getIDASESOR(){
         return IDASESOR;    
     }
     public void setIDASESOR (int Idasesor){
     this.IDASESOR=Idasesor;
     }

     public String getNombre_asesor(){
     return Nombre_asesor;
     }
     public void setNombre_asesor(String Nombre_ase){
     this.Nombre_asesor=Nombre_ase;
     }

     public String getDireccion(){
     return Direccion;
     }
     public void setDireccion(String Direc){
     this.Direccion=Direc;
     }

     public sucursal getSucursal(){
     return Sucursal;
     }
     public void setSucursal(sucursal Sucu){
     this.Sucursal=Sucu;
     }




private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        /* IDASESOR = IDASESOR_;
            Nombre_asesor = Nombre_asesor_;
            Direccion = Direccion_;
            Sucursal = Sucursal_;
            email=email_;
            num_cel= num_cel_;
            edad = edad_;
        */
        String IDAS1=IDAS.getText();
         String NOM1=NOM.getText();
         String DIR1=DIR.getText();
         String SUC1=SUC.getText();
         String EMA1=EMA.getText(); 
         String NUMC1=NUMC.getText();
         String EDA1=EDA.getText();

          if (!IDAS1.isEmpty()) {
              if (!NOM1.isEmpty()) {
                  if (!DIR1.isEmpty()) {
                      if (!SUC1.isEmpty()) {
                          if (!EMA1.isEmpty()) {
                              if (!NUMC1.isEmpty()) {
                                  if (!EDA1.isEmpty()) {

                                      int IDAS2=Integer.parseInt(IDAS1);
                                       int NUMC2=Integer.parseInt(NUMC1);
                                        int EDA2=Integer.parseInt(EDA1);




                                      asesor nuevo = new asesor(IDAS2, NOM1, DIR1, SUC1, EMA1, NUMC2, EDA2);
                                      Array_Asesores.add(nuevo);
                                      DAT.setText("Datos guardados con exito");



                             }else{
                                JOptionPane.showMessageDialog(null,"Caja de texto Edad esta vacía");
                               EDA.requestFocusInWindow();  
                                  }
                             }else{
                                JOptionPane.showMessageDialog(null,"Caja de texto celular esta vacía");
                               NUMC.requestFocusInWindow();
                                 }
                             }else{
                                JOptionPane.showMessageDialog(null,"Caja de texto Email esta vacía");
                                EMA.requestFocusInWindow();
                            }
                        }else{
                            JOptionPane.showMessageDialog(null,"Caja de texto Sucursal esta vacío");
                            SUC.requestFocusInWindow();
                        }
                    }else{
                        JOptionPane.showMessageDialog(null,"Caja de texto dirección vacío");
                        DIR.requestFocusInWindow();
                    }
                }else{
                    JOptionPane.showMessageDialog(null,"Caja de texto nombre asesor esta vacío");
                    NOM.requestFocusInWindow();
                }
          }else{
              JOptionPane.showMessageDialog(null,"Caja de texto Id asesor esta vacía");
              IDAS.requestFocusInWindow();
          }
        }

I need to turn in the work today afternoon friends, Thanks for your help

Here I leave the code of the other class "Branch" which is what I want to use in my other class "Advisor"

package clases;
/** * * @author Vivian Herrera */ public class sucursal {

private int IDSUCURSAL;
private String Nombre_sucursal;
private String Direccion_sucursal;
private int Telefono;
private String Localidad;


    public sucursal(int IDSUCURSAL_, String Nombre_sucursal_, String Direccion_sucursal_,int T,String L) {
        IDSUCURSAL = IDSUCURSAL_;
        Nombre_sucursal = Nombre_sucursal_;
        Direccion_sucursal = Direccion_sucursal_;
        Localidad = L;
        Telefono = T;
    }

    public int getIDSUCURSAL(){
         return IDSUCURSAL;    
     }
     public void setIDSUCURSAL (int IDSUCURSAL){
     this.IDSUCURSAL=IDSUCURSAL;
     }

     public String getNombre_sucursal(){
     return Nombre_sucursal;
     }
     public void setNombre_sucursal(String Nombre_sucursal){
     this.Nombre_sucursal=Nombre_sucursal;
     }

     public String getDireccion_sucursal(){
     return Direccion_sucursal;
     }
     public void setDireccion_sucursal(String Direccion_sucursal){
     this.Direccion_sucursal=Direccion_sucursal;
     }

     public String getLocalidad(){
 return Localidad;
     }
     public void setLocalidad(String Local){
     this.Localidad=Local;
     }

     public int getTelefono(){
         return Telefono;    
     }
     public void setTelefono (int Tele){
     this.Telefono=Tele;
     }
}
    
asked by Juan Villazon 07.12.2017 в 16:07
source

3 answers

0

As the other user told you, your mistake is that in the constructor you declared a parameter as a branch (which by custom should be capitalized because it is a class).

  

new advisor = new advisor (IDAS2, NOM1, DIR1, SUC1, EMA1, NUMC2, EDA2);

Where it says SUC1 , you should be receiving a branch object (new branch ()), however you're passing it a SUC1 that belongs to the String .

How do you fix it?

a) Changing the type of the constructor parameter to a String.

b) Changing what you say in the advisor, and instead of SUC1 you put new branch () . (Only it will throw you an error in some lines that you have used methods that depend on String )

In conclusion, if you do not have a lot of experience in java, I would tell you to change the constructor to String and that from a short while you can see the different customs that are used.

============= Annex: Points to consider =============

Since there are some things that I saw in your questions are loose I'll leave you the three classes, the copies in their respective classes and you give Run as java application in BancoController strong>.

This is not right since I am expanding in my answer but you see a complete example. I did not solve what you have in the Button because it is something you have to process yourself, surely calling the name of the bank as indicated in the example.

Class Branch

public class Sucursal {

    private int id;
    private String nombreSucursal;
    private String direccionSucursal;
    private int telefono;
    private String localidad;

    public Sucursal() {
        // TODO Auto-generated constructor stub
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getNombreSucursal() {
        return nombreSucursal;
    }

    public void setNombreSucursal(String nombreSucursal) {
        this.nombreSucursal = nombreSucursal;
    }

    public String getDireccionSucursal() {
        return direccionSucursal;
    }

    public void setDireccionSucursal(String direccionSucursal) {
        this.direccionSucursal = direccionSucursal;
    }

    public int getTelefono() {
        return telefono;
    }

    public void setTelefono(int telefono) {
        this.telefono = telefono;
    }

    public String getLocalidad() {
        return localidad;
    }

    public void setLocalidad(String localidad) {
        this.localidad = localidad;
    }

}

Class Advisor

public class Asesor {

    private int id;
    private Sucursal Sucursal;
    private String nombre;
    private String direccion;
    private String email;
    private int telefono;
    private int edad;

    public Asesor(int id, Sucursal sucursal, String nombre, String direccion, String email, int telefono,
            int edad) {
        super();
        this.id = id;
        Sucursal = sucursal;
        this.nombre = nombre;
        this.direccion = direccion;
        this.email = email;
        this.telefono = telefono;
        this.edad = edad;
    }

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public Sucursal getSucursal() {
        return Sucursal;
    }
    public void setSucursal(Sucursal sucursal) {
        Sucursal = sucursal;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public int getTelefono() {
        return telefono;
    }
    public void setTelefono(int telefono) {
        this.telefono = telefono;
    }
    public int getEdad() {
        return edad;
    }
    public void setEdad(int edad) {
        this.edad = edad;
    }
    public String getNombre() {
        return nombre;
    }
    public void setNombre(String nombre) {
        this.nombre = nombre;
    }
    public String getDireccion() {
        return direccion;
    }
    public void setDireccion(String direccion) {
        this.direccion = direccion;
    }

}

Class BankController

public class BancoControlador {

    public BancoControlador() {

        /* Creamos la sucursal que le vamos a pasar por parámetro al Asesor */
        Sucursal sucursal = new Sucursal();
        sucursal.setDireccionSucursal("Dirección de la misma");
        sucursal.setLocalidad("blabla");
        sucursal.setNombreSucursal("Sucursal1");

        /* Acá creamos el arrayList que no entiendo para qué lo necesitás, osea donde la vas a usar, sin embargo te digo que podés cargar varios objetos de Sucursal e iterar sobre los mismos */
        ArrayList<Sucursal> listadoSucursales = new ArrayList<>();
        listadoSucursales.add(sucursal);

        /* Le pasamos por parámetro la sucursal que acabamos de crear */
        Asesor asesor = new Asesor(1, sucursal, "Armando", "Springfield 252", "[email protected]", 1551112535, 21);

        /*
         * Como ejemplo nomás ponemos dentro del constructor el método que llama al
         * objeto Sucursal que a su vez tiene sus atributos como nombre direccion etc,
         */

        System.out.println(asesor.getSucursal().getNombreSucursal());

        /*
         * Fijate que hay un método que llama a la sucursal que le pasamos de parámetro
         * y otra que llama al nombre de ésa sucursal
         */
    }

    public static void main(String[] args) {
        /*
         * Creamos una instancia de esta misma clase y le damos Run as java application
         */
        BancoControlador bc = new BancoControlador();

    }

}

1) Note that in the variables camelCase is used, it starts with lowercase and there are separations with capitals, do not use underscore to name variables.

2) Classes should always start with capital letters, like constructors if they exist.

    
answered by 07.12.2017 / 16:59
source
2

In your advisor builder the fourth parameter is a branch

public asesor(int IDASESOR_, String Nombre_asesor_, String Direccion_, sucursal Sucursal_, String email_, int num_cel_, int edad_)

You are sending him a String

  asesor nuevo = new asesor(IDAS2, NOM1, DIR1, SUC1, EMA1, NUMC2, EDA2);

String SUC1=SUC.getText(); // cuarto parametro es String

As a tip, have good practices, the classes go with UpperCamelCase and the variables with lowCamelCase.

    
answered by 07.12.2017 в 16:17
0

Ok guys I understand your answers, but you have not understood me, what I need is to instantiate both classes, calling the branch class in the advisory class

    
answered by 07.12.2017 в 17:22