exercicio contacorrente
This commit is contained in:
parent
1cd9c13791
commit
3a67dd5e6a
8 changed files with 302 additions and 0 deletions
1
contacorrente/.tool-versions
Normal file
1
contacorrente/.tool-versions
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
java openjdk-25
|
||||||
32
contacorrente/README.md
Normal file
32
contacorrente/README.md
Normal file
|
|
@ -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
|
||||||
|
```
|
||||||
58
contacorrente/pom.xml
Normal file
58
contacorrente/pom.xml
Normal file
|
|
@ -0,0 +1,58 @@
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
|
||||||
|
https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<groupId>com.gabeefranco</groupId>
|
||||||
|
<artifactId>contacorrente</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.release>25</maven.compiler.release>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<!-- JUnit Jupiter para testes unitários -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
<artifactId>junit-jupiter</artifactId>
|
||||||
|
<version>5.10.0</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<!-- Compilação com Java 25 -->
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<version>3.11.0</version>
|
||||||
|
<configuration>
|
||||||
|
<release>25</release>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
|
||||||
|
<!-- Execução dos testes JUnit -->
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
|
<version>3.1.2</version>
|
||||||
|
<configuration>
|
||||||
|
<useModulePath>false</useModulePath>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
|
||||||
|
<!-- Execução da classe principal -->
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.codehaus.mojo</groupId>
|
||||||
|
<artifactId>exec-maven-plugin</artifactId>
|
||||||
|
<version>3.1.0</version>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
</project>
|
||||||
1
contacorrente/run.sh
Executable file
1
contacorrente/run.sh
Executable file
|
|
@ -0,0 +1 @@
|
||||||
|
mvn exec:java -Dexec.mainClass="com.gabeefranco.App"
|
||||||
24
contacorrente/src/main/java/com/gabeefranco/App.java
Normal file
24
contacorrente/src/main/java/com/gabeefranco/App.java
Normal file
|
|
@ -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);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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 +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
||||||
28
contacorrente/src/main/java/com/gabeefranco/Operacao.java
Normal file
28
contacorrente/src/main/java/com/gabeefranco/Operacao.java
Normal file
|
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
13
contacorrente/src/test/java/com/gabeefranco/AppTest.java
Normal file
13
contacorrente/src/test/java/com/gabeefranco/AppTest.java
Normal file
|
|
@ -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.");
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue