Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @author Dominik Tkalčec
- */
- package vjezba4;
- import java.io.Serializable;
- public class Student implements Serializable {
- private int maticnibroj;
- private String ime;
- private String prezime;
- private String fakultet;
- private String smjer;
- private int godina;
- private double prosjekocjena;
- public Student() {
- this(0, "", "", "", "", 0, 0.0);
- }
- public Student(int mb, String ime, String prezime, String fax, String smjer, int god, double po) {
- this.maticnibroj = mb;
- this.ime = ime;
- this.prezime = prezime;
- this.fakultet = fax;
- this.smjer = smjer;
- this.godina = god;
- this.prosjekocjena = po;
- }
- public int ucitajMaticniBroj() {
- return maticnibroj;
- }
- public String ucitajIme() {
- return ime;
- }
- public String ucitajPrezime() {
- return prezime;
- }
- public String ucitajFaks() {
- return fakultet;
- }
- public String ucitajSmjer() {
- return smjer;
- }
- public int ucitajGodinu() {
- return godina;
- }
- public double ucitajProsjek() {
- return prosjekocjena;
- }
- }
- /**
- * @author Dominik Tkalčec
- */
- package vjezba4;
- import java.io.IOException;
- import java.nio.file.Files;
- import java.nio.file.Paths;
- import java.util.NoSuchElementException;
- import java.util.Scanner;
- import java.io.ObjectOutputStream;
- public class SpremiUDatoteku {
- private static ObjectOutputStream izlaz;
- public static void main(String[] args) {
- otvoriDatoteku();
- snimiStudente();
- zatvoriDatoteku();
- }
- public static void otvoriDatoteku() {
- try {
- izlaz = new ObjectOutputStream(Files.newOutputStream(Paths.get("studenti.ser")));
- }
- catch (IOException IOgreska) {
- System.err.println("Pogreska otvaranja datoteke.");
- System.exit(1);
- }
- }
- public static void snimiStudente() {
- Scanner input = new Scanner(System.in);
- int count = 0;
- while (count < 2) {
- try {
- System.out.println("Unesite maticni broj, ime, prezime, fakultet, smjer, godinu i prosjek ocjena " + (count + 1) + ". studenta:");
- Student snimanje = new Student(input.nextInt(),
- input.next(), input.next(), input.next(), input.next(), input.nextInt(), input.nextDouble());
- izlaz.writeObject(snimanje);
- count++;
- }
- catch (NoSuchElementException elementgreska) {
- System.err.println("Pogresan unos, pokusajte ponovo.");
- input.nextLine();
- }
- catch (IOException IOgreska) {
- System.err.println("Pogreska pisanja u datoteku.");
- break;
- }
- }
- }
- public static void zatvoriDatoteku() {
- try {
- if (izlaz != null)
- izlaz.close();
- }
- catch (IOException IOgreska) {
- System.err.println("Pogreska zatvaranja datoteke.");
- }
- }
- }
- /**
- * @author Dominik Tkalčec
- */
- package vjezba4;
- import java.io.EOFException;
- import java.io.IOException;
- import java.io.ObjectInputStream;
- import java.nio.file.Files;
- import java.nio.file.Paths;
- public class UcitavanjeIzDatoteke {
- private static ObjectInputStream ulaz;
- public static void main(String[] args) {
- otvoriDatoteku();
- ucitajStudente();
- zatvoriDatoteku();
- }
- public static void otvoriDatoteku() {
- try {
- ulaz = new ObjectInputStream(Files.newInputStream(Paths.get("studenti.ser")));
- }
- catch (IOException ioException) {
- System.err.println("Pogreska otvaranja datoteke.");
- System.exit(1);
- }
- }
- public static void ucitajStudente() {
- double prosjek = 0;
- int count = 0;
- System.out.printf("%-18s%-12s%-12s%-12s%-12s%-12s%12s%n", "Maticni broj",
- "Ime", "Prezime", "Fakultet", "Smjer", "Godina", "Prosjek ocjena");
- try {
- while (true) {
- Student snimi = (Student) ulaz.readObject();
- System.out.printf("%-18d%-12s%-12s%-12s%-12s%-6d%10.2f%n",
- snimi.ucitajMaticniBroj(), snimi.ucitajIme(), snimi.ucitajPrezime(), snimi.ucitajFaks(), snimi.ucitajSmjer(), snimi.ucitajGodinu(), snimi.ucitajProsjek());
- prosjek += snimi.ucitajProsjek(); count++;
- }
- }
- catch (EOFException krajdatoteke) {
- System.out.println("\nProsjek ocjena svih studenata iznosi: " + (prosjek/count));
- System.out.println("Nema vise studenata.");
- }
- catch (ClassNotFoundException klasanijepronadena) {
- System.err.println("Pogresan tip objekta.");
- }
- catch (IOException IOgreska) {
- System.err.println("Pogreska citanja iz datoteke.");
- }
- }
- public static void zatvoriDatoteku() {
- try {
- if (ulaz != null)
- ulaz.close();
- }
- catch (IOException IOgreska) {
- System.err.println("Pogreska zatvaranja datoteke.");
- System.exit(1);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement