Guest User

Untitled

a guest
Nov 13th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.80 KB | None | 0 0
  1. import java.util.*;
  2. import java.lang.Thread;
  3. import java.util.concurrent.TimeUnit;
  4. import java.util.concurrent.locks.Condition;
  5.  
  6. public class PonteSemSinal {
  7.  
  8.  
  9. private Car carro;
  10. private Caminhao truck;
  11. private Bridge ponte;
  12. private Random opt = new Random(); //variável que sorteia a direção a ser criada para carros. Ao gerar 0=left, 1=right;
  13. private Random opt2 = new Random();
  14.  
  15. private List<Car> carrosEsquerda = new ArrayList<>();
  16. private List<Car> carrosDireita = new ArrayList<>();
  17. private List<Caminhao> caminhoesEsquerda = new ArrayList<>();
  18. private List<Caminhao> caminhoesDireita = new ArrayList<>();
  19.  
  20. private int direction;
  21. private int carsDir, carsEsq, camDir, camEsq;
  22. private int turn;
  23.  
  24. private boolean waiting = false;
  25.  
  26.  
  27. public PonteSemSinal () {
  28.  
  29. ponte = new Bridge();
  30. this.carsDir = 50;
  31. this.carsEsq = 50;
  32. this.camDir = 3;
  33. this.camEsq = 3;
  34.  
  35. }
  36.  
  37.  
  38. public void getSituation (int s) {}
  39.  
  40.  
  41. public synchronized void await() throws InterruptedException {
  42.  
  43. while(!waiting) wait();
  44.  
  45. try {
  46.  
  47. Random choose = new Random();
  48. Random dir = new Random();
  49.  
  50. turn = choose.nextInt(2); // Criar carro ou caminhão aleatoriamente.
  51.  
  52. if ((turn == 0) && ((camDir != 0) || (camEsq != 0))) {
  53.  
  54. if ((camDir == 0) && (camEsq != 0)) {
  55. direction = 0;
  56. truck = new Caminhao(ponte, 0);
  57. truck.setOp(3);
  58. truck.setTime();
  59. System.out.println(truck.toString());
  60. caminhoesEsquerda.add(truck);
  61.  
  62.  
  63. }
  64.  
  65.  
  66. if ((camEsq == 0) && (camDir != 0)) {
  67.  
  68. direction = 1;
  69. truck = new Caminhao(ponte, 1);
  70. truck.setOp(3);
  71. truck.setTime();
  72. System.out.println(truck.toString());
  73. caminhoesDireita.add(truck);
  74.  
  75.  
  76. } else {
  77.  
  78. direction = dir.nextInt(2);
  79. truck = new Caminhao(ponte, direction);
  80. truck.setOp(3);
  81. truck.setTime();
  82. System.out.println(truck.toString());
  83. if (direction == 1) {
  84. caminhoesDireita.add(truck);
  85.  
  86. } else {
  87. caminhoesEsquerda.add(truck);
  88.  
  89. }
  90. }
  91.  
  92.  
  93. }
  94.  
  95. if ((turn == 1) && ((carsDir != 0) || (carsEsq != 0))) {
  96.  
  97. if ((carsDir == 0) && (carsEsq != 0)) {
  98. direction = 0;
  99. carro = new Car(ponte, 0);
  100. carro.setOp(3);
  101. carro.setTime();
  102. System.out.println(carro.toString());
  103. carrosEsquerda.add(carro);
  104.  
  105.  
  106. }
  107.  
  108.  
  109. if ((carsEsq == 0) && (carsDir != 0)) {
  110. direction = 1;
  111. carro = new Car(ponte, 0);
  112. carro.setOp(3);
  113. carro.setTime();
  114. System.out.println(carro.toString());
  115. carrosDireita.add(carro);
  116.  
  117.  
  118. } else {
  119.  
  120. direction = dir.nextInt(2);
  121. carro = new Car(ponte, direction);
  122. carro.setOp(3);
  123. carro.setTime();
  124. System.out.println(carro.toString());
  125. if (direction == 1) {
  126. carrosDireita.add(carro);
  127.  
  128. } else {
  129. carrosEsquerda.add(carro);
  130.  
  131. }
  132. }
  133.  
  134.  
  135. }
  136.  
  137.  
  138. }catch (Exception e) {
  139. e.printStackTrace();
  140. }
  141. }
  142.  
  143.  
  144.  
  145. public void signal() {
  146.  
  147. try {
  148. if ((turn == 0) && (direction == 0)) {
  149. long ta = caminhoesEsquerda.get(3 - camEsq).getTime();
  150. TimeUnit.SECONDS.sleep(ta);
  151. ponte.atravessaCam(caminhoesEsquerda.get(3 - camEsq), 3);
  152. camEsq--;
  153.  
  154.  
  155.  
  156. }
  157. if ((turn == 0) && (direction == 1)) {
  158. long ta = caminhoesDireita.get(3 - camDir).getTime();
  159. TimeUnit.SECONDS.sleep(ta);
  160. ponte.atravessaCam(caminhoesDireita.get(3 - camDir), 3);
  161. camDir--;
  162. }
  163.  
  164. if ((turn == 1) && (direction == 0)) {
  165. TimeUnit.SECONDS.sleep(2);
  166. long ta = carrosEsquerda.get(50 - carsEsq).getTime();
  167. TimeUnit.SECONDS.sleep(ta);
  168. ponte.atravessaCar(carrosEsquerda.get(50 - carsEsq), 3);
  169. carsEsq--;
  170.  
  171. }
  172.  
  173. if ((turn == 1) && (direction == 1)) {
  174. TimeUnit.SECONDS.sleep(2);
  175. long ta = carrosEsquerda.get(50 - carsDir).getTime();
  176. TimeUnit.SECONDS.sleep(ta);
  177. ponte.atravessaCar(carrosEsquerda.get(50 - carsDir), 3);
  178. carsDir--;
  179. }
  180.  
  181.  
  182. } catch (Exception e) {
  183. e.printStackTrace();
  184. }
  185.  
  186. waiting=true;
  187. notifyAll();
  188.  
  189.  
  190. }
  191.  
  192.  
  193.  
  194. }
Add Comment
Please, Sign In to add comment