Advertisement
fcamuso

early static binding e conformità di tipo

Nov 29th, 2020
437
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.92 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Ereditarieta_conto_corrente
  4. {
  5.   class ContoCorrenteException : Exception
  6.   {
  7.     public ContoCorrenteException(string messaggio) { }
  8.   }
  9.  
  10.   class ContoCorrente
  11.   {
  12.     //dati intestatario
  13.     public String Cognome { get; set; } = "";
  14.     public String Nome { get; set; } = "";
  15.  
  16.     //dati conto
  17.     protected double saldo = 0;
  18.     public double Saldo
  19.     {
  20.       get { return saldo; }
  21.       set {
  22.       if (value >= 0)
  23.         saldo = value;
  24.       else throw new ContoCorrenteException("Saldo negativo");
  25.       }  
  26.     }
  27.  
  28.     private int NumeroConto { get; set; }
  29.     public static int ProgressivoNumeroConto { get; set; } = 0;
  30.  
  31.     //operazioni sul conto
  32.    
  33.  
  34.     public void Deposita(double cifra)
  35.     {
  36.       if (cifra > 0)
  37.         saldo += cifra;
  38.       else throw new ContoCorrenteException($"Valore deposito non valido: {cifra}");
  39.     }
  40.  
  41.     public bool Preleva(double cifra)
  42.     {
  43.       if (saldo >= cifra)
  44.       {
  45.         saldo -= cifra;
  46.         return true;
  47.       }
  48.       else return false;
  49.     }
  50.  
  51.     //costruttore
  52.     public ContoCorrente(string cognome, string nome, double saldo_iniziale = 0)
  53.     {
  54.       Cognome = cognome; Nome = nome;
  55.       Saldo= saldo_iniziale;
  56.       NumeroConto = ++ProgressivoNumeroConto;
  57.     }
  58.  
  59.     public bool Autentica(string user, string psw)
  60.     {
  61.       //controllo...
  62.  
  63.       return true;
  64.     }
  65.   }
  66.  
  67.   class ContoCorrentePrivilegiato : ContoCorrente
  68.   {
  69.     //protected new double saldo;
  70.  
  71.     private double tolleranza = 0;
  72.     public double Tolleranza {
  73.       get { return tolleranza; }
  74.  
  75.       set {
  76.         if (value >= 0) tolleranza = value; else throw new ContoCorrenteException("Tolleranza negativa");
  77.  
  78.       }
  79.     }
  80.  
  81.     public ContoCorrentePrivilegiato(string Cognome, string Nome, double tolleranza, double saldo_iniziale = 0)
  82.       : base(Cognome, Nome, saldo_iniziale)
  83.     {
  84.       Tolleranza = tolleranza;
  85.  
  86.     }
  87.  
  88.     public new bool Preleva(double cifra)
  89.     {
  90.  
  91.       if (cifra - saldo <= tolleranza)
  92.       {
  93.         saldo -= cifra;
  94.         return true;
  95.       }
  96.       else return false;
  97.  
  98.     }
  99.  
  100.     public new bool Autentica(string user, string psw)
  101.     {
  102.       if (base.Autentica(user, psw))
  103.         ;      //si prosegue con il controllo basato su pin e cellulare
  104.  
  105.       return true;
  106.     }
  107.   }
  108.  
  109.   class Program
  110.   {
  111.     static void Main(string[] args)
  112.     {
  113.       //conformità di tipo
  114.       ContoCorrente cc = new ContoCorrentePrivilegiato("a", "a", 500, 30000);
  115.  
  116.       //Random r = new Random();
  117.  
  118.       //if (r.Next() % 2 == 0)
  119.       //  cc = new ContoCorrente("a", "a", 30000);
  120.       //else
  121.       //  cc = new ContoCorrentePrivilegiato("a", "a", 500, 30000);
  122.  
  123.       ContoCorrentePrivilegiato ccp = (ContoCorrentePrivilegiato)cc;
  124.  
  125.       Console.WriteLine( ccp.Preleva(30400) );
  126.  
  127.       ccp = (ContoCorrentePrivilegiato) new ContoCorrente("b", "b", 30000);
  128.  
  129.      
  130.  
  131.  
  132.     }
  133.   }
  134. }
  135.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement