Advertisement
niske

Untitled

Oct 23rd, 2024
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Comparator;
  3.  
  4. import org.svetovid.io.SvetovidReader;
  5.  
  6. public class Filmovi {
  7. public static void main(String[] args) {
  8. String fajl=Svetovid.in.readLine("Unesite naziv fajla: ");
  9. Film[] niz = ucitajFilmove(fajl);
  10.  
  11. if (niz == null) {
  12. Svetovid.out.println("Dati fajl nije mogao da se procita! Proverite da li postoji.");
  13. return;
  14. }
  15.  
  16. stampajNiz(niz);
  17. sortirajNiz(niz);
  18.  
  19. Comparator c = new PoredjenjePoReditelju();
  20.  
  21. sort(listaFilmova, c);
  22.  
  23. for(Film i: listaFilmova) {
  24. System.out.println(i);
  25. }
  26. }
  27. public static Film[] ucitajFilmove(String fajl) {
  28. if(!Svetovid.testIn(fajl)) {
  29. return null;
  30. }
  31. SvetovidReader in=Svetovid.in(fajl);
  32. int br=in.readInt();
  33. Film[] rez=new Film[br];
  34. for (int i=0;i<br;i++) {
  35. int idf=in.readInt();
  36. String naziv=in.readLine();
  37. String reditelj=in.readLine();
  38.  
  39. Film film =new Film(idf,naziv,reditelj);
  40. rez[i]=film;
  41. }
  42. Svetovid.closeIn(fajl);
  43. return rez;
  44. }
  45. public static void stampajNiz( Film[] niz) {
  46. for (Film a : niz) {
  47. Svetovid.out.printf(" (%4d) %20s %n", a.idf, a.naziv, a.reditelj);
  48. }
  49. }
  50. private static void sortirajNiz(Film[] niz) {
  51. Arrays.sort(niz);
  52. }
  53.  
  54. static void sort(Film[] arr, Comparator<Film> c) {
  55. int n = arr.length;
  56. for(int i = 0; i < n-1; i++) {
  57. Film min = arr[i];
  58. int position = i;
  59.  
  60. for(int j = i+1; j < n; j++) {
  61. if(c.compare(arr[j], min) < 0) {
  62. min = arr[j];
  63. position = j;
  64. }
  65. }
  66. Film temp = arr[i];
  67. arr[i] = arr[position];
  68. arr[position] = temp;
  69. }
  70. }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement