Hakunin

OBP2_V2_Datoteke

Oct 31st, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.62 KB | None | 0 0
  1. //////////////////////////////////////// aplikacija.java /////////////////////////////////////////////////
  2.  
  3. package glavni;
  4.  
  5. import java.io.*;
  6. import java.nio.charset.Charset;
  7. import java.util.ArrayList;
  8. import java.util.Scanner;
  9.  
  10. /**
  11.  * Created by markort2815 on 31.10.17..
  12.  */
  13. public class aplikacija {
  14.     public static void main(String[] args) {
  15.  
  16.         Scanner ulaz=new Scanner(System.in);
  17.         ulaz.useDelimiter("\\n"); //oznacava kraj unosa podataka... umesto .getline() praznog;
  18. /*
  19.         System.out.print("Unesite ime: ");
  20.         String ime=ulaz.next();
  21.         System.out.print("Unesite godiste: ");
  22.         int godiste=ulaz.nextInt();
  23.         System.out.print("Unesite platu: ");
  24.         double plata=ulaz.nextDouble();
  25.         System.out.print(ime+" "+godiste+" "+plata);*/
  26.  
  27.         File f = new File("primer.txt"); // KREIRA SE U SOURCE FOLDERU!!
  28.  
  29.         if(!f.exists()){
  30.             try {
  31.                 f.createNewFile(); //U slucaju da se nije napravio!?!?
  32.                 System.out.println("Datoteka kreirana!");
  33.             } catch (IOException e) {
  34.                 e.printStackTrace();
  35.             }
  36.         }else {
  37.             System.out.println("Datoteka vec postoji");
  38.         }
  39.  
  40.         try {
  41.             FileWriter fw=new FileWriter("primer.txt",true);
  42.             PrintWriter pw = new PrintWriter(fw);
  43.  
  44.             pw.print("neki sadrzaj?"+System.getProperty("line.separator")+"Sadrzaj u novom redu");
  45.             pw.print(ulaz.next());
  46.             pw.close();
  47.             fw.close();
  48.         } catch (IOException e) {
  49.             e.printStackTrace();
  50.         }
  51.  
  52.         try {
  53.             FileInputStream fis=new FileInputStream("primer.txt");
  54.             InputStreamReader isr=new InputStreamReader(fis, Charset.forName("UTF-8"));
  55.             BufferedReader bf = new BufferedReader(isr);
  56.             ArrayList<String> sadrzaj = new ArrayList<>();
  57.             String linija = bf.readLine();
  58.  
  59.             while (linija!=null){
  60.                 sadrzaj.add(linija);
  61.                 linija=bf.readLine();
  62.             }
  63.  
  64.             for(String red : sadrzaj){
  65.                 System.out.println(red);
  66.             }
  67.  
  68.         } catch (FileNotFoundException e) {
  69.             e.printStackTrace();
  70.         } catch (IOException e){
  71.             e.printStackTrace();
  72.         }
  73.    
  74.             for(String ime:nizimena){
  75.             System.out.println(ime);
  76.         }
  77.  
  78.         for(int i=0;i<nizimena.length;i++){
  79.             for (int j=i+1;j<nizimena.length;j++){
  80.                         if(nizimena[i].compareTo(nizimena[j])>0){
  81.                             String tmp=nizimena[i];
  82.                             nizimena[i]=nizimena[j];
  83.                             nizimena[j]=tmp;
  84.                         }
  85.             }
  86.         }
  87.         System.out.println("\nPOSLE SORTIRANJA: \n");
  88.         for(String ime:nizimena){
  89.             System.out.println(ime);
  90.         }
  91.     }
  92. }
  93.  
  94. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  95.  
  96. package Kolektiv;
  97.  
  98. /**
  99.  * Created by markort2815 on 31.10.17..
  100.  */
  101. public abstract class Osoba {
  102.     protected String ime;
  103.     private String prezime,adresa;
  104.     private int godiste;
  105.  
  106.     public Osoba() {
  107.     }
  108.  
  109.     public Osoba(String ime, String prezime, String adresa, int godiste) {
  110.         this.ime = ime;
  111.         this.prezime = prezime;
  112.         this.adresa = adresa;
  113.         this.godiste = godiste;
  114.     }
  115.  
  116.     @Override
  117.     public String toString() {
  118.         return "Osoba{" +
  119.                 "ime='" + ime + '\'' +
  120.                 ", prezime='" + prezime + '\'' +
  121.                 ", adresa='" + adresa + '\'' +
  122.                 ", godiste=" + godiste +
  123.                 '}';
  124.     }
  125. }
  126.  
  127. //////////////////////////////////////
  128.  
  129. package Kolektiv;
  130.  
  131. import glavni.radSaDatotekama;
  132.  
  133. import java.io.*;
  134. import java.nio.charset.Charset;
  135. import java.util.ArrayList;
  136.  
  137. /**
  138.  * Created by markort2815 on 31.10.17..
  139.  */
  140. public class Zaposlen extends Osoba implements radSaDatotekama {
  141.     private static int id=0;
  142.     private int lid;
  143.     private String odeljenje;
  144.     private double plata;
  145.  
  146.     public Zaposlen() {
  147.         super("Ime", "Prezime", "Adresa", 1991);
  148.         lid=id;
  149.         id++;
  150.         odeljenje="Menadzment";
  151.         plata=0;
  152.     }
  153.  
  154.     public Zaposlen(String ime, String prezime, String adresa, int godiste, String odeljenje, double plata) {
  155.         super(ime, prezime, adresa, godiste);
  156.         lid=id;
  157.         id++;
  158.         this.odeljenje = odeljenje;
  159.         this.plata = plata;
  160.     }
  161.  
  162.  
  163.  
  164.     public static Zaposlen[] sortirajZaposlene(Zaposlen[] niz){
  165.         for(int i=0;i<niz.length;i++){
  166.             for (int j=i+1;j<niz.length;j++){
  167.                 if(niz[i].ime.compareTo(niz[j].ime)>0){ //compareTo vraca broj ako je  j veci!
  168.                     Zaposlen tmp=niz[i];
  169.                     niz[i]=niz[j];
  170.                     niz[j]=tmp;
  171.                 }
  172.             }
  173.         }
  174.  
  175.         return niz;
  176.     }
  177.  
  178.     public static void pronadjiZaposlenog(Zaposlen[] niz){
  179.         Zaposlen maxPlata=niz[0];
  180.  
  181.         for(Zaposlen z: niz){
  182.            if(z.plata>maxPlata.plata){
  183.                maxPlata=z;
  184.            }
  185.         }
  186.  
  187.         System.out.println(maxPlata);
  188.     }
  189.  
  190.  
  191.     @Override
  192.     public String toString() {
  193.         return super.toString() + "Zaposlen{" +
  194.                 "lid=" + lid +
  195.                 ", odeljenje='" + odeljenje + '\'' +
  196.                 ", plata=" + plata +
  197.                 '}';
  198.     }
  199.  
  200.     @Override
  201.     public void UpisiUDatoteku() {
  202.         try {
  203.             FileWriter fw=new FileWriter("primer.txt",true);
  204.             PrintWriter pw = new PrintWriter(fw);
  205.  
  206.             pw.print(this +System.getProperty("line.separator"));
  207.             pw.close();
  208.             fw.close();
  209.         } catch (IOException e) {
  210.             e.printStackTrace();
  211.         }
  212.     }
  213.  
  214.  
  215.     public static void ispisSadrzaja() {
  216.             try {
  217.                 FileInputStream fis=new FileInputStream("primer.txt");
  218.                 InputStreamReader isr=new InputStreamReader(fis, Charset.forName("UTF-8"));
  219.                 BufferedReader bf = new BufferedReader(isr);
  220.                 ArrayList<String> sadrzaj = new ArrayList<>();
  221.                 String linija = bf.readLine();
  222.  
  223.                 while (linija!=null){
  224.                     sadrzaj.add(linija);
  225.                     linija=bf.readLine();
  226.                 }
  227.  
  228.                 for(String red : sadrzaj){
  229.                     System.out.println(red);
  230.                 }
  231.  
  232.             } catch (FileNotFoundException e) {
  233.                 e.printStackTrace();
  234.             } catch (IOException e){
  235.                 e.printStackTrace();
  236.         }
  237.     }
  238.  
  239.  
  240. }
  241.  
  242. ///////////////////////////////////
  243.  
  244. package glavni;
  245.  
  246. /**
  247.  * Created by markort2815 on 31.10.17..
  248.  */
  249. public interface radSaDatotekama {
  250.     void UpisiUDatoteku();
  251.     static void ispisSadrzaja(){
  252.  
  253.     };
  254.  
  255.  
  256. }
  257.  
  258. //////////////////////////////////////
  259.  
  260. package glavni;
  261.  
  262. import Kolektiv.Zaposlen;
  263.  
  264. /**
  265.  * Created by markort2815 on 31.10.17..
  266.  */
  267. public class zadatakv2 {
  268.     public static void main(String[] args) {
  269.  
  270.         Zaposlen z1= new Zaposlen("marko","jovanovic","jg",1991,"prog",750);
  271.         Zaposlen[] nizZap = new Zaposlen[3];
  272.         nizZap[0]=z1;
  273.         nizZap[1]=new Zaposlen("marko1","jovanovic1","jg1",1991,"prog1",752);
  274.         nizZap[2]=new Zaposlen("marko1","jovanovic1","jg1",1991,"prog1",1752);
  275.  
  276.         Zaposlen.sortirajZaposlene(nizZap);
  277.         Zaposlen.pronadjiZaposlenog(nizZap);
  278.  
  279.         z1.UpisiUDatoteku();
  280.         nizZap[1].UpisiUDatoteku();
  281.         Zaposlen.ispisSadrzaja();
  282.     }
  283. }
Add Comment
Please, Sign In to add comment