Preço
This commit is contained in:
parent
09a949216e
commit
eb3f317085
3 changed files with 84 additions and 0 deletions
|
@ -0,0 +1,20 @@
|
|||
package casa.sotu.organizajogos.api.excecao;
|
||||
|
||||
public class OrganizaJogosExcecao extends RuntimeException {
|
||||
|
||||
public OrganizaJogosExcecao(String mensagem, Throwable causa) {
|
||||
super(mensagem, causa);
|
||||
}
|
||||
|
||||
public OrganizaJogosExcecao(Throwable causa) {
|
||||
super(causa);
|
||||
}
|
||||
|
||||
public OrganizaJogosExcecao(String mensagem) {
|
||||
super(mensagem);
|
||||
}
|
||||
|
||||
public OrganizaJogosExcecao() {
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package casa.sotu.organizajogos.api.excecao;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
import casa.sotu.organizajogos.api.modelo.Preco.TipoDePreco;
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
public class PrecoInvalidoExcecao extends OrganizaJogosExcecao {
|
||||
|
||||
private final BigDecimal valor;
|
||||
private final TipoDePreco tipo;
|
||||
|
||||
public PrecoInvalidoExcecao(BigDecimal valor, TipoDePreco tipo) {
|
||||
this.tipo = tipo;
|
||||
this.valor = valor;
|
||||
}
|
||||
}
|
46
src/main/java/casa/sotu/organizajogos/api/modelo/Preco.java
Normal file
46
src/main/java/casa/sotu/organizajogos/api/modelo/Preco.java
Normal file
|
@ -0,0 +1,46 @@
|
|||
package casa.sotu.organizajogos.api.modelo;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
import casa.sotu.organizajogos.api.excecao.PrecoInvalidoExcecao;
|
||||
import jakarta.persistence.Embeddable;
|
||||
import jakarta.persistence.EnumType;
|
||||
import jakarta.persistence.Enumerated;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import jakarta.validation.constraints.Min;
|
||||
|
||||
@Embeddable
|
||||
public record Preco(
|
||||
@Min(0) BigDecimal valor,
|
||||
@NotNull @Enumerated(EnumType.STRING) TipoDePreco tipo) {
|
||||
|
||||
public Preco {
|
||||
if (tipo == TipoDePreco.VALOR) {
|
||||
if (valor == null) {
|
||||
throw new PrecoInvalidoExcecao(valor, tipo);
|
||||
}
|
||||
} else {
|
||||
valor = null;
|
||||
}
|
||||
}
|
||||
|
||||
public Preco(BigDecimal valor) {
|
||||
this(valor, TipoDePreco.VALOR);
|
||||
}
|
||||
|
||||
public Preco(TipoDePreco tipo) {
|
||||
this(null, tipo);
|
||||
}
|
||||
|
||||
public enum TipoDePreco {
|
||||
VALOR,
|
||||
DE_GRACA,
|
||||
DESCONHECIDO,
|
||||
PRESENTE;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Preco preco = new Preco(BigDecimal.valueOf(10000, 2), TipoDePreco.DESCONHECIDO);
|
||||
System.out.println(preco);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue