Alx09

Poo Lista Cadouri v 1.01

Dec 11th, 2020 (edited)
771
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.89 KB | None | 0 0
  1. #include<iostream>
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #include<string>
  5. #include<time.h>
  6. #include<list>
  7. #include<iterator>
  8. #define _CRT_SECURE_NO_WARNING
  9. using namespace std;
  10.  
  11.  
  12.  
  13. typedef struct data {
  14.     unsigned short zi, luna, an;
  15. }data;
  16.  
  17. typedef struct adresa {
  18.     string localitate, judet, strada, codPostal;
  19.     unsigned short nr;
  20. }adresa;
  21. unsigned short GetAge(struct data &dataNastere) {
  22.     unsigned short age;
  23.     time_t t = time(NULL);
  24.     struct tm tm = *localtime(&t);
  25.     age = tm.tm_year + 1900 - dataNastere.an;
  26.     if (tm.tm_mon + 1 < dataNastere.luna) age--;
  27.     else if (tm.tm_mon + 1 == dataNastere.luna && tm.tm_mday < dataNastere.zi)age--;
  28.     return age;
  29. }
  30. unsigned short GetYear(unsigned short year) {
  31.     time_t t = time(NULL);
  32.     struct tm tm = *localtime(&t);
  33.     if (tm.tm_year % 100 < year) return 1800 + tm.tm_year + year - tm.tm_year % 100;
  34.     else
  35.         return tm.tm_year + 1900 - year- tm.tm_year % 100;
  36. }
  37.  
  38.  
  39. class Persoana;
  40. class Cadou
  41. {
  42. protected:
  43.     string nume, tip;
  44.     double pret;
  45. public:
  46.     Cadou(string nume, string tip, double pret);
  47.     virtual void Afisare() = 0;
  48.     string get_nume() {
  49.         return nume;
  50.     }
  51. };
  52. class Jucarii : public Cadou {
  53. private:
  54.     string marca;
  55.     bool bateriNecesare;
  56.     unsigned short varstaRecomandata;
  57. public:
  58.     Jucarii(string nume, string tip, double pret, string marca, bool bateriNecesare, unsigned short varstaRecomandata) :Cadou(nume, tip, pret) {
  59.         this->marca = marca;
  60.         this->bateriNecesare = bateriNecesare;
  61.         this->varstaRecomandata = varstaRecomandata;
  62.     }
  63.     void Afisare() {
  64.         cout << "\n" << nume << " " << marca << " " << tip;
  65.         if (bateriNecesare) cout << " bateri necesare ";
  66.         else cout << " nu sunt necesare bateri ";
  67.         cout << varstaRecomandata << " " << pret;
  68.     }
  69. };
  70. class PachetCadou : public Cadou {
  71. private:
  72.  
  73.     bool pentruBarbati;
  74.     unsigned short nrProd;
  75. public:
  76.     PachetCadou(string nume, string tip, double pret, bool pentruBarbati, unsigned short nrProd) :Cadou(nume, tip, pret) {
  77.         this->pentruBarbati = pentruBarbati;
  78.         this->nrProd = nrProd;
  79.     }
  80.     void Afisare() {
  81.         cout << "\n" << nume << " " << " " << tip << " " << nrProd;
  82.         if (pentruBarbati) cout << " de barbati ";
  83.         else cout << " de femei ";
  84.         cout << pret;
  85.     }
  86. };
  87. class Dulciuri : public Cadou {
  88. private:
  89.  
  90.     string ingrediente;
  91.     double calori, gramaj;
  92. public:
  93.     Dulciuri(string nume, string tip, double pret, string ingrediente, double calori, double gramaj) :Cadou(nume, tip, pret) {
  94.         this->ingrediente = ingrediente;
  95.         this->calori = calori;
  96.         this->gramaj = gramaj;
  97.     }
  98.     void Afisare() {
  99.         cout << "\n" << nume << " " << " " << tip << " " << gramaj << " " << round(calori * 100 / gramaj) << " " << ingrediente << " " << pret;
  100.  
  101.     }
  102. };
  103.  
  104. class Persoana {
  105. private:
  106.    
  107.     string nume;
  108.     char CNP[13];
  109.     unsigned short varsta;
  110.     bool isMan;
  111.     struct data dataNastere;
  112.     struct adresa adresa;
  113.     list <Cadou *> listaCadou;
  114.     double cheltuitCadouri;
  115. public:
  116.  
  117.     Persoana(string nume, char *CNP, struct adresa &adresa) {
  118.         this->nume = nume;
  119.         strcpy(this->CNP, CNP);
  120.         if (CNP[0] == '1' || CNP[0] == '5') isMan = true;
  121.         else isMan = false;
  122.         dataNastere.an = GetYear((CNP[1] - 48) * 10 + (CNP[2] - 48));
  123.         dataNastere.luna = (CNP[3] - 48) * 10 + (CNP[4] - 48);
  124.         dataNastere.zi = (CNP[5] - 48) * 10 + (CNP[6] - 48);
  125.         this->varsta = GetAge(dataNastere);
  126.         this->adresa.codPostal = adresa.codPostal;
  127.         this->adresa.judet = adresa.judet;
  128.         this->adresa.localitate = adresa.localitate;
  129.         this->adresa.nr = adresa.nr;
  130.         this->adresa.strada = adresa.strada;
  131.         this->cheltuitCadouri = 0.f;
  132.  
  133.     }
  134.     void CitireCadou();
  135.     char *get_CNP() {
  136.         return CNP;
  137.     }
  138.     string get_nume() {
  139.         return nume;
  140.     }
  141.     void Afisare() {
  142.         cout << "------------------------------------------------------------------------------------------\n";
  143.         cout << nume << " " << CNP<< varsta;
  144.         if (isMan) cout << " barbat\n";
  145.         else cout << " femeie\n";
  146.         cout << varsta<<"  "<< dataNastere.zi << "/" << dataNastere.luna << "/" << dataNastere.an<<endl;
  147.         cout << adresa.localitate << " " << adresa.judet <<" "<< adresa.strada << " " << " " << adresa.codPostal << " " << adresa.nr;
  148.         cout << "\nLista de cadouri:\n";
  149.  
  150.         cout << "=============================" << endl;
  151.         for (list <Cadou *>::iterator it = listaCadou.begin(); it != listaCadou.end(); it++) (*it)->Afisare();
  152.        
  153.         cout << "\n=============================" << endl;
  154.     }
  155. };
  156. list <Persoana *> listaPersone;
  157. list <Persoana *>::iterator it;
  158.  
  159. Cadou::Cadou(string nume, string tip, double pret)
  160. {
  161.     this->nume = nume;
  162.     this->pret = pret;
  163.     this->tip = tip;
  164.  
  165.  
  166. }
  167.  
  168. void Persoana::CitireCadou()
  169. {
  170.     list <Cadou *>::iterator it;
  171.     string nume, tip;
  172.     double pret;
  173.     char t;
  174.     getchar();
  175.     cout << "\nCe fel de cadou D- Dulciuri, P- Pachet Cadou, J -Jucarie: "; cin >> t;
  176.     cout << "Nume: "; cin >> nume;
  177.     cout << "Pret: "; cin >> pret;
  178.     cout << "Tip: "; cin >> tip;
  179.     cheltuitCadouri += pret;
  180.     if (t == 'P') {
  181.         string ingrediente;
  182.         double calori, gramaj;
  183.         cout << "Ingerdiente: ";   cin >> ingrediente;
  184.         cout << "Calori: ";     cin >>  calori;
  185.         cout << "Gramaj: "; cin >> gramaj;
  186.         if (listaCadou.empty())
  187.             listaCadou.push_back(new Dulciuri(nume, tip, pret,ingrediente, calori, gramaj));
  188.         else {
  189.             it = listaCadou.begin();
  190.             while (it != listaCadou.end() && (*it)->get_nume() < nume)
  191.                 advance(it, 1);
  192.             listaCadou.emplace(it, new Dulciuri(nume, tip, pret, ingrediente, calori, gramaj));
  193.         }
  194.     }
  195.     if (t == 'P') {
  196.    
  197.         bool pentruBarbati;
  198.         unsigned short nrProd;
  199.         cout << "Este pentru femei - 0, barbati - 1: ";   cin >> pentruBarbati;
  200.         cout << "Numar de produse in pachet: "; cin >> nrProd;
  201.         if (listaCadou.empty())
  202.             listaCadou.push_back(new PachetCadou(nume, tip, pret, pentruBarbati, nrProd));
  203.         else {
  204.             it = listaCadou.begin();
  205.             while (it != listaCadou.end() && (*it)->get_nume() < nume)
  206.                 advance(it, 1);
  207.             listaCadou.emplace(it, new PachetCadou(nume, tip, pret, pentruBarbati, nrProd));
  208.         }
  209.     }
  210.     if (t == 'J') {
  211.  
  212.         string marca;
  213.         bool bateriNecesare;
  214.         unsigned short varstaRecomandata;
  215.         cout << "Marca: ";   cin >> marca;
  216.         cout << "Are nevoie de bateri Nu - 0, Da - 1: ";   cin >> bateriNecesare;
  217.         cout << "Varsta recomandata: "; cin >> varstaRecomandata;
  218.         if (listaCadou.empty())
  219.             listaCadou.push_back(new Jucarii(nume, tip, pret, marca,  bateriNecesare, varstaRecomandata));
  220.         else {
  221.             it = listaCadou.begin();
  222.             while (it != listaCadou.end() && (*it)->get_nume() < nume)
  223.                 advance(it, 1);
  224.             listaCadou.emplace(it, new  Jucarii(nume, tip, pret, marca, bateriNecesare, varstaRecomandata));
  225.         }
  226.     }
  227.    
  228.    
  229. }
  230. bool VerificareCNP(char *CNP) {
  231.     for (it = listaPersone.begin(); it != listaPersone.end(); it++)
  232.         if ((*it)->get_CNP() == CNP) {
  233.             return  true;
  234.         }
  235.     return false;
  236. }
  237. void AdaugaPersoana() {
  238.     string nume;
  239.     char CNP[20];
  240.     Persoana *q;
  241.     struct adresa adresa;
  242.     getchar();
  243.     cout << "Nume persoana: "; getline(cin, nume);
  244.     cout << "CNP: "; cin >> CNP;
  245.     try
  246.     {
  247.         if (strlen(CNP) < 13) throw 1;
  248.         if (strlen(CNP) > 13) throw 2;
  249.         for (int i = 0; i < 13; i++)if (CNP[i] <'0' || CNP[i] > '9') throw 7;
  250.         if (VerificareCNP(CNP)) throw 3;
  251.         if (CNP[0] == '0' || CNP[0] == '3' || CNP[0] == '4' || CNP[0] == '7' || CNP[0] == '8' || CNP[0] == '9') throw 4;
  252.         if (CNP[3] > '1') throw 5;
  253.         if (CNP[3] == '1' && CNP[4] > '2') throw 6;
  254.  
  255.     }
  256.     catch (int error)
  257.     {
  258.         if (error == 1) cout << "\nCNP are prea putine cifre";
  259.         if (error == 2) cout << "\nCNP are prea multe cifre";
  260.         if (error == 3) cout << "\nCNP este deja in baza de date";
  261.         if (error == 4) cout << "\nPrima cifra din CNP este gresita";
  262.         if (error == 5) cout << "\nA patra cifra din CNP este gresita";
  263.         if (error == 6) cout << "\nA cincea cifra a CNP este gresita";
  264.         if (error == 7)cout <<  "\nAu fost introduse date invalide";
  265.         return;
  266.     }
  267.     getchar();
  268.     cout << "Localitatea: "; getline(cin, adresa.localitate);
  269.     cout << "Judet: "; getline(cin, adresa.judet);
  270.     cout << "Cod Postal: "; getline(cin, adresa.codPostal);
  271.     cout << "Strada: "; getline(cin, adresa.strada);
  272.     cout << "Numar casa: "; cin >> adresa.nr;
  273.     q = new Persoana(nume, CNP, adresa);
  274.     if (listaPersone.empty())
  275.         listaPersone.push_back(q);
  276.     else {
  277.         it = listaPersone.begin();
  278.         while (it != listaPersone.end() && (*it)->get_nume() < nume)
  279.             advance(it, 1);
  280.         listaPersone.emplace(it, q);
  281.     }
  282.     int nrCadouri;
  283.     cout << "Numar de cadouri: "; cin >> nrCadouri;
  284.     for (int i = 0; i < nrCadouri; i++) {
  285.         system("cls");
  286.         cout << "Cadoul " << i + 1 << endl;
  287.         q->CitireCadou();
  288.     }
  289. }
  290.  
  291.  
  292. int main() {
  293.    
  294.     int opt;
  295.     do {
  296.         cout << "\n\n";
  297.         cout << "1.Adaugare Persoana si lista de cadouri\n";
  298.         cout << "2.Afisare  persoane si lista caduri sub forma tabelara\n";
  299.         cout << "0.Iesire \n";
  300.         cout << "Dati optiunea dvs: ";
  301.         cin >> opt;
  302.         system("cls");
  303.         switch (opt) {
  304.         case 1:
  305.             AdaugaPersoana();
  306.             break;
  307.         case 2:
  308.             for (it = listaPersone.begin(); it != listaPersone.end(); it++)  (*it)->Afisare();
  309.             break;
  310.  
  311.         case 0:
  312.             return 0;
  313.  
  314.         }
  315.     } while (opt != 0);
  316.     return 0;
  317. }
  318.  
Advertisement
Add Comment
Please, Sign In to add comment