Advertisement
Morogn93

Untitled

Mar 28th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.67 KB | None | 0 0
  1.  
  2. import java.io.*;
  3. import java.io.FileInputStream;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.ObjectInputStream;
  7. import java.io.ObjectOutputStream;
  8. import java.util.HashMap;
  9. import java.util.Iterator;
  10. import java.util.LinkedList;
  11. import java.util.List;
  12. import java.util.Map;
  13. import java.util.Scanner;
  14. import java.util.Set;
  15.  
  16. public class Prototype {
  17.  
  18.     final String bazaStudentow = "BazaStudentow.ser";
  19.     public FileOutputStream fos;
  20.     public ObjectOutputStream oos;
  21.     static int counter;
  22.     public HashMap<Integer, String> hmap = new HashMap<Integer, String>();
  23.  
  24.     public void CreateFile() {
  25.         if (CheckFileIfExist() == false) { // jesli plik nie istnieje
  26.             try {
  27.                 fos = new FileOutputStream(bazaStudentow); // utworz plik
  28.                 oos = new ObjectOutputStream(fos); //
  29.             } catch (IOException ioe) {
  30.                 ioe.printStackTrace();
  31.             }
  32.         }
  33.     }
  34.    
  35.     public void Run() {
  36.         CreateFile();
  37.         ReadFromFile();
  38.         PrintHashMap();
  39.         addToHashMap(new Student("andrzej", "Duda"));
  40.         PrintHashMap();
  41.     }
  42.  
  43.     public boolean CheckFileIfExist() {
  44.         File f = new File("BazaStudentow.ser");
  45.         if (f.exists()) {
  46.             return true; // plik istnieje
  47.         } else
  48.             return false; // plik nie istnieje
  49.     }
  50.  
  51.     public void SaveFileAndClose() {
  52.         try {
  53.             oos.writeObject(hmap); // zapisanie hashmapy do CreateFile()
  54.             oos.close();
  55.             fos.close();
  56.             System.out.printf("Serialized HashMap data is saved in hashmap.ser");
  57.         } catch (IOException ioe) {
  58.             ioe.printStackTrace();
  59.         }
  60.     }
  61.  
  62.     public void addToHashMap(Student student) {
  63.  
  64.         hmap.put(counter, student.GetImieINazwisko());
  65.         counter++;
  66.     }
  67.  
  68.     // trzeba napisac Wczytanie pliku i jesli plik istnieje to dodaj zawartosc do
  69.     // mapy
  70.     public void ReadFromFile() {
  71.         HashMap<Integer, String> map = null;
  72.         if (CheckFileIfExist()) {
  73.             try {
  74.                 FileInputStream fis = new FileInputStream(bazaStudentow);
  75.                 ObjectInputStream ois = new ObjectInputStream(fis);
  76.                 map = (HashMap<Integer, String>) ois.readObject();
  77.                 hmap = (HashMap<Integer, String>) map.clone(); // klonowanie bazy
  78.                 ois.close();
  79.                 fis.close();
  80.             } catch (IOException ioe) {
  81.                 ioe.printStackTrace();
  82.                 System.out.println("Błąd wejscia wyjscia");
  83.                 return;
  84.             } catch (ClassNotFoundException c) {
  85.                 System.out.println("Nie znaleziono klasy");
  86.                 c.printStackTrace();
  87.                 return;
  88.             }
  89.             System.out.println("Deserialized HashMap..");
  90.             // Display content using Iterator
  91.             Set set = map.entrySet();
  92.             Iterator iterator = set.iterator();
  93.             while (iterator.hasNext()) { // iteracja wczytanej HashMapy
  94.                 Map.Entry mentry = (Map.Entry) iterator.next();
  95.                 System.out.print("key: " + mentry.getKey() + " & Value: ");
  96.                 System.out.println(mentry.getValue());
  97.             }
  98.         }
  99.     }
  100.  
  101.     public void PrintHashMap() {
  102.         Set set = hmap.entrySet();
  103.         Iterator iterator = set.iterator();
  104.         while (iterator.hasNext()) { // iteracja wczytanej HashMapy
  105.             Map.Entry mentry = (Map.Entry) iterator.next();
  106.             System.out.print("key: " + mentry.getKey() + " & Value: ");
  107.             System.out.println(mentry.getValue());
  108.         }
  109.  
  110.     }
  111.  
  112. }
  113.  
  114.  
  115. import java.util.ArrayList;
  116. import java.util.List;
  117.  
  118. public class Student {
  119.     public String imie = "empty";
  120.     public String nazwisko = "empty";
  121.     private List<Ocena> listaOcen = new ArrayList<Ocena>();
  122.     public Student(String imie, String nazwisko){
  123.         SetImieToStudent(imie);
  124.         SetNazwiskoToStudent(nazwisko);
  125.     }
  126.     public Student() {
  127.        
  128.     }
  129.    
  130.    
  131.     public void SetImieToStudent(String imie) {
  132.         Character.isUpperCase(imie.charAt(0));
  133.         this.imie = imie;
  134.     }
  135.     public void SetNazwiskoToStudent(String nazwisko) {
  136.         Character.isUpperCase(nazwisko.charAt(0));
  137.         this.nazwisko = nazwisko;
  138.     }
  139.     public String GetImie() {
  140.         return imie;
  141.     }
  142.     public String GetNazwisko() {
  143.         return nazwisko;
  144.     }
  145.     public void AddOcena(Ocena ocena) {
  146.         listaOcen.add(ocena);
  147.         // przez tą metode bedziesz musial zrobic switch case by wysylac do tej metody konkretne obiekty typu ocena   
  148.     }
  149.    
  150.     public String GetImieINazwisko() {
  151.         return GetImie()+ " " +GetNazwisko();
  152.     }
  153.            
  154. }
  155.  
  156.  
  157.  
  158. /*
  159. * Napisać program realizujący bazę grupy studentów, z uwzględnieniem
  160. *
  161. * - możliwość dodania do 10 studentów - klasa studentów (wprowadzenie danych z
  162. * klawiatury)
  163. *
  164. * - możliwość dodania do 10 ocen studenta (tablica 1-wymiarowa, wprowadzenie
  165. * danych z klawiatury)
  166. *
  167. * - wyznaczenie średniej oceny dla każdego studenta (metody klasy Student) -
  168. * wyznaczenie średniej oceny dla całej klasy
  169. *
  170. * - przeglądanie listy studentów i wyświetlanie wszystkich informacji o
  171. * studencie
  172. *
  173. * - menu tekstowe
  174. *
  175. * - zapis i odczyt danych z pliku
  176. *
  177. * - obsługa wyjątków (try - catch) - termin 2 tygodnie;
  178. *
  179.  
  180.  
  181. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement