Advertisement
fcamuso

Untitled

Aug 3rd, 2020
492
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.25 KB | None | 0 0
  1. using System;
  2.  
  3. namespace oop_2_properties
  4. {
  5.  
  6.  
  7.   class Data
  8.   {// membri interni
  9.  
  10.     //field
  11.     private byte g = 1;
  12.     private byte m = 1;
  13.     private int a = 1;
  14.  
  15.     public int G
  16.     {
  17.       get { return g; }
  18.  
  19.       set {
  20.         if (value <= GiorniMese() && value>0)
  21.           g = (byte)value;
  22.         else
  23.           throw new InvalidOperationException();
  24.           }
  25.     }
  26.  
  27.    
  28.  
  29.     //metodi
  30.     bool Bisestile(int anno)
  31.     {
  32.       return anno % 400 == 0 || (anno % 4 == 0 && anno % 100 != 0);
  33.     }
  34.  
  35.    
  36.     int GiorniMese() //parametri formali
  37.     {
  38.       switch (m)
  39.       {
  40.         case 4:
  41.         case 6:
  42.         case 9:
  43.         case 11:
  44.           return 30;
  45.  
  46.         case 2:
  47.           return 28 + (a % 400 == 0 || (a % 4 == 0 && a % 100 != 0) ? 1 : 0);
  48.  
  49.         default:
  50.           return 31;
  51.       }
  52.     }
  53.  
  54.   }
  55.  
  56.   class Program
  57.   {    
  58.    
  59.     static void Main(string[] args)
  60.     {
  61.       Data d1 = new Data();
  62.       Console.WriteLine(d1.G);
  63.       d1.G = 23;
  64.       Console.WriteLine(d1.G);
  65.  
  66.       try
  67.       {
  68.         d1.G = 45;
  69.       }
  70.       catch (InvalidOperationException eccezione)
  71.       {
  72.         Console.WriteLine("Giorno non valido, data NON modificata ...");
  73.       }
  74.  
  75.     }
  76.   }
  77. }
  78.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement