Advertisement
LePetitGlacon

FICHE 8 POO excercice (0) exercice 3

Jan 7th, 2020
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.01 KB | None | 0 0
  1. using System;
  2.  
  3. namespace objet
  4. {
  5.     class Program
  6.     {
  7.         public static void Main(string[] args)
  8.         {
  9.             Compte cpt1 = new Compte("Aubry", 1);
  10.             Console.WriteLine("CPT1 Création :");
  11.             Console.WriteLine(cpt1.Afficher());
  12.             cpt1.Crediter(1000);
  13.             Console.WriteLine("CPT1 Crédit 1000 :");
  14.             Console.WriteLine(cpt1.Afficher());
  15.            
  16.             bool bOpération1 = cpt1.Debiter(500);
  17.             Console.WriteLine("CPT1 Débit 500 : " + bOpération1);
  18.             Console.WriteLine(cpt1.Afficher());
  19.            
  20.             bool bOpération2 = cpt1.Debiter(250);
  21.             Console.WriteLine("CPT1 Débit 250 : " + bOpération2);
  22.             Console.WriteLine(cpt1.Afficher());
  23.            
  24.             bool bOpération3 = cpt1.Debiter(350);
  25.             Console.WriteLine("CPT1 Débit 350 : " + bOpération3);
  26.             Console.WriteLine(cpt1.Afficher());
  27.            
  28.             Compte cpt2 = new Compte("David", 2);
  29.             Console.WriteLine("CPT2 Création :");
  30.             Console.WriteLine(cpt2.Afficher());
  31.             cpt2.Crediter(1000);
  32.             Console.WriteLine("CPT2 Crédit 1000 :");
  33.             Console.WriteLine(cpt2.Afficher());
  34.            
  35.             bool bOpération4 = cpt2.Debiter(cpt1, 500);
  36.             Console.WriteLine("CPT2 Débit 500 pour CPT1 " + bOpération4);
  37.             Console.WriteLine(cpt1.Afficher());
  38.             Console.WriteLine(cpt2.Afficher());
  39.            
  40.             bool bOpération5 = cpt2.Debiter(cpt1, 700);
  41.             Console.WriteLine("CPT2 Débit 700 pour CPT1 " + bOpération5);
  42.             Console.WriteLine(cpt1.Afficher());
  43.             Console.WriteLine(cpt2.Afficher());
  44.            
  45.             Console.ReadKey();
  46.         }
  47.     }
  48. }  
  49.     /// <summary>
  50.     /// La classe compte
  51.     /// </summary>
  52.     class Compte
  53.     {
  54.         private int numero;
  55.         private double solde;
  56.         private string proprietaire;
  57.        
  58.  
  59.         public int GetNumero()
  60.         {
  61.             return 0;
  62.         }
  63.  
  64.         public double GetSolde()
  65.         {
  66.             return 0;
  67.         }
  68.  
  69.         public string GetProprietaire()
  70.         {
  71.             return "";
  72.            
  73.         }
  74.  
  75.         public Compte(string paramProprietaire, int parmaNum)
  76.         {
  77.             numero = parmaNum;
  78.             proprietaire = paramProprietaire;
  79.             solde = 0;
  80.         }
  81.  
  82.         /// <summary>
  83.         /// Ajoute la somme indiquée sur le compte
  84.         /// </summary>
  85.         /// <param name="somme"></param>
  86.         /// <returns></returns>
  87.         public void Crediter(double somme)
  88.         {
  89.             solde = solde + somme;
  90.         }
  91.  
  92.  
  93.         /// <summary>
  94.         /// Retire (débite) la somme indiquée si possible du compte. Si le montant n'est pas possible, cette fonction return false et aucune somme n'est débitée.
  95.         /// </summary>
  96.         /// <param name="somme"></param>
  97.         /// <returns>booléen à vrai si la somme a été débitée</returns>
  98.         public bool Debiter(double somme)
  99.         {
  100.             if(solde - somme > 0)
  101.             {
  102.                 solde = solde - somme;
  103.                 return true;
  104.             }
  105.             else
  106.             {
  107.                 return false;
  108.             }
  109.                
  110.         }
  111.        
  112.         /// <summary>
  113.         /// Surchage de débiter avec précision du compte crédité. Dédite le compte actuel en créditant le compte indiqué. Attention, le crédit n'est possible que si le compte a pu être débité.
  114.         /// </summary>
  115.         /// <param name="c"></param>
  116.         /// <param name="somme"></param>
  117.         /// <returns></returns>
  118.         public bool Debiter(Compte c, double somme)                    
  119.         {
  120.             if(solde > somme)
  121.                {
  122.                 solde = solde - somme;
  123.                 c.solde = c.solde + somme;
  124.                 return true;
  125.                }
  126.                 return false;
  127.         }
  128.  
  129.        
  130.  
  131.         /// <summary>
  132.         /// Méthode retournant une chaîne contenant les informations de ce compte
  133.         /// </summary>
  134.         /// <returns></returns>
  135.         public string Afficher()
  136.         {
  137.             string str = "";
  138.             str += "Numéro : " + numero + "\n";
  139.             str += "Propriétaire : " + proprietaire + "\n";
  140.             str += "Solde : " + solde + "\n";
  141.  
  142.             return str;
  143.         }
  144.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement