metoda addStudent zawiesza wykonanie program

0

witam ma pewien problem w metodzie addStudent próbuje dodac imie nazwisko wiek i indeks ale po wpisaniu indeksu nic dalej się nie dzieje a program powinie wrócić do głównego menu. I co dziwne po wpisaniu w studentdatabase indeksu nie wypisuje mi w ogolę co jest w danym obiekcje.

klasa main:

import java.util.ArrayList;

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

        int choice = 0;

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

        choice = anInterface.manuMain();
        do {
            switch (choice) {
                case 1:
                    studentDatabase.addStudent();
                    break;
                case 4:
                    studentDatabase.showAllStudent();
                    break;
            }
        } while (choice != 0);
    }
}

klasa interface:

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);
    }
}

klasa student:

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 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;
            }
            if (indeksToCheck.length() < 5 || 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;
                            }
                        }
                    }
                }
            }
        }while (test == true);

        return indeksToCheck;
    }
}


klasa studentdatabase:

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

public class StudentDatabase {

    private String inPut;
    private int agePut;

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

    public void addStudent() {
        Student student = new Student();

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

        System.out.println(student);

        listAllStudents.add(student);
    }

    public void showAllStudent() {
        for (Student s : listAllStudents) {
            System.out.println(s.toString());
        }
    }
}

0

Przede wszystkim widze że jest łamana SRP//en.wikipedia.org/wiki/Single_responsibility_principle
Model odpowiadający za wywowałnie gui? ...

0

Wyrzuć metody checkAnd.... z klasy User do jakiejś innej np. UserService, UserValidation czy coś takiego. Jest taki wzorzec projektowy: MVC. Poczytaj sobie o nim bo chyba do Twojego przypadku będzie najlepszy. Skoro mówisz, że po wpisaniu indeksu program się blokuje to znaczy, że pewnie w metodzie checkAndChangeIndeks nie wychodzisz z tego while(). Odpal debuger i zobacz co Ci się tam dzieje w tej metodzie.

0

Świetna okazja by nauczyć się obsługiwać debuger.

PS.
test jest polem klasy, nie wiesz na co jest ustawiony przy wykonaniu checkAndChangeIndeks. Co oznacza, że jeżeli w momencie wejścia do checkAndChangeIndeks test był true, a długość indeksu wynosiła 5, to program utknie w nieskończonej pętli.
Fajnie by było znać założenia początkowe, uniknąłbyś takich sytuacji.

0

Przede wszystkim powinieneś nauczyć się obsługiwać debuger. Potężne narzędzie w wyszukiwaniu błędów.

0

ok trochę poprawiłem. Ale mam inny problem po podaniu index od nowa karze mi podawać wszystkie dane i tak wkoło

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 {
            System.out.println("poczatek");
            System.out.println("size: " + indeksToCheck.length());
            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;
    }
}


1

W klasie Main, do pętli wchodzisz z wyborem którego później już nie zmieniasz.

  choice = anInterface.manuMain(); // skoro tu ustawiasz wartość "choice" 
        do { // a tu zaczyna się pętla, to właśnie dlatego cały czas kaRZą Ci podawać wszystkie dane od nowa
            switch (choice) {
                case 1:
                    studentDatabase.addStudent();
                    break;
                case 4:
                    studentDatabase.showAllStudent();
                    break;
            }
        } while (choice != 0); 

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