Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.LinkedList;
- import java.util.List;
- import java.util.Map;
- import java.util.Scanner;
- import java.util.Set;
- public class Prototype {
- final String bazaStudentow = "BazaStudentow.ser";
- public FileOutputStream fos;
- public ObjectOutputStream oos;
- static int counter;
- public HashMap<Integer, String> hmap = new HashMap<Integer, String>();
- public void CreateFile() {
- if (CheckFileIfExist() == false) { // jesli plik nie istnieje
- try {
- fos = new FileOutputStream(bazaStudentow); // utworz plik
- oos = new ObjectOutputStream(fos); //
- } catch (IOException ioe) {
- ioe.printStackTrace();
- }
- }
- }
- public void Run() {
- CreateFile();
- ReadFromFile();
- PrintHashMap();
- addToHashMap(new Student("andrzej", "Duda"));
- PrintHashMap();
- }
- public boolean CheckFileIfExist() {
- File f = new File("BazaStudentow.ser");
- if (f.exists()) {
- return true; // plik istnieje
- } else
- return false; // plik nie istnieje
- }
- public void SaveFileAndClose() {
- try {
- oos.writeObject(hmap); // zapisanie hashmapy do CreateFile()
- oos.close();
- fos.close();
- System.out.printf("Serialized HashMap data is saved in hashmap.ser");
- } catch (IOException ioe) {
- ioe.printStackTrace();
- }
- }
- public void addToHashMap(Student student) {
- hmap.put(counter, student.GetImieINazwisko());
- counter++;
- }
- // trzeba napisac Wczytanie pliku i jesli plik istnieje to dodaj zawartosc do
- // mapy
- public void ReadFromFile() {
- HashMap<Integer, String> map = null;
- if (CheckFileIfExist()) {
- try {
- FileInputStream fis = new FileInputStream(bazaStudentow);
- ObjectInputStream ois = new ObjectInputStream(fis);
- map = (HashMap<Integer, String>) ois.readObject();
- hmap = (HashMap<Integer, String>) map.clone(); // klonowanie bazy
- ois.close();
- fis.close();
- } catch (IOException ioe) {
- ioe.printStackTrace();
- System.out.println("Błąd wejscia wyjscia");
- return;
- } catch (ClassNotFoundException c) {
- System.out.println("Nie znaleziono klasy");
- c.printStackTrace();
- return;
- }
- System.out.println("Deserialized HashMap..");
- // Display content using Iterator
- Set set = map.entrySet();
- Iterator iterator = set.iterator();
- while (iterator.hasNext()) { // iteracja wczytanej HashMapy
- Map.Entry mentry = (Map.Entry) iterator.next();
- System.out.print("key: " + mentry.getKey() + " & Value: ");
- System.out.println(mentry.getValue());
- }
- }
- }
- public void PrintHashMap() {
- Set set = hmap.entrySet();
- Iterator iterator = set.iterator();
- while (iterator.hasNext()) { // iteracja wczytanej HashMapy
- Map.Entry mentry = (Map.Entry) iterator.next();
- System.out.print("key: " + mentry.getKey() + " & Value: ");
- System.out.println(mentry.getValue());
- }
- }
- }
- import java.util.ArrayList;
- import java.util.List;
- public class Student {
- public String imie = "empty";
- public String nazwisko = "empty";
- private List<Ocena> listaOcen = new ArrayList<Ocena>();
- public Student(String imie, String nazwisko){
- SetImieToStudent(imie);
- SetNazwiskoToStudent(nazwisko);
- }
- public Student() {
- }
- public void SetImieToStudent(String imie) {
- Character.isUpperCase(imie.charAt(0));
- this.imie = imie;
- }
- public void SetNazwiskoToStudent(String nazwisko) {
- Character.isUpperCase(nazwisko.charAt(0));
- this.nazwisko = nazwisko;
- }
- public String GetImie() {
- return imie;
- }
- public String GetNazwisko() {
- return nazwisko;
- }
- public void AddOcena(Ocena ocena) {
- listaOcen.add(ocena);
- // przez tą metode bedziesz musial zrobic switch case by wysylac do tej metody konkretne obiekty typu ocena
- }
- public String GetImieINazwisko() {
- return GetImie()+ " " +GetNazwisko();
- }
- }
- /*
- * Napisać program realizujący bazę grupy studentów, z uwzględnieniem
- *
- * - możliwość dodania do 10 studentów - klasa studentów (wprowadzenie danych z
- * klawiatury)
- *
- * - możliwość dodania do 10 ocen studenta (tablica 1-wymiarowa, wprowadzenie
- * danych z klawiatury)
- *
- * - wyznaczenie średniej oceny dla każdego studenta (metody klasy Student) -
- * wyznaczenie średniej oceny dla całej klasy
- *
- * - przeglądanie listy studentów i wyświetlanie wszystkich informacji o
- * studencie
- *
- * - menu tekstowe
- *
- * - zapis i odczyt danych z pliku
- *
- * - obsługa wyjątków (try - catch) - termin 2 tygodnie;
- *
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement