From 3a67dd5e6aa1d58acf977f3f90be09bfabfab9bc Mon Sep 17 00:00:00 2001 From: Gabriel Franco Date: Sat, 15 Aug 2026 12:35:05 -0300 Subject: [PATCH] exercicio contacorrente --- contacorrente/.tool-versions | 1 + contacorrente/README.md | 32 ++++ contacorrente/pom.xml | 58 +++++++ contacorrente/run.sh | 1 + .../src/main/java/com/gabeefranco/App.java | 24 +++ .../gabeefranco/ContaCorrenteComExtrato.java | 145 ++++++++++++++++++ .../main/java/com/gabeefranco/Operacao.java | 28 ++++ .../test/java/com/gabeefranco/AppTest.java | 13 ++ 8 files changed, 302 insertions(+) create mode 100644 contacorrente/.tool-versions create mode 100644 contacorrente/README.md create mode 100644 contacorrente/pom.xml create mode 100755 contacorrente/run.sh create mode 100644 contacorrente/src/main/java/com/gabeefranco/App.java create mode 100644 contacorrente/src/main/java/com/gabeefranco/ContaCorrenteComExtrato.java create mode 100644 contacorrente/src/main/java/com/gabeefranco/Operacao.java create mode 100644 contacorrente/src/test/java/com/gabeefranco/AppTest.java diff --git a/contacorrente/.tool-versions b/contacorrente/.tool-versions new file mode 100644 index 0000000..054715b --- /dev/null +++ b/contacorrente/.tool-versions @@ -0,0 +1 @@ +java openjdk-25 diff --git a/contacorrente/README.md b/contacorrente/README.md new file mode 100644 index 0000000..bad9d07 --- /dev/null +++ b/contacorrente/README.md @@ -0,0 +1,32 @@ +# contacorrente + +Projeto Maven criado automaticamente com Java 25 e JUnit 5. + +## Requisitos + +- Java 25 +- Maven + +## Como compilar + +```bash +mvn compile +``` + +## Como executar + +```bash +mvn exec:java -Dexec.mainClass="com.gabeefranco.App" +``` + +## Como executar os testes + +```bash +mvn test +``` + +## Como gerar o pacote + +```bash +mvn package +``` diff --git a/contacorrente/pom.xml b/contacorrente/pom.xml new file mode 100644 index 0000000..c196935 --- /dev/null +++ b/contacorrente/pom.xml @@ -0,0 +1,58 @@ + + + 4.0.0 + + com.gabeefranco + contacorrente + 1.0-SNAPSHOT + + + 25 + UTF-8 + + + + + + org.junit.jupiter + junit-jupiter + 5.10.0 + test + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.11.0 + + 25 + + + + + + org.apache.maven.plugins + maven-surefire-plugin + 3.1.2 + + false + + + + + + org.codehaus.mojo + exec-maven-plugin + 3.1.0 + + + + + diff --git a/contacorrente/run.sh b/contacorrente/run.sh new file mode 100755 index 0000000..197c1d8 --- /dev/null +++ b/contacorrente/run.sh @@ -0,0 +1 @@ +mvn exec:java -Dexec.mainClass="com.gabeefranco.App" diff --git a/contacorrente/src/main/java/com/gabeefranco/App.java b/contacorrente/src/main/java/com/gabeefranco/App.java new file mode 100644 index 0000000..74e4efb --- /dev/null +++ b/contacorrente/src/main/java/com/gabeefranco/App.java @@ -0,0 +1,24 @@ +package com.gabeefranco; + +import java.time.LocalDate; + +public class App { + + public static void main(String[] args) { + ContaCorrenteComExtrato conta = new ContaCorrenteComExtrato(99, "Gabriel Franco"); + System.out.println(conta.toString()); + conta.deposita(20.99); + System.out.println("DEPOSITADOS R$ 20.99"); + System.out.println(conta.toString()); + conta.saca(10.50); + System.out.println("SACADOS R$ 10.50"); + System.out.println(conta.toString()); + + // mostrar extrato do mês e ano atual + LocalDate hoje = LocalDate.now(); + String extrato = conta.extratoMesEAnoFormatado(hoje.getYear(), hoje.getMonthValue()); + System.out.println("MOSTRANDO EXTRATO FORMATADO"); + System.out.println(extrato); + + } +} diff --git a/contacorrente/src/main/java/com/gabeefranco/ContaCorrenteComExtrato.java b/contacorrente/src/main/java/com/gabeefranco/ContaCorrenteComExtrato.java new file mode 100644 index 0000000..8058657 --- /dev/null +++ b/contacorrente/src/main/java/com/gabeefranco/ContaCorrenteComExtrato.java @@ -0,0 +1,145 @@ +package com.gabeefranco; + +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; + +public class ContaCorrenteComExtrato { + private int numero; + private String correntista; + private double saldo; + private boolean ativa; + private Operacao[] extrato; + private int extratoIdx; + + public ContaCorrenteComExtrato(int numero, String correntista) { + if (numero < 0) { + throw new IllegalArgumentException("Número da conta não pode ser negativo."); + } + if (correntista == null || correntista.length() < 2) { + throw new IllegalArgumentException("Correntista não pode ser nulo e deve ter pelo menos dois caracteres."); + } + this.numero = numero; + this.correntista = correntista; + this.saldo = 0.0; + this.ativa = true; + this.extrato = new Operacao[1024]; + this.extratoIdx = 0; + } + + public int getNumero() { + return numero; + } + + public String getCorrentista() { + return correntista; + } + + public double getSaldo() { + return saldo; + } + + public boolean isAtiva() { + return ativa; + } + + public void deposita(double valor) { + if (!ativa) { + throw new IllegalStateException("Operação não permitida: conta inativa."); + } + if (valor <= 0) { + throw new IllegalArgumentException("Valor do depósito deve ser positivo."); + } + extrato[extratoIdx] = new Operacao(LocalDate.now(), "DEPOSITO", valor); + extratoIdx++; + saldo += valor; + } + + public void saca(double valor) { + if (!ativa) { + throw new IllegalStateException("Operação não permitida: conta inativa."); + } + if (valor <= 0) { + throw new IllegalArgumentException("Valor do saque deve ser positivo."); + } + if (valor >= saldo) { + throw new IllegalArgumentException("Saldo insuficiente."); + } + extrato[extratoIdx] = new Operacao(LocalDate.now(), "RETIRADA", valor); + extratoIdx++; + saldo -= valor; + } + + public void transfere(double valor, ContaCorrenteComExtrato contaDestino) { + if (!ativa) { + throw new IllegalStateException("Operação não permitida: conta inativa."); + } + if (valor <= 0) { + throw new IllegalArgumentException("Valor da transferência deve ser positivo."); + } + if (valor > saldo) { + throw new IllegalArgumentException("Saldo insuficiente."); + } + if (contaDestino == null) { + throw new NullPointerException("A conta desctino não pode ser nula"); + } + if (!contaDestino.isAtiva()) { + throw new IllegalStateException("Conta destino inativa"); + } + extrato[extratoIdx] = new Operacao(LocalDate.now(), "RETIRADA", valor); + extratoIdx++; + // Faz a transferencia + saldo -= valor; + contaDestino.deposita(valor); + } + + public void fecha() { + if (saldo > 0.0) { + throw new IllegalArgumentException("A conta ainda tem saldo e não pode ser fechada"); + } + ativa = false; + } + + public Operacao[] getExtrato() { + return extrato; + } + + public Operacao[] extratoMesEAno(int ano, int mes) { + Operacao[] extratoFiltrado = new Operacao[extrato.length]; + int extratoFiltradoIdx = 0; + for (int i = 0; i < extrato.length; i++) { + Operacao op = extrato[i]; + if (op != null && op.getData().getYear() == ano && op.getData().getMonthValue() == mes) { + extratoFiltrado[extratoFiltradoIdx] = op; + extratoFiltradoIdx++; + } + } + + return extratoFiltrado; + + } + + public String extratoMesEAnoFormatado(int ano, int mes) { + String resultado = "EXTRATO: MÊS " + mes + " DO ANO " + ano + ":\n"; + for (int i = 0; i < extrato.length; i++) { + Operacao op = extrato[i]; + if (op != null && op.getData().getYear() == ano && op.getData().getMonthValue() == mes) { + String dataFormatada = op.getData().format(DateTimeFormatter.ofPattern("dd/MM/yyyy")); + resultado += "OPERAÇÃO EM " + dataFormatada + ": TIPO=" + op.getTipo() + ", VALOR=" + op.getValor() + + "\n"; + + } + } + + return resultado; + } + + @Override + public String toString() { + return "ContaCorrente{" + + "numero=" + numero + + ", correntista='" + correntista + '\'' + + ", saldo=" + saldo + + ", ativa=" + ativa + + '}'; + } +} diff --git a/contacorrente/src/main/java/com/gabeefranco/Operacao.java b/contacorrente/src/main/java/com/gabeefranco/Operacao.java new file mode 100644 index 0000000..8901e53 --- /dev/null +++ b/contacorrente/src/main/java/com/gabeefranco/Operacao.java @@ -0,0 +1,28 @@ +package com.gabeefranco; + +import java.time.LocalDate; + +public class Operacao { + private LocalDate data; + private String tipo; + private double valor; + + public Operacao(LocalDate data, String tipo, double valor) { + this.data = data; + this.tipo = tipo; + this.valor = valor; + } + + public LocalDate getData() { + return data; + } + + public String getTipo() { + return tipo; + } + + public double getValor() { + return valor; + } + +} diff --git a/contacorrente/src/test/java/com/gabeefranco/AppTest.java b/contacorrente/src/test/java/com/gabeefranco/AppTest.java new file mode 100644 index 0000000..21ff66f --- /dev/null +++ b/contacorrente/src/test/java/com/gabeefranco/AppTest.java @@ -0,0 +1,13 @@ +package com.gabeefranco; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.junit.jupiter.api.Test; + +class AppTest { + + @Test + void testApp() { + assertTrue(true, "Teste básico de funcionamento."); + } +}