FAILURE TO CALL METHOD

0

Simplify it to understand it and make it a little bit clearer. The issue is that with the alternative that I received, it still fails to call the method to add a bottle to a shelf .. Probe and I have the correct instance of the bottle and the shelf. thanks.

import java.util.Scanner;
public class Ejecutora {
   public static void main(String[] args) {
    Compania compa = new Compania();
    Scanner entra = new Scanner(System.in);
    entra.useDelimiter(System.getProperty("line.separator"));
    int opc = 0;
    do {
        System.out.println("1- AGREGA FRASCO");
        System.out.println("2- agrega estante");
        System.out.println("3- AGREGA UN FRASCO A UN ESTANTE");
        System.out.println("4- muestra todo lo que hay en los estantes");
        opc = entra.nextInt();
        switch(opc) {
        case 1:{
            compa.addFrasco();
            break;
        }
        case 2:{
            compa.addEstante();
            break;
        }
        case 3:{
            compa.addFrascoEnEstante();
            break;
        }
        case 4:{
            compa.listFrascos();
            break;
        }
        }
    }while(opc != 0);
    }}


 import java.util.*;
 public class Compania {
 private ArrayList<Estante>estantes;
 private ArrayList<Frasco>frascos;

public Compania() {
    estantes = new ArrayList<Estante>();
    frascos = new ArrayList<Frasco>();
}

public void listFrascos() {
    for (Estante estante: estantes) {
        System.out.println(estante.getNumeroDeEstante());
        for (Frasco frasco: frascos) {
            System.out.println(frasco.getCodigoDelFrasco());
        }
    }
}

public void addFrascoEnEstante() {
    Scanner entra = new Scanner(System.in);
    System.out.println("INGRESE EL CODIGO DEL ESTANTE");
    int codEstante = entra.nextInt();
    boolean estaElEstante = false;
    int posicionDelEstante = 0;
    for ( int i = 0; i < estantes.size() ; i++) {
        if ( estantes.get(i).getNumeroDeEstante() == codEstante) {
            estaElEstante = true;
            posicionDelEstante = i;
        }
    }
    if ( estaElEstante) {
        System.out.println("INGR -1- SIGUE -0- PARA SALIR");
        int rta = entra.nextInt();
        do {
            System.out.println("INGRESE EL CODIGO DEL FRASCO");
            int codFrasco = entra.nextInt();
            boolean frascoEsta = false;
            int lugarDelFrasco = 0;
            for ( int a = 0; a < frascos.size(); a++) {
                if ( frascos.get(a).getCodigoDelFrasco() == codFrasco) {
                    frascoEsta = true;
                    lugarDelFrasco = a;
                }
            }
            if ( frascoEsta) {
                Frasco instanciaDelFrasco = frascos.get(lugarDelFrasco);
                estantes.get(posicionDelEstante).addFrasco(instanciaDelFrasco);
                System.out.println("SE AGREGO UN FRASCO A UN ESTANTE");
            }
        }while ( rta != 0 );
    }
}

public void addFrasco() {
    Scanner entra = new Scanner(System.in);
    System.out.println("INGRESE EL CODIGO DEL FRASCO");
    int codFrasco = entra.nextInt();
    Frasco frasco = this.searchFrasco(codFrasco);
    if ( frasco != null) {
        System.out.println("EL FRASCO YA EXISTE");
    }
    else {
        System.out.println("INGRESE EL PESO");
        float elpeso = entra.nextFloat();
        System.out.println("INGRESE LA DENSIDAD DEL PRODUCTO");
        Float ladensi = entra.nextFloat();
        System.out.println("INGRESE LA MARCA");
        String lamarca = entra.next();
        Frasco elfrasco = new Frasco (codFrasco, elpeso, ladensi, lamarca);
        frascos.add(elfrasco);
        System.out.println("SE AGREGO UN FRASCO CON EXITO");
    }
}

public void addEstante() {
    Scanner entra = new Scanner(System.in);
    System.out.println("INGRESE EL NUMERO DEL ESTANTE");
    int numEstante = entra.nextInt();
    Estante estante = this.searchEstante(numEstante);
    if ( estante != null) {
        System.out.println("EL ESTANTE YA EXISTE");
    }
    else {
            System.out.println("INGRESE  CANTIDAD DE FRASCOS DEL ESTANTE");
            int cantFrascos = entra.nextInt();
            System.out.println("PESO MAXIMO QUE CARGA EL ESTANTE");
            float pesoMax = entra.nextFloat();
            Estante estant = new Estante( numEstante, cantFrascos, pesoMax);
            estantes.add(estant);
            System.out.println("EL ESTANTE SE AGREGO CON EXITO");
        }
    }

public Frasco searchFrasco (int val) {
    int a = 0;
    while ( a < frascos.size() && !( frascos.get(a).sosCodigoDelFrasco(val)))
        a++;
    if ( a < frascos.size()) {
        return frascos.get(a);
    }
    else {
        return null;
    }
}

public Estante searchEstante (int val) {
    int e = 0;
    while ( e < estantes.size() && !( estantes.get(e).sosNumEstante(val)))
        e++;
    if ( e < estantes.size()) {
        return estantes.get(e);
    }
    else {
        return null;
    }}}


import java.util.*;
public class Estante {
private int numeroDeEstante;
private int cantidadDeFrascos;
private float pesoMaximo;
private ArrayList <Frasco> frascos;

public Estante ( int numDeEstante, int cantDeFras, float pesoMax) {
    numeroDeEstante = numDeEstante;
    cantidadDeFrascos = cantDeFras;
    pesoMaximo = pesoMax;
}

public int getNumeroDeEstante() {
    return numeroDeEstante;
}

public void setNumeroDeEstante(int numeroDeEstante) {
    this.numeroDeEstante = numeroDeEstante;
}

public int getCantidadDeFrascos() {
    return cantidadDeFrascos;
}

public void setCantidadDeFrascos(int cantidadDeFrascos) {
    this.cantidadDeFrascos = cantidadDeFrascos;
}

public float getPesoMaximo() {
    return pesoMaximo;
}

public void setPesoMaximo(float pesoMaximo) {
    this.pesoMaximo = pesoMaximo;
}

public ArrayList<Frasco> getFrascos() {
    return frascos;
}

public void setFrascos(ArrayList<Frasco> frascos) {
    this.frascos = frascos;
}

public boolean sosNumEstante(int valor) {
    return numeroDeEstante == valor;
}

public void addFrasco(Frasco frasco) {
    frascos.add(frasco);
}}


public class Frasco {
private int codigoDelFrasco;
private float peso;
private float densidad;
private String marcaDelProducto;

public Frasco (int codDelFrasco, float pes, float densid, String marcaDelProd) {
    codigoDelFrasco = codDelFrasco;
    peso = pes;
    densidad = densid;
    marcaDelProducto = marcaDelProd;
}

public int getCodigoDelFrasco() {
    return codigoDelFrasco;
}

public void setCodigoDelFrasco(int codigoDelFrasco) {
    this.codigoDelFrasco = codigoDelFrasco;
}

public float getPeso() {
    return peso;
}

public void setPeso(float peso) {
    this.peso = peso;
}

public float getDensidad() {
    return densidad;
}

public void setDensidad(float densidad) {
    this.densidad = densidad;
}

public String getMarcaDelProducto() {
    return marcaDelProducto;
}

public void setMarcaDelProducto(String marcaDelProducto) {
    this.marcaDelProducto = marcaDelProducto;
}

public boolean sosCodigoDelFrasco(int valor) {
    return codigoDelFrasco == valor;
}}
    
asked by Mauricio Vega 29.05.2018 в 04:37
source

1 answer

0

You did not have to create a new question if you already have one open about your same exercise here: JAVA - ERROR IN THE MESSAGE . I see that my answer helped you because you use some of the code I proposed, I'm glad but, you have not thanked me by choosing my answer as selected and you have not voted for it either.

  • In switch you do not have to have { } after case . They are not necessary, you can check the official documentation here: The switch Statement .

  • In your class Compania you do not show a warning message when you can not find a bottle / shelf with the code provided. In addition, you do not have a way out of do-while . When you add a bottle to a shelf you must initialize rta = 0 .

  • Fixed code, inside the addFrascoEnEstante method:

    .
    ...
    ....
    if (estaElEstante) {
                ..
                ...
                ....
                    if (frascoEsta) {
                        Frasco instanciaDelFrasco = frascos.get(lugarDelFrasco);
                        estantes.get(posicionDelEstante).addFrasco(instanciaDelFrasco);
                        System.out.println("SE AGREGO UN FRASCO A UN ESTANTE");
                        rta = 0; // Para salir del do-while
                    } else { // Avisar al usuario si el frasco no ha sido encontrado
                        System.out.println("FRASCO NO ENCONTRADO");
                    }
                } while (rta != 0);
            } else { // Avisar al usuario si el estante no ha sido encontrado
                System.out.println("ESTANTE NO ENCONTRADO");
            }
    ...
    ..
    .
    
  • The problem you have is a NullPointerException because within the class Estante , the ArrayList<Frasco> frascos is not created. You have the declared variable but, you never do new ArrayList<Frasco>() .
  • Fixed code, inside class Estante :

    public class Estante {
        .
        ..
        private ArrayList<Frasco> frascos = new ArrayList<Frasco>();
        ..
        .
    }
    

    You can try to do this in the constructor of the class or in the place that you want.

    I have tried the exercise again with these changes and it works correctly.

    I hope I helped you. If any Answer has helped you, do not forget to select it as an Elected Response and vote for it. Greetings.

        
    answered by 29.05.2018 / 05:41
    source