Advertisement
madalinaradu

Magazin produse alocare dinam euro lei

Apr 15th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.33 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;
  10.     float pret;//pretul este in Euro
  11.     static float CURS_VALUTAR;
  12. public:
  13.     Produs();
  14.     Produs(char *nume,float pret=0 );
  15.     Produs(const Produs &p);
  16.     ~Produs();
  17.     void afisare();
  18.     void setNume(char* nume);
  19.     char * getNume();
  20.     void setPret(float pret);
  21.     float getPret();
  22.     float getPretLei();
  23.  
  24.  
  25. };
  26. float Produs:: CURS_VALUTAR=4.758;//12 apr 2019
  27. Produs::Produs() {
  28.     nume=NULL;
  29.     pret=0;
  30.     cout<<"apel constructor implicit clasa Produs";
  31.     cout<<endl;
  32. }
  33. Produs::Produs(char *nume,float pret ) {
  34.     if(nume!=NULL) {
  35.         this->nume=new char[strlen(nume)+1];
  36.         strcpy(this->nume,nume);
  37.     } else {
  38.         this->nume=NULL;
  39.     }
  40.     this->pret=pret;
  41.     cout<<"Apel constructor cu parametri clasa Produs";
  42.     cout<<endl;
  43. }
  44.  
  45. Produs::Produs(const Produs &p) {
  46.     if(p.nume!=NULL) {
  47.         this->nume=new char[strlen(p.nume)+1];
  48.         strcpy(this->nume,p.nume);
  49.     } else {
  50.         this->nume=NULL;
  51.     }
  52.     this->pret=p.pret;
  53.     cout<<"Apel constructor copiere clasa Produs";
  54.     cout<<endl;
  55. }
  56.  
  57. Produs::~Produs() {
  58.     if(nume!=NULL) {
  59.         delete nume;
  60.     }
  61.     nume=NULL;                                       // de ce?
  62.     cout<<"Apel destructor clasa Produs.";
  63.     cout<<endl;
  64. }
  65.  
  66. float Produs::getPretLei() {                         //functie ce calculeaza pretul in lei
  67.     return pret*CURS_VALUTAR;
  68. }
  69. void Produs::afisare() {
  70.     cout<<"Nume:  "<<(nume==NULL ? "Necunoscut" : nume)<<endl;
  71.     cout<<"Pret in Euro: "<<pret<<endl;
  72.     cout<<"Pret in lei:  "<<getPretLei()<<endl;
  73. }
  74. void Produs::setNume(char *nume) {
  75.     if(nume!=NULL) {
  76.         if(this->nume!=NULL) {
  77.             if(strlen(nume)>strlen(this->nume)) {
  78.                 delete this->nume;
  79.                 this->nume=new char[strlen(nume)+1];
  80.             }
  81.         } else {                                       //numele curent a fost nul, numele nou!= nul
  82.             this->nume=new char[strlen(nume)+1];
  83.         }
  84.         strcpy(this->nume, nume);
  85.     } else {                                           //numele nou este nul - discutie despre obiectul curent
  86.         if(this->nume!=NULL) {
  87.             delete nume;
  88.         }
  89.         this->nume=NULL;
  90.     }
  91. }
  92. char *Produs::getNume() {
  93.     return nume;
  94.  
  95. }
  96. void Produs::setPret(float pret) {
  97.     if(pret>0)
  98.         this->pret=pret;
  99. }
  100. float Produs::getPret() {
  101.     return pret;
  102. }
  103.  
  104.  
  105. class Magazin {                              //clasa Magazin
  106. private:
  107.     int capacitate;//nr max de produse din magazin
  108.     int nrProduse;//nr efectiv de produse
  109.     Produs *produse;
  110. public:
  111.     Magazin(int capacitate);
  112.     Magazin(const Magazin &m);
  113.     ~Magazin();
  114.     void adaugare(Produs p);
  115.     void afisare();
  116.     void eliminare(Produs p);
  117. };
  118. Magazin::Magazin(int capacitate) {
  119.     this->capacitate = capacitate;
  120.     this->produse = new Produs[capacitate];
  121.     this->nrProduse=0;
  122. }
  123.  
  124.  
  125. Magazin::Magazin(const Magazin &m) {
  126.     this->capacitate = m.capacitate;
  127.     this->nrProduse=m.nrProduse;
  128.     this->produse = new Produs[m.capacitate];
  129.     for(int i=0; i<nrProduse; i++) {
  130.         this->produse[i]= m.produse[i];
  131.     }
  132. }
  133. void Magazin::adaugare(Produs p) {
  134.     if(nrProduse<capacitate) {
  135.         produse[nrProduse]=p;
  136.         nrProduse++;
  137.     } else {
  138.         cout<<"nu mai am loc"<<endl;
  139.     }
  140. }
  141. void Magazin::afisare() {
  142.     cout<<endl;
  143.     cout<<"Lista produselor este:"<<endl;
  144.     for(int i=0; i<nrProduse; i++) {
  145.         produse[i].afisare();
  146.     }
  147. }
  148. void Magazin::eliminare(Produs p) {
  149.     int gasit=-1;
  150.     for(int i=0; i<nrProduse; i++){
  151.         if(strcmp(p.getNume(),produse[i].getNume())==0){
  152.             gasit=i;
  153.             cout<<"Produsul a fost gasit, urmeaza a fi eliminat"<<endl;
  154.             break;
  155.         }
  156.     }
  157.         if(gasit>=0){
  158.             for(int i=gasit; i<nrProduse-1; i++){
  159.                 produse[i]=produse[i+1];
  160.             }
  161.             nrProduse--;
  162.         }
  163. }
  164.  
  165.  
  166. int main() {
  167.  
  168.  
  169.     cout<<endl;
  170.     Produs p1("vafa",4);
  171.     Produs p2("pufarina",1);
  172.     p1.afisare();
  173.     cout<<endl;
  174.     p2.afisare();
  175.     cout<<endl;
  176.     Magazin m(10);
  177.     m.adaugare(p1);
  178.     m.adaugare(p2);
  179.     m.afisare();
  180.     m.eliminare(p1);
  181.     return 0;
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement