Alx09

Tema POO lista in lista

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