Store items in a cart

Asked

Viewed 47 times

0

Good afternoon!

Guys, I have a problem in my code where I have a method to open the catalog (open catalog) with some items and select the iens that I wish, but I can’t pass these selected items to another method where I will just visualize the cart (cart method) in which I selected my items.

I just need to view them and have options to go back and continue selecting or finalizing the purchase of orders

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 2:
                    carrinho();    
                    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("\n===========================");
            System.out.println("Digite 1 para selecionar itens");
            System.out.println("Digite 2 para ver o carrinho");
            System.out.println("Digite 3 para sair");
            System.out.println("===========================\n");

            int opcao = in.nextInt();
            switch(opcao){
                case 1:

            System.out.println("Selecione itens ou pressione 2 para ir ao carrinho:");        

            int user = in.nextInt();
            int quantidade = 3;
            int cod [] = new int[quantidade];

            for (int i = 0; i < cod.length; i++) {
                 cod[i] = in.nextInt();
                 carrinho(cod);
                if(cod[i] == 2 ){
                    carrinho(cod);
                    break;
                }else{

                }
                System.out.println("#" + (i+1) + "ª selecionado e enviado ao carrinho");
            }  
                case 3:
                System.exit(0);
                break;
            }
    }

    public static void carrinho(int cod[]){
        for (int i = 0; i < cod.length; i++) {
            System.out.println("cod");
        }
    }

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

2 answers

0

Good night, Arthur

I haven’t performed any tests yet but just by reading the code you can kindly test?

int opcao = in.nextInt();
                switch (opcao){
                    case 1:
                    carregarcatalogo();
                    break;

                    case 2:
                    carrinho();
                    break;

                    case 3: 
                    finalizarcompra();
                    break;

                   default:
                   System.out.println("Opção invalida");


                }

0

The cart method expects an argument, an integer vector.

 switch (opcao){
                case 1:
                carregarcatalogo();
                break;

                case 2:
                carrinho(codigo);    
                case 3: 
                finalizarcompra();
            }

Remembering that the vector must be initialized before if not you will have Nullpointerexception problem inside the cart method().

Browser other questions tagged

You are not signed in. Login or sign up in order to post.