C++: Correction Client, Article, Facture

 

1 Une première solution

1.1 Un schéma UML pour l'ensemble

 

\includegraphics[scale=0.4]{livr/td3-uml.eps}

 

1.2 Une ébauche de traduction du schéma UML

Voici l'interface minimale à mettre en œuvre : 


#include <vector>
#include <string>

class Client 
{
public:
  Client(int _code, string _raison_sociale);
  string get_raison_sociale() const;
  int get_code() const;

private:
  int code;
  string raison_sociale;
};

class Article
{
public:
  Article(int _code, string _designation, float _pu);
  string get_designation() const;
  int get_code() const;

private:
  int code;
  string designation;
  float prix_unitaire
};

class LigneFacture
{
public:
  LigneFacture(Article * _article, int _qte);

protected:
  Article * article;
  int quantite;
};

class Facture
{
public:
  Facture(int _numero, Client * _client);
  ~Facture();
  Client * get_client() const;
  int get_numero() const;
  void ajouter_ligne(Article * article, int qte);

private:
  int numero;
  Client * client;
  vector<LigneFacture*> lignes;
};

 

2 La solution complète

 

2.1 Article

2.1.1 Interface


#ifndef __ARTICLE_H__
#define __ARTICLE_H__

#include <string>

class Article 
{
public:
  Article(int _code, string _designation, float _pu);
  string get_designation() const { return designation; }
  int get_code() const { return code; }
  float get_prix_unitaire() const { return prix_unitaire; }
private:
  int code;
  string designation;
  float prix_unitaire;
};

#endif

2.1.2 Mise en œuvre


#include "article.h"

Article::
Article(int _code, string _designation, float _pu)
{
  code = _code;
  designation = _designation;
  prix_unitaire = _pu;
}

 

2.2 Client

2.2.1 Interface


#ifndef __CLIENT_H__
#define __CLIENT_H__

#include <string>

class Client 
{
public:
  Client(int _code, string _raison_sociale);
  string get_raison_sociale() const { return raison_sociale; }
  int get_code() const { return code; }

private:
  int code;
  string raison_sociale;
};

#endif

2.2.2 Mise en œuvre


#include "client.h"

Client::
Client(int _code, string _raison_sociale)
{
  code = _code;
  raison_sociale = _raison_sociale;
}

 

2.3 Ligne de facture

2.3.1 Interface


#ifndef __LIGNE_FACTURE_H__
#define __LIGNE_FACTURE_H__

#include "article.h"

class LigneFacture
{
public:
  LigneFacture(Article * _article, int _qte);
  Article * get_article() const { return article; }
  int get_quantite() const { return quantite; }

protected:
  Article * article;
  int quantite;
};

#endif

2.3.2 Mise en œuvre


#include "ligneFacture.h"

LigneFacture::  
LigneFacture(Article * _article, int _qte)
{
  article = _article;
  quantite = _qte;
}

 

2.4 Facture

2.4.1 Interface


#ifndef __FACTURE_H__
#define __FACTURE_H__

#include <vector>
#include "client.h"
#include "ligneFacture.h"

class Facture
{
public:
  Facture(int _numero, Client * _client);
  ~Facture();
  Client * get_client() const { return client; }
  int get_numero() const { return numero; }
  LigneFacture * get_ligne(int _pos) const { return lignes[_pos]; }
  int get_ligneSize() const { return lignes.size(); }
  void ajouter_ligne(Article * article, int qte);
  float prix() const;

private:
  int numero;
  Client * client;
  vector<LigneFacture*> lignes;
};

#endif

2.4.2 Mise en œuvre


#include "facture.h"

Facture::
Facture(int _numero, Client * _client)
{
  numero = _numero;
  client = _client;
}

Facture::
~Facture()
{
  for (int i = 0; i < lignes.size(); i++) {
    delete lignes[i];
  }
}

void Facture::
ajouter_ligne(Article * article, int qte)
{
  lignes.push_back(new LigneFacture(article, qte));
}

float Facture::
prix() const
{
  float cum = 0.0;
  for (int i = 0; i < lignes.size(); i++) {
    LigneFacture * l = lignes[i];
    Article * a = l->get_article();
    cum += a->get_prix_unitaire() * l->get_quantite();
  }
  return cum;
}

ostream & operator << (ostream & o, const Facture & f)
{
  o << "----------------" << endl;
  o << "Facture: " << f.get_numero() << endl;
  o << "Client:  " << f.get_client()->get_code() << " " << f.get_client()->get_raison_sociale() << endl;

  for (int i = 0; i < f.get_ligneSize(); i++) {
    LigneFacture * l = f.get_ligne(i);
    Article * a = l->get_article();
    cout << "  " << a->get_code() << " " << a->get_designation() << " " << l->get_quantite();
    cout << " : " << a->get_prix_unitaire() * l->get_quantite() << endl;
  }
  o << "Total : " << f.prix() << endl;
  o << "----------------" << endl;
  return o;
}

 

2.5 Un programme de test


#include <iostream.h>
#include "facture.h"

main()
{
  // Creation d'articles
  Article cle(17450, "CLEAMOLLETTE", 24.76);
  Article visse(96831, "VISSE", 0.09);
  Article pince(27832, "PINCE", 5.55);  
  Article marteau(742, "MARTEAU", 10.2);

  // Creation de clients
  Client momoLaBricole(5840, "Chez Momo");
  Client jojoToucheATout(731, "Jojo & fils");
  Client zazaRepareAFond(84692, "Zaza et ses mains");

  // Une facture
  Facture fmomo(10234, &momoLaBricole);
  fmomo.ajouter_ligne(&visse, 876);
  fmomo.ajouter_ligne(&marteau, 1);

  cout << fmomo << endl;
}

Résultat sur la sortie standard

td3-corr> main
----------------
Facture: 10234
Client:  5840 Chez Momo
  96831 VISSE 876 : 78.84
  742 MARTEAU 1 : 10.2
Total : 89.04
----------------

td3-corr>