Advertisement
Guest User

Untitled

a guest
Sep 14th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. package regata;
  2.  
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.io.PrintWriter;
  6. import java.util.ArrayList;
  7. import java.util.Comparator;
  8. import java.util.InputMismatchException;
  9. import java.util.Iterator;
  10. import java.util.List;
  11. import java.util.Map;
  12. import java.util.NoSuchElementException;
  13. import java.util.Scanner;
  14. import java.util.Set;
  15. import java.util.TreeMap;
  16. import java.util.TreeSet;
  17.  
  18. public class Regata {
  19. private Set<Barco> participantes;
  20.  
  21. public Regata() {
  22. participantes = new TreeSet<>();
  23. }
  24.  
  25. public void agrega(Barco b) {
  26. participantes.add(b);
  27. }
  28.  
  29. public boolean velocidadSuperiorA(int velocidad) {
  30. Iterator< Barco> it = participantes.iterator();
  31. while (it.hasNext() && it.next().getVelocidad() < velocidad) {
  32. }
  33. return it.hasNext();
  34. }
  35.  
  36. public List<Barco> dentroDelCirculo(Posicion p, int km) {
  37. List<Barco> barcos = new ArrayList<>();
  38. for(Barco b : participantes) {
  39. if (b.getPosicion().distancia(p) < km) {
  40. barcos.add(b);
  41. }
  42. }
  43. return barcos;
  44. }
  45.  
  46. public Map<Integer, Set<Barco>> barcosPorVelocidad() {
  47. Map<Integer, Set<Barco>> map = new TreeMap<>();
  48. for(Barco b: participantes) {
  49. int v = b.getVelocidad()/10;
  50. Set<Barco> set = map.get(v);
  51. if (set == null) {
  52. set = new TreeSet<>();
  53. map.put(v, set);
  54. }
  55. set.add(b);
  56. }
  57. return map;
  58. }
  59.  
  60. public void avanza(int mnt) {
  61. for(Barco b : participantes) {
  62. b.avanza(mnt);
  63. }
  64. }
  65.  
  66. public Set<Barco> getParticipantes() {
  67. return participantes;
  68. }
  69.  
  70. public Set<Barco> ordenadosPorDistancia() {
  71. Set<Barco> set = new TreeSet<>(new SatBarco());
  72. set.addAll(participantes);
  73. return set;
  74. }
  75.  
  76. public Barco creaBarcoString(String dato) {
  77. try (Scanner sc = new Scanner(dato)) {
  78. sc.useDelimiter("[ ,]+");
  79. String nombre = sc.next();
  80. double lat = sc.nextDouble();
  81. double lon = sc.nextDouble();
  82. Posicion posicion = new Posicion(lat, lon);
  83. int rumbo = sc.nextInt();
  84. int velocidad = sc.nextInt();
  85. return new Barco(nombre, posicion, rumbo, velocidad);
  86. } catch (InputMismatchException e) {
  87. throw new RegataException("Algun dato numerico es erroneo en " + dato);
  88. } catch (NoSuchElementException e) {
  89. throw new RegataException("Faltan datos en " + dato);
  90. }
  91. }
  92.  
  93. public void leeFichero(String nFichero) throws FileNotFoundException {
  94. try (Scanner scanner = new Scanner(new File(nFichero))) {
  95. lee(scanner);
  96. }
  97. }
  98.  
  99. public void lee(Scanner sc) {
  100. while (sc.hasNextLine()) {
  101. agrega(creaBarcoString(sc.nextLine()));
  102. }
  103. }
  104.  
  105. public void escribeFichero(String nFichero) throws FileNotFoundException {
  106. try (PrintWriter printWriter = new PrintWriter(nFichero)) {
  107. escribe(printWriter);
  108. }
  109. }
  110.  
  111. public void escribe(PrintWriter pw) {
  112. for (Barco b: participantes) {
  113. pw.println(b);
  114. }
  115. }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement