1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
public void ajoutDUnMémoire(Mémoire mémoire, String section)throws IdentifiantException
{
if (tailleLogique == MAX) agrandirTableau();
String id = créerIdentifiant(mémoire, section);//Expliquer mode ajout id ds reg (intro)
if (tailleLogique == 0)insertion(id, mémoire,0);
else
{
int j = tailleLogique;
while(j > 0 && registre[j-1].getIdentifiant().compareTo(id)>0)//??Erreur??
{
registre [j+1] = registre[j];
j--;
}
if (j > 0 && registre[j-1].getIdentifiant().compareTo(id)==0)
{
if (registre[j-1].getIdentifiant().length()==8)//pas de chiffre
{
id += 1;
insertion(id, mémoire,j);
return;
}
else //des chiffres length = 9
{
int num = (int) registre[j-1].getIdentifiant().charAt(8);
id += num++;
insertion(id, mémoire,j);
return;
}
}
else insertion(id, mémoire,j);
}
}
private void insertion (String id , Mémoire mémoire, int pos)throws IdentifiantException
{
registre [pos] = mémoire;
registre[pos].setIdentifiant(id);
tailleLogique ++;
}
private String créerIdentifiant (Mémoire mémoire, String section)
{
String identifiant = mémoire.getSection().substring(0, 3);
String année = Integer.toString(mémoire.getAnnée());// transforme l'année en String
identifiant += année.substring (année.length()-2, année.length());
String nom = mémoire.getNom().toUpperCase();
if (nom.length() < 3)
{
if ( nom.length() == 2) identifiant += nom+"_";
else identifiant+= nom+"__" ;
}
else identifiant+= nom.substring(0,3);
return identifiant.toUpperCase();
}
private void agrandirTableau()
{
MAX *= 2;
Mémoire [] registre2 = new Mémoire [MAX];
for (int i = 0; i< tailleLogique; i++)
registre2 [i] = registre[i];
registre = registre2;
} |
Partager