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