Advertisement
fcamuso

early static binding e conformità di tipo, parte B

Dec 5th, 2020
439
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.24 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 virtual 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 virtual 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.      
  114.       int quantiConti = 10;
  115.       ContoCorrente[] listaConti = new ContoCorrente[quantiConti];
  116.       for (int i = 0; i < quantiConti; i++)
  117.         listaConti[i] = (i % 3 == 0) ?
  118.             new ContoCorrentePrivilegiato($"cognome{i}", $"nome{i}", 500, 10000) :
  119.             new ContoCorrente($"cognome{i}", $"nome{i}", 10000);
  120.  
  121.  
  122.       for(int i = 0; i < quantiConti; i++)
  123.         Console.WriteLine($"Conto {i} esito operazione: {listaConti[i].Preleva(10001)}");
  124.      
  125.      
  126.       //for (int i = 0; i < quantiConti; i++)
  127.       //  if (listaConti[i].GetType() == typeof(ContoCorrente))
  128.       //    Console.WriteLine($"Conto {i} esito operazione: {listaConti[i].Preleva(10001)}");
  129.       //  else
  130.       //    Console.WriteLine($"Conto {i} esito operazione: { ((ContoCorrentePrivilegiato) listaConti[i]).Preleva(10001)}")
  131.  
  132.     }
  133.   }
  134. }
  135.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement