Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package lista1;
- public class Pracownik implements java.io.Serializable{
- String n;
- String i;
- long p;
- int s;
- double pn;
- Pracownik (String n, String i, long p, int s, double pn) {
- this.n = n;
- this.i = i;
- this.p = p;
- this.s = s;
- this.pn = pn;
- }
- void wyswietl () {
- System.out.printf("| %-15s | %-15s | %11d | %4d | %9.2f | %n", n, i, p, s, pn);
- } //do tabeli
- public String toString () {
- return String.format("%-15s %-15s %11d %4d %9.2f %n", n, i, p, s, pn);
- }
- }
- package lista1;
- import java.util.Scanner;
- import java.io.*;
- public class Main {
- Pracownik [] tab;
- Main () {
- String t1 = "Liczba pracownikow: ";
- Scanner sc = new Scanner (new BufferedReader (
- new InputStreamReader(System.in)));
- System.out.print(t1);
- while (!sc.hasNextInt()) {
- System.out.print(t1);
- sc.next();
- } //while
- tab = new Pracownik[sc.nextInt()];
- } //Main konstruktor
- Pracownik prac () {
- String tn = "Nazwisko: ";
- String ti = "Imie: ";
- String tp = "PESEL: ";
- String ts = "Staz: ";
- String tpn = "Pensja: ";
- Scanner sc = new Scanner (new BufferedReader (
- new InputStreamReader (System.in)));
- System.out.print(tn);
- String n = sc.next();
- System.out.print(ti);
- String i = sc.next();
- System.out.print(tp);
- while (!sc.hasNextLong()) {
- System.out.print(tp);
- sc.next();
- }
- long p = sc.nextLong();
- System.out.print(ts);
- while (!sc.hasNextInt()) {
- System.out.print(ts);
- sc.next();
- }
- int s = sc.nextInt();
- System.out.print(tpn);
- while (!sc.hasNextDouble()) {
- System.out.print(tpn);
- sc.next();
- }
- double pn = sc.nextDouble();
- return new Pracownik (n,i,p,s,pn);
- } //stworzenie pracownika
- void dodaj () {
- for (int i = 0; i < tab.length; i++) {
- tab[i] = prac();
- }
- } //dodaj
- void tabela () {
- for (int i = 0; i < 70; i++) System.out.print("-");
- System.out.println();
- System.out.printf("| %-15s | %-15s | %-11s | %-4s | %-9s | %n",
- "Nazwisko", "Imie", "Pesel", "Staz", "Pensja");
- for (int i = 0; i < 70; i++) System.out.print("-");
- System.out.println();
- for (int i = 0; i < tab.length; i++) tab[i].wyswietl();
- System.out.println();
- } //tabela
- void doPliku () {
- File f = new File ("plik");
- try {
- ObjectOutputStream oos = new ObjectOutputStream (new FileOutputStream(f));
- oos.writeInt(tab.length);
- for (int i = 0; i < tab.length; i++) oos.writeObject(tab[i]);
- oos.close();
- } catch (IOException io) { System.out.println("Blad z zapisem");
- }
- }
- void zPliku () {
- try {
- ObjectInputStream ois = new ObjectInputStream (new FileInputStream("plik"));
- Pracownik tab2 [] = new Pracownik [ois.readInt()];
- for (int i = 0; i < tab2.length; i++) {
- tab2[i] = (Pracownik) ois.readObject();
- System.out.print(tab2[i].toString());
- }
- ois.close();
- } catch (ClassNotFoundException cn) { System.out.println("Blad z klasa");
- } catch (IOException io) { System.out.println("Blad z odczytem");
- }
- }
- public static void main (String [] args) {
- Main x = new Main ();
- x.dodaj();
- x.tabela();
- x.doPliku();
- x.zPliku();
- } //main
- } //class main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement