Advertisement
Guest User

Untitled

a guest
Mar 1st, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. package lista1;
  2.  
  3.  
  4. public class Pracownik implements java.io.Serializable{
  5.  
  6. String n;
  7. String i;
  8. long p;
  9. int s;
  10. double pn;
  11.  
  12. Pracownik (String n, String i, long p, int s, double pn) {
  13. this.n = n;
  14. this.i = i;
  15. this.p = p;
  16. this.s = s;
  17. this.pn = pn;
  18. }
  19.  
  20. void wyswietl () {
  21. System.out.printf("| %-15s | %-15s | %11d | %4d | %9.2f | %n", n, i, p, s, pn);
  22. } //do tabeli
  23.  
  24. public String toString () {
  25. return String.format("%-15s %-15s %11d %4d %9.2f %n", n, i, p, s, pn);
  26. }
  27.  
  28. }
  29.  
  30.  
  31.  
  32. package lista1;
  33.  
  34. import java.util.Scanner;
  35. import java.io.*;
  36.  
  37. public class Main {
  38.  
  39. Pracownik [] tab;
  40.  
  41. Main () {
  42. String t1 = "Liczba pracownikow: ";
  43. Scanner sc = new Scanner (new BufferedReader (
  44. new InputStreamReader(System.in)));
  45. System.out.print(t1);
  46. while (!sc.hasNextInt()) {
  47. System.out.print(t1);
  48. sc.next();
  49. } //while
  50. tab = new Pracownik[sc.nextInt()];
  51. } //Main konstruktor
  52.  
  53.  
  54. Pracownik prac () {
  55. String tn = "Nazwisko: ";
  56. String ti = "Imie: ";
  57. String tp = "PESEL: ";
  58. String ts = "Staz: ";
  59. String tpn = "Pensja: ";
  60.  
  61. Scanner sc = new Scanner (new BufferedReader (
  62. new InputStreamReader (System.in)));
  63. System.out.print(tn);
  64. String n = sc.next();
  65. System.out.print(ti);
  66. String i = sc.next();
  67. System.out.print(tp);
  68. while (!sc.hasNextLong()) {
  69. System.out.print(tp);
  70. sc.next();
  71. }
  72. long p = sc.nextLong();
  73. System.out.print(ts);
  74. while (!sc.hasNextInt()) {
  75. System.out.print(ts);
  76. sc.next();
  77. }
  78. int s = sc.nextInt();
  79. System.out.print(tpn);
  80. while (!sc.hasNextDouble()) {
  81. System.out.print(tpn);
  82. sc.next();
  83. }
  84. double pn = sc.nextDouble();
  85. return new Pracownik (n,i,p,s,pn);
  86. } //stworzenie pracownika
  87.  
  88.  
  89. void dodaj () {
  90. for (int i = 0; i < tab.length; i++) {
  91. tab[i] = prac();
  92. }
  93. } //dodaj
  94.  
  95. void tabela () {
  96. for (int i = 0; i < 70; i++) System.out.print("-");
  97. System.out.println();
  98. System.out.printf("| %-15s | %-15s | %-11s | %-4s | %-9s | %n",
  99. "Nazwisko", "Imie", "Pesel", "Staz", "Pensja");
  100. for (int i = 0; i < 70; i++) System.out.print("-");
  101. System.out.println();
  102. for (int i = 0; i < tab.length; i++) tab[i].wyswietl();
  103. System.out.println();
  104. } //tabela
  105.  
  106. void doPliku () {
  107. File f = new File ("plik");
  108. try {
  109. ObjectOutputStream oos = new ObjectOutputStream (new FileOutputStream(f));
  110. oos.writeInt(tab.length);
  111. for (int i = 0; i < tab.length; i++) oos.writeObject(tab[i]);
  112. oos.close();
  113. } catch (IOException io) { System.out.println("Blad z zapisem");
  114. }
  115. }
  116.  
  117. void zPliku () {
  118. try {
  119. ObjectInputStream ois = new ObjectInputStream (new FileInputStream("plik"));
  120. Pracownik tab2 [] = new Pracownik [ois.readInt()];
  121. for (int i = 0; i < tab2.length; i++) {
  122. tab2[i] = (Pracownik) ois.readObject();
  123. System.out.print(tab2[i].toString());
  124. }
  125. ois.close();
  126. } catch (ClassNotFoundException cn) { System.out.println("Blad z klasa");
  127. } catch (IOException io) { System.out.println("Blad z odczytem");
  128. }
  129. }
  130.  
  131. public static void main (String [] args) {
  132. Main x = new Main ();
  133. x.dodaj();
  134. x.tabela();
  135. x.doPliku();
  136. x.zPliku();
  137. } //main
  138.  
  139. } //class main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement