Refactor CalcolaPrimaTerna
This commit is contained in:
parent
0b86b0d7a3
commit
e5069081a0
@ -22,7 +22,6 @@ public class GestisciCodiceFiscale {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static String CalcolaPrimaTerna(String cognome) {
|
static String CalcolaPrimaTerna(String cognome) {
|
||||||
boolean exit = false;
|
|
||||||
String ritorno;
|
String ritorno;
|
||||||
for (String carattere : CARATTERI_DA_RIMUOVERE) {
|
for (String carattere : CARATTERI_DA_RIMUOVERE) {
|
||||||
cognome = GestisciStringhe.rimuoviCarattere(cognome, carattere);
|
cognome = GestisciStringhe.rimuoviCarattere(cognome, carattere);
|
||||||
@ -31,71 +30,24 @@ public class GestisciCodiceFiscale {
|
|||||||
cognome = cognome.toLowerCase();
|
cognome = cognome.toLowerCase();
|
||||||
|
|
||||||
char[] caratteriCognome = cognome.toCharArray();
|
char[] caratteriCognome = cognome.toCharArray();
|
||||||
int[] posizioniConsonanti = new int[4];
|
int[] posizioniConsonanti = trovaConsonanti(cognome);
|
||||||
|
|
||||||
for (int j = 0; j < posizioniConsonanti.length; j++) {
|
|
||||||
for (int i = posizioniConsonanti[j]; i < caratteriCognome.length && !exit; i++) {
|
|
||||||
if (GestisciStringhe.isConsonante(caratteriCognome[i])) {
|
|
||||||
exit = true;
|
|
||||||
posizioniConsonanti[j + 1] = i;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (posizioniConsonanti[1] == 0) { // non è stata trovata la prima consonante, ovvero non ce ne sono
|
|
||||||
char primaVocale = 0;
|
|
||||||
int posizionePrimaVocale = 0;
|
|
||||||
|
|
||||||
for (int i = 0; i < caratteriCognome.length && !exit; i++) {
|
|
||||||
if (GestisciStringhe.isVocale(caratteriCognome[i])) {
|
|
||||||
exit = true;
|
|
||||||
primaVocale = caratteriCognome[i];
|
|
||||||
posizionePrimaVocale = i;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
char secondaVocale = 0;
|
|
||||||
|
|
||||||
for (int i = posizionePrimaVocale; i < caratteriCognome.length && !exit; i++) {
|
|
||||||
if (GestisciStringhe.isVocale(caratteriCognome[i])) {
|
|
||||||
exit = true;
|
|
||||||
secondaVocale = caratteriCognome[i];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ritorno = Character.toString(primaVocale)
|
|
||||||
+ Character.toString(secondaVocale)
|
|
||||||
+ Character.toString('x');
|
|
||||||
|
|
||||||
|
if (posizioniConsonanti[0] == 0) { // non è stata trovata la prima consonante, ovvero non ce ne sono
|
||||||
|
ritorno = quintoCasoCognome(caratteriCognome);
|
||||||
} else {
|
} else {
|
||||||
if (posizioniConsonanti[3] == 0) { // non è stata trovata la terza consonante, AKA l'array nell'ultima
|
if (posizioniConsonanti[2] == 0) { // non è stata trovata la terza consonante, AKA l'array nell'ultima
|
||||||
// posizione
|
// posizione
|
||||||
// ha valore di default
|
// ha valore di default
|
||||||
char primaVocale = 0;
|
char primaVocale = trovaPrimaVocale(caratteriCognome)[0];
|
||||||
int posizionePrimaVocale = 0;
|
int posizionePrimaVocale = trovaPrimaVocale(caratteriCognome)[1];
|
||||||
|
if (posizioniConsonanti[1] == 0) { // non è stata trovata la seconda consonante, AKA l'array nella
|
||||||
for (int i = 0; i < caratteriCognome.length && !exit; i++) {
|
|
||||||
if (GestisciStringhe.isVocale(caratteriCognome[i])) {
|
|
||||||
exit = true;
|
|
||||||
primaVocale = caratteriCognome[i];
|
|
||||||
posizionePrimaVocale = i;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (posizioniConsonanti[2] == 0) { // non è stata trovata la seconda consonante, AKA l'array nella
|
|
||||||
// penultima
|
// penultima
|
||||||
// posizione
|
// posizione
|
||||||
// ha valore di default
|
// ha valore di default
|
||||||
|
|
||||||
char secondaVocale = 0;
|
int secondaVocale = trovaSecondaVocale(caratteriCognome, posizionePrimaVocale);
|
||||||
|
|
||||||
for (int i = posizionePrimaVocale; i < caratteriCognome.length && !exit; i++) {
|
if (secondaVocale == -1) {
|
||||||
if (GestisciStringhe.isVocale(caratteriCognome[i])) {
|
|
||||||
exit = true;
|
|
||||||
secondaVocale = caratteriCognome[i];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!exit) {
|
|
||||||
ritorno = Character.toString(caratteriCognome[posizioniConsonanti[1]])
|
ritorno = Character.toString(caratteriCognome[posizioniConsonanti[1]])
|
||||||
+ Character.toString(primaVocale)
|
+ Character.toString(primaVocale)
|
||||||
+ Character.toString('x');
|
+ Character.toString('x');
|
||||||
@ -144,6 +96,70 @@ public class GestisciCodiceFiscale {
|
|||||||
return ritorno;
|
return ritorno;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int[] trovaConsonanti(String cognome) {
|
||||||
|
boolean exit = false;
|
||||||
|
char[] caratteriCognome = cognome.toCharArray();
|
||||||
|
int[] posizioniConsonanti = new int[4];
|
||||||
|
int[] ritorno = new int[3];
|
||||||
|
|
||||||
|
for (int j = 0; j < posizioniConsonanti.length; j++) {
|
||||||
|
for (int i = posizioniConsonanti[j]; i < caratteriCognome.length && !exit; i++) {
|
||||||
|
if (GestisciStringhe.isConsonante(caratteriCognome[i])) {
|
||||||
|
exit = true;
|
||||||
|
posizioniConsonanti[j + 1] = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ritorno[0] = posizioniConsonanti[1];
|
||||||
|
ritorno[1] = posizioniConsonanti[2];
|
||||||
|
ritorno[2] = posizioniConsonanti[3];
|
||||||
|
|
||||||
|
return ritorno;
|
||||||
|
}
|
||||||
|
|
||||||
|
static char[] trovaPrimaVocale(char[] caratteriCognome) {
|
||||||
|
boolean exit = false;
|
||||||
|
char[] ritorno = new char[2];
|
||||||
|
|
||||||
|
for (int i = 0; i < caratteriCognome.length && !exit; i++) {
|
||||||
|
if (GestisciStringhe.isVocale(caratteriCognome[i])) {
|
||||||
|
exit = true;
|
||||||
|
ritorno[0] = caratteriCognome[i];
|
||||||
|
ritorno[1] = (char) i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ritorno;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int trovaSecondaVocale(char[] caratteriCognome, int posizionePrimaVocale) {
|
||||||
|
int ritorno = -1;
|
||||||
|
boolean exit = false;
|
||||||
|
|
||||||
|
for (int i = posizionePrimaVocale; i < caratteriCognome.length && !exit; i++) {
|
||||||
|
if (GestisciStringhe.isVocale(caratteriCognome[i])) {
|
||||||
|
exit = true;
|
||||||
|
ritorno = caratteriCognome[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ritorno;
|
||||||
|
}
|
||||||
|
|
||||||
|
static String quintoCasoCognome(char[] caratteriCognome) {
|
||||||
|
char primaVocale = trovaPrimaVocale(caratteriCognome)[0];
|
||||||
|
int posizionePrimaVocale = trovaPrimaVocale(caratteriCognome)[1];
|
||||||
|
char secondaVocale = (char) trovaSecondaVocale(caratteriCognome, posizionePrimaVocale);
|
||||||
|
String ritorno;
|
||||||
|
|
||||||
|
ritorno = Character.toString(primaVocale)
|
||||||
|
+ Character.toString(secondaVocale)
|
||||||
|
+ Character.toString('x');
|
||||||
|
|
||||||
|
return ritorno;
|
||||||
|
}
|
||||||
|
|
||||||
static String CalcolaSecondaTerna(String nome) {
|
static String CalcolaSecondaTerna(String nome) {
|
||||||
for (String carattere : CARATTERI_DA_RIMUOVERE) {
|
for (String carattere : CARATTERI_DA_RIMUOVERE) {
|
||||||
nome = GestisciStringhe.rimuoviCarattere(nome, carattere);
|
nome = GestisciStringhe.rimuoviCarattere(nome, carattere);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user