Changing the value of an arraylist

Asked

Viewed 27 times

1

I have a problem in CASE 4 of my code, I want to change the price of the movie by calling the set, But it doesn’t change. I think it’s a logic problem, I’m a beginner.

public static void main(String[] args) {
    Locale.setDefault(Locale.US);
    Scanner dados = new Scanner(System.in);

    List<Filme> filme = new ArrayList<>();
    Filme filmeC = new Filme();

    System.out.println("1 - Cadastrar filme \n2 - Excluir por ID \n3 - Checar filmes cadastrados");
    int numeroOpcoes = dados.nextInt();

    switch (numeroOpcoes) {
        case 1:
            for (int i = 0; i < 2; i++) {
                System.out.println("Digite ID");
                int id = dados.nextInt();

                System.out.println("Digite Nome");
                dados.nextLine();
                String nome = dados.nextLine();

                System.out.println("Genero");
                String genero = dados.nextLine();

                System.out.println("Quantidade");
                int quantidade = dados.nextInt();

                System.out.println("Preço");
                double preco = dados.nextDouble();

                filme.add(new Filme(id, nome, genero, quantidade, preco));
            }
        //break;
        case 2:
            System.out.println("entre com o ID para ser excluido");
            int idExcluir = dados.nextInt();
            Filme emp = filme.stream().filter(x -> x.getId() == idExcluir).findFirst().orElse(null);
            if (emp == null) {
                System.out.println("ID não existe");
            } else {
                filme.remove(emp);
                System.out.println("Removido com sucesso");
            }
        // break;
        case 3:
            for (Filme x : filme) {
                System.out.println(x);
            }
        // break;
        case 4:
            System.out.println("Digite o ID para modificar o preço");
            int idPreco = dados.nextInt();
            Filme valor = filme.stream().filter(x -> x.getId() == idPreco).findFirst().orElse(null);
            if (valor == null) {
                System.out.println("ID não existe");
            } else {
                System.out.println("Digite novo valor");
                double novoValor = dados.nextDouble(); // NÃO ENTENDO PQ NAO ALTERA AQUI
                filmeC.setPreco(novoValor);
                System.out.println("Valor alterado");
            }
            for (Filme x : filme) {
                System.out.println(x);
            }
            break;
    }

}

}
  • The first three lines of your code ended up missing from the question. Please change it so that the lines appear within the code structure.

  • From what I could notice, at the time of changing the price of the movie you used the 'filmeC' object, but the object that references the movie whose ID is equal to that reported by the user is the variable 'value'.

  • I cannot access the set for the movie. Then I created the filmC, but it does not change the list of the movie.

  • I ran your code here, and made this change (I replaced 'filmeC' with 'value') and managed to change the value of the Movie. Try to make this change to check if the code runs. I suggest that in that repetition that show the films, instead of just doing "System.out.println(x)", do "System.out.println(x.getPreco()" (getter of the price variable) to check only the prices of the registered films.

1 answer

0

Your error is because, when it comes to changing the price, you are using the 'filmeC' object, while the object that references the film class object whose ID is equal to that given by the user is 'value'. To make the code work properly, replace the line

filmeC.setPreco(novoValor);

for

 valor.setPreco(novoValor);
  • Thank you very much, solved my problem.

Browser other questions tagged

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