Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayDeque;
- import java.util.Deque;
- import java.util.Iterator;
- /**
- * @author
- *
- */
- public class Pista{
- private int id;
- private int comprimento;
- private Deque<Carro> pista = new ArrayDeque<Carro>();
- /**
- *
- * @param identificacao
- * @param comprimentoGaragem
- * @requires
- */
- public Pista (int identificacao, int comprimentoGaragem) {
- this.id = identificacao;
- this.comprimento = comprimentoGaragem;
- // pista = new ArrayDeque<Carro>();
- }
- /**
- *
- * @return
- */
- public int obtemComprimento () {
- return comprimento;
- }
- /**
- *
- * @return
- */
- public int obtemComprimentoOcupado () {
- int resultado = 0;
- // int i = 0;
- // while(i < pista.size()) {
- // resultado += pista.poll().obtemComprimento();
- // i++;
- // }
- Carro auxCar;
- for(Iterator<Carro> itr = pista.iterator(); itr.hasNext();) {
- auxCar = itr.next();
- resultado += auxCar.obtemComprimento();
- }
- return resultado;
- }
- /**
- *
- * @return
- */
- public int obtemIdentificacao() {
- return id;
- }
- /**
- *
- * @return
- */
- public boolean estaVazia() {
- return pista.size() == 0 ? true : false;
- }
- /**
- *
- * @return
- * @requires
- */
- public Carro carroNoTopo() {
- return pista.peek();
- }
- /**
- *
- * @return
- * @requires
- */
- public Carro tiraCarro() {
- return pista.pollFirst();
- }
- /**
- *
- * @param carro
- * @return
- * @requires
- */
- public boolean cabeNaPista(Carro carro) {
- // int tamanho = 0;
- // int i = 0;
- // while(i < pista.size()) {
- // tamanho += pista.poll().obtemComprimento();
- // }
- // if(carro.obtemComprimento() <= (comprimento-tamanho)) {
- // return true;
- // }
- // return false;
- return (carro.obtemComprimento() + obtemComprimentoOcupado() <= comprimento) ? true : false;
- }
- /**
- *
- * @param carro
- * @requires
- */
- public boolean estacionaCarro(Carro carro) {
- if(cabeNaPista(carro)) {
- pista.push(carro);
- return true;
- }
- return false;
- }
- /**
- * Representacao da pista
- */
- public String toString () {
- String resultado = "";
- Carro auxCar;
- for(Iterator<Carro> itr = pista.iterator(); itr.hasNext();) {
- auxCar = itr.next();
- resultado = auxCar.toString();
- }
- StringBuilder sb = new StringBuilder();
- sb.append("Pista " + id + ":" + "\n " + comprimento + " cm de espaco livre." + "\n " + '[' + resultado + ']');
- return sb.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement