Wrzucam cały kod:
package pw_5;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Locale;
import java.util.Scanner;
/**
*
*
* @author Mateusz
*/
public class PW_5 {
public static void main(String[] args) {
int wybor = 0;
int nrAlbumu = 0;
float ocena = 0;
Scanner in = new Scanner(System.in, "Windows-1250");
Studenci studenci = new Studenci();
do{
System.out.println("Wybierz co chcesz zrobić:");
System.out.println("\n1. Odczytaj z pliku\n" + "2. Zapisz do pliku\n" + "3. Dodaj studenta\n" +
"4. Dodaj oceny dla studenta (o danym numerze albumu)\n" + "5. Usuń studenta (o danym numerze albumu)\n" + "6. Wyświetl listę studentów\n"
+ "7. Wyszukaj studentów\n" + "8. Zakończ pracę");
wybor = in.nextInt();
switch(wybor){
case 1:
//odczyt z pliku
studenci.odczyt();
studenci.wyswietl();
break;
case 2:
//Zapis do pliku
studenci.zapisz();
studenci.wyswietl();
break;
case 3:
//Utworzenie nowych obiektów
int ile = 0;
System.out.println("Ile studentów: ");
ile = in.nextInt();
for (int i = 0; i < ile; i++) {
studenci.dodajStudenta();
}
break;
case 4:
// dodaje ile ocen chcemy
System.out.println("Dodaj ocene dla studenta o numerze równym: ");
nrAlbumu = in.nextInt();
System.out.println("Jaka ocena: ");
ocena = in.nextFloat();
for (Student student : studenci.getListaStudentow()) {
student.addOcena(ocena, nrAlbumu);
}
break;
case 5:
// usuwanie studenta po id
System.out.println("Usun studenta o id: ");
nrAlbumu = in.nextInt();
for (int i = 0; i < nrAlbumu; i++) {
studenci.usunStudenta(nrAlbumu);
}
break;
case 6:
studenci.wyswietl();
break;
case 7:
//szuka studento po sredniej
System.out.println("Szukasz po sredniej: ");
float srednia;
srednia = in.nextFloat();
for (Student student : studenci.getListaStudentow()) {
if(student.getSrednia() == srednia)
System.out.println("Wynik: "+student.getNrAlbumu()+" "+student.getImie()+" "+student.getNazwisko()+" " + student.getSrednia());
}
break;
default:
System.out.println("\nWybierz poprawną wartość");
if(wybor == 8)
System.out.println("\nPa pa !");
}
}while(wybor != 8);
}
}
class Student implements Comparable<Student>, Serializable {
private int nrAlbumu;
private String imie;
private String nazwisko;
private List<Float> oceny = new ArrayList<>();
private float srednia = 0;
// Scanner z obsługa polskich znaków - transient powoduje że te dane nie są zapisywane do pliku
transient private Locale loc = new Locale("pl", "PL");
transient private Scanner in = new Scanner(System.in, "Windows-1250");
public Student(int nrAlbumu, String imie, String nazwisko) {
this.nrAlbumu = nrAlbumu;
this.imie = imie;
this.nazwisko = nazwisko;
}
public Student() {
wczytajDane();
}
public void wczytajDane() {
in.useLocale(loc);
System.out.println("Podaj dane studenta");
boolean czyOk = false;
while(!czyOk)
{
try{
System.out.print("Nr albumu: ");
Scanner s = new Scanner(System.in);
nrAlbumu = s.nextInt();
s.nextLine(); // konieczne po nextInt
czyOk = true;
}catch(Exception e)
{
System.out.println("Podaj prawidłowy nr albumu");
}
}
System.out.print("Imię: ");
imie = in.nextLine();
System.out.print("Nazwisko: ");
nazwisko = in.nextLine();
}
public int getNrAlbumu() {
return nrAlbumu;
}
public void setNrAlbumu(int nrAlbumu) {
this.nrAlbumu = nrAlbumu;
}
public String getImie() {
return imie;
}
public void setImie(String imie) {
this.imie = imie;
}
public String getNazwisko() {
return nazwisko;
}
public void setNazwisko(String nazwisko) {
this.nazwisko = nazwisko;
}
public List<Float> getOceny() {
return oceny;
}
// ta funkcja jest wywoływana prze deserializacji więc konieczne jest policzenie średniej
public void setOceny(List<Float> oceny) {
this.oceny = oceny;
policzSrednia();
}
public float getSrednia() {
return srednia;
}
public void setSrednia(float srednia) {
this.srednia = srednia;
}
public void addOcena(float ocena, int nrAlbumu) {
if(getNrAlbumu() == nrAlbumu)
{
srednia = (srednia * (float) oceny.size() + ocena) / ((float) oceny.size() + 1.0f);
oceny.add(ocena);
}
}
// ocena z klawiatury
public void addOcena() {
System.out.print("Nowa coena dla "+imie +" "+ nazwisko+": ");
addOcena(in.nextFloat(),in.nextInt());
}
private void policzSrednia() {
float sum = 0;
for (float ocena : oceny) {
sum += ocena;
}
srednia = sum / (float) oceny.size();
}
@Override
public String toString() {
return String.format("|%-6d|%-20s|%-20s| %1.2f|", nrAlbumu, imie, nazwisko, srednia);
}
@Override
public int compareTo(Student s2) {
return Integer.compare(nrAlbumu, s2.getNrAlbumu());
}
}
/**
* klasa zawierająca listę studentów, metody do zapisu/odczytu z pliku i metody sortujące
*/
class Studenci {
private List<Student> listaStudentow = new ArrayList<>();
private StudentComparator studentComparator = new StudentComparator();
private Iterator<Student> itStudent = listaStudentow.iterator();
public void dodajStudenta(Student student){
listaStudentow.add(student);
}
public void dodajStudenta(){
listaStudentow.add(new Student());
}
/**
* Metoda sortująca listę studentów po średniej
*/
public void sortujPoSredniej() {
studentComparator.setSortBy(StudentComparator.BYSREDNIA);
Collections.sort(listaStudentow, studentComparator);
}
/**
* Metoda wyświetlająca listę studentów
*/
public void usunStudenta(int ile)
{
while (itStudent.hasNext()) {
Student student = itStudent.next();
if(student.getNrAlbumu()== ile)
itStudent.remove();
}
}
public void wyswietl() {
for (Student student : listaStudentow) {
System.out.println(student);
}
}
/**
* Metoda zapisuje listę studentów do pliku studenci.data
*/
public void zapisz() {
try {
ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream("studenci.data"));
outputStream.writeObject(listaStudentow);
} catch (IOException ex) {
System.out.println("Wystąpił błąd zapisu");
ex.printStackTrace();
}
}
/**
* Metoda odczytuje listę studentów z pliku studenci.data
*/
public void odczyt() {
try {
ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream("studenci.data"));
listaStudentow = (List<Student>) inputStream.readObject();
} catch (Exception ex) {
System.out.println("Wystąpił błąd odczytu");
ex.printStackTrace();
}
}
public StudentComparator getStudentComparator() {
return studentComparator;
}
public Iterator<Student> getItStudent() {
return itStudent;
}
public void setStudentComparator(StudentComparator studentComparator) {
this.studentComparator = studentComparator;
}
public void setItStudent(Iterator<Student> itStudent) {
this.itStudent = itStudent;
}
public List<Student> getListaStudentow() {
return listaStudentow;
}
public void setListaStudentow(List<Student> listaStudentow) {
this.listaStudentow = listaStudentow;
}
}
class StudentComparator implements Comparator<Student> {
private int sortBy = 1;
public static final int BYNR = 1, BYNAZWISKO = 2, BYIMIE = 3, BYSREDNIA = 4;
public int getSortBy() {
return sortBy;
}
public void setSortBy(int sortBy) {
this.sortBy = sortBy;
}
@Override
public int compare(Student s1, Student s2) {
switch (sortBy) {
case BYNAZWISKO:
return s1.getNazwisko().compareToIgnoreCase(s2.getNazwisko());
case BYIMIE:
return s1.getImie().compareToIgnoreCase(s2.getImie());
case BYNR:
return Integer.compare(s1.getNrAlbumu(), s2.getNrAlbumu());
case BYSREDNIA:
return Float.compare(s1.getSrednia(), s2.getSrednia());
}
return 0;
}
}