Advertisement
nicoladc89

Untitled

Oct 29th, 2014
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.03 KB | None | 0 0
  1. public class Data {
  2.  
  3.     int giorno, mese, anno;
  4.    
  5.     /**
  6.      * Crea nuovo oggetto data, eseguendo alcuni controlli sulla correttezza
  7.      * dell'input.
  8.      * @param giorno
  9.      * @param mese
  10.      * @param anno
  11.      */
  12.     public Data(int giorno, int mese, int anno) {
  13.         if (0 > giorno || anno < 0 || 0 > mese) {
  14.             System.err.println("Non sono ammessi valori negativi");
  15.             System.exit(1);
  16.         }
  17.         switch (mese) {
  18.  
  19.         case 1:
  20.             case 3:
  21.             case 5:
  22.             case 7:
  23.             case 8:
  24.             case 10:
  25.             case 12:{
  26.                 if (giorno > 31) {
  27.                     System.err.println("Questo mese ha 31 giorni");
  28.                     System.exit(1);
  29.                 }
  30.                 break;
  31.             }
  32.             case 4:
  33.             case 6:
  34.             case 9:
  35.             case 11: {
  36.                 if (giorno > 30) {
  37.                     System.err.println("Questo mese ha 30 giorni");
  38.                     System.exit(1);
  39.                 }
  40.                 break;
  41.             }
  42.             case 2: {
  43.                 if ((anno % 4 == 0 && anno % 100 != 0) || anno % 400 == 0) {
  44.                     if (giorno > 29) {
  45.                         System.err.println("Febbraio non può avere più di 29 giorni");
  46.                         System.exit(1);
  47.                     }
  48.                 } else {
  49.                     if (giorno > 28) {
  50.                         System.err.println("Febbraio non può avere più di 28 giorni");
  51.                         System.exit(1);
  52.                     }
  53.                 }
  54.         break;
  55.             }
  56.             default: {
  57.                 System.err.println("I mesi sono 12 (da 1 a 12)");
  58.                 System.exit(1);
  59.             }
  60.         }
  61.         this.giorno = giorno;
  62.         this.mese = mese;
  63.         this.anno = anno;
  64.     }
  65.  
  66.     /**
  67.      * Confronta this con una data in ingresso d e restituisce un numero negativo
  68.      * se this è precedente a d, positivo se this è successiva a d e 0 se sono
  69.      * uguali
  70.      * @param d Data in ingresso
  71.      * @return <ul> <li> intero < di 0 se this è precedente a d </li>
  72.      * <li>intero > di 0 se this è successivo a d </li>
  73.      * <li>0 se le due date sono uguali</li></ul>
  74.      */
  75.     public int compareTo(Data d) {
  76.         int compare = this.anno-d.anno;
  77.         if (compare != 0) {
  78.             return compare;
  79.         }
  80.         compare = this.mese - d.mese;
  81.         if (compare != 0) {
  82.             return compare;
  83.         }
  84.         return this.giorno - d.giorno;
  85.     }
  86.  
  87.     public static void main(String args[]) {
  88.         Data d1, d2;
  89.     //input giorno, mese, anno e inizializzazione d1 e d2 (es. d1 = new Data(giorno, mese, anno) )
  90.         int risComparazione = d1.compareTo(d2);
  91.         if(risComparazione < 0) System.out.println("d1 è precedente a d2");
  92.         else{
  93.             if(risComparazione > 0) System.out.println("d1 è successivo a d2");
  94.             else System.out.println("Le due date sono uguali");
  95.         }
  96.     }
  97.  
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement