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();
}
}