Advertisement
madalinaradu

Magazin produse alocare statica

Apr 15th, 2019
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.07 KB | None | 0 0
  1. #include <iostream>
  2. #include <conio.h>
  3. #include <string.h>
  4.  
  5. using namespace std;
  6. class Magazin;
  7. class Produs {
  8. private:
  9.     char nume[64];
  10.     float pret;//pretul este in Euro
  11.     static float CURS_VALUTAR;
  12. public:
  13.     Produs(const char *nume="",float pret=0 );
  14.     void afisare();
  15.     void setNume(char* nume);
  16.     char *getNume();
  17.     void setPret(float pret);
  18.     float getPret();
  19.     float getPretLei();
  20. };
  21. float Produs:: CURS_VALUTAR=4.758;//12 apr 2019
  22.  
  23. Produs::Produs(const char *nume,float pret ) {
  24.     strcpy(this->nume,nume);
  25.     this->pret=pret;
  26. }
  27.  
  28. void Produs::afisare() {
  29.     cout<<"Nume:  "<< nume<<endl;
  30.     cout<<"Pret in Euro: "<<pret<<endl;
  31.     cout<<"Pret in lei:  "<<getPretLei()<<endl;
  32.     cout<<"-----------------------------"<<endl;
  33. }
  34. void Produs::setNume(char *nume) {
  35.     strcpy(this->nume, nume);
  36. }
  37. char *Produs::getNume() {
  38.     return nume;
  39. }
  40. void Produs::setPret(float pret) {
  41.     if(pret>0)
  42.         this->pret=pret;
  43. }
  44. float Produs::getPret() {
  45.     return pret;
  46. }
  47.  
  48. float Produs::getPretLei() {                         //functie ce calculeaza pretul in lei
  49.     return pret*CURS_VALUTAR;
  50. }
  51.  
  52. class Magazin {                              //clasa Magazin
  53. private:
  54.     int capacitate;//nr max de produse din magazin
  55.     int nrProduse;//nr efectiv de produse
  56.     Produs *produse;
  57. public:
  58.     Magazin(int capacitate);
  59.     Magazin(const Magazin &m);
  60.     void adaugare(Produs p);
  61.     void afisare();
  62.     void eliminare(Produs p);
  63. };
  64. Magazin::Magazin(int capacitate) {
  65.     this->capacitate = capacitate;
  66.     this->produse = new Produs[capacitate];
  67.     this->nrProduse=0;
  68. }
  69.  
  70. Magazin::Magazin(const Magazin &m) {
  71.     this->capacitate = m.capacitate;
  72.     this->nrProduse=m.nrProduse;
  73.     this->produse = new Produs[m.capacitate];
  74.     for(int i=0; i<nrProduse; i++) {
  75.         this->produse[i]= m.produse[i];
  76.     }
  77. }
  78. void Magazin::adaugare(Produs p) {
  79.     if(nrProduse<capacitate) {
  80.         produse[nrProduse]=p;
  81.         nrProduse++;
  82.     } else {
  83.         cout<<"nu mai am loc"<<endl;
  84.     }
  85. }
  86. void Magazin::afisare() {
  87.     cout<<endl;
  88.     cout<<"Lista produselor este:"<<endl;
  89.     for(int i=0; i<nrProduse; i++) {
  90.         produse[i].afisare();
  91.     }
  92. }
  93. void Magazin::eliminare(Produs p) {
  94.     int gasit=-1;
  95.     for(int i=0; i<nrProduse; i++){
  96.         if(strcmp(p.getNume(),produse[i].getNume())==0){
  97.             gasit=i;
  98.             cout<<"Produsul a fost gasit, urmeaza a fi eliminat"<<endl;
  99.             break;
  100.         }
  101.     }
  102.         if(gasit>=0){
  103.             for(int i=gasit; i<nrProduse-1; i++){
  104.                 produse[i]=produse[i+1];
  105.             }
  106.             nrProduse--;
  107.         }
  108. }
  109.  
  110.  
  111. int main() {
  112.  
  113.     Produs p1("vafa",4);
  114.     Produs p2("pufarina",1);
  115.     Produs p3("varza",3);
  116.     Magazin m(5);
  117.     m.adaugare(p1);
  118.     m.adaugare(p2);
  119.     m.afisare();
  120.     m.adaugare(p3);
  121.     m.afisare();
  122.     m.eliminare(p2);
  123.     cout<<endl;
  124.     cout<<"***********";
  125.     m.afisare();
  126.     /*
  127.     Magazin m2=m;
  128.     cout<<"----------"<<endl;
  129.     cout<<"magazinul 2"<<endl;
  130.     m2.afisare();*/
  131.     return 0;
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement