doubts with java program structure [closed]

-3

I have been working with a code to simulate an e-commerce website. As I am new with java, I have tried to save some concepts by viewing information on the web. Finally I have the program. Three classes, one for products, another for the car and the application class. Everything is fine except that in the end the code does not allow entering products into the shopping cart. Why the program comes up in this part, maybe the main class is wrong? I appreciate any help you can give me.

Product.java

public class Producto {
    private String categoria;
    private String categoriaMarca;
    public Producto(String categoria, String categoriaMarca) {
        this.categoria = categoria;
        this.categoriaMarca = categoriaMarca;
    }
    public String getCategoria() {
        return categoria;
    }
    public String getCategoriaMarca() {
        return categoriaMarca;
    }
    public String toString() {
        return "Categoria: "+this.getCategoria() + " /  Marca: " + this.getCategoriaMarca();
    }
}

Carro.java

public class Carro {
    private String ingreso;
    public Carro(String ingreso) {
        this.ingreso = ingreso; 
    }
    public String getIngreso() {
        return ingreso;
    }
    public String toString() {
        return "Este producto " + this.getIngreso() + " ha sido ingresado al carro de compras.";
    }
}

Application.java

import java.util.*;
public class Aplicacion {
    public static void main(String[] arg) {
        Scanner s = new Scanner(System.in);
        ArrayList<Producto> catalogo = new ArrayList<Producto>();
        ArrayList<Carro> ingreso = new ArrayList<Carro>(); 
        int productos = 1;
        int contador = 0;
        String cat, catMarca;
        String resp1, resp2, resp3, resp4;
        for(int i=0; i<2; i++) {
            System.out.println("Escriba la categoría del producto: ");
            cat = s.next();
            System.out.println("Ingrese el producto: ");
            catMarca = s.next(); 
            catalogo.add(new Producto(cat, catMarca)); 
        }
        System.out.println("Para ver el catálogo de productos, presione 0 ");
        productos = s.nextInt();
        if (productos == 0) {
            for (int f = 0; f < catalogo.size(); f++) {
                System.out.println(catalogo.get(f));
            } 
        } 
        System.out.println("Deseas agregar alguno de estos productos al carro de compras?: ");
        resp1 = s.next();
        if (resp1 == "si" || resp1 == "SI" || resp1 == "Si") {
            System.out.println("Indica el producto que deseas ingresar");
            resp2 = s.next();
            ingreso.add(new Carro(resp2));
            System.out.println("Deseas agregar algun producto más al carro de compras?: ");
            resp3 = s.next();
            while (resp3 == "si" || resp1 == "SI" || resp1 == "Si") {
                contador = contador + 1;
                ingreso.add(new Carro(resp3));
            }
        }
        System.out.println("Deseas ver los productos ingresados al carro de compras? ");
        resp4 = s.next();
        if (resp4 == "si" || resp4 == "SI" || resp4 == "Si") {
            for (int h = 0; h < ingreso.size(); h++) {
                System.out.println(ingreso.get(h));
            }
        }
    }
}
    
asked by virtual8870 14.11.2017 в 16:43
source

1 answer

2

As other colleagues say, when you ask a question, you must clarify what your question is, that is, what is really your problem, where and in what line to save time. I had to run your program and debugge a little to detect "the problems your code has".

  • Do not use next() , use nextLine() .
  • The other problem is with .nextInt() . When you enter a number and press "Enter", .nextInt() only consume the number and not the end of the line. The end of every line is \n . Thus, when you execute .nextLine() you consume the "end of line" and you will be ready to read the next String you need.

You can solve this problem in the following ways:

Add an% extra% co, between .nextLine() and .nextInt() . The .nextLine() extra will consume the "end of the line" always.

Example:

int number = scanner.nextInt();
scanner.nextLine(); // Consume "\n"
String string1 = scanner.nextLine();

Another way is to use .nextLine() instead of .nextLine() . to read the number. Remember that .nextInt() always consumes "the end of the line". Since .nextLine() returns a .nextLine() , you have to convert that String to String . How ?, using Integer .

Example of this:

int number = Integer.parseInt(scanner.nextLine());

If you use this form, you can receive a Integer.parseInt(...) of the Exception method if the Integer.parseInt(...) that you pass to it as an argument can not be converted to String . For example, if you pass "Hello World" and try to convert that String to Integer, obviously it will not be able to. To handle those exceptions, you must use a Integer block. For this reason, make sure that if you use this form, the value entered is always a number. Another advantage of this form is that you do not have to worry about doing try-catch every time you want to read a .nextLine() .

  • Another problem in your code is with line INT . Look closely at what is inside the while (resp3 == "si" || resp1 == "SI" || resp1 == "Si") , you have ( ) in the first and in the others you have resp3 Second, when you use resp1 you are making your program stay at while and it never ends. Why? Because the value of BUBLE INFINITO will always be resp3 and therefore the si, SI o Si It will never end.
  • On that same line of WHILE , do not use WHILE , use if () . Example: .equals() . It never fails as it is specifically for variables type resp1.equals("si") .
  • Another problem is that you do not close the String at the end of your code, when you use Scanner , you should always do Scanner . This what indicates the official documentation, although many people do not, but, if the bosses say it, why not do it ?, xDDD.

Your code fixed:

import java.util.*;
public class Aplicacion {
    public static void main(String[] arg) {

        Scanner scan = new Scanner(System.in);
        ArrayList<Producto> catalogo = new ArrayList<Producto>();
        ArrayList<Carro> ingreso = new ArrayList<Carro>(); 

        int productos = 1;
        int carrito = 0;
        String categoria, producto;
        String resp1, resp2, resp3, resp4;

        for(int i=0; i<2; i++) {
            System.out.println("Escriba la categoría del producto: ");
            categoria = scan.nextLine();
            System.out.println("Ingrese el producto: ");
            producto = scan.nextLine(); 
            catalogo.add(new Producto(categoria, producto)); 
        }

        System.out.println("Para ver el catálogo de productos, presione 0 ");
        int opcion = Integer.parseInt(scan.nextLine());
        if (opcion == 0) {
            for (int f = 0; f < catalogo.size(); f++) {
                System.out.println(catalogo.get(f));
            } 
        } 

        System.out.println("Deseas agregar alguno de estos productos al carro de compras?: ");
        resp1 = scan.nextLine();
        System.out.println(resp1);

        if (resp1.equals("si") || resp1.equals("SI") || resp1.equals("Si")) {
            System.out.println("Indica el producto que deseas ingresar");
            resp2 = scan.nextLine();
            ingreso.add(new Carro(resp2));

            System.out.println("Deseas agregar algun producto más al carro de compras?: ");
            resp3 = scan.nextLine();
            if (resp3.equals("si") || resp3.equals("SI") || resp3.equals("Si")) {
                carrito = carrito + 1;
                ingreso.add(new Carro(resp3));
            }
        }


        scan.close(); // Debes cerrar el Scanner
    }
}

I hope I have helped you friend.

    
answered by 14.11.2017 / 17:51
source