| 12
 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
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 
 | #include <iostream>
#include <vector>
#include <sstream>
 
using namespace std;
 
/*****************************************************
  * Compléter le code à partir d'ici
 *****************************************************/
class Produit{
protected:
    string nom;
    string unite;
public:
    Produit(string nom="",string unite=""): nom(nom), unite(unite) {} //initialize and assignement's difference
    string getNom() const{return nom;}
    string getUnite() const{return unite;}
 
    virtual string toString() const{return nom;}
 
    virtual const Produit* adapter(double n) const{
        return this;
    }
};
 
class Ingredient{
private:
    Produit p;//Changed to refernce
    double quantite;
 
public:
    Ingredient(Produit p,double quantite): p(p),quantite(quantite) {}
 
    Produit const & getProduit() const{return p;}//return une reference??
    double getQuantite() const{return quantite;}
 
    string descriptionAdaptee() const{
        stringstream ss;
        const Produit* ptr = p.adapter(quantite);
        ss << fixed << quantite << " " << p.getUnite() << " de " << ptr->toString();
        return ss.str();
    }
};
 
class Recette{
private:
    vector<Ingredient> lst;
    string nom;
    double nbFois_;
public:
    Recette(string nom, double nbFois_ = 1.0): nom(nom), nbFois_(nbFois_) {}
 
    void ajouter( Produit& p, double quantite ){
        Ingredient a(p, quantite*nbFois_);
        lst.push_back(a);
    }
 
    double quantiteTotale(string nom) const{
        for (auto const i : lst){
            if (i.getProduit().getNom() == nom){return i.getQuantite();}
        }return 0.0;
    }
 
    const Recette adapter(double n) const{
        double newNbFois = nbFois_ * n;
        Recette x(nom, newNbFois);
        for (auto const i : lst){
            const Produit & tmp = i.getProduit();
            x.lst.push_back(Ingredient(tmp,i.getQuantite()*n));//get the thing the pointer points at.
        }
        return x;
    }
 
    virtual string toString() const{
        stringstream ss;
        //for (vector<Ingredient>::size_type j=0;j<lst.size();j++){
            ss << " Recette \"" << nom << "\" x " << nbFois_<< ":\n";
            unsigned int i = 1;
            for (auto const & ingredient : lst){
                ss << " " << i << ". " << ingredient.descriptionAdaptee();//getProduit().toString();
                if (i != lst.size()){
                    ss << endl;
                }
                i += 1;
            }
        //    ss << endl;
        //}
        string s = ss.str();
        return s;
    }
 
};
 
class ProduitCuisine: public Produit{
private:
    Recette r;
public:
    ProduitCuisine(string nom): Produit(nom,"portion(s)"),r(nom) {}
 
    void ajouterARecette(Produit& produit, double quantite){
        r.ajouter(produit,quantite);
    }
    const Produit* adapter(double n) const override{
        ProduitCuisine *p = new ProduitCuisine(nom);
        p->r = r.adapter(n);
        return this;
    }
 
    string toString() const override{
        stringstream ss;
        ss << Produit::toString() << "\n" << r.toString();
        return ss.str();
    }
 
};
/*******************************************
 * Ne rien modifier après cette ligne.
 *******************************************/
void afficherQuantiteTotale(const Recette& recette, const Produit& produit)
{
  string nom = produit.getNom();
  cout << "Cette recette contient " << recette.quantiteTotale(nom)
       << " " << produit.getUnite() << " de " << nom << endl;
}
 
int main()
{
  // quelques produits de base
  Produit oeufs("oeufs");
  Produit farine("farine", "grammes");
  Produit beurre("beurre", "grammes");
  Produit sucreGlace("sucre glace", "grammes");
  Produit chocolatNoir("chocolat noir", "grammes");
  Produit amandesMoulues("amandes moulues", "grammes");
  Produit extraitAmandes("extrait d'amandes", "gouttes");
 
  ProduitCuisine glacage("glaçage au chocolat");
  // recette pour une portion de glaçage:
  glacage.ajouterARecette(chocolatNoir, 200);
  glacage.ajouterARecette(beurre, 25);
  glacage.ajouterARecette(sucreGlace, 100);
  cout << glacage.toString() << endl;
 
  ProduitCuisine glacageParfume("glaçage au chocolat parfumé");
  // besoin de 1 portions de glaçage au chocolat et de 2 gouttes
  // d'extrait d'amandes pour 1 portion de glaçage parfumé
 
  glacageParfume.ajouterARecette(extraitAmandes, 2);
  glacageParfume.ajouterARecette(glacage, 1);
  cout << glacageParfume.toString() << endl;
 
  Recette recette("tourte glacée au chocolat");
  recette.ajouter(oeufs, 5);
  recette.ajouter(farine, 150);
  recette.ajouter(beurre, 100);
  recette.ajouter(amandesMoulues, 50);
  recette.ajouter(glacageParfume, 2);
 
  cout << "===  Recette finale  =====" << endl;
  cout << recette.toString() << endl;
  afficherQuantiteTotale(recette, beurre);
  cout << endl;
 
  // double recette
  Recette doubleRecette = recette.adapter(2);
  cout << "===  Recette finale x 2 ===" << endl;
  cout << doubleRecette.toString() << endl;
 
  afficherQuantiteTotale(doubleRecette, beurre);
  afficherQuantiteTotale(doubleRecette, oeufs);
  afficherQuantiteTotale(doubleRecette, extraitAmandes);
  afficherQuantiteTotale(doubleRecette, glacage);
  cout << endl;
 
  cout << "===========================\n" << endl;
  cout << "Vérification que le glaçage n'a pas été modifié :\n";
  cout << glacage.toString() << endl;
 
  return 0;
} | 
Partager