Advertisement
fcamuso

Untitled

Aug 14th, 2020
566
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.50 KB | None | 0 0
  1. using System;
  2.  
  3. namespace OOP_4_Costruttori_1
  4. {
  5.   class Data
  6.   {// membri interni
  7.  
  8.     static string[] nomiMese = {"Gennaio", "Febbraio", "Marzo", "Aprile", "Maggio", "Giugno",
  9.                         "Luglio", "Agosto", "Settembre", "Ottobre", "Novembre", "Dicembre"};
  10.    
  11.     //field
  12.     private int g=1;
  13.     private int m=1;
  14.     private int a=1;
  15.  
  16.  
  17.  
  18.     public Data(int _g, int _m, int _a) {
  19.       A = _a; M = _m; G = _g;
  20.     }
  21.    
  22.    
  23.     public Data() : this(1,1,1) { }
  24.  
  25.     static int MeseInt(string mm)
  26.     {
  27.       return Array.IndexOf(nomiMese, Char.ToUpper(mm[0]) + mm.Substring(1).ToLower()) + 1;
  28.     }
  29.  
  30.     public Data(int gg, string mm, int aa) : this(gg, MeseInt(mm), aa) { }
  31.  
  32.     public Data(string laData)
  33.     {
  34.       //accettiamo giorni e mesi a una o due cifre, anno a due o 4 cifre
  35.       //int posTrattino = laData.IndexOf('-');
  36.       //if (posTrattino == -1) throw new InvalidOperationException("Formato data non valido");
  37.       //string g  = laData.Substring(0, posTrattino); //"1O-10-19"
  38.  
  39.       //posTrattino = laData.IndexOf('-', posTrattino + 1);
  40.       //if (posTrattino == -1) throw new InvalidOperationException("Formato data non valido");
  41.       //string m = laData.Substring(g.Length + 1, posTrattino - g.Length - 1); //"10-10-19"
  42.       //string a = laData.Substring(posTrattino + 1, laData.Length - posTrattino - 1); //"10-10-19"
  43.  
  44.       //A = int.Parse(a);
  45.       //M = int.Parse(m);
  46.       //G = int.Parse(g);
  47.  
  48.  
  49.       string[] n = laData.Split('-');
  50.       if (n.Length != 3 || n[0].Length == 0 || n[1].Length == 0 || n[2].Length != 4)
  51.          throw new InvalidOperationException();
  52.       A = int.Parse(n[2]);
  53.       M = int.Parse(n[1]);
  54.       G = int.Parse(n[0]);
  55.     }
  56.    
  57.     public int G
  58.     {
  59.       get => g;
  60.       set
  61.       {
  62.         if (value > GiorniMese() || value < 1)
  63.           throw new InvalidOperationException("Valore giorno non valido");
  64.         else g = value;
  65.       }
  66.     }
  67.  
  68.     public int M
  69.     {
  70.       get => m;
  71.       set
  72.       {
  73.         if (value > 12 || value < 1)
  74.           throw new InvalidOperationException("Valore mese non valido");
  75.         else m = value;
  76.       }
  77.     }
  78.  
  79.     public int A
  80.     {
  81.       get => a;
  82.       set
  83.       {
  84.         if (value < 1)
  85.           throw new InvalidOperationException("L'anno non può essere minore di 1");
  86.         else a = value;
  87.       }
  88.     }
  89.  
  90.  
  91.     //metodi
  92.  
  93.     int GiorniMese() //parametri formali
  94.     {
  95.       switch (m)
  96.       {
  97.         case 4:
  98.         case 6:
  99.         case 9:
  100.         case 11:
  101.           return 30;
  102.  
  103.         case 2:
  104.           return 28 + (a % 400 == 0 || (a % 4 == 0 && a % 100 != 0) ? 1 : 0);
  105.  
  106.         default:
  107.           return 31;
  108.       }
  109.     }
  110.  
  111.     public void SettaData(int gg, int mm, int aa )
  112.     {
  113.       G = gg; M = mm; A = aa;
  114.     }
  115.  
  116.     override public string ToString()
  117.     {
  118.       return $"{G}-{M}-{A}";
  119.     }
  120.  
  121.   }
  122.   class Program
  123.   {
  124.     static void Main(string[] args)
  125.     {
  126.       Data d = new Data(15,10,10);
  127.  
  128.       try
  129.       {
  130.         Data d2 = new Data("29-2-2019");
  131.       }
  132.       catch (InvalidOperationException errore)
  133.       {
  134.         Console.WriteLine(errore.Message);
  135.       }
  136.  
  137.      Data d3 = new Data(15, "", 2020);
  138.      Console.WriteLine(d3);
  139.  
  140.       //d.SettaData(15, 10, 10);
  141.  
  142.  
  143.       //Console.WriteLine(d);
  144.  
  145.       //Cerchio c = new Cerchio(???);
  146.       //Console.WriteLine(c.Area()/Perimetro())
  147.  
  148.       //GeneratorePassword g = new GeneratorePassword(???)
  149.       //string psw = g.Genera(???);
  150.     }
  151.   }
  152. }
  153.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement