Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Desafio POO 2/5 - Registro de Transações Bancárias com Stream API #65

Open
Anselmo-S-Oliveira opened this issue Jul 12, 2024 · 0 comments

Comments

@Anselmo-S-Oliveira
Copy link

Meu código para o desafio 2/5 - Registro de Transações Bancárias com STREAM API está produzindo a saída esperada mas está dando erro, não sei o que está faltando...

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;

public class RegistroTransacoesComStream {

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);

    double saldo = scanner.nextDouble();
    int quantidadeTransacoes = scanner.nextInt();

    List<Transacao> transacoes = new ArrayList<>();

    for (int i = 0; i < quantidadeTransacoes; i++) {
        char tipoTransacao = scanner.next().charAt(0);
        double valorTransacao = scanner.nextDouble();

        // Create a new transaction and add it to the list
        Transacao transacao = new Transacao(tipoTransacao, valorTransacao);
        transacoes.add(transacao);

        // Update the balance based on the transaction type
        if (Character.toUpperCase(transacao.getTipo()) == 'D') {
            saldo += valorTransacao; // Deposit
        } else if (Character.toUpperCase(transacao.getTipo()) == 'S') {
            saldo -= valorTransacao; // Withdrawal
        }
    }

    // Display the final balance
    System.out.println("Saldo: " + saldo);
    System.out.println("Transacoes:");
    
    // Display all transactions
    transacoes.stream()
            .map(transacao -> transacao.getTipo() + " de " + transacao.getValor())
            .collect(Collectors.toList())
            .forEach(System.out::println);

    // Close the scanner to prevent resource leaks
    scanner.close();
}

}

// Transaction class to hold transaction details
class Transacao {
private char tipo; // 'D' for deposit, 'S' for withdrawal
private double valor;

public Transacao(char tipo, double valor) {
    this.tipo = tipo;
    this.valor = valor;
}

public char getTipo() {
    return tipo;
}

public double getValor() {
    return valor;
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant