Advertisement
fcamuso

Untitled

Aug 10th, 2020
546
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.55 KB | None | 0 0
  1. using System;
  2.  
  3. namespace OOP_4_Costruttori_1
  4. {
  5.   class Data
  6.   {// membri interni
  7.  
  8.     //field
  9.     private int g;
  10.     private int m;
  11.     private int a;
  12.  
  13.     public Data(int _g, int _m, int _a) {
  14.  
  15.       G = _g; M = _m; A = _a;
  16.     }
  17.    
  18.    
  19.     public Data() { }
  20.  
  21.     public int G
  22.     {
  23.       get => g;
  24.       set
  25.       {
  26.         if (value > GiorniMese() || value < 1)
  27.           throw new InvalidOperationException();
  28.         else g = value;
  29.       }
  30.     }
  31.  
  32.     public int M
  33.     {
  34.       get => m;
  35.       set
  36.       {
  37.         if (value > 12 || value < 1)
  38.           throw new InvalidOperationException();
  39.         else m = value;
  40.       }
  41.     }
  42.  
  43.     public int A
  44.     {
  45.       get => a;
  46.       set
  47.       {
  48.         if (value < 1)
  49.           throw new InvalidOperationException();
  50.         else a = value;
  51.       }
  52.     }
  53.  
  54.  
  55.     //metodi
  56.  
  57.     int GiorniMese() //parametri formali
  58.     {
  59.       switch (m)
  60.       {
  61.         case 4:
  62.         case 6:
  63.         case 9:
  64.         case 11:
  65.           return 30;
  66.  
  67.         case 2:
  68.           return 28 + (a % 400 == 0 || (a % 4 == 0 && a % 100 != 0) ? 1 : 0);
  69.  
  70.         default:
  71.           return 31;
  72.       }
  73.     }
  74.  
  75.     public void SettaData(int gg, int mm, int aa )
  76.     {
  77.       G = gg; M = mm; A = aa;
  78.     }
  79.  
  80.   }
  81.   class Program
  82.   {
  83.     static void Main(string[] args)
  84.     {
  85.       Data d = new Data(15,10,10);
  86.  
  87.       //Data d2 = new Data("15-10-20");
  88.      // Data d3 = new Data(15, "Ottobre", "20");
  89.  
  90.       //d.SettaData(15, 10, 10);
  91.  
  92.  
  93.       Console.WriteLine(d.G);
  94.     }
  95.   }
  96. }
  97.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement