Advertisement
LePetitGlacon

FICHE 8 POO exercices 2 exercice 4 Bateau a container

Jan 14th, 2020
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.05 KB | None | 0 0
  1. //class BateauAcontainer
  2. using System;
  3. public class BateauAContainer
  4. {
  5.     private string immatriculation;
  6.     private float poidsVide;
  7.     private int maxContainer;
  8.     private Container[] tabContainer;
  9.     private int nbContainerAct = 0;
  10.  
  11.     public BateauAContainer(string paramImmatriculation, float paramPoidsVide, int paramMaxContainer)
  12.     {
  13.         immatriculation = paramImmatriculation;
  14.         poidsVide = paramPoidsVide;
  15.         maxContainer = paramMaxContainer;
  16.         tabContainer = new Container[paramMaxContainer];
  17.        
  18.     }
  19.    
  20.     /// <summary>
  21.     /// Affiche les informations de ce bateau sur la console
  22.     /// </summary>
  23.     public void AfficherInfo()
  24.     {
  25.         Console.WriteLine("Immatriculation {0}, poid vide {1}, poid total {2}, max container {3}, nb container {4}",immatriculation,poidsVide,this.DonnePoidsTotal(),maxContainer,nbContainerAct);
  26.         for(int i=0;i<tabContainer.Length;i++)
  27.         {
  28.             if(tabContainer[i] != null)
  29.             {
  30.                 tabContainer[i].AfficherInfo();
  31.             }
  32.         }
  33.        
  34.     }
  35.    
  36.     /// <summary>
  37.     /// Si la capacité du bateau le permet : ajoute un container  au tableau des containers  et retourne vrai. Retourne faux sinon.
  38.     /// </summary>
  39.     /// <param name="containerAAjouter">le container à ajouter</param>
  40.     /// <returns>booléen en fonction de l'ajout du container</returns>
  41.     public bool AjouterContainer(Container containerAAjouter)
  42.     {
  43.         if(nbContainerAct >= maxContainer)
  44.         {
  45.             return false;  
  46.         }
  47.         else
  48.         {
  49.             tabContainer[nbContainerAct] = containerAAjouter;
  50.             nbContainerAct = nbContainerAct + 1;
  51.             return true;
  52.         }
  53.     }
  54.    
  55.     /// <summary>
  56.     /// Donne le poids total du bateau avec l'ensemble de ses containers.
  57.     /// </summary>
  58.     /// <returns>Le poids total</returns>
  59.     public float DonnePoidsTotal()
  60.     {
  61.         float poidTotal = 0;
  62.         for(int i= 0;i < tabContainer.Length;i++)
  63.         {
  64.             if(tabContainer[i] == null)
  65.             {
  66.                 poidTotal = poidTotal + 0;
  67.             }
  68.             else
  69.             {
  70.                 poidTotal = poidTotal + tabContainer[i].DonnerPoids();
  71.             }
  72.            
  73.         }
  74.         return poidTotal;
  75.     }
  76. }
  77.  
  78. -------------------
  79. //class Container
  80. using System;
  81. /// <summary>
  82. /// Description of Container.
  83. /// </summary>
  84. public class Container
  85. {
  86.     private float poids;
  87.     private string contenu;
  88.    
  89.     public Container(float paramPoids, string paramContenu)
  90.     {
  91.         poids = paramPoids;
  92.         contenu = paramContenu;
  93.     }
  94.    
  95.     /// <summary>
  96.     /// Affiche les informations de ce container sur la console
  97.     /// </summary>
  98.     public void AfficherInfo()
  99.     {
  100.         Console.WriteLine("contenu : {0}     poids : {1}", contenu, poids);
  101.     }
  102.    
  103.     /// <summary>
  104.     /// Retourne le poids du container
  105.     /// </summary>
  106.     /// <returns></returns>
  107.     public float DonnerPoids()
  108.     {
  109.         return poids;
  110.     }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement