| 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
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 
 | #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) const {return this;}
 
    //la classe Produit : où elle retournera 1 si le produit a pour nom nomProduit et 0 sinon ;
	virtual double quantiteTotale(const string & nom) const{
        if (Produit::getNom() == nom) {return 1.0;}
        return 0.0;
    } 
 
};
 
class Ingredient{
private:
    const Produit *p;//Changed to refernce
    double quantite;
 
public:
    Ingredient(const 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();
    }
 
/* 	 la classe Ingredient : où elle retournera la quantité de lingrédient
multipliée par la quantité totale du produit recherché dans le produit de lingrédient.*/
	double quantiteTotale(const string & nom) const{
		return getQuantite()*getProduit().quantiteTotale(nom); 
    }
 
};
 
class Recette{
private:
    vector<Ingredient> lst;
    string nom;
    double nbFois_;
public:
    Recette(string nom, double nbFois_ = 1.0): nom(nom), nbFois_(nbFois_) {}
 
    void ajouter(const Produit& p, double quantite ){
        lst.emplace_back(p, quantite * nbFois_);
    }
    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;
    }
 
/*  la classe Recette : où elle retournera la somme des quantités totales de
ce produit trouvées dans chaque ingrédient ; */
    double quantiteTotale(const string & nom) const{
		double tot = 0;
        for (auto const i : lst){
            if (i.getProduit().getNom() == nom) {
                tot = tot + i.getProduit().quantiteTotale(nom);
			}
        }
		return tot; 
    }
};
 
class ProduitCuisine: public Produit {
private:
    Recette r;
public:
    ProduitCuisine(string nom): Produit(nom,"portion(s)"), r(nom) {}
 
    void ajouterARecette(const Produit &produit, double quantite) {
        r.ajouter(produit,quantite);
    }
    virtual const Produit* adapter(double n) const override{
        ProduitCuisine *p = new ProduitCuisine(nom);
        p->r = r.adapter(n);
        return p;
    }
    virtual string toString() const override{
        stringstream ss;
        ss << Produit::toString() << "\n" << r.toString();
        return ss.str();
    }
 
/*  la classe ProduitCuisine : où elle retournera 1. si le produit a pour nom nomProduit, 
 et sinon, la quantité totale de ce produit dans la recette du produit cuisiné,
 cette méthode doit re-implémenter celle définie dans Produit ; */
     double quantiteTotale(const string & nom) const{
        if (Produit::getNom() == nom) {
			return 1.0;
		}
        return Produit::quantiteTotale(nom);
    } 
};
/*******************************************
 * 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;
  //SUPPRIMER POUR CORRECTION
  Ingredient bobo(beurre,500);
  cout << "PRODUIT  beurre" <<beurre.quantiteTotale("beurre") << "CACA" <<beurre.quantiteTotale("caca")<< endl;
  cout << "INGREDIENT  extraitamende" <<bobo.quantiteTotale("beurre") << "CACA" <<bobo.quantiteTotale("CACA")<< endl;
  cout << "RECETTE  beurre" <<doubleRecette.quantiteTotale("beurre") << "CACA" <<doubleRecette.quantiteTotale("CACA")<< endl;  
  cout << "PRODUITCUISINE  beurre" <<glacageParfume.quantiteTotale("glacage") << "CACA" <<glacageParfume.quantiteTotale("caca")<< endl;
  return 0;
} | 
Partager