utiliser dans une methode toString() celle d'une autre classe

utiliser dans une methode toString() celle d'une autre classe - Java - Programmation

Marsh Posté le 17-10-2008 à 19:19:50    

Bonsoir, mon problème est donc que je souhaite faire apparaitre le toString de ma classe CarteSIM dans celui de ma classe TelephonePortable. Est ce que cela est possible si oui comment ?
J'aimerai que lorsque je fais mon System.out d'un new TelephonePortable sa m'affiche a la suite les deux texte. Je sais qu'il serait beaucoup plus simple de ne faire que le toString de ma classe TelephonePortable. Mais c'est pour un devoir et je dois donc laisser le toString dans ma classe CarteSIM et j'aimerai tant qu'a faire reussir a reexploiter le premier toString plutot que de le retaper dans le deuxieme.
 
 
Merci d'avance
 
Voici mon code source :
 

Citation :

public class TelephonePortable
{
private CarteSIM simtelephone;
private String nomproprietaire;
 
 public TelephonePortable(String nomproprio)
 {
 CarteSIM simtelephone = new CarteSIM(5000,500);
 nomproprietaire=nomproprio;
 }  
 
 public TelephonePortable(int capacitemax,int nbnumeromax,String nomproprio)
 {
 CarteSIM simtelephone = new CarteSIM(capacitemax,nbnumeromax);
 nomproprietaire=nomproprio;
 }
 
 public CarteSIM getCarteSIM()
 {
 return simtelephone;
 }
 
 public String getNomProprietaire()
 {
 return nomproprietaire;
 }
 
 public void ajustementCapaciteMemoire()
 {
 int capacitemini=simtelephone.getNbNumeroMax()*10;
  if (simtelephone.getCapaciteMax()<capacitemini) {
  simtelephone.setCapaciteMax(capacitemini);
  }
 }
 
 public void ajouterNumeroTelephone(int n)
 {
 simtelephone.addNumeroTel(n);
 simtelephone.oqpCapacite(n*10);
 }
 
 public void effacerNumeroTelephone(int n)
 {
 simtelephone.addNumeroTel(-n);
 simtelephone.oqpCapacite(-n*10);
 }
 
 public String toString()
 {
 return "Le proprietaire est : "+this.getNomProprietaire()+" Et : " +this.getCarteSIM();
 }
}


 

Citation :

public class CarteSIM
{
private String identifiant;
private int capacitemax;
private int nbnumeromax;
private int capaciteactuelle;
private int nbnumeroenregistre;
 
 public CarteSIM()
 {
 capacitemax=5000;
 nbnumeromax=500;
 capaciteactuelle=0;
 nbnumeroenregistre=0;
 identifiant="defautsim";
 }
 
 public CarteSIM(int capamax,int nummax)
 {
 capacitemax=capamax;
 nbnumeromax=nummax;
 capaciteactuelle=0;
 nbnumeroenregistre=0;
 identifiant="defautsim";
 }
 
 public CarteSIM(int capamax,int nummax,String id)
 {
 capacitemax=capamax;
 nbnumeromax=nummax;
 capaciteactuelle=0;
 nbnumeroenregistre=0;
 identifiant=id;
 }
 
 public CarteSIM(int capamax,int nummax,int capaactu,int nbnumenr,String id)
 {
 capacitemax=capamax;
 nbnumeromax=nummax;
 capaciteactuelle=capaactu;
 nbnumeroenregistre=nbnumenr;
 identifiant=id;
 }
 
 public int getCapaciteMax()
 {
 return capacitemax;
 }
 
 public int getNbNumeroMax()
 {
 return nbnumeromax;
 }
 
 public int getCapaciteActuelle()
 {
 return capaciteactuelle;
 }
 
 public int getNbNumeroEnregistre()
 {
 return nbnumeroenregistre;
 }
 
 public String getIdentifiant()
 {
 return identifiant;
 }
 
 public void setCapaciteMax(int capamax)
 {
 capacitemax=capamax;
 }
 
 public boolean depassementAnnuaire()
 {
  if (capaciteactuelle>capacitemax) {
  return true;}
  else {return false;}
 }
 
 public boolean depassementCapacite()
 {
  if (nbnumeromax<nbnumeroenregistre) {
  return true;}
  else {return false;}
 }
 
 public void addNumeroTel(int n)
 {
  nbnumeroenregistre+=n;
  if (depassementCapacite()) {
  System.out.println("Nombre de numero de telephone maximum atteint." );
  nbnumeroenregistre-=n;  
  }
 }
 
 public void oqpCapacite(int c)
 {
 capaciteactuelle+=c;
  if (depassementAnnuaire()) {
  System.out.println("Capacite maximum atteinte." );
  capaciteactuelle-=c;  
  }
 }
 
 public String toString()
 {
 int pourcentageutilise=100*this.getCapaciteActuelle()/this.getCapaciteMax();
 int nbnumerorestant=this.getNbNumeroMax()-this.getNbNumeroEnregistre();
 return "La carte SIM "+this.getIdentifiant().substring(0,4)+" occupe "+pourcentageutilise+" pour cent de la capacité maximale et peut encore enregistrer "+nbnumerorestant+" n° de telephone";
 }
}


 

Citation :

public class Test
{
 public static void main(String []args)
 {
 System.out.println(new CarteSIM(1000,100,50,95,"sim543" ));
 System.out.println(new TelephonePortable(5000, 500,"Dery" ));
 }
}

Reply

Marsh Posté le 17-10-2008 à 19:19:50   

Reply

Marsh Posté le 17-10-2008 à 22:35:19    

pas compris, qu'est ce qui t'empêche d'appeler this.getCarteSIM().toString() dans la méthode toString() de TelephonePortable ?


---------------
- mon feed-back
Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed