Created
August 18, 2014 04:46
-
-
Save anonymous/36d03e763518bac7229d to your computer and use it in GitHub Desktop.
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.ArrayList; | |
import java.util.List; | |
public class Casa { | |
String cor; | |
List<Porta> portas; | |
//Variável para controlar a quantidade máxima de portas desejada | |
int numMaxDePortas; | |
public Casa(int numMaxDePortas) { | |
//Cria uma lista de Porta | |
this.portas = new ArrayList<Porta>(); | |
//Armazena o número máximo permitido | |
this.numMaxDePortas = numMaxDePortas; | |
} | |
public void setCor(String s) { | |
cor = s; | |
} | |
public int quantasPortasEstaoAbertas() { | |
int contador = 0; | |
for (Porta porta: portas) { | |
if (porta.estaAberta()) { | |
contador++; | |
} | |
} | |
return contador; | |
} | |
public boolean adicionaPorta(Porta p) { | |
if(portas.size()<numMaxDePortas){ | |
portas.add(p); | |
return true; | |
} | |
return false; | |
} | |
public int getTotalDePortas() { | |
return portas.size(); | |
} | |
} |
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 Porta { | |
private boolean aberta; | |
private String cor; | |
public void abre() { | |
// Não é necessário verificar se estava aberta já que nã há retorno ao usuário | |
aberta = true; | |
} | |
public void fecha() { | |
aberta = false; | |
} | |
void setCor(String cor) { | |
this.cor = cor; | |
} | |
public boolean estaAberta() { | |
return aberta; | |
} | |
public void mostra() { | |
System.out.println("A porta está aberta? " + this.estaAberta()); | |
System.out.println("A cor da porta é: " + this.cor); | |
} | |
} |
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 TestaCasa { | |
public static void main(String[] args) { | |
Casa house = new Casa(30); | |
house.cor = "Verde"; | |
Porta p1 = new Porta(); | |
Porta p2 = new Porta(); | |
Porta p3 = new Porta(); | |
house.adicionaPorta(p1); | |
house.adicionaPorta(p2); | |
house.adicionaPorta(p3); | |
p1.abre(); | |
p2.fecha(); | |
p3.abre(); | |
p2.abre(); | |
System.out.println("Número de portas abertas =\t" + house.quantasPortasEstaoAbertas()); | |
System.out.println("Número total de portas =\t" + house.getTotalDePortas()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment