JAVA - ERROR IN THE MESSAGE

0

I have these two classes. Briefly, to the shelf I have to add a bottle ... weight, quantity are variables that I do not yet define ... for that matter they do not matter. The issue is that I find the shelf but it does not add the bottle. I think I'm wrong in the message to add and I do not realize it. Does anyone realize the error?

Any clarification, give me notice ...

public class Almacen {
private ArrayList<Frasco>frascos;
private ArrayList<Estante>estantes;

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

public void agregaFrasco() {
    Scanner entra = new Scanner(System.in);
    System.out.println("INGRESE EL CODIGO DEL FRASCO");
    int cdf = entra.nextInt();
    Frasco fra = this.buscaFrasco(cdf);
    if ( fra != null)
        System.out.println("EL FRASCO EXISTE");
    else {
        System.out.println("INGRESE EL PESO");
        float pe = entra.nextFloat();
        System.out.println("INGRESE LA DENSIDAD DEL PRODUCTO");
        Float de = entra.nextFloat();
        System.out.println("INGRESE LA MARCA");
        String ma = entra.next();
        Frasco fras = new Frasco(cdf, pe, de, ma);
        frascos.add(fras);
        System.out.println("EL FRASCO SE AGREGO CORRECTAMENTE");
    }
}


public void agregaEstante() {
    Scanner entra = new Scanner(System.in);
    System.out.println("INGRESE EL NUMERO DEL ESTANTE");
    int nd = entra.nextInt();
    Estante esta = this.buscaEstante(nd);
    if(esta != null)
        System.out.println("ESTANTE YA CARGADO");
    else {
        System.out.println("INGRESE LA CANTIDAD DE FRASCOS QUE SOPORTA EL ESTANTE");
        int cd = entra.nextInt();
        System.out.println("INGRESE EL PESO MAXIMO");
        float d = entra.nextFloat();
        Estante es = new Estante(nd, cd, d);
        estantes.add(es);
        System.out.println("SE AGREGO UN ESTANTE NUEVO");
    }
}

public void agregaFrascosAlEstante() {
    Scanner entra = new Scanner(System.in);
    System.out.println("INGRESE EL NUMERO DE ESTANTE");
    int ne = entra.nextInt();
    int b = 0;
    while(b < estantes.size() && !(estantes.get(b).sosElEstante(ne)))
        b++;
    //Estante estan = this.buscaEstante(ne);
    //if(estan != null) {
    System.out.println(estantes.get(b).darCantidadDeFrascos());
    System.out.println(estantes.get(b).darNumeroDeEstante());
    System.out.println(estantes.get(b).darPesoMaximo());

        if(b < estantes.size()) {
            System.out.println("QUIERE AGREGAR UN FRASCO AL ESTANTE?");
            System.out.println("INGRESE 1 O 0 PARA SALIR");
            int op = entra.nextInt();
        do {
            System.out.println("INGRESE EL CODIGO DEL FRASCO");
            int cd = entra.nextInt();
            Frasco fra = this.buscaFrasco(cd);
            if(fra != null) {
                Estante estan = estantes.get(b);       //<<<
                estan.agregaFrasco(fra);               //<<< problem!
                //estantes.get(b).agregaFrasco(fra);   //<<<
                //estan.agregaFrasco(fra);             //<<<
                System.out.println("SE AGREGO UN FRASCO AL ESTANTE ok");
            }
            else
                System.out.println("EL FRASCO NO EXISTE");
        }while(op != 0);
    }
    else
        System.out.println("ESE ESTANTE NO EXISTE");
}

public class Estante {
private int numeroDeEstante;
private int cantidadDeFrascos;
private float pesoMaximo;
private ArrayList <Frasco>frascos;

public Estante(int nde, int cdf, float pm) {
    numeroDeEstante = nde;
    cantidadDeFrascos = cdf;
    pesoMaximo = pm;
}

public void agregaFrasco(Frasco fr) {
    frascos.add(fr);
}

The complete code is: I think I'm mishandling the way I add a jar to a shelf ...

import java.util.*;
public class Empresa {
public static void main(String[] args) {
    Almacen esta = new Almacen();
    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("6- agrega un estante");
        System.out.println("7- AGREGA UN FRASCO A UN ESTANTE");
        opc = entra.nextInt();
        switch(opc) {
        case 1:{
            esta.agregaFrasco();
            break;
        }
        case 6:{
            esta.agregaEstante();;
            break;
        }
        case 7:{
            esta.agregaFrascosAlEstante();;
            break;
        }
        }
    }while(opc != 0);

   }
  }

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

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

public void agregaFrasco() {
    Scanner entra = new Scanner(System.in);
    System.out.println("INGRESE EL CODIGO DEL FRASCO");
    int cdf = entra.nextInt();
    Frasco fra = this.buscaFrasco(cdf);
    if ( fra != null)
        System.out.println("EL FRASCO EXISTE");
    else {
        System.out.println("INGRESE EL PESO");
        float pe = entra.nextFloat();
        System.out.println("INGRESE LA DENSIDAD DEL PRODUCTO");
        Float de = entra.nextFloat();
        System.out.println("INGRESE LA MARCA");
        String ma = entra.next();
        Frasco fras = new Frasco(cdf, pe, de, ma);
        frascos.add(fras);
        System.out.println("EL FRASCO SE AGREGO CORRECTAMENTE");
    }
}

public void agregaEstante() {
    Scanner entra = new Scanner(System.in);
    System.out.println("INGRESE EL NUMERO DEL ESTANTE");
    int nd = entra.nextInt();
    Estante esta = this.buscaEstante(nd);
    if(esta != null)
        System.out.println("ESTANTE YA CARGADO");
    else {
        System.out.println("INGRESE LA CANTIDAD DE FRASCOS QUE SOPORTA EL ESTANTE");
        int cd = entra.nextInt();
        System.out.println("INGRESE EL PESO MAXIMO");
        float d = entra.nextFloat();
        Estante es = new Estante(nd, cd, d);
        estantes.add(es);
        System.out.println("SE AGREGO UN ESTANTE NUEVO");
    }
}

public void agregaFrascosAlEstante() {
    Scanner entra = new Scanner(System.in);
    System.out.println("INGRESE EL NUMERO DE ESTANTE");
    int ne = entra.nextInt();
    int b = 0;
    while(b < estantes.size() && !(estantes.get(b).sosElEstante(ne)))
        b++;
    //Estante estan = this.buscaEstante(ne);
    //if(estan != null) {
    System.out.println(estantes.get(b).darCantidadDeFrascos());
    System.out.println(estantes.get(b).darNumeroDeEstante());
    System.out.println(estantes.get(b).darPesoMaximo());

        if(b < estantes.size()) {
            System.out.println("QUIERE AGREGAR UN FRASCO AL ESTANTE?");
            System.out.println("INGRESE 1 O 0 PARA SALIR");
            int op = entra.nextInt();
        do {
            System.out.println("INGRESE EL CODIGO DEL FRASCO");
            int cd = entra.nextInt();
            Frasco fra = this.buscaFrasco(cd);
            if(fra != null) {
                Estante estan = estantes.get(b);
                //estan.agregandoFrasco(fra);       <<
                //estantes.get(b).agregaFrasco(fra);    << esta comentado, ac'a esta el error
                //estan.agregaFrasco(fra);      <<
                System.out.println("SE AGREGO UN FRASCO AL ESTANTE CON EXITO");
            }
            else
                System.out.println("EL FRASCO NO EXISTE");
        }while(op != 0);
    }
    else
        System.out.println("ESE ESTANTE NO EXISTE");
}

import java.util.*;
public class Estante {

    private int numeroDeEstante;
    private int cantidadDeFrascos;
    private float pesoMaximo;
    private ArrayList <Frasco>frascos;

public Estante(int nde, int cdf, float pm) {
    numeroDeEstante = nde;
    cantidadDeFrascos = cdf;
    pesoMaximo = pm;
}

public void agregandoFrasco(Frasco fr) {
    frascos.add(fr);
}

public int darNumeroDeEstante() {
    return numeroDeEstante;
}

public boolean sosElEstante(int cde) {
    return ( numeroDeEstante == cde);
}

public void setCantidadDeFrascos(int v) {
    cantidadDeFrascos = v;
}

public void setPesoMaximo(float s) {
    pesoMaximo = s;
}

public int darCantidadDeFrascos() {
    return cantidadDeFrascos;
}

public float darPesoMaximo() {
    return pesoMaximo;
  }
}


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

public Frasco(int cdf, float pe, float den, String mdp) {
    codigoDelFrasco = cdf;
    peso = pe;
    densidad = den;
    marcaDelProducto = mdp;
}

public boolean sosElFrasco(int ff) {
    return ( codigoDelFrasco == ff);
}

public int darCodigo() {
    return codigoDelFrasco;
}

public float darPeso() {
    return peso;
}

public float darDensidad() {
    return densidad;
}

public String darMarcaDelProducto() {
    return marcaDelProducto;
 }
}
    
asked by Mauricio Vega 27.05.2018 в 03:20
source

1 answer

0

I have had a lot of difficulty understanding your exercise due to the lack of convention in the names, variables, methods, etc ... Always try to use generic names and make use of the convention that has a language for when others Programmers analyze your code can understand it the first time.

I have modified your code to try to comply with the conventions in Java . Avoid the use of variables called: opt, cdf, fra, etc .. No other person can understand its meaning except you. When you are working with a group of programmers on a project, the use of the convention is very important.

I used Getters and Setters in your classes as well as For-each loop to determine if a jar / shelf exists or not.

The Java documentation summarizes your naming convention.

Right now I have done your exercise completely, tell yourself Add a bottle, Add a shelf, Add a bottle to a shelf, Validate whether a bottle / shelf exists or not. As I made modifications in each of your classes, I will have to paste all the code, each class separately.

I've tried to comment on all the changes so you can understand what I've done.

Business Class:

import java.util.Scanner;

public class Empresa {
    public static void main(String[] args) {

        Almacen almacen = new Almacen();
        Scanner scanner = new Scanner(System.in);
        scanner.useDelimiter(System.getProperty("line.separator"));

        int opcion = 0;
        do {
            System.out.println("1- AGREGA FRASCO");
            System.out.println("6- agrega un estante");
            System.out.println("7- AGREGA UN FRASCO A UN ESTANTE");
            opcion = scanner.nextInt();
            switch (opcion) {
            case 1: {
                almacen.agregaFrasco();
                break;
            }
            case 6: {
                almacen.agregaEstante();
                break;
            }
            case 7: {
                almacen.agregaFrascosAlEstante();
                break;
            }
            }
        } while (opcion != 0);

        // Cerrar Scanner
        scanner.close();
    }
}

Storage Class:

import java.util.ArrayList;
import java.util.Scanner;

public class Almacen {

    private ArrayList<Frasco> frascos;
    private ArrayList<Estante> estantes;

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

    public void agregaFrasco() {

        Scanner scanner = new Scanner(System.in);
        System.out.println("INGRESE EL CODIGO DEL FRASCO");
        int codigoFrasco = scanner.nextInt();

        // Variable boolean para determinar si existe o no el frasco
        boolean existeElFrasco = false;

        // Ver si existe un frasco con ese código
        // For-each loop
        for (Frasco frasco : frascos) {
            if (frasco.getCodigoDelFrasco() == codigoFrasco) {

                // Frasco encontrado
                existeElFrasco = true;
            }
        }

        if (existeElFrasco) { // Si existe

            System.out.println("EL FRASCO EXISTE");

        } else { // Si no existe

            System.out.println("INGRESE EL PESO");
            float peso = scanner.nextFloat();
            System.out.println("INGRESE LA DENSIDAD DEL PRODUCTO");
            Float densidad = scanner.nextFloat();
            System.out.println("INGRESE LA MARCA");
            String marcaDelProducto = scanner.next();
            Frasco frasco = new Frasco(codigoFrasco, peso, densidad, marcaDelProducto);
            frascos.add(frasco);
            System.out.println("EL FRASCO SE AGREGO CORRECTAMENTE");

        }
    }

    public void agregaEstante() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("INGRESE EL NUMERO DEL ESTANTE");
        int numeroDeEstante = scanner.nextInt();

        // Variable boolean para determinar si existe o no el estante
        boolean existeElEstante = false;

        // Ver si existe un estante con ese número
        // For-each loop
        for (Estante estante : estantes) {
            if (estante.getNumeroDeEstante() == numeroDeEstante) {

                // Estante encontrado
                existeElEstante = true;
            }
        }

        if (existeElEstante) // Si existe

            System.out.println("ESTANTE YA CARGADO");

