IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

avec Java Discussion :

les exceptions en java


Sujet :

avec Java

  1. #1
    Membre du Club
    Profil pro
    Inscrit en
    Janvier 2008
    Messages
    120
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2008
    Messages : 120
    Points : 43
    Points
    43
    Par défaut les exceptions en java
    Bonjour,
    voilà le but est de créer une agence qui gère une liste de propriétaire qui travaille avec elle et une liste de local (entreprise) et de logement (particulier).

    nouveauProprio->ajoute un propriétaire à la liste des propriétaires de l'agence

    nouveauLogement,nouveauLocal->ajoute un logement à la liste des logements de l'agence et met à jour la liste des locations que le propriétaire possède (si le propriétaire ne fait pas partie de la liste il doit levé une exception)

    rechercheLogement->renvoie la liste des logements adapté à la demande du client (avec jardin ou sans / avec un certain nombre de chambre)

    Ma classe agence:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    public class agence{
        private proprietaire[] tabProprio;
        private local[] tabLoc;
        private logement[] tabLog;
        private static final int MAX=100;
        private int nbProprio=0;
        private int nbLoc=0;
        private int nbLog=0;
     
        public agence(){
    	tabProprio=new proprietaire[MAX];
    	tabLoc=new local[MAX];
    	tabLog=new logement[MAX];
        }
     
        public void nouveauProprio(String nom,String prenom,String adresse){
    	if(nbProprio<MAX)
    	    tabProprio[nbProprio++]=new proprietaire(nom,prenom,adresse);
        }
     
        public boolean nouveauLogement(float loyer,float charge,float superficie,String adresse, String proprio,int nbChambres,boolean jardin)throws ProprietaireInconnuException{
    	tabLog[nbLog++]=new logement(loyer,charge,superficie,adresse,proprio,nbChambres,jardin);
    	for(int i=0;i<nbProprio;i++)
    	    if(tabProprio[i].getNom().equals(proprio)){
    		tabProprio[i].ajouteLocation(loyer,charge,superficie,adresse);
    		return true;
    	    }
    	throw new ProprietaireInconnuException();
        }
     
        public boolean nouveauLocal(float loyer,float charge,float superficie,String adresse, String proprio,String descriptif)throws ProprietaireInconnuException{
    	tabLoc[nbLoc++]=new local(loyer,charge,superficie,adresse,proprio,descriptif);
    	for(int i=0;i<nbProprio;i++)
    	    if(tabProprio[i].getNom().equals(proprio)){
    		tabProprio[i].ajouteLocation(loyer,charge,superficie,adresse);
    		return true;
    	    }
    	throw new ProprietaireInconnuException();
        }
     
        public logement[]  rechercheLogement(int nbChambre,boolean jardin){
    	logement[] tab=new logement[100];
    	int nbL=0;
    	for(int i=0;i<nbLog;i++)
    	    if(tabLog[i].getNbChambres().equals(nbChambre) && tabLog[i].getJardin().equals(jardin))
    		tab[nbL++]=tabLog[i];
    	return tab;
    	}
     
     
        public String toString(){
    	StringBuilder tmp=new StringBuilder();
    	tmp.append("Liste des locations de l'agence\n");
    	tmp.append("Local:\n");
    	for(int i=0;i<nbLoc;i++)
    	    tmp.append(tabLoc[i]+"\n");
    	tmp.append("logement:\n");
    	for(int i=0;i<nbLog;i++)
    	    tmp.append(tabLog[i]+"\n");
    	tmp.append("Liste des locations des propriétaire qui tavaille avec l'agence\n");
    	for(int i=0;i<nbProprio;i++)
    	    tmp.append(tabProprio[i]);
    	return tmp.toString();
        }
     
    }
    Ma classe proprietaire:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
     
    public class proprietaire{
        private String nom;
        private String prenom;
        private String adresse;
        private location[] tabLoc;
        private int nbLoc=0;
        private static final int MAXLOC=10;
     
        public proprietaire(String nom,String prenom,String adresse){
    	this.nom=nom;
    	this.prenom=prenom;
    	this.adresse=adresse;
    	tabLoc=new location[MAXLOC];
        }
     
        public String getNom(){
    	return nom;
        }
        public String getPrenom(){
    	return prenom;
        }
     
        public void ajouteLocation(float loyer,float charge,float superficie,String adresse){
    	tabLoc[nbLoc++]=new location(loyer,charge,superficie,adresse,nom);
        }
        public String toString(){
    	StringBuilder tmp=new StringBuilder();
    	tmp.append("Propriétaire:");
    	tmp.append(nom+" "+prenom+"\n");
    	tmp.append("Adresse:"+adresse+"\n");
    	tmp.append("propriétés:\n");
    	for(int i=0;i<nbLoc;i++)
    	    tmp.append(tabLoc[i]+"\n");
    	return tmp.toString();
        }			
    }
    ma classe pour l'exception:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
     
    public class ProprietaireInconnuException extends Exception{
     
      public ProprietaireInconnuException(){
        super("Ce propriétaire n'existe pas");
      }
     
    }
    Ma classe pour test tous sa:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
     
    public class test{
        public static void main(String[] args){
    	agence a=new agence();
    	a.nouveauProprio("ROLAND","Nicolas","7 rue des eaux");
    	a.nouveauProprio("SENTENZA","Delnir","8 rue des lys");
    	a.nouveauLogement(835f,150f,50f,"5 rue des magnolia","ROLAND",4,true);
    	System.out.println(a);
        }
    }
    voilà les erreurs:
    agence.java:45: int cannot be dereferenced
    if(tabLog[i].getNbChambres().equals(nbChambre) && tabLog[i].getJardin().equals(jardin))
    ^
    agence.java:45: boolean cannot be dereferenced
    if(tabLog[i].getNbChambres().equals(nbChambre) && tabLog[i].getJardin().equals(jardin))

    et quand j'execute la classe test sans la méthode rechercheLogement ->
    unreported exception ProprietaireInconnuException; must be caught or declared to be thrown
    a.nouveauLogement(835f,150f,50f,"5 rue des magnolia","ROLAND",4,true);

    pourquoi est-je ces erreurs?
    j'ai probablement mal comprit les exeptions comment l'utiliser dans mon cas précis?

  2. #2
    Modérateur
    Avatar de dinobogan
    Homme Profil pro
    ingénieur
    Inscrit en
    Juin 2007
    Messages
    4 073
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 42
    Localisation : France

    Informations professionnelles :
    Activité : ingénieur
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juin 2007
    Messages : 4 073
    Points : 7 163
    Points
    7 163
    Par défaut
    Donne le code de la classe "logement", "chambre" et "jardin". Et regarde les signatures de tes fonctions "equals". Elle n'acceptent pas les types de bases int et boolean.
    Pour l'exception, si tu ne la catch pas, le programme va s'arrêter en erreur, c'est normal.
    N'oubliez pas de consulter les FAQ Java et les cours et tutoriels Java
    Que la force de la puissance soit avec le courage de ta sagesse.

  3. #3
    Membre du Club
    Profil pro
    Inscrit en
    Janvier 2008
    Messages
    120
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2008
    Messages : 120
    Points : 43
    Points
    43
    Par défaut
    la classe location
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    public class location{
        private float loyer;
        private float charge;
        private float superficie;
        private String adresse;
        private String proprio;
        private boolean louer;
     
        public location(float loyer,float charge,float superficie,String adresse,
    		    String proprio){
    	this.loyer=loyer;
    	this.charge=charge;
    	this.superficie=superficie;
    	this.adresse=adresse;
    	this.proprio=proprio;
    	louer=false;
        }
        public String getProprio(){
    	return proprio;
        }
        public void LeLouer(){
    	louer=true;
        }
     
        public void upLoyer(int prix){
    	if(prix<((loyer*5)/100)+loyer)
    		loyer=loyer+prix;
        }
     
        public void downLoyer(int prix){
    	loyer=loyer-prix;
        }
     
        public String toString(){
    	return "loyer:"+loyer+" charge:"+charge+" superficie:"+superficie+" adresse:"+adresse+" propriétaire:"+proprio+" loué?:"+louer;
        }
     
    }
    la classe logement qui hérite de la classe location:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
     
    public class logement extends location{
        private int nbChambres;
        private boolean jardin;
     
        public logement(float loyer,float charge,float superficie,String adresse,
    		    String proprio,int nbChambres,boolean jardin){
    	super(loyer,charge,superficie,adresse,proprio);
    	this.nbChambres=nbChambres;
    	this.jardin=jardin;
        }
     
        public int getNbChambres(){
    	return nbChambres;
        }
     
        public boolean getJardin(){
    	return jardin;
        }
     
        public String toString(){
    	return super.toString()+" nombre de chambre:"+nbChambres+" jardin?"+jardin;
        }
    }

  4. #4
    Expert éminent sénior Avatar de Uther
    Homme Profil pro
    Tourneur Fraiseur
    Inscrit en
    Avril 2002
    Messages
    4 562
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Pyrénées Orientales (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Tourneur Fraiseur

    Informations forums :
    Inscription : Avril 2002
    Messages : 4 562
    Points : 15 502
    Points
    15 502
    Par défaut
    Pour l'exception, si tu ne la catch pas, le programme va s'arrêter en erreur, c'est normal.
    En fait dans ton cas tu es même obligé de catcher les execption.
    Seules les exceptions qui héritent de java.lang.RuntimeException peuvent être ignorées, mais planteront à l'exécution.

  5. #5
    Modérateur
    Avatar de dinobogan
    Homme Profil pro
    ingénieur
    Inscrit en
    Juin 2007
    Messages
    4 073
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 42
    Localisation : France

    Informations professionnelles :
    Activité : ingénieur
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juin 2007
    Messages : 4 073
    Points : 7 163
    Points
    7 163
    Par défaut
    Citation Envoyé par Delnir Voir le message
    voilà les erreurs:
    agence.java:45: int cannot be dereferenced
    if(tabLog[i].getNbChambres().equals(nbChambre) && tabLog[i].getJardin().equals(jardin))
    ^
    agence.java:45: boolean cannot be dereferenced
    if(tabLog[i].getNbChambres().equals(nbChambre) && tabLog[i].getJardin().equals(jardin))
    Remplace cette ligne par :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    if( tabLog[i].getNbChambres() == nbChambre && tabLog[i].getJardin() == jardin)
    et quand j'execute la classe test sans la méthode rechercheLogement ->
    unreported exception ProprietaireInconnuException; must be caught or declared to be thrown
    a.nouveauLogement(835f,150f,50f,"5 rue des magnolia","ROLAND",4,true);

    pourquoi est-je ces erreurs?
    j'ai probablement mal comprit les exeptions comment l'utiliser dans mon cas précis?
    Tu n'exécutes pas, tu compiles !
    Sinon, tu as deux possibilités :
    - placer l'appel de la méthode dans un bloc try/catch
    - transformer l'exception "ProprietaireInconnuException" en "RuntimeException" (il suffit d'hériter de cette classe au lieu de "Exception")
    N'oubliez pas de consulter les FAQ Java et les cours et tutoriels Java
    Que la force de la puissance soit avec le courage de ta sagesse.

Discussions similaires

  1. Les exceptions en Java
    Par zpico dans le forum Débuter avec Java
    Réponses: 5
    Dernier message: 28/09/2011, 18h46
  2. Les exceptions en java
    Par jeedev dans le forum Langage
    Réponses: 2
    Dernier message: 13/05/2009, 12h30
  3. [Debug] Rediriger les exceptions JAVA dans un log
    Par matt8-5 dans le forum Logging
    Réponses: 3
    Dernier message: 25/01/2008, 11h34
  4. Les exceptions en java
    Par Dev_info dans le forum Langage
    Réponses: 5
    Dernier message: 24/05/2007, 16h02
  5. les exceptions java
    Par nadir2 dans le forum Langage
    Réponses: 5
    Dernier message: 14/02/2007, 17h05

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo