Alx09

Untitled

Dec 13th, 2020
917
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 13.32 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. class Exception: public exception {
  12. private:
  13.     int error;
  14.     string mesajEroare;
  15. public:
  16.     Exception(int error) {
  17.         this->error = error;
  18.         mesajEroare = "";
  19.     }
  20.     Exception(string mesajEroare) {
  21.         this->error = 0;
  22.         this->mesajEroare = mesajEroare;
  23.     }
  24.     virtual  void what() {
  25.         switch (error)
  26.         {
  27.         case 0:                                                                           break;
  28.         case 1:  cout << " CNP are prea putine cifre(CNP tebuie sa contina 13 cifre)\n";  break;
  29.         case 2:  cout << " CNP are prea multe cifre(CNP tebuie sa contina 13 cifre)\n";   break;
  30.         case 3:  cout << " CNP este deja in baza de date\n";                              break;
  31.         case 4:  cout << " Prima cifra din CNP este gresita\n";                           break;
  32.         case 5:  cout << " A patra cifra din CNP este gresita\n";                         break;
  33.         case 6:  cout << " A cincea cifra a CNP este gresita\n";                          break;
  34.         case 7:  cout << " Luna nasteri gresita\n";                                       break;
  35.         case 8:  cout << " A 6 cifra din CNP este gresita\n";                             break;
  36.         case 9:  cout << " A 7 cfra din CNP este gresita\n";                              break;
  37.         case 10: cout << " Ziua nasteri din CNP este gresita\n";                          break;
  38.         default: cout << " Au fost introduse date invalide\n";
  39.             break;
  40.         }
  41.         if (mesajEroare == "") return;
  42.         if (mesajEroare == "nume invalid") { cout << "Numele trebuie sa contine doar litere ale alfabetului englez,\n cel putin o litera si singurul carcater permies este '-'\n"; return; }
  43.         if (mesajEroare == "denumire invalida") { cout << "Denumirea aleasa contine date invalide \n"; return; }
  44.         if (mesajEroare == "CNP_not_found") { cout << "CNP introdus nu se gaseste in baza de date\n"; return; }
  45.         if (mesajEroare == "lista_goala") { cout << "Lista este goala\n"; return; }
  46.        
  47.     }
  48. };
  49.  
  50.  
  51. typedef struct data {
  52.     unsigned short zi, luna, an;
  53. }data;
  54.  
  55. typedef struct adresa {
  56.     string localitate, judet, strada, codPostal;
  57.     unsigned short nr;
  58. }adresa;
  59. unsigned short GetAge(struct data &dataNastere) {
  60.     unsigned short age;
  61.     time_t t = time(NULL);
  62.     struct tm tm = *localtime(&t);
  63.     age = tm.tm_year + 1900 - dataNastere.an;
  64.     if (tm.tm_mon + 1 < dataNastere.luna) age--;
  65.     else if (tm.tm_mon + 1 == dataNastere.luna && tm.tm_mday < dataNastere.zi)age--;
  66.     return age;
  67. }
  68. unsigned short GetYear(unsigned short year) {
  69.     time_t t = time(NULL);
  70.     struct tm tm = *localtime(&t);
  71.     if (tm.tm_year % 100 < year) return 1800 + tm.tm_year + year - tm.tm_year % 100;
  72.     else
  73.         return tm.tm_year + 1900 - year- tm.tm_year % 100;
  74. }
  75.  
  76.  
  77. class Persoana;
  78. class Cadou
  79. {
  80. protected:
  81.     string nume, tip;
  82.     double pret;
  83. public:
  84.     Cadou(string nume, string tip, double pret);
  85.     virtual void Afisare() = 0;
  86.     string get_nume() {
  87.         return nume;
  88.     }
  89. };
  90. class Jucarii : public Cadou {
  91. private:
  92.     string marca;
  93.     bool bateriNecesare;
  94.     unsigned short varstaRecomandata;
  95. public:
  96.     Jucarii(string nume, string tip, double pret, string marca, bool bateriNecesare, unsigned short varstaRecomandata) :Cadou(nume, tip, pret) {
  97.         this->marca = marca;
  98.         this->bateriNecesare = bateriNecesare;
  99.         this->varstaRecomandata = varstaRecomandata;
  100.     }
  101.     void Afisare() override {
  102.         cout << "\n" << nume << " " << marca << " " << tip;
  103.         if (bateriNecesare) cout << " bateri necesare ";
  104.         else cout << " nu sunt necesare bateri ";
  105.         cout << varstaRecomandata << " " << pret;
  106.     }
  107. };
  108. class PachetCadou : public Cadou {
  109. private:
  110.  
  111.     bool pentruBarbati;
  112.     unsigned short nrProd;
  113. public:
  114.     PachetCadou(string nume, string tip, double pret, bool pentruBarbati, unsigned short nrProd) :Cadou(nume, tip, pret) {
  115.         this->pentruBarbati = pentruBarbati;
  116.         this->nrProd = nrProd;
  117.     }
  118.     void Afisare() override {
  119.         cout << "\n" << nume << " " << " " << tip << " " << nrProd;
  120.         if (pentruBarbati) cout << " de barbati ";
  121.         else cout << " de femei ";
  122.         cout << pret;
  123.     }
  124. };
  125. class Dulciuri : public Cadou {
  126. private:
  127.  
  128.     string ingrediente;
  129.     double calori, gramaj;
  130. public:
  131.     Dulciuri(string nume, string tip, double pret, string ingrediente, double calori, double gramaj) :Cadou(nume, tip, pret) {
  132.         this->ingrediente = ingrediente;
  133.         this->calori = calori;
  134.         this->gramaj = gramaj;
  135.     }
  136.     void Afisare() final override {
  137.  
  138.         cout << "\n" << nume << " " << " " << tip << " " << gramaj << " " << round(calori * 100 / gramaj) << " " << ingrediente << " " << pret;
  139.  
  140.     }
  141. };
  142.  
  143. class Persoana {
  144. private:
  145.    
  146.     string nume;
  147.     char CNP[13];
  148.     unsigned short varsta;
  149.     bool isMan;
  150.     struct data dataNastere;
  151.     struct adresa adresa;
  152.     list <Cadou *> listaCadou;
  153.     double cheltuitCadouri;
  154. public:
  155.  
  156.     Persoana(string nume, char *CNP, struct adresa &adresa) {
  157.         this->nume = nume;
  158.         strcpy(this->CNP, CNP);
  159.         if (CNP[0] == '1' || CNP[0] == '5') isMan = true;
  160.         else isMan = false;
  161.         dataNastere.an = GetYear((CNP[1] - 48) * 10 + (CNP[2] - 48));
  162.         dataNastere.luna = (CNP[3] - 48) * 10 + (CNP[4] - 48);
  163.         dataNastere.zi = (CNP[5] - 48) * 10 + (CNP[6] - 48);
  164.         this->varsta = GetAge(dataNastere);
  165.         this->adresa.codPostal = adresa.codPostal;
  166.         this->adresa.judet = adresa.judet;
  167.         this->adresa.localitate = adresa.localitate;
  168.         this->adresa.nr = adresa.nr;
  169.         this->adresa.strada = adresa.strada;
  170.         this->cheltuitCadouri = 0.f;
  171.  
  172.     }
  173.     void CitireCadou();
  174.     char *get_CNP() {
  175.         return CNP;
  176.     }
  177.     string get_nume() {
  178.         return nume;
  179.     }
  180.     void Afisare() {
  181.         cout << "------------------------------------------------------------------------------------------\n";
  182.         cout << nume << " " << CNP<< varsta;
  183.         if (isMan) cout << " barbat\n";
  184.         else cout << " femeie\n";
  185.         cout << varsta << "  ";
  186.         if (dataNastere.zi < 10)cout << '0';
  187.         cout << dataNastere.zi << "/";
  188.         if(dataNastere.luna < 10) cout << '0';
  189.         cout << dataNastere.luna << "/" << dataNastere.an << endl;
  190.         cout << adresa.localitate << " " << adresa.judet <<" "<< adresa.strada << " " << " " << adresa.codPostal << " " << adresa.nr;
  191.         cout << "\nLista de cadouri:\n";
  192.  
  193.         cout << "=============================" << endl;
  194.         try
  195.         {
  196.             if (listaCadou.empty()) throw Exception("lista_goala");
  197.         }
  198.         catch (Exception &e)
  199.         {
  200.             e.what();
  201.             cout << "\n=============================" << endl;
  202.             return;
  203.         }
  204.        
  205.         for (list <Cadou *>::iterator it = listaCadou.begin(); it != listaCadou.end(); it++) (*it)->Afisare();
  206.         cout << "\n=============================" << endl;
  207.     }
  208.     void AfisareCheltuitTotal() {
  209.         cout << "Buget cheltuit = " << cheltuitCadouri << " ron" << endl;
  210.     }
  211. };
  212. list <Persoana *> listaPersone;
  213. list <Persoana *>::iterator it;
  214.  
  215. Cadou::Cadou(string nume, string tip, double pret){
  216.     this->nume = nume;
  217.     this->pret = pret;
  218.     this->tip = tip;
  219. }
  220.  
  221. void Persoana::CitireCadou()
  222. {
  223.     list <Cadou *>::iterator it;
  224.     string nume, tip;
  225.     double pret;
  226.     char t;
  227.     getchar();
  228.     cout << "\nCe fel de cadou D-Dulciuri, P- Pachet Cadou, J -Jucarie: "; cin >> t;
  229.     getchar();
  230.     cout << "Nume: "; getline(cin, nume);
  231.     cout << "Pret: "; cin >> pret;
  232.     getchar();
  233.     cout << "Tip: "; getline(cin, tip);
  234.     cheltuitCadouri += pret;
  235.     if (t == 'D') {
  236.         string ingrediente;
  237.         double calori, gramaj;
  238.         cout << "Ingerdiente: ";   getline(cin, ingrediente);
  239.         cout << "Calori: ";        cin >>   calori;
  240.         cout << "Gramaj: ";        cin >> gramaj;
  241.         if (listaCadou.empty())
  242.             listaCadou.push_back(new Dulciuri(nume, tip, pret,ingrediente, calori, gramaj));
  243.         else {
  244.             it = listaCadou.begin();
  245.             while (it != listaCadou.end() && (*it)->get_nume() < nume)
  246.                 advance(it, 1);
  247.             listaCadou.emplace(it, new Dulciuri(nume, tip, pret, ingrediente, calori, gramaj));
  248.         }
  249.     }
  250.     if (t == 'P') {
  251.    
  252.         bool pentruBarbati;
  253.         unsigned short nrProd;
  254.         cout << "Este pentru femei - 0, barbati - 1: ";   cin >> pentruBarbati;
  255.         cout << "Numar de produse in pachet: "; cin >> nrProd;
  256.         if (listaCadou.empty())
  257.             listaCadou.push_back(new PachetCadou(nume, tip, pret, pentruBarbati, nrProd));
  258.         else {
  259.             it = listaCadou.begin();
  260.             while (it != listaCadou.end() && (*it)->get_nume() < nume)
  261.                 advance(it, 1);
  262.             listaCadou.emplace(it, new PachetCadou(nume, tip, pret, pentruBarbati, nrProd));
  263.         }
  264.     }
  265.     if (t == 'J') {
  266.  
  267.         string marca;
  268.         bool bateriNecesare;
  269.         unsigned short varstaRecomandata;
  270.         cout << "Marca: ";   getline(cin, marca);
  271.         cout << "Are nevoie de bateri Nu - 0, Da - 1: ";   cin >> bateriNecesare;
  272.         cout << "Varsta recomandata: "; cin >> varstaRecomandata;
  273.         if (listaCadou.empty())
  274.             listaCadou.push_back(new Jucarii(nume, tip, pret, marca,  bateriNecesare, varstaRecomandata));
  275.         else {
  276.             it = listaCadou.begin();
  277.             while (it != listaCadou.end() && (*it)->get_nume() < nume)
  278.                 advance(it, 1);
  279.             listaCadou.emplace(it, new  Jucarii(nume, tip, pret, marca, bateriNecesare, varstaRecomandata));
  280.         }
  281.     }
  282.    
  283.    
  284. }
  285. void VerificareCNP(char *CNP){
  286.     if (strlen(CNP) < 13)              throw Exception(1);
  287.     if (strlen(CNP) > 13)              throw Exception(2);
  288.     if (CNP[0] == '0' || CNP[0] == '3' || CNP[0] == '4' || CNP[0] == '7' || CNP[0] == '8' || CNP[0] == '9') throw Exception(4);
  289.     if (CNP[3] > '1')                  throw Exception(5);
  290.     if (CNP[3] == '1' && CNP[4] > '2') throw Exception(6);
  291.     if (CNP[3] == '0' && CNP[4] == '0') throw Exception(7);
  292.     if (CNP[5] > '3')                  throw Exception(8);
  293.     if (CNP[5] == '3' && CNP[6] > '1') throw Exception(9);
  294.     if (CNP[5] == '0' && CNP[6] == '0')throw Exception(10);
  295.     for (int i = 0; i < 13; i++)if (CNP[i] <'0' || CNP[i] > '9') throw Exception(99999);
  296.  
  297. }
  298. void VerificareNume(string &nume) {
  299.     while (nume[0] == ' ' || nume[0] == '-')
  300.     {
  301.         while (nume[0] == ' ') nume.erase(0, 1);
  302.         while (nume[0] == '-') nume.erase(0, 1);
  303.     }
  304.  
  305.     if (nume == "")throw Exception("nume invalid");
  306.     for (int i = 0, n = nume.size(); i < n; i++)
  307.         if (nume[i] == ' ' || nume[i] == '-') continue;
  308.         else if (nume[i] >= 'a' && nume[i] <= 'z') continue;
  309.         else if (nume[i] >= 'A' && nume[i] <= 'Z') continue;
  310.         else throw Exception("nume invalid");
  311. }
  312. void AdaugaPersoana() {
  313.     string nume;
  314.     char CNP[20];
  315.     Persoana *q;
  316.     struct adresa adresa;
  317.     getchar();
  318.     CitireNume:
  319.     try {
  320.         cout << "Nume persoana( valoarea 0 pentru a reveni la meniu): "; getline(cin, nume);
  321.         VerificareNume(nume);
  322.     }
  323.     catch (Exception &e) {
  324.         if (nume == "0") return;
  325.         e.what();
  326.         goto CitireNume;
  327.     }
  328.    
  329.  
  330.     CitireCNP:
  331.     try
  332.     {
  333.         cout << "CNP( valoarea 0 pentru a reveni la meniu):  "; cin >> CNP;
  334.         VerificareCNP(CNP);
  335.         for (it = listaPersone.begin(); it != listaPersone.end(); it++)
  336.             if ((*it)->get_CNP() == CNP)
  337.                 throw Exception(3);
  338.  
  339.     }
  340.     catch (Exception &e)
  341.     {
  342.         if (strcmp(CNP, "0") == 0) return;
  343.         e.what();
  344.         goto CitireCNP;
  345.     }
  346.  
  347.    
  348.     getchar();
  349.     cout << "Localitatea: "; getline(cin, adresa.localitate);
  350.     cout << "Judet: "; getline(cin, adresa.judet);
  351.     cout << "Cod Postal: "; getline(cin, adresa.codPostal);
  352.     cout << "Strada: "; getline(cin, adresa.strada);
  353.     cout << "Numar casa: "; cin >> adresa.nr;
  354.     q = new Persoana(nume, CNP, adresa);
  355.     if (listaPersone.empty())
  356.         listaPersone.push_back(q);
  357.     else {
  358.         it = listaPersone.begin();
  359.         while (it != listaPersone.end() && (*it)->get_nume() < nume)
  360.             advance(it, 1);
  361.         listaPersone.emplace(it, q);
  362.     }
  363.     int nrCadouri;
  364.     cout << "Numar de cadouri: "; cin >> nrCadouri;
  365.     for (int i = 0; i < nrCadouri; i++) {
  366.         system("cls");
  367.         cout << "Cadoul " << i + 1 << endl;
  368.         q->CitireCadou();
  369.     }
  370. }
  371. void CitireCadou(char *CNP) {
  372.     try { if (listaPersone.empty()) throw Exception("lista_goala"); }
  373.     catch (Exception &e) { e.what(); }
  374.     char CNP2[20];
  375.     if (CNP == NULL) {
  376.     CitireCNP:
  377.         try
  378.         {
  379.             cout << "CNP( valoarea 0 pentru a reveni la meniu): "; cin >> CNP2;
  380.             VerificareCNP(CNP2);
  381.             for (it = listaPersone.begin(); it != listaPersone.end(); it++)
  382.                 if (strcmp((*it)->get_CNP(), CNP2) == 0) break;
  383.             if (it == listaPersone.end()) throw Exception("CNP_not_found");
  384.  
  385.         }
  386.         catch (Exception &e)
  387.         {
  388.             if (strcmp(CNP2, "0") == 0) return;
  389.             e.what();
  390.             goto CitireCNP;
  391.         }
  392.         CNP = CNP2;
  393.     }
  394.    
  395.     int nrCadouri;
  396.     (*it)->Afisare();
  397.     cout << "Numar de cadouri: "; cin >> nrCadouri;
  398.     for (int i = 0; i < nrCadouri; i++) {
  399.         system("cls");
  400.         cout << "Cadoul " << i + 1 << endl;
  401.         (*it)->CitireCadou();
  402.     }
  403. }
  404. void CautarePersoana() {
  405.     char CNP[20];
  406.     CitireCNP:
  407.         try
  408.         {
  409.             cout << "CNP( valoarea 0 pentru a reveni la meniu): "; cin >> CNP;
  410.             VerificareCNP(CNP);
  411.             for (it = listaPersone.begin(); it != listaPersone.end(); it++)
  412.                 if (strcmp((*it)->get_CNP(), CNP) == 0) break;
  413.             if (it == listaPersone.end()) throw Exception("CNP_not_found");
  414.  
  415.         }
  416.         catch (Exception &e)
  417.         {
  418.             if (strcmp(CNP, "0") == 0) return;
  419.             e.what();
  420.             goto CitireCNP;
  421.         }
  422.         (*it)->Afisare();
  423.         (*it)->AfisareCheltuitTotal();
  424. }
  425.  
  426. int main() {
  427.    
  428.     int opt;
  429.     do {
  430.         cout << "\n\n";
  431.         cout << "1. Adaugare persoana si lista de cadouri\n";
  432.         cout << "2. Afisare  persoane si lista cadouri sub forma tabelara\n";
  433.         cout << "3. Adaugare cadou pentru o persoana oarecare\n";
  434.         cout << "4. Cautarea unei persoane, afisarea listei de cadouri si pretul total\n";
  435.         cout << "0. Iesire \n";
  436.         cout << "Dati optiunea dvs: ";
  437.         cin >> opt;
  438.         system("cls");
  439.         switch (opt) {
  440.         case 1:
  441.             AdaugaPersoana();
  442.             break;
  443.         case 2:
  444.             try { if (listaPersone.empty()) throw Exception("lista_goala"); }
  445.             catch (Exception &e) { e.what(); }
  446.        
  447.             for (it = listaPersone.begin(); it != listaPersone.end(); it++)     (*it)->Afisare();
  448.            
  449.             break;
  450.         case 3:
  451.             CitireCadou(NULL); break;
  452.         case 4:
  453.             CautarePersoana(); break;
  454.         case 0:
  455.             return 0;
  456.  
  457.         }
  458.     } while (1);
  459.     return 0;
  460. }
  461.  
Advertisement
Add Comment
Please, Sign In to add comment