        else { // Si no existe

            System.out.println("INGRESE LA CANTIDAD DE FRASCOS QUE SOPORTA EL ESTANTE");
            int cantidadDeFrascos = scanner.nextInt();
            System.out.println("INGRESE EL PESO MAXIMO");
            float pesoMaximo = scanner.nextFloat();
            Estante estante = new Estante(numeroDeEstante, cantidadDeFrascos, pesoMaximo);
            estantes.add(estante);
            System.out.println("SE AGREGO UN ESTANTE NUEVO");

        }
    }

    public void agregaFrascosAlEstante() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("INGRESE EL NUMERO DE ESTANTE");
        int numeroDeEstante = scanner.nextInt();

        // Variable boolean para determinar si existe o no el estante
        boolean existeElEstante = false;

        // Posición del estante en el ArrayList
        int posicionEstante = 0;

        // Ver si existe un estante con ese número
        for (int i = 0; i < estantes.size(); i++) {
            if (estantes.get(i).getNumeroDeEstante() == numeroDeEstante) {

                // Estante encontrado
                existeElEstante = true;

                // Guardar posicion
                posicionEstante = i;

            }
        }

        if (existeElEstante) {
            System.out.println("ESTANTE ENCONTRADO");
            System.out.println("Numero de estante: " + estantes.get(posicionEstante).getNumeroDeEstante());
            System.out.println("Cantidad de frascos: " + estantes.get(posicionEstante).getCantidadDeFrascos());
            System.out.println("Peso maximo: " + estantes.get(posicionEstante).getPesoMaximo());


            System.out.println("QUIERE AGREGAR UN FRASCO AL ESTANTE?");
            System.out.println("INGRESE 1 O 0 PARA SALIR");
            int opcion = scanner.nextInt();

            do {
                System.out.println("INGRESE EL CODIGO DEL FRASCO");
                int codigoFrasco = scanner.nextInt();

                // Variable boolean para determinar si existe o no el frasco
                boolean existeElFrasco = false;

                // Posición del frasco en el ArrayList
                int posicionFrasco = 0;

                // Ver si existe un frasco con ese código
                // For-each loop
                for (int i = 0; i < estantes.size(); i++) {
                    if (frascos.get(i).getCodigoDelFrasco() == codigoFrasco) {

                        // Estante encontrado
                        existeElFrasco = true;

                        // Guardar posicion
                        posicionFrasco = i;

                    }
                }

                if (existeElFrasco) { // Si existe

                    // Obtener el frasco deseado en forma de instancia
                    Frasco instanciaFrasco = frascos.get(posicionFrasco);

                    // Agregar al ArrayList de frascos que tiene el estante
                    estantes.get(posicionEstante).addFrasco(instanciaFrasco);

                    System.out.println("SE AGREGO UN FRASCO AL ESTANTE CON EXITO");

                } else {// Si no existe

                    System.out.println("EL FRASCO NO EXISTE");

                }

                // Salir del do-while
                opcion = 0;

            } while (opcion != 0);

        } else {
            System.out.println("ESTANTE NO ENCONTRADO");
        }
    }
}

Shelf Class:

import java.util.ArrayList;

public class Estante {

    private int numeroDeEstante;
    private int cantidadDeFrascos;
    private float pesoMaximo;

    // Crear ArrayList a la primera
    private ArrayList<Frasco> frascos = new ArrayList<Frasco>();

    // Constructor
    public Estante(int numeroDeEstante, int cantidadDeFrascos, float pesoMaximo) {
        super();
        this.numeroDeEstante = numeroDeEstante;
        this.cantidadDeFrascos = cantidadDeFrascos;
        this.pesoMaximo = pesoMaximo;
    }

    // Getters & Setters
    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;
    }

    // Agregar un frasco al ArrayList
    public void addFrasco(Frasco frasco) {
        this.frascos.add(frasco);
    }

}

Flask Class:

public class Frasco {

    private int codigoDelFrasco;
    private float peso;
    private float densidad;
    private String marcaDelProducto;

    // Constructor
    public Frasco(int codigoDelFrasco, float peso, float densidad, String marcaDelProducto) {
        super();
        this.codigoDelFrasco = codigoDelFrasco;
        this.peso = peso;
        this.densidad = densidad;
        this.marcaDelProducto = marcaDelProducto;
    }

    // Getters & Setters
    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;
    }

}

I hope I have helped you friend! I hope you can complete whatever you need.

    
answered by 27.05.2018 / 18:48
source