First classes and C.F.
This commit is contained in:
parent
a456477d12
commit
94373bb1b9
10 changed files with 83139 additions and 0 deletions
9002
FileEsercizio/IDpersone.json
Normal file
9002
FileEsercizio/IDpersone.json
Normal file
File diff suppressed because it is too large
Load diff
9003
FileEsercizio/IDpersone.xml
Normal file
9003
FileEsercizio/IDpersone.xml
Normal file
File diff suppressed because it is too large
Load diff
32370
FileEsercizio/comuni.json
Normal file
32370
FileEsercizio/comuni.json
Normal file
File diff suppressed because it is too large
Load diff
32371
FileEsercizio/comuni.xml
Normal file
32371
FileEsercizio/comuni.xml
Normal file
File diff suppressed because it is too large
Load diff
27
src/it/unibs/fp/patenteelibbrettopregoh/Comune.java
Normal file
27
src/it/unibs/fp/patenteelibbrettopregoh/Comune.java
Normal file
|
@ -0,0 +1,27 @@
|
|||
package it.unibs.fp.patenteelibbrettopregoh;
|
||||
|
||||
public class Comune {
|
||||
private final String nome;
|
||||
private final String codice;
|
||||
|
||||
public Comune(String nome, String codice) {
|
||||
this.nome = nome;
|
||||
this.codice = codice;
|
||||
}
|
||||
|
||||
public String getNome() {
|
||||
return nome;
|
||||
}
|
||||
|
||||
public String getCodice() {
|
||||
return codice;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Comune{" +
|
||||
"nome='" + nome + '\'' +
|
||||
", codice='" + codice + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
33
src/it/unibs/fp/patenteelibbrettopregoh/Comuni.java
Normal file
33
src/it/unibs/fp/patenteelibbrettopregoh/Comuni.java
Normal file
|
@ -0,0 +1,33 @@
|
|||
package it.unibs.fp.patenteelibbrettopregoh;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Comuni {
|
||||
private final ArrayList<Comune> comuni;
|
||||
|
||||
public Comuni() {
|
||||
this.comuni = new ArrayList<>();
|
||||
}
|
||||
|
||||
public void addComune(Comune comune) {
|
||||
this.comuni.add(comune);
|
||||
}
|
||||
|
||||
public void removeComune(Comune comune) {
|
||||
this.comuni.remove(comune);
|
||||
}
|
||||
|
||||
public ArrayList<Comune> getComuni() {
|
||||
return comuni;
|
||||
}
|
||||
|
||||
public String getCodiceComune(String nomeComune) {
|
||||
for (Comune comune : comuni) {
|
||||
if (comune.getNome().equalsIgnoreCase(nomeComune)) {
|
||||
return comune.getCodice();
|
||||
}
|
||||
}
|
||||
return null; // Comune non trovato
|
||||
}
|
||||
|
||||
}
|
5
src/it/unibs/fp/patenteelibbrettopregoh/Formato.java
Normal file
5
src/it/unibs/fp/patenteelibbrettopregoh/Formato.java
Normal file
|
@ -0,0 +1,5 @@
|
|||
package it.unibs.fp.patenteelibbrettopregoh;
|
||||
|
||||
public enum Formato {
|
||||
JSON, XML;
|
||||
}
|
77
src/it/unibs/fp/patenteelibbrettopregoh/Parser.java
Normal file
77
src/it/unibs/fp/patenteelibbrettopregoh/Parser.java
Normal file
|
@ -0,0 +1,77 @@
|
|||
package it.unibs.fp.patenteelibbrettopregoh;
|
||||
|
||||
import javax.xml.stream.*;
|
||||
import java.io.*;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Parser {
|
||||
public static ArrayList<Persona> parsePersone(Formato formato, String fileName) {
|
||||
ArrayList<Persona> persone = new ArrayList<>();
|
||||
// Implement parsing logic here based on the format
|
||||
// For example, if formato is JSON, use a JSON library to parse the file
|
||||
// and populate the persone list.
|
||||
return persone;
|
||||
}
|
||||
|
||||
public static Comuni parseComuni(Formato formato, String fileName) {
|
||||
Comuni comuni = new Comuni();
|
||||
if (formato == Formato.XML) {
|
||||
XMLInputFactory xmlif;
|
||||
XMLStreamReader xmlr;
|
||||
String currentElement = null;
|
||||
String nome = null;
|
||||
String codice = null;
|
||||
|
||||
try (FileInputStream reader = new FileInputStream(fileName)) {
|
||||
xmlif = XMLInputFactory.newInstance();
|
||||
xmlr = xmlif.createXMLStreamReader(fileName, reader);
|
||||
|
||||
while (xmlr.hasNext()) {
|
||||
int event = xmlr.getEventType();
|
||||
|
||||
switch (event) {
|
||||
|
||||
case XMLStreamConstants.START_ELEMENT:
|
||||
currentElement = xmlr.getLocalName(); // <- sposta qui, nel posto giusto
|
||||
break;
|
||||
|
||||
|
||||
case XMLStreamConstants.CHARACTERS:
|
||||
String text = xmlr.getText().trim();
|
||||
if (!text.isEmpty() && currentElement != null) {
|
||||
if (currentElement.equals("nome")) {
|
||||
nome = text;
|
||||
} else if (currentElement.equals("codice")) {
|
||||
codice = text;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case XMLStreamConstants.END_ELEMENT:
|
||||
String endTag = xmlr.getLocalName();
|
||||
if (endTag.equals("comune")) {
|
||||
Comune comune = new Comune(nome, codice);
|
||||
comuni.addComune(comune);
|
||||
|
||||
nome = null;
|
||||
codice = null;
|
||||
}
|
||||
currentElement = null; // reset solo alla fine di un elemento
|
||||
break;
|
||||
}
|
||||
|
||||
xmlr.next(); // importante: avanza solo una volta per ciclo
|
||||
}
|
||||
|
||||
xmlr.close();
|
||||
} catch (FactoryConfigurationError | XMLStreamException | IOException e) {
|
||||
System.out.println("Error reading the file: " + e.getMessage());
|
||||
}
|
||||
|
||||
} else if (formato == Formato.JSON) {
|
||||
// Implement XML parsing logic here
|
||||
// For example, use an XML library to read the file and populate the comuni list
|
||||
}
|
||||
return comuni;
|
||||
}
|
||||
}
|
|
@ -1,7 +1,14 @@
|
|||
package it.unibs.fp.patenteelibbrettopregoh;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class PatenteELibbrettoPregohMain {
|
||||
public static void main(String[] args) {
|
||||
System.out.println("PatenteELibbrettoPregoh!");
|
||||
|
||||
Comuni comuni = Parser.parseComuni(Formato.XML, "FileEsercizio/comuni.xml");
|
||||
//ArrayList<Persona> persone = Parser.parsePersone(Formato.JSON, "persone.txt");
|
||||
Persona p = new Persona("Mario", "Rossi", "M", "1980-01-01", "Roma", "", "2005-01-15");
|
||||
System.out.println(p.calcolaCodiceFiscale(comuni));
|
||||
}
|
||||
}
|
||||
|
|
244
src/it/unibs/fp/patenteelibbrettopregoh/Persona.java
Normal file
244
src/it/unibs/fp/patenteelibbrettopregoh/Persona.java
Normal file
|
@ -0,0 +1,244 @@
|
|||
package it.unibs.fp.patenteelibbrettopregoh;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Persona {
|
||||
private final String nome;
|
||||
private final String cognome;
|
||||
private final String sesso;
|
||||
private final String dataNascita;
|
||||
private final String comuneNascita;
|
||||
private final String codiceFiscale;
|
||||
private final String scadenzaDocumento;
|
||||
|
||||
public Persona(String nome, String cognome, String sesso, String dataNascita, String comuneNascita, String codiceFiscale, String scadenzaDocumento) {
|
||||
this.nome = nome;
|
||||
this.cognome = cognome;
|
||||
this.sesso = sesso;
|
||||
this.dataNascita = dataNascita;
|
||||
this.comuneNascita = comuneNascita;
|
||||
this.codiceFiscale = codiceFiscale;
|
||||
this.scadenzaDocumento = scadenzaDocumento;
|
||||
}
|
||||
|
||||
public String getNome() {
|
||||
return nome;
|
||||
}
|
||||
|
||||
public String getCognome() {
|
||||
return cognome;
|
||||
}
|
||||
|
||||
public String getSesso() {
|
||||
return sesso;
|
||||
}
|
||||
|
||||
public String getDataNascita() {
|
||||
return dataNascita;
|
||||
}
|
||||
|
||||
public String getComuneNascita() {
|
||||
return comuneNascita;
|
||||
}
|
||||
|
||||
public String getCodiceFiscale() {
|
||||
return codiceFiscale;
|
||||
}
|
||||
|
||||
public String getScadenzaDocumento() {
|
||||
return scadenzaDocumento;
|
||||
}
|
||||
|
||||
public boolean isCodiceFiscaleValido(Comuni comuni) {
|
||||
return codiceFiscale.equals(calcolaCodiceFiscale(comuni));
|
||||
}
|
||||
|
||||
public String calcolaCodiceFiscale(Comuni comuni) {
|
||||
ArrayList<String> consonantiCognome = getCaratteri(cognome, true);
|
||||
ArrayList<String> consonantiNome = getCaratteri(nome, true);
|
||||
StringBuilder codiceFiscaleCalcolato = new StringBuilder();
|
||||
|
||||
int stillMissing = 3 - consonantiCognome.size();
|
||||
if (stillMissing <= 0) {
|
||||
for (int i = 0; i < 3; i++) {
|
||||
codiceFiscaleCalcolato.append(consonantiCognome.get(i));
|
||||
}
|
||||
} else {
|
||||
ArrayList<String> vocaliCognome = getCaratteri(cognome, false);
|
||||
for (String consonante : consonantiCognome) {
|
||||
codiceFiscaleCalcolato.append(consonante);
|
||||
}
|
||||
|
||||
for (int i = 0; i < stillMissing; i++) {
|
||||
if (i < vocaliCognome.size()) {
|
||||
codiceFiscaleCalcolato.append(vocaliCognome.get(i));
|
||||
} else {
|
||||
codiceFiscaleCalcolato.append("X");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (consonantiNome.size() > 3){
|
||||
codiceFiscaleCalcolato.append(consonantiNome.get(0));
|
||||
codiceFiscaleCalcolato.append(consonantiNome.get(2));
|
||||
codiceFiscaleCalcolato.append(consonantiNome.get(3));
|
||||
} else if (consonantiNome.size() == 3){
|
||||
for (String consonante : consonantiNome) {
|
||||
codiceFiscaleCalcolato.append(consonante);
|
||||
}
|
||||
} else {
|
||||
stillMissing = 3 - consonantiNome.size();
|
||||
ArrayList<String> vocaliNome = getCaratteri(nome, false);
|
||||
for (String consonante : consonantiNome) {
|
||||
codiceFiscaleCalcolato.append(consonante);
|
||||
}
|
||||
if (vocaliNome.size() >= stillMissing){
|
||||
for (int i = 0; i < stillMissing; i++) {
|
||||
codiceFiscaleCalcolato.append(vocaliNome.get(i));
|
||||
}
|
||||
} else {
|
||||
stillMissing = stillMissing - vocaliNome.size();
|
||||
for (String vocali : vocaliNome) {
|
||||
codiceFiscaleCalcolato.append(vocali);
|
||||
}
|
||||
|
||||
for (int i = 0; i < stillMissing; i++) {
|
||||
codiceFiscaleCalcolato.append("X");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
codiceFiscaleCalcolato.append(dataNascita, 2, 4);
|
||||
;
|
||||
int mese = Integer.parseInt(dataNascita.substring(5, 7));
|
||||
|
||||
codiceFiscaleCalcolato.append(getLetteraMese(mese));
|
||||
|
||||
int giorno = Integer.parseInt(dataNascita.substring(8, 10));
|
||||
|
||||
if (sesso.equalsIgnoreCase("F")) {
|
||||
giorno += 40;
|
||||
}
|
||||
|
||||
if (giorno < 10) {
|
||||
codiceFiscaleCalcolato.append("0");
|
||||
}
|
||||
|
||||
codiceFiscaleCalcolato.append(giorno);
|
||||
|
||||
String codiceComune = comuni.getCodiceComune(comuneNascita);
|
||||
|
||||
codiceFiscaleCalcolato.append(codiceComune);
|
||||
|
||||
Character carattereDiControllo;
|
||||
|
||||
int somma = 0;
|
||||
|
||||
for (int i = 0; i < codiceFiscaleCalcolato.length(); i++) {
|
||||
char c = codiceFiscaleCalcolato.charAt(i);
|
||||
somma += getValueOf(c, (i + 1)%2 == 0);
|
||||
}
|
||||
|
||||
int resto = somma % 26;
|
||||
|
||||
carattereDiControllo = (char) ('A' + resto);
|
||||
|
||||
codiceFiscaleCalcolato.append(carattereDiControllo);
|
||||
|
||||
|
||||
return codiceFiscaleCalcolato.toString();
|
||||
}
|
||||
|
||||
private int getValueOf(char c, boolean b) {
|
||||
if (!b) {
|
||||
return switch (c) {
|
||||
case '0', 'A' -> 1;
|
||||
case '1', 'B' -> 0;
|
||||
case '2', 'C' -> 5;
|
||||
case '3', 'D' -> 7;
|
||||
case '4', 'E' -> 9;
|
||||
case '5', 'F' -> 13;
|
||||
case '6', 'G' -> 15;
|
||||
case '7', 'H' -> 17;
|
||||
case '8', 'I' -> 19;
|
||||
case '9', 'J' -> 21;
|
||||
case 'K' -> 2;
|
||||
case 'L' -> 4;
|
||||
case 'M' -> 18;
|
||||
case 'N' -> 20;
|
||||
case 'O' -> 11;
|
||||
case 'P' -> 3;
|
||||
case 'Q' -> 6;
|
||||
case 'R' -> 8;
|
||||
case 'S' -> 12;
|
||||
case 'T' -> 14;
|
||||
case 'U' -> 16;
|
||||
case 'V' -> 10;
|
||||
case 'W' -> 22;
|
||||
case 'X' -> 25;
|
||||
case 'Y' -> 24;
|
||||
case 'Z' -> 23;
|
||||
default -> -1; // Invalid character
|
||||
};
|
||||
} else {
|
||||
return switch (c) {
|
||||
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' -> c - '0';
|
||||
case 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' -> c - 'A';
|
||||
default -> -1; // Invalid character
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
private String getLetteraMese(int mese) {
|
||||
return switch (mese) {
|
||||
case 1 -> "A";
|
||||
case 2 -> "B";
|
||||
case 3 -> "C";
|
||||
case 4 -> "D";
|
||||
case 5 -> "E";
|
||||
case 6 -> "H";
|
||||
case 7 -> "L";
|
||||
case 8 -> "M";
|
||||
case 9 -> "P";
|
||||
case 10 -> "R";
|
||||
case 11 -> "S";
|
||||
case 12 -> "T";
|
||||
default -> "";
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
public boolean isScadenzaDocumentoValido(){
|
||||
// Implement logic to validate the expiration date of the document
|
||||
return false;
|
||||
}
|
||||
|
||||
private static boolean isVocale(char c) {
|
||||
c = Character.toLowerCase(c);
|
||||
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
|
||||
}
|
||||
|
||||
public static boolean isConsonante(char c) {
|
||||
return Character.isLetter(c) && !isVocale(c);
|
||||
}
|
||||
|
||||
private ArrayList<String> getCaratteri(String s, boolean tipo){
|
||||
ArrayList<String> ret = new ArrayList<>();
|
||||
if (tipo){ // consonanti
|
||||
for (char c : s.toCharArray()) {
|
||||
if (isConsonante(c)) {
|
||||
ret.add(String.valueOf(c).toUpperCase());
|
||||
}
|
||||
}
|
||||
} else { // vocali
|
||||
for (char c : s.toCharArray()) {
|
||||
if (isVocale(c)) {
|
||||
ret.add(String.valueOf(c).toUpperCase());
|
||||
}
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue