Advertisement
Guest User

Untitled

a guest
Mar 21st, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1.  
  2. import java.util.ArrayDeque;
  3. import java.util.Deque;
  4. import java.util.Iterator;
  5.  
  6. /**
  7. * @author
  8. *
  9. */
  10. public class Pista{
  11. private int id;
  12. private int comprimento;
  13. private Deque<Carro> pista = new ArrayDeque<Carro>();
  14.  
  15. /**
  16. *
  17. * @param identificacao
  18. * @param comprimentoGaragem
  19. * @requires
  20. */
  21. public Pista (int identificacao, int comprimentoGaragem) {
  22. this.id = identificacao;
  23. this.comprimento = comprimentoGaragem;
  24. // pista = new ArrayDeque<Carro>();
  25.  
  26. }
  27.  
  28.  
  29. /**
  30. *
  31. * @return
  32. */
  33. public int obtemComprimento () {
  34. return comprimento;
  35. }
  36.  
  37. /**
  38. *
  39. * @return
  40. */
  41. public int obtemComprimentoOcupado () {
  42. int resultado = 0;
  43. // int i = 0;
  44. // while(i < pista.size()) {
  45. // resultado += pista.poll().obtemComprimento();
  46. // i++;
  47. // }
  48. Carro auxCar;
  49. for(Iterator<Carro> itr = pista.iterator(); itr.hasNext();) {
  50. auxCar = itr.next();
  51. resultado += auxCar.obtemComprimento();
  52. }
  53. return resultado;
  54. }
  55.  
  56.  
  57. /**
  58. *
  59. * @return
  60. */
  61. public int obtemIdentificacao() {
  62. return id;
  63. }
  64.  
  65.  
  66. /**
  67. *
  68. * @return
  69. */
  70. public boolean estaVazia() {
  71. return pista.size() == 0 ? true : false;
  72. }
  73.  
  74. /**
  75. *
  76. * @return
  77. * @requires
  78. */
  79. public Carro carroNoTopo() {
  80. return pista.peek();
  81. }
  82.  
  83.  
  84. /**
  85. *
  86. * @return
  87. * @requires
  88. */
  89. public Carro tiraCarro() {
  90. return pista.pollFirst();
  91. }
  92.  
  93.  
  94. /**
  95. *
  96. * @param carro
  97. * @return
  98. * @requires
  99. */
  100. public boolean cabeNaPista(Carro carro) {
  101. // int tamanho = 0;
  102. // int i = 0;
  103. // while(i < pista.size()) {
  104. // tamanho += pista.poll().obtemComprimento();
  105. // }
  106. // if(carro.obtemComprimento() <= (comprimento-tamanho)) {
  107. // return true;
  108. // }
  109. // return false;
  110. return (carro.obtemComprimento() + obtemComprimentoOcupado() <= comprimento) ? true : false;
  111. }
  112.  
  113. /**
  114. *
  115. * @param carro
  116. * @requires
  117. */
  118. public boolean estacionaCarro(Carro carro) {
  119. if(cabeNaPista(carro)) {
  120. pista.push(carro);
  121. return true;
  122. }
  123. return false;
  124. }
  125.  
  126.  
  127. /**
  128. * Representacao da pista
  129. */
  130. public String toString () {
  131. String resultado = "";
  132. Carro auxCar;
  133. for(Iterator<Carro> itr = pista.iterator(); itr.hasNext();) {
  134. auxCar = itr.next();
  135. resultado = auxCar.toString();
  136. }
  137. StringBuilder sb = new StringBuilder();
  138. sb.append("Pista " + id + ":" + "\n " + comprimento + " cm de espaco livre." + "\n " + '[' + resultado + ']');
  139. return sb.toString();
  140. }
  141.  
  142.  
  143.  
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement