I encountered a problem using equals, let me first add the code here :
Operateur o = new Operateur(nom, age, sexe, grade, role);
for(Operateur op : Membre.getOperateurs()){
if(o.equals((Object)op)){
already_exists = true;
break;
}
}
if(already_exists){
ret = ">Can't create it : already exists";
Membre.removeLast();
o.finalize();
}else{
ret = ">Created it";
}
Here I check if a new Operateur
isn't just a copy of an old one (Membre.getOperateurs
gets me the list of all instances of Membre
that are actual Operateur
).
My problem is that when I run this bit of code, if my Operateur
is brand new it appears as already existing (which he should not, since he's brand new).
For instance, when I create the first Operateur
ever it evaluates it as a duplicate of an already existing one.
As :
import cmd.*;
import matrice.*;
import persona.*;
import non_grata.non.*;
import non_grata.grata.*;
//all above is importing packages to use all classes required
public class Debug{
public static void out(String s){
System.out.println(s);
}//handy
public static void main(String[] args){
Membre.init();//Membre.membre = new ArrayList<Membre>();
Personnel.init();//Personnel.membre = new ArrayList<Membre>();
Vaisseau.init();//Vaisseau.vaisseaux = new ArrayList<Vaisseau>();
MatrixCmd cmd = new MatrixCmd();//Create the command parser
String parser_input = "afficherMembres";//get some input
String parser_output = cmd.execCommand(parser_input);//process it
out(parser_output);//use it
//empty list, correct since we only initialized it
parser_input = "newOPsion mikebike 10 o string string";
parser_output = cmd.execCommand(parser_input);
out(parser_output);
//outputs ">Impossible de créer cet Opérateur : Il existe déjà" which it shouldn't
//since there are no other Operateur and this is only
//given as output when it founds a similar Operateur
parser_input = "afficherMembres";
parser_output = cmd.execCommand(parser_input);
out(parser_output);
//outputs an empty list, meaning that there are no Membre created
//and therefore no Operateur
parser_input = "newOPsion mikebke 8 f tring sring";//command that will trigger the bit of code above
parser_output = cmd.execCommand(parser_input);
out(parser_output);
//outputs">Impossible de créer cet Opérateur : Il existe déjà" which it shouldn't
}
}