Advertisement
Guest User

Main.java

a guest
Mar 1st, 2015
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.01 KB | None | 0 0
  1. package inputCodFisc;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6.  
  7.  
  8. public class Main {
  9.  
  10.     public static void main(String[] args) {
  11.         String nome,cognome,data,sesso,comune;
  12.        
  13.         BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  14.        
  15.         //--------------------------------------INPUT NOME
  16.         show("Nome: ");
  17.         nome="";
  18.        
  19.         try {
  20.             //leggo la riga
  21.             String tempNome = in.readLine();
  22.            
  23.             //tolgo eventuali spazi
  24.             String[] nomeParts=tempNome.split(" ");
  25.             for(String x:nomeParts){
  26.                 nome+=x;
  27.             }
  28.            
  29.             //tolgo eventuali accenti
  30.             nome.toLowerCase();
  31.             nome = nome.replaceAll("[à]","a");
  32.             nome = nome.replaceAll("[èé]","e");
  33.             nome = nome.replaceAll("[ì]","i");
  34.             nome = nome.replaceAll("[ò]","o");
  35.             nome = nome.replaceAll("[ù]","u");
  36.            
  37.             //controllo se il nome è composto solo da lettere
  38.             for (char x : nome.toCharArray()) {
  39.                 if(!Character.isLetter(x)) {
  40.                     show(x+" non è una lettera valida");
  41.                     System.exit(1);
  42.                 }
  43.             }  
  44.         } catch (IOException e) {
  45.             e.printStackTrace();
  46.         }
  47.        
  48.         //--------------------------------------INPUT COGNOME
  49.         show("Cognome: ");
  50.         cognome="";
  51.        
  52.         try {
  53.             //leggo la riga
  54.             String tempCognome = in.readLine();
  55.            
  56.             //tolgo eventuali spazi
  57.             String[] cognomeParts=tempCognome.split(" ");
  58.             for(String x:cognomeParts){
  59.                 cognome+=x;
  60.             }
  61.            
  62.             //tolgo eventuali accenti
  63.             cognome.toLowerCase();
  64.             cognome = cognome.replaceAll("[à]","a");
  65.             cognome = cognome.replaceAll("[èé]","e");
  66.             cognome = cognome.replaceAll("[ì]","i");
  67.             cognome = cognome.replaceAll("[ò]","o");
  68.             cognome = cognome.replaceAll("[ù]","u");
  69.            
  70.             //controllo se il cognome è composto solo da lettere
  71.             for (char x : cognome.toCharArray()) {
  72.                 if(!Character.isLetter(x)) {
  73.                     show(x+" non è una lettera valida");
  74.                     System.exit(1);
  75.                 }
  76.             }  
  77.         } catch (IOException e) {
  78.             e.printStackTrace();
  79.         }
  80.        
  81.         //--------------------------------------INPUT DATA
  82.         show("Data di nascita (gg/mm/aaaa): ");
  83.         data="";
  84.        
  85.         try {
  86.             //leggo la riga
  87.             String tempData = in.readLine();
  88.            
  89.             //isolo i numeri
  90.             String[] dataParts=tempData.split("/");
  91.            
  92.             //controllo che l'utente abbia inserito il formato che mi aspetto
  93.             if (dataParts.length!=3){
  94.                 show(tempData+" non è una data");
  95.                 System.exit(1);
  96.             }
  97.            
  98.             String g= dataParts[0];
  99.             String m= dataParts[1];
  100.             String a= dataParts[2];
  101.            
  102.             //valori di default nel caso l'utente tenti di inserire campi vuoti
  103.             if (g.equals("")) g="1";
  104.             if (m.equals("")) m="1";
  105.             if (a.equals("")) a="1950";
  106.            
  107.             if(!isNumero(g)||           //se non è un numero (include il caso di numeri negativi, vedi funzione isNumero)
  108.                     new Integer(g)>31|| //se è maggiore di 31 (non esiste un mese del genere)
  109.                     new Integer(g)==0){ //se è uguale a zero
  110.                 show(g+" non è un numero di giorno valido");
  111.             }
  112.            
  113.             if(!isNumero(m)||
  114.                     new Integer(m)>12||
  115.                     new Integer(m)==0){
  116.                 show(m+" non è un numero di mese valido");
  117.             }
  118.             if(!isNumero(a)||
  119.                     new Integer(a)<1900||
  120.                     new Integer(a)==0){
  121.                 show(a+" non è un numero di anno valido");
  122.             }
  123.             data=g+"/"+m+"/"+a;
  124.         } catch (IOException e) {
  125.             e.printStackTrace();
  126.         }
  127.        
  128.         //--------------------------------------INPUT SESSO
  129.         show("Sesso (m|f): ");
  130.         sesso="";
  131.  
  132.         try {
  133.             String tempSesso = in.readLine();
  134.  
  135.             switch (tempSesso){
  136.                 case "m":
  137.                     sesso="m";
  138.                     break;
  139.                    
  140.                 case "f":
  141.                     sesso="f";
  142.                     break;
  143.                
  144.                 default:
  145.                     show(tempSesso+" non è un valore valido");
  146.                     System.exit(1);
  147.             }
  148.         } catch (IOException e) {
  149.             e.printStackTrace();
  150.         }
  151.  
  152.         //--------------------------------------INPUT COMUNE
  153.         //per limitare i casi di controllo errori costringo l'utente a scegliere tra le opzioni previste dal metodo generaCodiceFiscale
  154.         show("Comune:");
  155.         show("1:Roma");
  156.         show("2:Rimini");
  157.         show("3:Bologna");
  158.         comune="";
  159.        
  160.         try {
  161.             String tempComune = in.readLine();
  162.            
  163.             switch (tempComune){       
  164.                 case "1":
  165.                     comune="roma";
  166.                     break;
  167.                    
  168.                 case "2":
  169.                     comune="rimini";
  170.                     break;
  171.                    
  172.                 case "3":
  173.                     comune="bologna";
  174.                     break;
  175.                
  176.                 default:
  177.                     show(tempComune+" non è un valore valido");
  178.                     System.exit(1);
  179.             }
  180.            
  181.  
  182.         } catch (IOException e) {
  183.             e.printStackTrace();
  184.         }
  185.        
  186.         show(CodiceFiscale.generaCodiceFiscale(new String[]{cognome,nome,data,sesso,comune}));
  187.  
  188.     }
  189.  
  190.     private static void show(String string) {
  191.         System.out.println(string);    
  192.     }
  193.    
  194.     //true se le lettere della string sono tutti numeri (è quindi un numero valido)
  195.     //false se almeno una lettera non è un numero (quindi anche se si tenta di inserire un numero negativo)
  196.     private static boolean isNumero(String string)
  197.     {
  198.         for (char x : string.toCharArray())
  199.         {
  200.             if (!Character.isDigit(x)) return false;
  201.         }
  202.         return true;
  203.     }
  204.  
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement