Extract form main-Class:
Client k = getClient();
Truster kg = new Truster();
boolean ok = false;
while (!ok) {
System.out.print("\n name: ");
//io: is a class for my input from Keyboard
kg.setName(io.readString()); // <--need the input from here
//argu_vali is my ValidationClass
if (argu_vali.analyse(k, "mylist") == 0) {
ok = true;
}
}
ok = false;
while (!ok) {
System.out.print("\n: ");
kg.setSumme(io.readInteger());
if (argu_vali.analyse(kg, "sum") == 0) {
ok = true;
}
}
k.add_credit(kg);
my class Client:
class Client{
String name;
@t_list_Check
private ArrayList <truster> t_list= new ArrayList<>();
}
my class truster:
class Truster{
String name;
@Min(0)
int sum;
}
my own ValidatorClass for t_list:
public class t_list_Validator implements ConstraintValidator<t_list_Check, Collection> {
@Override
public void initialize(t_list_Check a) {
}
@Override
public boolean isValid(Collection kg_list, ConstraintValidatorContext cvc) {
Object[] objects = kg_list.toArray();
if (objects.length < 1) {
return true;
}
String new_name = objects[objects.length-1].toString(); //<---need here the
//right input from above
for (Object o : kg_list) {
if (new_name.equals(o.toString())) {
return false;
}
}
return true;
}
}
My problem is now, that I just get the right name for the first element in t_list. For the second input for my trusters (kg.setName(io.readString());) I receive from objects[objects.length-1].toString() in my ownValidator just the last element from t_list not the actual element.
For Example:
- 1.run:
- Input in main: test1
- value new_name in t_list_Validator: test1
…
- 2.run:
- Input in main: test 2
- value new_name in t_list_Validator: test1 // need here the actual input: “test2”
Just adding the element to an list, can solve the problem. After validation delete it from list an go on with validation….