list LinkedList pokazuje tylko ostatni dodany objekt

0

witam ma takie problem wpisuje dane trzech studentów i chce jednego wykasować ale w metodzie showAllStudent dostaje tylko ostatniego studenta.

import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {

        int choice = 0;

        Interface anInterface = new Interface();
        StudentDatabase studentDatabase = new StudentDatabase();
        Student student = new Student();

//        StudentDatabase sd = new StudentDatabase();
//        sd.addStudent();
//        sd.showAllStudent();
        do {
            choice = anInterface.manuMain();
            switch (choice) {
                case 1:
                    student.addStudent();
                    studentDatabase.setStudentToList(student);
                    break;
                case 2:
                    studentDatabase.deleteStudentWithList(student);
                    break;
                case 4:
                    studentDatabase.showAllStudent(student);
                    break;
            }
        } while (choice != 0);


    }
}

import javax.swing.*;

public class Interface {

    String inPut;

    public Interface() {}

    public int manuMain() {
        inPut = JOptionPane.showInputDialog("Co chcesz wykonać " +
                "\n 1: Dodaj studenta" +
                "\n 2: Usun studenta " +
                "\n 3: Dodaj ocene " +
                "\n 4: Pokaz wszystkich studentów: " +
                "\n 0: Koniec");

        return Integer.parseInt(inPut);
    }
}

import javax.swing.*;

public class Student {

    private String inPut;
    private boolean test = false;
    private char checkSingleChar = ' ';

    private String firstName;
    private String lastName;
    private int age;
    private String indeks;

    public Student() {
        firstName = "";
        lastName = "";
        age = 0;
        indeks = "";
    }

    public Student(String firstName, String lastName, int age, String index) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.age = age;
        this.indeks = index;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public int getAge() {
        return age;
    }

    public String getIndeks() {
        return indeks;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void setIndeks(String index) {
        this.indeks = index;
    }

    @Override
    public String toString() {
        return "Imie: " + firstName + " Nazwisko: "  + lastName + " Age: " + age + " Indeks: " + indeks;
    }

    @Override
    public boolean equals(Object obj) {
        return super.equals(obj);
    }

    public void addStudent() {

        inPut = JOptionPane.showInputDialog("Podaj Imie");
        inPut = checkAndChangeFirstName(inPut);
        setFirstName(inPut);
        inPut = JOptionPane.showInputDialog("Podaj Nazwisko");
        inPut = checkAndChangeLastName(inPut);
        setLastName(inPut);
        inPut = JOptionPane.showInputDialog("Podaj Wiek");
        int agePut = checkAndChangeAge(inPut);
        setAge(agePut);
        inPut = JOptionPane.showInputDialog("Podaj Indeks");
        inPut = checkAndChangeIndeks(inPut);
        setIndeks(inPut);

        System.out.println(toString());
    }


    public String checkAndChangeFirstName(String change) {
        for (int i = 0; i < change.length() ; i++) {
            for (int j = 0; j < 128; j++) {
                checkSingleChar = change.charAt(i);
                if ( j <= 64) {
                    if ( j == (int)checkSingleChar) {
                        inPut = JOptionPane.showInputDialog("W podanym imieniu znajdują sie nie poprawne znaki." +
                                    "\n Podaj imię jeszcze raz:");
                        i = -1;
                        change = inPut;
                        j = 128;
                    }
                }
                else if ( j >= 91 && j <= 96) {
                    if ( j == (int)checkSingleChar) {
                        inPut = JOptionPane.showInputDialog("W podanym imieniu znajdują sie nie poprawne znaki." +
                                "\n Podaj imię jeszcze raz:");
                        i = -1;
                        change = inPut;
                        j = 128;
                    }
                }
                else if ( j >= 123 && j <= 127) {
                    if ( j == (int)checkSingleChar) {
                        inPut = JOptionPane.showInputDialog("W podanym imieniu znajdują sie nie poprawne znaki." +
                                "\n Podaj imię jeszcze raz:");
                        i = -1;
                        change = inPut;
                        j = 127;
                    }
                }
            }
        }
        return change;
    }

    public String checkAndChangeLastName(String change) {
        for (int i = 0; i < change.length() ; i++) {
            for (int j = 0; j < 128; j++) {
                checkSingleChar = change.charAt(i);
                if ( j <= 64) {
                    if ( j == (int)checkSingleChar) {
                        inPut = JOptionPane.showInputDialog("W podanym imieniu znajdują sie nie poprawne znaki." +
                                "\n Podaj Nazwisko jeszcze raz:");
                        i = -1;
                        change = inPut;
                        j = 128;
                    }
                }
                else if ( j >= 91 && j <= 96) {
                    if ( j == (int)checkSingleChar) {
                        inPut = JOptionPane.showInputDialog("W podanym imieniu znajdują sie nie poprawne znaki." +
                                "\n Podaj Nazwisko jeszcze raz:");
                        i = -1;
                        change = inPut;
                        j = 128;
                    }
                }
                else if ( j >= 123 && j <= 127) {
                    if ( j == (int)checkSingleChar) {
                        inPut = JOptionPane.showInputDialog("W podanym imieniu znajdują sie nie poprawne znaki." +
                                "\n Podaj Nazwisko jeszcze raz:");
                        i = -1;
                        change = inPut;
                        j = 127;
                    }
                }
            }
        }
        return change;
    }

    public int checkAndChangeAge(String a) {
        age = Integer.parseInt(a);
        do {
            if (age <= 18) {
                inPut = JOptionPane.showInputDialog("Podales wiek mniejszy niz 18 lat czy " +
                        "jestes pewny ze dobrze wpisales wiek " +
                        "\n 1 - tak " +
                        "\n 2 - nie ");
                if (1 == Integer.parseInt(inPut)) {
                    setAge(Integer.parseInt(inPut));
                    test = true;
                }
                else {
                    inPut = JOptionPane.showInputDialog("Podaj prawidlowy wiek");
                    test = false;
                }
            }
            else
                test = true;
        }while (test != true);
        return age;
    }

    public String checkAndChangeIndeks(String indeksToCheck) {

        do {
            if (indeksToCheck.length() < 5 || indeksToCheck.length() > 5) {
                inPut = JOptionPane.showInputDialog("Podales zly rozmiar numeru index." +
                        "\nPowinno byc 5 liczb." +
                        "\n Wprowadz indeks ponownie: ");
                test = false;
                indeksToCheck = inPut;
            }
            else if (indeksToCheck.length() == 5) {
                for (int i = 0; i < indeksToCheck.length() ; i++) {
                    for (int j = 0; j < 128; j++) {
                        if (j >= 48 && j <= 57) {
                            test = true;
                        }
                        else {
                            checkSingleChar = indeksToCheck.charAt(i);
                            if (j == (int)checkSingleChar) {
                                inPut = JOptionPane.showInputDialog("Podales nie prawidlowe znaki (" +
                                        "tylko liczby) podaj indeks ponownie");
                                indeksToCheck = inPut;
                                test = false;
                                i = indeksToCheck.length()+1;
                                j = 128;
                            }
                        }
                    }
                }
            }
            System.out.println("test: " + test);
        }while (test != true);

        return indeksToCheck;
    }
}


import javax.swing.*;
import java.util.LinkedList;
import java.util.List;

public class StudentDatabase {

    private List<Student> listAllStudents = new LinkedList<Student>();

    public void setStudentToList (Student student) {
        System.out.println("size: " + listAllStudents.size());
        listAllStudents.add(student);
    }

    public void deleteStudentWithList(Student student) {
        showAllStudent(student);
        String indexToFind = JOptionPane.showInputDialog("Podaj indeks do wykasowania");
        boolean flag = false;
        do {
            for (int i = 0; i < listAllStudents.size(); i++) {
                if (true == listAllStudents.get(i).getIndeks().equals(indexToFind)) {
                    listAllStudents.remove(i);
                    flag = true;
                }
                else if (flag == false && i == listAllStudents.lastIndexOf(student)) {
                    indexToFind = JOptionPane.showInputDialog("index ktory wybrales nie znajduje " +
                            "\n w liscie podaj ponownie");
                    flag = false;
                }
            }
        }while (flag != true);
    }

    public void showAllStudent(Student student) {
        for (int i = 0; i < listAllStudents.size(); i++) {
            System.out.println(listAllStudents.get(i).toString());
        }
    }
}

0

Jak zrobiłeś nowego (Student student = new Student();)
w tej swojej metodzie ustawiłeś mu pola, np Adam Nowak, po czym dodałeś do listy.
Potem znowu zmieniłeś temu studentowi pola, na Jan Kowalski i znowu dodałeś.
I w momencie zmiany parametrów zmienił się też Adam Nowak w Jana Kowalskiego.
Rozumiesz? Dodajesz do listy studenta, a nie Adama Nowaka.

Spójrz na przykładowy kodzik:

//Klasa Dupa z jednym polem name.
public class Dupa {
		String name;		
		public Dupa(String bame) {
			this.name = bame;
		}
}

public class Main {
	public static void main(String[] args) {
	        List<Dupa> list = new LinkedList<>();   //Tworzymy listę
		Dupa dupa = new Dupa("ej");  //Tworzymy obiekt dupa, który ma nazwę "ej".	
		list.add(dupa);  //Dodajemy dupe do listy.
		dupa.name = "test";   //Zmieniamy nazwę dupy na "test" (Czyli to co Ty robisz w metodzie "addStudent"
		list.add(dupa);  // Dodajemy dupe do listy.
		System.out.println(list.get(0).name);   // Wynik = "test"
		System.out.println(list.get(1).name); // Wynik = "test", dlaczego? Bo oba te elementy listy to jakby taka strzałeczka na obiekt dupa. Który stworzyliśmy tylko raz.

	}

}

1 użytkowników online, w tym zalogowanych: 0, gości: 1