Advertisement
Alx09

Untitled

Dec 14th, 2020
706
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 19.92 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. #include<exception>
  9. #include<fstream>
  10. using namespace std;
  11.  
  12. #define _CRT_SECURE_NO_WARNING
  13. class Exception : public exception {
  14. private:
  15.     int error;
  16.     string mesajEroare;
  17. public:
  18.     Exception(int error) {
  19.         this->error = error;
  20.         mesajEroare = "";
  21.     }
  22.     Exception(string mesajEroare) {
  23.         this->error = 0;
  24.         this->mesajEroare = mesajEroare;
  25.     }
  26.     const char* what() const throw() {
  27.         switch (error)
  28.         {
  29.         case 0: break;
  30.         case 1:  return " CNP are prea putine cifre(CNP tebuie sa contina 13 cifre)\n";
  31.         case 2:  return " CNP are prea multe cifre(CNP tebuie sa contina 13 cifre)\n";
  32.         case 3:  return " CNP este deja in baza de date\n";
  33.         case 4:  return " Prima cifra din CNP este gresita\n";
  34.         case 5:  return " A patra cifra din CNP este gresita\n";
  35.         case 6:  return " A cincea cifra a CNP este gresita\n";
  36.         case 7:  return " Luna nasteri gresita\n";
  37.         case 8:  return " A 6 cifra din CNP este gresita\n";
  38.         case 9:  return " A 7 cfra din CNP este gresita\n";
  39.         case 10: return " Ziua nasteri din CNP este gresita\n";
  40.         default: return " Au fost introduse date invalide\n";
  41.             break;
  42.         }
  43.         if (mesajEroare == "") return "Nu au fost intruse date";
  44.         if (mesajEroare == "nume invalid") return "Numele trebuie sa contine doar litere ale alfabetului englez,\n cel putin o litera si singurul carcater permies este '-'\n";
  45.         if (mesajEroare == "denumire invalida")  return "Denumirea aleasa contine date invalide \n";
  46.         if (mesajEroare == "CNP_not_found")  return  "CNP introdus nu se gaseste in baza de date\n";
  47.         if (mesajEroare == "lista_goala")  return "Lista este goala\n";
  48.         if (mesajEroare == "nume_negasit") return "Numele cautat nu se afla in baza de date\n";
  49.         return "Eroare nespcificata";
  50.     }
  51. };
  52.  
  53.  
  54. typedef struct data {
  55.     unsigned short zi, luna, an;
  56. }data;
  57.  
  58. typedef struct adresa {
  59.     string localitate, judet, strada, codPostal;
  60.     unsigned short nr;
  61. }adresa;
  62. unsigned short GetAge(struct data &dataNastere) {
  63.     unsigned short age;
  64.     time_t t = time(NULL);
  65.     struct tm tm = *localtime(&t);
  66.     age = tm.tm_year + 1900 - dataNastere.an;
  67.     if (tm.tm_mon + 1 < dataNastere.luna) age--;
  68.     else if (tm.tm_mon + 1 == dataNastere.luna && tm.tm_mday < dataNastere.zi)age--;
  69.     return age;
  70. }
  71. unsigned short GetYear(unsigned short year) {
  72.     time_t t = time(NULL);
  73.     struct tm tm = *localtime(&t);
  74.     if (tm.tm_year % 100 < year) return 1800 + tm.tm_year + year - tm.tm_year % 100;
  75.     else
  76.         return tm.tm_year + 1900 - year - tm.tm_year % 100;
  77. }
  78.  
  79.  
  80. class Persoana;
  81. class Cadou
  82. {
  83. protected:
  84.     string nume, tip;
  85.     double pret;
  86. public:
  87.     Cadou(string nume, string tip, double pret);
  88.     Cadou() {}
  89.     virtual void Afisare() = 0;
  90.     string get_nume() {
  91.         return nume;
  92.     }
  93.     string get_tip() {
  94.         return tip;
  95.     }
  96. };
  97. class Jucarii : public Cadou {
  98. private:
  99.     string marca;
  100.     bool bateriNecesare;
  101.     unsigned short varstaRecomandata;
  102. public:
  103.     Jucarii(string nume, string tip, double pret, string marca, bool bateriNecesare, unsigned short varstaRecomandata) :Cadou(nume, tip, pret) {
  104.         this->marca = marca;
  105.         this->bateriNecesare = bateriNecesare;
  106.         this->varstaRecomandata = varstaRecomandata;
  107.     }
  108.     void Afisare() override {
  109.         cout << "\n" << nume << " " << marca << " " << tip;
  110.         if (bateriNecesare) cout << " bateri necesare ";
  111.         else cout << " nu sunt necesare bateri ";
  112.         cout << varstaRecomandata << " " << pret;
  113.     }
  114.    
  115. };
  116. class PachetCadou : public Cadou {
  117. private:
  118.  
  119.     bool pentruBarbati;
  120.     unsigned short nrProd;
  121. public:
  122.     PachetCadou(string nume, string tip, double pret, bool pentruBarbati, unsigned short nrProd) :Cadou(nume, tip, pret) {
  123.         this->pentruBarbati = pentruBarbati;
  124.         this->nrProd = nrProd;
  125.     }
  126.    
  127.     void Afisare() override {
  128.         cout << "\n" << nume << " " << " " << tip << " " << nrProd;
  129.         if (pentruBarbati) cout << " de barbati ";
  130.         else cout << " de femei ";
  131.         cout << pret;
  132.     }
  133. };
  134. class Dulciuri : public Cadou {
  135. private:
  136.  
  137.     string ingrediente;
  138.     double calori, gramaj;
  139. public:
  140.     Dulciuri(string nume, string tip, double pret, string ingrediente, double calori, double gramaj) :Cadou(nume, tip, pret) {
  141.         this->ingrediente = ingrediente;
  142.         this->calori = calori;
  143.         this->gramaj = gramaj;
  144.     }
  145.     void Afisare() final override {
  146.  
  147.         cout << "\n" << nume << " " << " " << tip << " " << gramaj << " " << round(calori * 100 / gramaj) << " " << ingrediente << " " << pret;
  148.  
  149.     }
  150. };
  151.  
  152. class Persoana {
  153. private:
  154.  
  155.     string nume;
  156.     char CNP[13];
  157.     unsigned short varsta;
  158.     bool isMan, addedByFile;
  159.     struct data dataNastere;
  160.     struct adresa adresa;
  161.     list <Cadou *> listaCadou;
  162.     double cheltuitCadouri;
  163. public:
  164.  
  165.     Persoana(string nume, char *CNP, struct adresa &adresa) {
  166.         this->nume = nume;
  167.         strcpy(this->CNP, CNP);
  168.         if (CNP[0] == '1' || CNP[0] == '5') isMan = true;
  169.         else isMan = false;
  170.         dataNastere.an = GetYear((CNP[1] - 48) * 10 + (CNP[2] - 48));
  171.         dataNastere.luna = (CNP[3] - 48) * 10 + (CNP[4] - 48);
  172.         dataNastere.zi = (CNP[5] - 48) * 10 + (CNP[6] - 48);
  173.         this->varsta = GetAge(dataNastere);
  174.         this->adresa.codPostal = adresa.codPostal;
  175.         this->adresa.judet = adresa.judet;
  176.         this->adresa.localitate = adresa.localitate;
  177.         this->adresa.nr = adresa.nr;
  178.         this->adresa.strada = adresa.strada;
  179.         this->cheltuitCadouri = 0.f;
  180.         this->addedByFile = false;
  181.  
  182.     }
  183.     void CitireCadou();
  184.     char *get_CNP() {
  185.         return CNP;
  186.     }
  187.     string get_nume() {
  188.         return nume;
  189.     }
  190.     void Afisare(bool afisareCadouri) {
  191.         cout << "------------------------------------------------------------------------------------------\n";
  192.         cout << nume << " " << CNP;
  193.         if (isMan) cout << " barbat\n";
  194.         else cout << " femeie\n";
  195.         cout << varsta << "  ";
  196.         if (dataNastere.zi < 10)cout << '0';
  197.         cout << dataNastere.zi << "/";
  198.         if (dataNastere.luna < 10) cout << '0';
  199.         cout << dataNastere.luna << "/" << dataNastere.an << endl;
  200.         cout << adresa.localitate << " " << adresa.judet << " " << adresa.strada << " " << " " << adresa.codPostal << " " << adresa.nr;
  201.         if (afisareCadouri == false) return;
  202.         cout << "\nLista de cadouri:\n";
  203.  
  204.         cout << "=============================" << endl;
  205.         try
  206.         {
  207.             if (listaCadou.empty()) throw Exception("lista_goala");
  208.         }
  209.         catch (exception &e)
  210.         {
  211.             e.what();
  212.             cout << "\n=============================" << endl;
  213.             return;
  214.         }
  215.  
  216.         for (list <Cadou *>::iterator it = listaCadou.begin(); it != listaCadou.end(); it++) (*it)->Afisare();
  217.         cout << "\n=============================" << endl;
  218.     }
  219.     void AfisareCheltuitTotal() {
  220.         cout << "Buget cheltuit = " << cheltuitCadouri << " ron" << endl;
  221.     }
  222.     bool CautCadou(string tip) {
  223.         for (list <Cadou *>::iterator it = listaCadou.begin(); it != listaCadou.end(); it++)
  224.             if ((*it)->get_tip() == tip) return true;
  225.         return false;
  226.     }
  227.     void DublareCadou();
  228.     void StergeCadou();
  229.     void ClearList() { listaCadou.clear(); }
  230.     void AddedByFile(bool added) {
  231.         this->addedByFile = added;
  232.     }
  233.     void CitireCadouriFiser(ifstream &f);
  234. };
  235. list <Persoana *> listaPersone;
  236. list <Persoana *>::iterator it;
  237.  
  238. Cadou::Cadou(string nume, string tip, double pret) {
  239.     this->nume = nume;
  240.     this->pret = pret;
  241.     this->tip = tip;
  242. }
  243.  
  244. void Persoana::CitireCadou()
  245. {
  246.     list <Cadou *>::iterator it;
  247.     string nume, tip;
  248.     double pret;
  249.     char t;
  250.     getchar();
  251.     cout << "\nCe fel de cadou D-Dulciuri, P- Pachet Cadou, J -Jucarie: "; cin >> t;
  252.     getchar();
  253.     cout << "Nume: "; getline(cin, nume);
  254.     cout << "Pret: "; cin >> pret;
  255.     getchar();
  256.     cout << "Tip: "; getline(cin, tip);
  257.     cheltuitCadouri += pret;
  258.     if (t == 'D') {
  259.         string ingrediente;
  260.         double calori, gramaj;
  261.         cout << "Ingerdiente: ";   getline(cin, ingrediente);
  262.         cout << "Calori: ";        cin >> calori;
  263.         cout << "Gramaj: ";        cin >> gramaj;
  264.         if (listaCadou.empty())
  265.             listaCadou.push_back(new Dulciuri(nume, tip, pret, ingrediente, calori, gramaj));
  266.         else {
  267.             it = listaCadou.begin();
  268.             while (it != listaCadou.end() && (*it)->get_nume() < nume)
  269.                 advance(it, 1);
  270.             listaCadou.emplace(it, new Dulciuri(nume, tip, pret, ingrediente, calori, gramaj));
  271.         }
  272.     }
  273.     if (t == 'P') {
  274.  
  275.         bool pentruBarbati;
  276.         unsigned short nrProd;
  277.         cout << "Este pentru femei - 0, barbati - 1: ";   cin >> pentruBarbati;
  278.         cout << "Numar de produse in pachet: "; cin >> nrProd;
  279.         if (listaCadou.empty())
  280.             listaCadou.push_back(new PachetCadou(nume, tip, pret, pentruBarbati, nrProd));
  281.         else {
  282.             it = listaCadou.begin();
  283.             while (it != listaCadou.end() && (*it)->get_nume() < nume)
  284.                 advance(it, 1);
  285.             listaCadou.emplace(it, new PachetCadou(nume, tip, pret, pentruBarbati, nrProd));
  286.         }
  287.     }
  288.     if (t == 'J') {
  289.  
  290.         string marca;
  291.         bool bateriNecesare;
  292.         unsigned short varstaRecomandata;
  293.         cout << "Marca: ";   getline(cin, marca);
  294.         cout << "Are nevoie de bateri Nu - 0, Da - 1: ";   cin >> bateriNecesare;
  295.         cout << "Varsta recomandata: "; cin >> varstaRecomandata;
  296.         if (listaCadou.empty())
  297.             listaCadou.push_back(new Jucarii(nume, tip, pret, marca, bateriNecesare, varstaRecomandata));
  298.         else {
  299.             it = listaCadou.begin();
  300.             while (it != listaCadou.end() && (*it)->get_nume() < nume)
  301.                 advance(it, 1);
  302.             listaCadou.emplace(it, new  Jucarii(nume, tip, pret, marca, bateriNecesare, varstaRecomandata));
  303.         }
  304.     }
  305.  
  306.  
  307. }
  308. void Persoana::DublareCadou() {
  309.     string nume;
  310.     list <Cadou *>::iterator it;
  311.     getchar();
  312.     CitireNumeCadou:
  313.     cout << "Introduceti nume cadou: "; getline(cin, nume);
  314.     for (it = listaCadou.begin(); it != listaCadou.end(); it++)
  315.         if ((*it)->get_nume() == nume)break;
  316.     try {
  317.         if (it == listaCadou.end()) throw Exception("nume_negasit");
  318.     }
  319.     catch (Exception &e) {
  320.         if (nume == "0") return;
  321.         cerr << e.what();
  322.         goto CitireNumeCadou;
  323.     }
  324.     listaCadou.emplace(it, *it);
  325.  
  326. }
  327. void Persoana::StergeCadou() {
  328. list <Cadou *>::iterator it1;
  329. string nume;
  330. CitireNumeCadou:
  331.     cout << "Introduceti nume cadou: "; getline(cin, nume);
  332.     for (it1 = listaCadou.begin(); it1 != listaCadou.end(); it1++)
  333.         if ((*it1)->get_nume() == nume)break;
  334.     try {
  335.         if (it1 == listaCadou.end()) throw Exception("nume_negasit");
  336.     }
  337.     catch (Exception &e) {
  338.         if (nume == "0") return;
  339.         cerr << e.what();
  340.         goto CitireNumeCadou;
  341.     }
  342.     listaCadou.erase(it1, it1);
  343. }
  344. void Persoana::CitireCadouriFiser(ifstream &f) {
  345.     char temp;
  346.     string nume, tip;
  347.     double pret;
  348.     list <Cadou *>::iterator it;
  349.     getline(f, nume, '\n');
  350.  
  351.     while (!f.eof()) {
  352.         f >> temp;
  353.         if (temp == '\n' || temp == ' ')return;
  354.         if (f.eof()) return;
  355.         getline(f, nume, '\n');
  356.         getline(f, nume, '\n');
  357.         getline(f, tip, '\n');
  358.         f >> pret;
  359.         if (temp == 'D') {
  360.             string ingrediente;
  361.             double calori, gramaj;
  362.             getline(f, ingrediente, '\n');
  363.             getline(f, ingrediente, '\n');
  364.             f >> calori >> gramaj;
  365.             if (listaCadou.empty())
  366.                 listaCadou.push_back(new Dulciuri(nume, tip, pret, ingrediente, calori, gramaj));
  367.             else {
  368.                 it = listaCadou.begin();
  369.                 while (it != listaCadou.end() && (*it)->get_nume() < nume)
  370.                     advance(it, 1);
  371.                 listaCadou.emplace(it, new Dulciuri(nume, tip, pret, ingrediente, calori, gramaj));
  372.             }
  373.         }
  374.         else if (temp == 'P') {
  375.  
  376.             bool pentruBarbati;
  377.             unsigned short nrProd;
  378.              f >> pentruBarbati;
  379.              f >> nrProd;
  380.             if (listaCadou.empty())
  381.                 listaCadou.push_back(new PachetCadou(nume, tip, pret, pentruBarbati, nrProd));
  382.             else {
  383.                 it = listaCadou.begin();
  384.                 while (it != listaCadou.end() && (*it)->get_nume() < nume)
  385.                     advance(it, 1);
  386.                 listaCadou.emplace(it, new PachetCadou(nume, tip, pret, pentruBarbati, nrProd));
  387.             }
  388.         }
  389.         else if (temp == 'J') {
  390.  
  391.             string marca;
  392.             bool bateriNecesare;
  393.             unsigned short varstaRecomandata;
  394.              getline(f, marca, '\n');
  395.              getline(f, marca, '\n');
  396.             f >> bateriNecesare;
  397.             f >> varstaRecomandata;
  398.             if (listaCadou.empty())
  399.                 listaCadou.push_back(new Jucarii(nume, tip, pret, marca, bateriNecesare, varstaRecomandata));
  400.             else {
  401.                 it = listaCadou.begin();
  402.                 while (it != listaCadou.end() && (*it)->get_nume() < nume)
  403.                     advance(it, 1);
  404.                 listaCadou.emplace(it, new  Jucarii(nume, tip, pret, marca, bateriNecesare, varstaRecomandata));
  405.             }
  406.         }
  407.     }
  408. }
  409. void VerificareCNP(char *CNP) {
  410.     if (strlen(CNP) < 13)              throw Exception(1);
  411.     if (strlen(CNP) > 13)              throw Exception(2);
  412.     if (CNP[0] == '0' || CNP[0] == '3' || CNP[0] == '4' || CNP[0] == '7' || CNP[0] == '8' || CNP[0] == '9') throw Exception(4);
  413.     if (CNP[3] > '1')                  throw Exception(5);
  414.     if (CNP[3] == '1' && CNP[4] > '2') throw Exception(6);
  415.     if (CNP[3] == '0' && CNP[4] == '0') throw Exception(7);
  416.     if (CNP[5] > '3')                  throw Exception(8);
  417.     if (CNP[5] == '3' && CNP[6] > '1') throw Exception(9);
  418.     if (CNP[5] == '0' && CNP[6] == '0')throw Exception(10);
  419.     for (int i = 0; i < 13; i++)if (CNP[i] <'0' || CNP[i] > '9') throw Exception(99999);
  420.  
  421. }
  422. void VerificareNume(string &nume) {
  423.     while (nume[0] == ' ' || nume[0] == '-')
  424.     {
  425.         while (nume[0] == ' ') nume.erase(0, 1);
  426.         while (nume[0] == '-') nume.erase(0, 1);
  427.     }
  428.  
  429.     if (nume == "")throw Exception("nume invalid");
  430.     for (int i = 0, n = nume.size(); i < n; i++)
  431.         if (nume[i] == ' ' || nume[i] == '-') continue;
  432.         else if (nume[i] >= 'a' && nume[i] <= 'z') continue;
  433.         else if (nume[i] >= 'A' && nume[i] <= 'Z') continue;
  434.         else throw Exception("nume invalid");
  435. }
  436. void AdaugaPersoana() {
  437.     string nume;
  438.     char CNP[20];
  439.     Persoana *q;
  440.     struct adresa adresa;
  441.     getchar();
  442. CitireNume:
  443.  
  444.     try {
  445.         cout << "Nume persoana( valoarea 0 pentru a reveni la meniu): "; getline(cin, nume);
  446.         VerificareNume(nume);
  447.     }
  448.     catch (exception &e) {
  449.         if (nume == "0") return;
  450.         cerr << e.what();
  451.         goto CitireNume;
  452.     }
  453.  
  454.  
  455. CitireCNP:
  456.     try
  457.     {
  458.         cout << "CNP( valoarea 0 pentru a reveni la meniu):  "; cin >> CNP;
  459.         VerificareCNP(CNP);
  460.         for (it = listaPersone.begin(); it != listaPersone.end(); it++)
  461.             if (strcmp((*it)->get_CNP(), CNP) == 0)
  462.                 throw Exception(3);
  463.  
  464.     }
  465.     catch (exception &e)
  466.     {
  467.         if (strcmp(CNP, "0") == 0) return;
  468.         cerr << e.what();
  469.         goto CitireCNP;
  470.     }
  471.  
  472.  
  473.     getchar();
  474.     cout << "Localitatea: "; getline(cin, adresa.localitate);
  475.     cout << "Judet: "; getline(cin, adresa.judet);
  476.     cout << "Cod Postal: "; getline(cin, adresa.codPostal);
  477.     cout << "Strada: "; getline(cin, adresa.strada);
  478.     cout << "Numar casa: "; cin >> adresa.nr;
  479.     q = new Persoana(nume, CNP, adresa);
  480.     if (listaPersone.empty())
  481.         listaPersone.push_back(q);
  482.     else {
  483.         it = listaPersone.begin();
  484.         while (it != listaPersone.end() && (*it)->get_nume() < nume)
  485.             advance(it, 1);
  486.         listaPersone.emplace(it, q);
  487.     }
  488.     int nrCadouri;
  489.     cout << "Numar de cadouri: "; cin >> nrCadouri;
  490.     for (int i = 0; i < nrCadouri; i++) {
  491.         system("cls");
  492.         cout << "Cadoul " << i + 1 << endl;
  493.         q->CitireCadou();
  494.     }
  495. }
  496. void CitireCadou(char *CNP) {
  497.     try { if (listaPersone.empty()) throw Exception("lista_goala"); }
  498.     catch (exception &e) { cerr << e.what(); }
  499.     char CNP2[20];
  500.     if (CNP == NULL) {
  501.     CitireCNP:
  502.         try
  503.         {
  504.             cout << "CNP( valoarea 0 pentru a reveni la meniu): "; cin >> CNP2;
  505.             VerificareCNP(CNP2);
  506.             for (it = listaPersone.begin(); it != listaPersone.end(); it++)
  507.                 if (strcmp((*it)->get_CNP(), CNP2) == 0) break;
  508.             if (strcmp((*it)->get_CNP(), CNP2)) throw Exception("CNP_not_found");
  509.  
  510.         }
  511.         catch (exception &e)
  512.         {
  513.             if (strcmp(CNP2, "0") == 0) return;
  514.             cerr << e.what();
  515.             goto CitireCNP;
  516.         }
  517.         CNP = CNP2;
  518.     }
  519.  
  520.     int nrCadouri;
  521.     (*it)->Afisare(true);
  522.     cout << "Numar de cadouri: "; cin >> nrCadouri;
  523.     for (int i = 0; i < nrCadouri; i++) {
  524.         system("cls");
  525.         cout << "Cadoul " << i + 1 << endl;
  526.         (*it)->CitireCadou();
  527.     }
  528. }
  529. void CautarePersoana() {
  530.     char CNP[20];
  531. CitireCNP:
  532.     try
  533.     {
  534.         cout << "CNP( valoarea 0 pentru a reveni la meniu): "; cin >> CNP;
  535.         VerificareCNP(CNP);
  536.         for (it = listaPersone.begin(); it != listaPersone.end(); it++)
  537.             if (strcmp((*it)->get_CNP(), CNP) == 0) break;
  538.         if (it == listaPersone.end()) throw Exception("CNP_not_found");
  539.  
  540.     }
  541.     catch (exception &e)
  542.     {
  543.         if (strcmp(CNP, "0") == 0) return;
  544.         cerr << e.what();
  545.         goto CitireCNP;
  546.     }
  547.     (*it)->Afisare(true);
  548.     (*it)->AfisareCheltuitTotal();
  549. }
  550. void CautareTipCadou() {
  551.     string tip;
  552.     bool gasitPersoana = 0;
  553.     getchar();
  554.     cout << "Introduceti tipul: ";  getline(cin, tip);
  555.     for (it = listaPersone.begin(); it != listaPersone.end(); it++) {
  556.         if ((*it)->CautCadou(tip)) {
  557.             (*it)->Afisare(false);
  558.             cout << "--------------------------------------------------------------------------";
  559.             gasitPersoana = 1;
  560.         }
  561.     }
  562.     if (gasitPersoana == false)cout << "Nu s-a gasit nici-o persoana care sa aiba un cadou de tipul " << tip << endl;
  563. }
  564. void DublareCadou() throw() {
  565.     char CNP[20];
  566. CitireCNP:
  567.     try
  568.     {
  569.         cout << "CNP( valoarea 0 pentru a reveni la meniu):  "; cin >> CNP;
  570.         VerificareCNP(CNP);
  571.         for (it = listaPersone.begin(); it != listaPersone.end(); it++)
  572.             if (strcmp((*it)->get_CNP(), CNP) == 0) break;
  573.         if (it == listaPersone.end()) throw Exception("CNP_not_found");
  574.     }
  575.     catch (exception &e)
  576.     {
  577.         if (strcmp(CNP, "0") == 0) return;
  578.         cerr << e.what();
  579.         goto CitireCNP;
  580.     }
  581.     (*it)->DublareCadou();
  582. }
  583. void StergereDinlista() {
  584.     bool option;
  585.    
  586.     cout << "Persoana = 0, Cadou = 1"; cin >> option;
  587.     char CNP[20];
  588.     CitireCNP:
  589.     try
  590.     {
  591.         cout << "CNP( valoarea 0 pentru a reveni la meniu):  "; cin >> CNP;
  592.         VerificareCNP(CNP);
  593.         for (it = listaPersone.begin(); it != listaPersone.end(); it++)
  594.             if (strcmp((*it)->get_CNP(), CNP) == 0) break;
  595.         if (it == listaPersone.end()) throw Exception("CNP_not_found");
  596.     }
  597.     catch (exception &e)
  598.     {
  599.         if (strcmp(CNP, "0") == 0) return;
  600.         cerr << e.what();
  601.         goto CitireCNP;
  602.     }
  603.     if (option) (*it)->StergeCadou();
  604.     else {
  605.         (*it)->ClearList();
  606.         listaPersone.erase(it, it);
  607.     }
  608.    
  609. }
  610. void CitirePersoaneFiser() {
  611.     char CNP[20];
  612.    
  613.     string nume;
  614.     struct adresa adresa;
  615.     int nrEroriCitrePersoana = 0;
  616.     Persoana *q = NULL;
  617.     ifstream f("in.txt");
  618.     bool  ok = 0, ok2;
  619.     if (f.eof()) return; ;
  620.    
  621.     while (!f.eof()) {
  622.    
  623.          f >> CNP;
  624.         try {
  625.             ok2 = 1;
  626.             VerificareCNP(CNP);
  627.             for (it = listaPersone.begin(); it != listaPersone.end(); it++)
  628.                 if (strcmp((*it)->get_CNP(), CNP) == 0) {
  629.                     ok = 0;
  630.                     ok2 = 0;
  631.                     break;
  632.             }// daca introducem un cnp deja aflat in baza de date inseamna ca dorims a mai adaugam caoduri
  633.         }
  634.         catch (...) {
  635.             if (ok) {
  636.                 nrEroriCitrePersoana++;
  637.                 ok = 0;
  638.             }
  639.             continue;
  640.         }
  641.         if (ok2 == 0) continue;
  642.        getline(f, nume, '\n');
  643.        getline(f, nume, '\n');
  644.         try {
  645.             VerificareNume(nume);
  646.         }
  647.         catch (...) {
  648.             if (ok) {
  649.                 nrEroriCitrePersoana++;
  650.                 ok = 0;
  651.             }
  652.             continue;
  653.         }
  654.         getline(f, adresa.localitate, '\n');
  655.         getline(f, adresa.judet, '\n');
  656.         getline(f, adresa.strada, '\n');
  657.         getline(f, adresa.codPostal, '\n');
  658.         f >> adresa.nr;
  659.         q = new Persoana(nume, CNP, adresa);
  660.         q->AddedByFile(true);
  661.         if (listaPersone.empty())
  662.             listaPersone.push_back(q);
  663.         else {
  664.             it = listaPersone.begin();
  665.             while (it != listaPersone.end() && (*it)->get_nume() < nume)
  666.                 advance(it, 1);
  667.             listaPersone.emplace(it, q);
  668.         }
  669.         q->CitireCadouriFiser(f);
  670.         ok = true;
  671.     }
  672.     f.close();
  673. }
  674.  
  675. int main() {
  676.  
  677.     int opt;
  678.     do {
  679.         cout << "\n\n";
  680.         cout << "1. Adaugare persoana si lista de cadouri\n";
  681.         cout << "2. Afisare  persoane si lista cadouri sub forma tabelara\n";
  682.         cout << "3. Adaugare cadou pentru o persoana oarecare\n";
  683.         cout << "4. Cautarea unei persoane, afisarea listei de cadouri si pretul total\n";
  684.         cout << "5. Cautarea dupa un tip de cadou si afisarea persoanel0r\n";
  685.         cout << "6. Dublare cadou pentru o persoana\n";
  686.         cout << "7. Citire date din fiser in.txt\n";
  687.         cout << "0. Iesire \n";
  688.         cout << "Dati optiunea dvs: ";
  689.         cin >> opt;
  690.         system("cls");
  691.         switch (opt) {
  692.         case 1:
  693.             AdaugaPersoana();
  694.             break;
  695.         case 2:
  696.             try { if (listaPersone.empty()) throw Exception("lista_goala"); }
  697.             catch (exception &e) { cerr << e.what(); }
  698.  
  699.             for (it = listaPersone.begin(); it != listaPersone.end(); it++)     (*it)->Afisare(true);
  700.  
  701.             break;
  702.         case 3:
  703.             CitireCadou(NULL); break;
  704.         case 4:
  705.             CautarePersoana(); break;
  706.         case 5:
  707.             CautareTipCadou(); break;
  708.         case 6:
  709.             DublareCadou(); break;
  710.         case 7: CitirePersoaneFiser(); break;
  711.         case 0:
  712.             return 0;
  713.  
  714.         }
  715.     } while (1);
  716.     return 0;
  717. }
  718.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement