Last active
May 14, 2016 22:55
-
-
Save alvarowolfx/4f00b974c4e9d1eb783cf57e01f8a696 to your computer and use it in GitHub Desktop.
Exercício Zoológico - Modelagem
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
package br.com.aviebrantz.zoo; | |
/** | |
* Created by alvaroviebrantz on 14/05/16. | |
*/ | |
public abstract class Animal{ | |
private Integer identificacao; | |
private String nome; | |
private Porte porte; | |
public Animal(Integer identificacao, String nome, Porte porte){ | |
this.identificacao = identificacao; | |
this.nome = nome; | |
this.porte = porte; | |
} | |
public Animal(Porte porte){ | |
this(0, "", porte); | |
} | |
public Porte getPorte() { | |
return porte; | |
} | |
public String getDescricao(){ | |
return this.getClass().getSimpleName() + " é de porte " + porte.name(); | |
} | |
public Integer getIdentificacao() { | |
return identificacao; | |
} | |
public void setIdentificacao(Integer identificacao) { | |
this.identificacao = identificacao; | |
} | |
public String getNome() { | |
return nome; | |
} | |
public void setNome(String nome) { | |
this.nome = nome; | |
} | |
} | |
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
package br.com.aviebrantz.zoo; | |
import java.util.ArrayList; | |
import java.util.List; | |
/** | |
* Created by alvaroviebrantz on 14/05/16. | |
*/ | |
public class Gaiola<T extends Animal> { | |
private List<T> animais; | |
private Porte porte; | |
public Gaiola(Porte porte){ | |
this.animais = new ArrayList<T>(); | |
this.porte = porte; | |
} | |
public void adiciona(T animal) throws PorteInvalidoException{ | |
if(animal.getPorte() == this.porte) { | |
this.animais.add(animal); | |
}else { | |
throw new PorteInvalidoException("Essa gaiola só aceita animais de porte " + this.porte.name() + " e " + animal.getDescricao() + " não tem os critérios corretos"); | |
} | |
} | |
public List<T> getAnimais() { | |
List<T> copia = new ArrayList<T>(); | |
copia.addAll(animais); | |
return copia; | |
} | |
} |
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
package br.com.aviebrantz.zoo; | |
/** | |
* Created by alvaroviebrantz on 14/05/16. | |
*/ | |
public class Gaviao extends Passaro { | |
public Gaviao(){ | |
super(Porte.GRANDE); | |
} | |
} |
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 Main { | |
public static void main(String[] args) { | |
Gaiola<Passaro> gaiola = new Gaiola<Passaro>(Porte.PEQUENO); | |
List<Passaro> passaros = new ArrayList<Passaro>(); | |
passaros.add(new Tucano()); | |
passaros.add(new Gaviao()); | |
passaros.add(new Gaviao()); | |
passaros.add(new Tucano()); | |
System.out.println("Adicionando passaros na gaiola"); | |
for(Passaro passaro: passaros) { | |
try { | |
System.out.println("Tentando adicionar o " + passaro.getDescricao()); | |
gaiola.adiciona(passaro); | |
System.out.println(passaro.getDescricao() + " adicionado"); | |
} catch (PorteInvalidoException e) { | |
System.out.println(e.getMessage()); | |
} | |
} | |
List<Animal> animais = new ArrayList<Animal>(); | |
animais.add(new Tucano()); | |
animais.add(new Gaviao()); | |
animais.add(new Urso()); | |
System.out.println(); | |
System.out.println("Adicionando animais genericos na gaiola"); | |
for(Animal animal: animais) { | |
try { | |
System.out.println("Tentando adicionar o " + animal.getDescricao()); | |
if(animal instanceof Passaro){ | |
gaiola.adiciona((Passaro) animal); | |
System.out.println(animal.getDescricao() + " adicionado"); | |
}else{ | |
System.out.println(animal.getDescricao() + " não é um passaro"); | |
} | |
} catch (PorteInvalidoException e) { | |
System.out.println(e.getMessage()); | |
} | |
} | |
System.out.println(); | |
System.out.println("Animais na gaiola"); | |
for(Animal animal: gaiola.getAnimais()){ | |
System.out.println(animal.getDescricao()); | |
} | |
} | |
} |
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
package br.com.aviebrantz.zoo; | |
/** | |
* Created by alvaroviebrantz on 14/05/16. | |
*/ | |
public abstract class Passaro extends Animal{ | |
public Passaro(Porte porte){ | |
super(porte); | |
} | |
} |
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
package br.com.aviebrantz.zoo; | |
public enum Porte { | |
PEQUENO, MEDIO, GRANDE | |
} |
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
package br.com.aviebrantz.zoo; | |
/** | |
* Created by alvaroviebrantz on 14/05/16. | |
*/ | |
public class PorteInvalidoException extends Throwable { | |
public PorteInvalidoException(String message) { | |
super(message); | |
} | |
} |
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
package br.com.aviebrantz.zoo; | |
/** | |
* Created by alvaroviebrantz on 14/05/16. | |
*/ | |
public class Tucano extends Passaro { | |
public Tucano(){ | |
super(Porte.PEQUENO); | |
} | |
} |
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
package br.com.aviebrantz.zoo; | |
/** | |
* Created by alvaroviebrantz on 14/05/16. | |
*/ | |
public class Urso extends Animal { | |
public Urso(){ | |
super(Porte.GRANDE); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment