0
Good morning guys,
I am new here and new also in JAVA. I have a problem trying to solve it and I am not getting.
When I run the program, the array within the open catalog method repeats countless times even if I put a parameter of the file I opened and read the size of the . db.
When generating the catalog in a function, the catalog becomes huge and there are only 10 items inside.
Could you help me?
package javaapplication6;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Scanner;
public class JavaApplication6 {
public static int[] codigo;
public static String[] nome;
public static double[] preco;
public static String[] descricao;
public static void main(String[] args) throws IOException{
Scanner in = new Scanner(System.in);
System.out.println("Muito obrigado pelo Acesso! O que deseja fazer primeiro?");
System.out.println("\n===========================");
System.out.println("| 1 - Catálogo |");
System.out.println("| 2 - Carrinho |");
System.out.println("| 3 - Finalizar |");
System.out.println("| 4 - Voltar |");
System.out.println("| 0 - Sair |");
System.out.println("===========================\n");
int opcao = in.nextInt();
switch (opcao){
case 1:
carregarcatalogo();
break;
case 3:
finalizarcompra();
}
}
public static void carregarcatalogo() throws IOException{
Scanner in = new Scanner (System.in);
DataInputStream br = new DataInputStream(new FileInputStream("produto.db"));
int tamanho = br.readInt();
codigo = new int[tamanho];
nome = new String[tamanho];
preco = new double[tamanho];
descricao = new String[tamanho];
for (int i = 0; i < tamanho; i++) {
codigo[i] = br.readInt();
nome[i] = br.readUTF();
preco[i] = br.readDouble();
descricao[i] = br.readUTF();
abrircatalogo(tamanho, codigo, nome, preco, descricao);
}
}
public static void abrircatalogo(int tamanho, int[] codigo, String[] nome, double[] preco, String[] descricao){
Scanner in = new Scanner (System.in);
System.out.printf("+-----+------+------+------+---|\n");
for (int i = 0; i < tamanho; i++) {
System.out.printf("| %06d | %-15s | R$%7.2f | %-35s |\n",
codigo[i], nome[i], preco[i], descricao[i]);
System.out.printf("+-----+------+------+------+---|\n");
}
System.out.println("Digite o código para selcionar o produto e pressione enter para confirmar");
}
public static void finalizarcompra(){
Scanner in = new Scanner(System.in);
System.out.println("Para finalizar sua compra, preencha os dados");
System.out.print("Nome completo que está no Cartão de Crédito: ");
String cliente = in.next();
System.out.print("Numero do Cartão de Crédito: ");
int numCredito = in.nextInt();
System.out.print("Data de vencimento: ");
int vencimento = in.nextInt();
System.out.print("CVV: ");
int cvv = in.nextInt();
}
}
Within the
carregarcatalogo()
has afor
who calls theabrircatalogo()
, which also has afor
. I believe this call toabrircatalogo()
should be out of thefor
.– Ronaldo Araújo Alves
That’s right! Thank you so much for your help
– Arthur Bianchi Quiessi
I am happy to have helped. I put as an answer to finish this question.
– Ronaldo Araújo Alves
No problem. Thank you very much
– Arthur Bianchi Quiessi