problem with bringing array from different classes

-1

I have a program in Java that simulates the events that occur in an e-commerce site. My program consists of two classes. One for the product category and another for the car. The first class allows the user to enter products into an array of products, and with the second the user decides without a product goes to the shopping cart array. I need, in this way, to call the array of products of the first class (category) to bring it to the second (shopping cart) but I do not know how to bring arrays between classes. Is there any way to do it?

CATEGORY

import java.util.Scanner;
public class Categoria {
    private Scanner teclado;
    private String[] categoria;
    private String[] categoriaMarca;
    public void cargar() {
        teclado = new Scanner(System.in);
        categoria = new String[4];
        categoriaMarca = new String[4];
        System.out.println("Carga de productos");
        for(int f=0; f<categoria.length;f++) {
            System.out.print("Ingrese la categoría del producto ");
            categoria[f] = teclado.next();
            System.out.print("Ingrese la marca del producto ");
            categoriaMarca[f] = teclado.next(); 
        }
    }
    public String getCategoria(int indice) {
        return categoria[indice];
    }
    public String getCategoriaMarca(int indice) {
        return categoriaMarca[indice];
    }
    public void imprimir() {
        for (int f = 0; f<categoria.length;f++) {
            System.out.println(categoria[f] + " - " + categoriaMarca[f]);
        }
    }
}

CARRO

import java.util.Scanner;
public class Carro extends Categoria {
    private Scanner teclado;
    private String respuesta1;
    private String respuesta2;
    private int respuesta3;
    private String[] carro;
    public void mostrar() {
        teclado = new Scanner(System.in);
        System.out.println("deseas ver los productos? ");
        respuesta1 = teclado.next();
            if (respuesta1 == "si") {
                System.out.println(getCategoria());
            }
    }
    public void carro() {
        teclado = new Scanner(System.in);
        System.out.println("desea ingresar algún producto al carro? ");
        if (respuesta2 == "si") {
            System.out.println("ingrese el número del producto ");
            respuesta3 = teclado.nextInt();
            if (respuesta3 == 1) {
                carro[1] = categoría[1];
            }
        }
    }
}

APPLICATION

public class Aplicacion {
    public static void main(String[] arg) {
        categoria cta = new categoria();
        cta.cargar();
        System.out.println("Lista de productos ingresados: ");
        pv.imprimir();
        Carro cr = new Carro();
        cr.mostrar();
        cr.carro();
    }
}
    
asked by virtual8870 12.11.2017 в 16:50
source

2 answers

1

Simply the main should be in the class car because it is the most logical, is the one that will work with the instances of the Class category.

This way you create an instance of the Class category in the main of the class Car and work with it, just take the code that you have inside the main of your class category, delete that main and create a main in the class Cart with that code inside, I have added you in the Class category some methods to be able to work with the elements of the array attributes of the Class category based on an index.

CATEGORY

import java.util.Scanner;

public class Categoria {

    private Scanner teclado;
    private String[] categoria;
    private String[] categoriaMarca;

    public void cargar() {
        teclado = new Scanner(System.in);
        categoria = new String[4];
        categoriaMarca = new String[4];
        System.out.println("Carga de productos");

        for(int f=0; f<categoria.length;f++) {
            System.out.print("Ingrese la categoría del producto ");
            categoria[f] = teclado.next();
            System.out.print("Ingrese la marca del producto ");
            categoriaMarca[f] = teclado.next(); 
        }
    }

    public String getCategoria(int indice){
        return categoria[indice];
    }

    public String getCategoriaMarca(int indice){
        return categoriaMarca[indice];
    }

    public void imprimir() {
        for (int f = 0; f<categoria.length;f++) {
            System.out.println(categoria[f] + " - " + categoriaMarca[f]);
        }
    }
}

CARRO

import java.util.Scanner;

public class Carro {

    private Scanner teclado;
    private String respuesta1;
    private String respuesta2;
    private int respuesta3;
    private String[] carro;

    public static void main(String[] arg) {

        Categoria pv = new Categoria();
        pv.cargar();
        System.out.println("Lista de productos ingresados: ");
        pv.imprimir();

    }

    public void mostrar() {
        teclado = new Scanner(System.in);
        System.out.println("deseas ver los productos? ");
        respuesta1 = teclado.next();
            if (respuesta1 == "si") {
                //mostrar la lista de productos de la clase categoria;
            }
    }
    public void carro() {
        teclado = new Scanner(System.in);
        System.out.println("desea ingresar algún producto al carro? ");
        if (respuesta2.equals("si")) {
            System.out.println("ingrese el número del producto ");
            respuesta3 = teclado.nextInt();
            if (respuesta3 == 1) {
                //ingresar el primer elemento del array producto al array carro;
            }
        }
    }
}

I recommend working with an object of type ArrayListy to parameterize it with the type String, as an attribute of the array of cars instead of a normal String array.

This way you can use the add method of the ArrayList class to add a concatenation of getCategoria (index) + getCategoriaMarca (index) the index being the product number that the person wants to add to the car - 1.

This is because the indexes start at 0, and I guess you want the first product to be 1, so if we talk about a product 1, this would be in index 0, it would be:

 respuesta3 = teclado.nextInt() - 1;

After that in the condition you must bear in mind that answer3 represents the index, so you must value the 0, you could call answer 3 'index' better to clarify.

    
answered by 12.11.2017 в 17:56
0

I have returned to the structure with the carriage class in main, using arraylist, but when adding elements to my arraylist I can not solve this error: "constructor Product in class Product can not be applied to given types; required: no arguments found: java.lang.String, java.lang.String reason: current and formal argument list differ in length ". From what I have been able to see, this error is mainly caused by incorrectly declared attributes or variables, but I see the code and I do not find anything irregular.

Product.java

public class Producto {
    private String categoria;
    private String categoriaMarca;
    public void Producto(String categoria, String categoriaMarca) {
        this.categoria = categoria;
        this.categoriaMarca = categoriaMarca;
    }
    public String getCategoria() {
        return categoria;
    }
    public String getCategoriaMarca() {
        return categoriaMarca;
    }
}

Aplicacion.java

import java.util.*;
public class Aplicacion {
    public static void main(String[] arg) {
        Scanner s = new Scanner(System.in);
        ArrayList<Producto> carro = new ArrayList<Producto>();
        String cat, catMarca;
        for(int i=0; i<4; i++) {
            System.out.print("Escriba la categoría: ");
            cat = s.next();
            System.out.print("Escriba la categoría marca: ");
            catMarca = s.next();
            carro.add(new Producto(cat, catMarca));
        }
        Iterator<Producto> iterator = carro.iterator();
        while(iterator.hasNext()) {
            Producto temp = iterator.next();
            System.out.print(temp.getCategoria());
            System.out.print(temp.getCategoriaMarca());
        }
    }
}
    
answered by 13.11.2017 в 17:23