Alx09

POO Lista cadouri v1

Dec 11th, 2020
858
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.73 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 1900 + year;
  34.     else
  35.         return tm.tm_year + 1900 - year;
  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.     Persoana(string nume, char *CNP, struct adresa &adresa) {
  117.         this->nume = nume;
  118.         strcpy(this->CNP, CNP);
  119.         if (CNP[0] == '1' || '5') isMan = true;
  120.         else isMan = false;
  121.         dataNastere.an = GetYear((CNP[1] - 48) * 10 + (CNP[2] - 48));
  122.         dataNastere.luna = (CNP[3] - 48) * 10 + (CNP[4] - 48);
  123.         dataNastere.zi = (CNP[5] - 48) * 10 + (CNP[6] - 48);
  124.         this->adresa.codPostal = adresa.codPostal;
  125.         this->adresa.judet = adresa.judet;
  126.         this->adresa.localitate = adresa.localitate;
  127.         this->adresa.nr = adresa.nr;
  128.         this->adresa.strada = adresa.strada;
  129.         this->cheltuitCadouri = 0.f;
  130.  
  131.     }
  132.     void CitireCadou();
  133.     char *get_CNP() {
  134.         return CNP;
  135.     }
  136.     string get_nume() {
  137.         return nume;
  138.     }
  139.     void Afisare() {
  140.         cout << "------------------------------------------------------------------------------------------\n";
  141.         cout << nume << " " << CNP<< varsta;
  142.         if (isMan) cout << " barbat\n";
  143.         else cout << " femeie\n";
  144.         cout << dataNastere.zi << "/" << dataNastere.luna << "/" << dataNastere.an<<endl;
  145.         cout << adresa.localitate << " " << adresa.judet << adresa.strada << " " << " " << adresa.codPostal << " " << adresa.nr;
  146.         cout << "\nLista de cadouri:\n";
  147.  
  148.         cout << "=============================" << endl;
  149.         for (list <Cadou *>::iterator it = listaCadou.begin(); it != listaCadou.end(); it++) {
  150.             (*it)->Afisare();
  151.         }
  152.         cout << "=============================" << endl;
  153.     }
  154. };
  155. list <Persoana *> listaPersone;
  156. list <Persoana *>::iterator it;
  157.  
  158. Cadou::Cadou(string nume, string tip, double pret)
  159. {
  160.     this->nume = nume;
  161.     this->pret = pret;
  162.     this->tip = tip;
  163.  
  164.  
  165. }
  166.  
  167. void Persoana::CitireCadou()
  168. {
  169.     list <Cadou *>::iterator it;
  170.     string nume, tip;
  171.     double pret;
  172.     char t;
  173.     cout << "\nCe fel de cadou D- Dulciuri, P- Pachet Cadou, J -Jucarie: "; cin >> t;
  174.     cout << "Nume: "; cin >> nume;
  175.     cout << "Pret: "; cin >> pret;
  176.     cout << "Tip: "; cin >> tip;
  177.     cheltuitCadouri += pret;
  178.     if (t == 'P') {
  179.         string ingrediente;
  180.         double calori, gramaj;
  181.         cout << "Ingerdiente: ";   cin >> ingrediente;
  182.         cout << "Calori: ";     cin >>  calori;
  183.         cout << "Gramaj: "; cin >> gramaj;
  184.         if (listaCadou.empty())
  185.             listaCadou.push_back(new Dulciuri(nume, tip, pret,ingrediente, calori, gramaj));
  186.         else {
  187.             it = listaCadou.begin();
  188.             while (it != listaCadou.end() && (*it)->get_nume() < nume)
  189.                 advance(it, 1);
  190.             listaCadou.emplace(it, new Dulciuri(nume, tip, pret, ingrediente, calori, gramaj));
  191.         }
  192.     }
  193.     if (t == 'P') {
  194.    
  195.         bool pentruBarbati;
  196.         unsigned short nrProd;
  197.         cout << "Este pentru femei - 0, barbati - 1: ";   cin >> pentruBarbati;
  198.         cout << "Numar de produse in pachet: "; cin >> nrProd;
  199.         if (listaCadou.empty())
  200.             listaCadou.push_back(new PachetCadou(nume, tip, pret, pentruBarbati, nrProd));
  201.         else {
  202.             it = listaCadou.begin();
  203.             while (it != listaCadou.end() && (*it)->get_nume() < nume)
  204.                 advance(it, 1);
  205.             listaCadou.emplace(it, new PachetCadou(nume, tip, pret, pentruBarbati, nrProd));
  206.         }
  207.     }
  208.     if (t == 'J') {
  209.  
  210.         string marca;
  211.         bool bateriNecesare;
  212.         unsigned short varstaRecomandata;
  213.         cout << "Marca: ";   cin >> marca;
  214.         cout << "Are nevoie de bateri Nu - 0, Da - 1: ";   cin >> bateriNecesare;
  215.         cout << "Varsta recomandata: "; cin >> varstaRecomandata;
  216.         if (listaCadou.empty())
  217.             listaCadou.push_back(new Jucarii(nume, tip, pret, marca,  bateriNecesare, varstaRecomandata));
  218.         else {
  219.             it = listaCadou.begin();
  220.             while (it != listaCadou.end() && (*it)->get_nume() < nume)
  221.                 advance(it, 1);
  222.             listaCadou.emplace(it, new  Jucarii(nume, tip, pret, marca, bateriNecesare, varstaRecomandata));
  223.         }
  224.     }
  225.    
  226.    
  227. }
  228. bool VerificareCNP(char *CNP) {
  229.     for (it = listaPersone.begin(); it != listaPersone.end(); it++)
  230.         if ((*it)->get_CNP() == CNP) {
  231.             return  true;
  232.         }
  233.     return false;
  234. }
  235. void AdaugaPersoana() {
  236.     string nume;
  237.     char CNP[20];
  238.     Persoana *q;
  239.     struct adresa adresa;
  240.     cout << "Nume persoana: "; cin >> nume;
  241.     cout << "CNP: "; cin >> CNP;
  242.     try
  243.     {
  244.  
  245.  
  246.         if (strlen(CNP) < 13) throw 1;
  247.         if (strlen(CNP) > 13) throw 2;
  248.         for (int i = 0; i < 13; i++)if (CNP[i] <'0' || CNP[i] > '9') throw 7;
  249.         if (VerificareCNP(CNP)) throw 3;
  250.         if (CNP[0] == '0' || CNP[0] == '3' || CNP[0] == '4' || CNP[0] == '7' || CNP[0] == '8' || CNP[0] == '9') throw 4;
  251.         if (CNP[3] > '1') throw 5;
  252.         if (CNP[3] == '1' && CNP[4] > '2') throw 6;
  253.  
  254.     }
  255.     catch (int error)
  256.     {
  257.         if (error == 1) cout << "\nCNP are prea putine cifre";
  258.         if (error == 2) cout << "\nCNP are prea multe cifre";
  259.         if (error == 3) cout << "\nCNP este deja in baza de date";
  260.         if (error == 4) cout << "\nPrima cifra din CNP este gresita";
  261.         if (error == 5) cout << "\nA patra cifra din CNP este gresita";
  262.         if (error == 6) cout << "\nA cincea cifra a CNP este gresita";
  263.         if (error == 7)cout <<  "\nAu fost introduse date invalide";
  264.         return;
  265.     }
  266.  
  267.     cout << "Localitatea: "; cin >> adresa.localitate;
  268.     cout << "Judet: "; cin >> adresa.judet;
  269.     cout << "Cod Postal: "; cin >> adresa.codPostal;
  270.     cout << "Strada: "; cin >> adresa.strada;
  271.     cout << "Numar casa: "; cin >> adresa.nr;
  272.     q = new Persoana(nume, CNP, adresa);
  273.     if (listaPersone.empty())
  274.         listaPersone.push_back(q);
  275.     else {
  276.         it = listaPersone.begin();
  277.         while (it != listaPersone.end() && (*it)->get_nume() < nume)
  278.             advance(it, 1);
  279.         listaPersone.emplace(it, q);
  280.     }
  281.     int nrCadouri;
  282.     cout << "Numar de cadouri: "; cin >> nrCadouri;
  283.     for (int i = 0; i < nrCadouri; i++) {
  284.         system("cls");
  285.         cout << "Cadoul " << i + 1 << endl;
  286.         q->CitireCadou();
  287.     }
  288. }
  289.  
  290.  
  291. int main() {
  292.    
  293.     int opt;
  294.     do {
  295.         cout << "\n\n";
  296.         cout << "1.Adaugare Persoana si lista de cadouri\n";
  297.         cout << "2.Afisare  persoane si lista caduri sub forma tabelara\n";
  298.         cout << "0.Iesire \n";
  299.         cout << "Dati optiunea dvs: ";
  300.         cin >> opt;
  301.         system("cls");
  302.         switch (opt) {
  303.         case 1:
  304.             AdaugaPersoana();
  305.             break;
  306.         case 2:
  307.  
  308.             for (it = listaPersone.begin(); it != listaPersone.end(); it++) {
  309.                 (*it)->Afisare();
  310.            
  311.             }
  312.            
  313.             break;
  314.  
  315.         case 0:
  316.             break;
  317.  
  318.         }
  319.     } while (opt != 0);
  320.     return 0;
  321. }
  322.  
Advertisement
Add Comment
Please, Sign In to add comment