Advertisement
fcamuso

C# protected

Nov 22nd, 2020
574
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.52 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.     private int NumeroConto { get; set; }
  19.     public static int ProgressivoNumeroConto { get; set; } = 0;
  20.  
  21.     //operazioni sul conto
  22.     public void SetSaldo(double nuovoSaldo)
  23.     {
  24.       if (nuovoSaldo >= 0)
  25.         saldo = nuovoSaldo;
  26.       else throw new ContoCorrenteException("Saldo negativo");
  27.     }
  28.  
  29.     public void Deposita(double cifra)
  30.     {
  31.       if (cifra > 0)
  32.         saldo += cifra;
  33.       else throw new ContoCorrenteException($"Valore deposito non valido: {cifra}");
  34.     }
  35.  
  36.     public bool Preleva(double cifra)
  37.     {
  38.       if (saldo >= cifra)
  39.       {
  40.         saldo -= cifra;
  41.         return true;
  42.       }
  43.       else return false;
  44.     }
  45.  
  46.     //costruttore
  47.     public ContoCorrente(string cognome, string nome, double saldo_iniziale = 0)
  48.     {
  49.       Cognome = cognome; Nome = nome;
  50.       SetSaldo(saldo_iniziale);
  51.       NumeroConto = ++ProgressivoNumeroConto;
  52.     }
  53.   }
  54.  
  55.   class ContoCorrentePrivilegiato : ContoCorrente
  56.   {
  57.     private double tolleranza = 0;
  58.     public double Tolleranza {
  59.       get { return tolleranza; }
  60.  
  61.       set {
  62.         if (value >= 0) tolleranza = value; else throw new ContoCorrenteException("Tolleranza negativa");
  63.  
  64.       }
  65.     }
  66.  
  67.     public ContoCorrentePrivilegiato(string Cognome, string Nome, double tolleranza, double saldo_iniziale = 0)
  68.       : base(Cognome, Nome)
  69.     {
  70.       Tolleranza = tolleranza;
  71.  
  72.     }
  73.  
  74.     public bool Preleva(double cifra)
  75.     {
  76.  
  77.       if (cifra - saldo <= tolleranza)
  78.       {
  79.         saldo -= cifra;
  80.         return true;
  81.       }
  82.       else return false;
  83.  
  84.     }
  85.   }
  86.  
  87.   class Program
  88.   {
  89.     static void Main(string[] args)
  90.     {
  91.      
  92.       //ContoCorrente c1 = new ContoCorrente("Gastone", "Paperone", 100);
  93.       //c1.Deposita(-9);
  94.       //Figlia f = new Figlia();
  95.  
  96.       //if (c1.Preleva(101))
  97.       //  Console.WriteLine("ok");
  98.       //else
  99.       //  Console.WriteLine("Non puoi andare in rosso!");
  100.  
  101.       ContoCorrentePrivilegiato cpriv1 =
  102.         new ContoCorrentePrivilegiato("super", "boss", 5000, 1000000);
  103.  
  104.       cpriv1.Deposita(1000);
  105.       Console.WriteLine(cpriv1.Preleva(5500));
  106.  
  107.       cpriv1.Preleva(10000);
  108.        
  109.     }
  110.   }
  111. }
  112.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement