Last active
March 23, 2017 00:04
-
-
Save jeffersontpadua/0a24cea3a0e9f741e5eaa52886e02dc7 to your computer and use it in GitHub Desktop.
Como utilizar exceptions em java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BungeeJumping { | |
public void executarSaltosDoDia(List<Pessoa> pessoas) { | |
for(Pessoa pessoa : pessoas) { | |
if (podeSaltar(pessoa)) { | |
System.out.println(pessoa.getGritoDeEmocao()); | |
} | |
} | |
} | |
private boolean podeSaltar(Pessoa pessoa) { | |
if(pessoa.getIdade() >= 60){ | |
throw new IllegalArgumentException("Pessoas com mais de 60 anos não devem saltar."); | |
}else { | |
if(pessoa.getIdade() >= 18) { | |
return true; | |
}else { | |
System.out.println("Menores de 18 anos não podem saltar."); | |
return false; | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Pessoa { | |
private final String nome; | |
private final String gritoDeEmocao; | |
private final int idade; | |
public Pessoa (String nome, int idade, String gritoDeEmocao) { | |
this.nome = nome; | |
this.idade = idade; | |
this.gritoDeEmocao = gritoDeEmocao; | |
} | |
public int getIdade() { | |
return idade; | |
} | |
public String getNome() { | |
return nome; | |
} | |
public String getGritoDeEmocao() { | |
return gritoDeEmocao; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
import java.util.List; | |
public class RunExceptions { | |
public static void main(String[] args) { | |
Pessoa jefferson = new Pessoa("Jefferson", 22, "AAAAAAAAAAAAAA"); | |
Pessoa joao = new Pessoa("João", 65, "UHUUUUUUUUUUUUU"); | |
Pessoa marcos = new Pessoa("Marcos", 35, "OWWWWWWWWW"); | |
BungeeJumping bungeeJumping = new BungeeJumping(); | |
bungeeJumping.executarSaltosDoDia(Arrays.asList(jefferson, joao, marcos)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment