Advertisement
Domy131097

LV4: Zadatak 2

Nov 23rd, 2017
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.29 KB | None | 0 0
  1. /**
  2.  * @author Dominik Tkalčec
  3.  */
  4.  
  5. package vjezba4;
  6.  
  7. import java.io.Serializable;
  8.  
  9. public class Student implements Serializable {
  10.     private int maticnibroj;
  11.     private String ime;
  12.     private String prezime;
  13.     private String fakultet;
  14.     private String smjer;
  15.     private int godina;
  16.     private double prosjekocjena;
  17.  
  18.     public Student() {
  19.         this(0, "", "", "", "", 0, 0.0);
  20.     }
  21.  
  22.     public Student(int mb, String ime, String prezime, String fax, String smjer, int god, double po) {
  23.         this.maticnibroj = mb;
  24.         this.ime = ime;
  25.         this.prezime = prezime;
  26.         this.fakultet = fax;
  27.         this.smjer = smjer;
  28.         this.godina = god;
  29.         this.prosjekocjena = po;
  30.     }
  31.  
  32.     public int ucitajMaticniBroj() {
  33.         return maticnibroj;
  34.     }
  35.     public String ucitajIme() {
  36.         return ime;
  37.     }
  38.     public String ucitajPrezime() {
  39.         return prezime;
  40.     }
  41.     public String ucitajFaks() {
  42.         return fakultet;
  43.     }
  44.     public String ucitajSmjer() {
  45.         return smjer;
  46.     }
  47.     public int ucitajGodinu() {
  48.         return godina;
  49.     }
  50.     public double ucitajProsjek() {
  51.         return prosjekocjena;
  52.     }
  53. }
  54.  
  55. /**
  56.  * @author Dominik Tkalčec
  57.  */
  58.  
  59. package vjezba4;
  60.  
  61. import java.io.IOException;
  62. import java.nio.file.Files;
  63. import java.nio.file.Paths;
  64. import java.util.NoSuchElementException;
  65. import java.util.Scanner;
  66. import java.io.ObjectOutputStream;
  67.  
  68. public class SpremiUDatoteku {
  69.     private static ObjectOutputStream izlaz;
  70.     public static void main(String[] args) {
  71.         otvoriDatoteku();
  72.         snimiStudente();
  73.         zatvoriDatoteku();
  74.     }
  75.  
  76.     public static void otvoriDatoteku() {
  77.         try {
  78.             izlaz = new ObjectOutputStream(Files.newOutputStream(Paths.get("studenti.ser")));
  79.         }
  80.         catch (IOException IOgreska) {
  81.             System.err.println("Pogreska otvaranja datoteke.");
  82.             System.exit(1);
  83.         }
  84.     }
  85.  
  86.     public static void snimiStudente() {
  87.         Scanner input = new Scanner(System.in);
  88.         int count = 0;
  89.         while (count < 2) {
  90.             try {
  91.                 System.out.println("Unesite maticni broj, ime, prezime, fakultet, smjer, godinu i prosjek ocjena " + (count + 1) + ". studenta:");
  92.                 Student snimanje = new Student(input.nextInt(),
  93.                 input.next(), input.next(), input.next(), input.next(), input.nextInt(), input.nextDouble());
  94.                 izlaz.writeObject(snimanje);
  95.                 count++;
  96.             }
  97.             catch (NoSuchElementException elementgreska) {
  98.                 System.err.println("Pogresan unos, pokusajte ponovo.");
  99.                 input.nextLine();
  100.             }
  101.             catch (IOException IOgreska) {
  102.                 System.err.println("Pogreska pisanja u datoteku.");
  103.                 break;
  104.             }
  105.         }
  106.     }
  107.  
  108.     public static void zatvoriDatoteku() {
  109.         try {
  110.             if (izlaz != null)
  111.             izlaz.close();
  112.         }
  113.         catch (IOException IOgreska) {
  114.             System.err.println("Pogreska zatvaranja datoteke.");
  115.         }
  116.     }
  117. }
  118.  
  119. /**
  120.  * @author Dominik Tkalčec
  121.  */
  122.  
  123. package vjezba4;
  124.  
  125. import java.io.EOFException;
  126. import java.io.IOException;
  127. import java.io.ObjectInputStream;
  128. import java.nio.file.Files;
  129. import java.nio.file.Paths;
  130.  
  131. public class UcitavanjeIzDatoteke {
  132.     private static ObjectInputStream ulaz;
  133.     public static void main(String[] args) {
  134.         otvoriDatoteku();
  135.         ucitajStudente();
  136.         zatvoriDatoteku();
  137.        
  138.     }
  139.  
  140.     public static void otvoriDatoteku() {
  141.         try {
  142.             ulaz = new ObjectInputStream(Files.newInputStream(Paths.get("studenti.ser")));
  143.         }
  144.         catch (IOException ioException) {
  145.             System.err.println("Pogreska otvaranja datoteke.");
  146.             System.exit(1);
  147.         }
  148.     }
  149.  
  150.     public static void ucitajStudente() {
  151.         double prosjek = 0;
  152.         int count = 0;
  153.         System.out.printf("%-18s%-12s%-12s%-12s%-12s%-12s%12s%n", "Maticni broj",
  154.         "Ime", "Prezime", "Fakultet", "Smjer", "Godina", "Prosjek ocjena");
  155.  
  156.         try {
  157.             while (true) {
  158.                 Student snimi = (Student) ulaz.readObject();
  159.                 System.out.printf("%-18d%-12s%-12s%-12s%-12s%-6d%10.2f%n",
  160.                 snimi.ucitajMaticniBroj(), snimi.ucitajIme(), snimi.ucitajPrezime(), snimi.ucitajFaks(), snimi.ucitajSmjer(), snimi.ucitajGodinu(), snimi.ucitajProsjek());
  161.                 prosjek += snimi.ucitajProsjek(); count++;
  162.             }
  163.         }
  164.         catch (EOFException krajdatoteke) {
  165.             System.out.println("\nProsjek ocjena svih studenata iznosi: " + (prosjek/count));
  166.             System.out.println("Nema vise studenata.");
  167.         }
  168.         catch (ClassNotFoundException klasanijepronadena) {
  169.             System.err.println("Pogresan tip objekta.");
  170.         }
  171.         catch (IOException IOgreska) {
  172.             System.err.println("Pogreska citanja iz datoteke.");
  173.         }
  174.     }
  175.  
  176.     public static void zatvoriDatoteku() {
  177.         try {
  178.             if (ulaz != null)
  179.                 ulaz.close();
  180.         }
  181.         catch (IOException IOgreska) {
  182.             System.err.println("Pogreska zatvaranja datoteke.");
  183.             System.exit(1);
  184.         }
  185.     }
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement