Alx09

Untitled

Dec 13th, 2020
827
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 16.41 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.  
  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;
  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.  
  181.     }
  182.     void CitireCadou();
  183.     char *get_CNP() {
  184.         return CNP;
  185.     }
  186.     string get_nume() {
  187.         return nume;
  188.     }
  189.     void Afisare(bool afisareCadouri) {
  190.         cout << "------------------------------------------------------------------------------------------\n";
  191.         cout << nume << " " << CNP;
  192.         if (isMan) cout << " barbat\n";
  193.         else cout << " femeie\n";
  194.         cout << varsta << "  ";
  195.         if (dataNastere.zi < 10)cout << '0';
  196.         cout << dataNastere.zi << "/";
  197.         if (dataNastere.luna < 10) cout << '0';
  198.         cout << dataNastere.luna << "/" << dataNastere.an << endl;
  199.         cout << adresa.localitate << " " << adresa.judet << " " << adresa.strada << " " << " " << adresa.codPostal << " " << adresa.nr;
  200.         if (afisareCadouri == false) return;
  201.         cout << "\nLista de cadouri:\n";
  202.  
  203.         cout << "=============================" << endl;
  204.         try
  205.         {
  206.             if (listaCadou.empty()) throw Exception("lista_goala");
  207.         }
  208.         catch (exception &e)
  209.         {
  210.             e.what();
  211.             cout << "\n=============================" << endl;
  212.             return;
  213.         }
  214.  
  215.         for (list <Cadou *>::iterator it = listaCadou.begin(); it != listaCadou.end(); it++) (*it)->Afisare();
  216.         cout << "\n=============================" << endl;
  217.     }
  218.     void AfisareCheltuitTotal() {
  219.         cout << "Buget cheltuit = " << cheltuitCadouri << " ron" << endl;
  220.     }
  221.     bool CautCadou(string tip) {
  222.         for (list <Cadou *>::iterator it = listaCadou.begin(); it != listaCadou.end(); it++)
  223.             if ((*it)->get_tip() == tip) return true;
  224.         return false;
  225.     }
  226.     void DublareCadou();
  227.     void StergeCadou();
  228.     void ClearList() { listaCadou.clear(); }
  229. };
  230. list <Persoana *> listaPersone;
  231. list <Persoana *>::iterator it;
  232.  
  233. Cadou::Cadou(string nume, string tip, double pret) {
  234.     this->nume = nume;
  235.     this->pret = pret;
  236.     this->tip = tip;
  237. }
  238.  
  239. void Persoana::CitireCadou()
  240. {
  241.     list <Cadou *>::iterator it;
  242.     string nume, tip;
  243.     double pret;
  244.     char t;
  245.     getchar();
  246.     cout << "\nCe fel de cadou D-Dulciuri, P- Pachet Cadou, J -Jucarie: "; cin >> t;
  247.     getchar();
  248.     cout << "Nume: "; getline(cin, nume);
  249.     cout << "Pret: "; cin >> pret;
  250.     getchar();
  251.     cout << "Tip: "; getline(cin, tip);
  252.     cheltuitCadouri += pret;
  253.     if (t == 'D') {
  254.         string ingrediente;
  255.         double calori, gramaj;
  256.         cout << "Ingerdiente: ";   getline(cin, ingrediente);
  257.         cout << "Calori: ";        cin >> calori;
  258.         cout << "Gramaj: ";        cin >> gramaj;
  259.         if (listaCadou.empty())
  260.             listaCadou.push_back(new Dulciuri(nume, tip, pret, ingrediente, calori, gramaj));
  261.         else {
  262.             it = listaCadou.begin();
  263.             while (it != listaCadou.end() && (*it)->get_nume() < nume)
  264.                 advance(it, 1);
  265.             listaCadou.emplace(it, new Dulciuri(nume, tip, pret, ingrediente, calori, gramaj));
  266.         }
  267.     }
  268.     if (t == 'P') {
  269.  
  270.         bool pentruBarbati;
  271.         unsigned short nrProd;
  272.         cout << "Este pentru femei - 0, barbati - 1: ";   cin >> pentruBarbati;
  273.         cout << "Numar de produse in pachet: "; cin >> nrProd;
  274.         if (listaCadou.empty())
  275.             listaCadou.push_back(new PachetCadou(nume, tip, pret, pentruBarbati, nrProd));
  276.         else {
  277.             it = listaCadou.begin();
  278.             while (it != listaCadou.end() && (*it)->get_nume() < nume)
  279.                 advance(it, 1);
  280.             listaCadou.emplace(it, new PachetCadou(nume, tip, pret, pentruBarbati, nrProd));
  281.         }
  282.     }
  283.     if (t == 'J') {
  284.  
  285.         string marca;
  286.         bool bateriNecesare;
  287.         unsigned short varstaRecomandata;
  288.         cout << "Marca: ";   getline(cin, marca);
  289.         cout << "Are nevoie de bateri Nu - 0, Da - 1: ";   cin >> bateriNecesare;
  290.         cout << "Varsta recomandata: "; cin >> varstaRecomandata;
  291.         if (listaCadou.empty())
  292.             listaCadou.push_back(new Jucarii(nume, tip, pret, marca, bateriNecesare, varstaRecomandata));
  293.         else {
  294.             it = listaCadou.begin();
  295.             while (it != listaCadou.end() && (*it)->get_nume() < nume)
  296.                 advance(it, 1);
  297.             listaCadou.emplace(it, new  Jucarii(nume, tip, pret, marca, bateriNecesare, varstaRecomandata));
  298.         }
  299.     }
  300.  
  301.  
  302. }
  303. void Persoana::DublareCadou() {
  304.     string nume;
  305.     list <Cadou *>::iterator it;
  306.     getchar();
  307.     CitireNumeCadou:
  308.     cout << "Introduceti nume cadou: "; getline(cin, nume);
  309.     for (it = listaCadou.begin(); it != listaCadou.end(); it++)
  310.         if ((*it)->get_nume() == nume)break;
  311.     try {
  312.         if (it == listaCadou.end()) throw Exception("nume_negasit");
  313.     }
  314.     catch (Exception &e) {
  315.         if (nume == "0") return;
  316.         cerr << e.what();
  317.         goto CitireNumeCadou;
  318.     }
  319.     listaCadou.emplace(it, *it);
  320.  
  321. }
  322. void Persoana::StergeCadou() {
  323. list <Cadou *>::iterator it1;
  324. string nume;
  325. CitireNumeCadou:
  326.     cout << "Introduceti nume cadou: "; getline(cin, nume);
  327.     for (it1 = listaCadou.begin(); it1 != listaCadou.end(); it1++)
  328.         if ((*it1)->get_nume() == nume)break;
  329.     try {
  330.         if (it1 == listaCadou.end()) throw Exception("nume_negasit");
  331.     }
  332.     catch (Exception &e) {
  333.         if (nume == "0") return;
  334.         cerr << e.what();
  335.         goto CitireNumeCadou;
  336.     }
  337.     listaCadou.erase(it1, it1);
  338. }
  339. void VerificareCNP(char *CNP) {
  340.     if (strlen(CNP) < 13)              throw Exception(1);
  341.     if (strlen(CNP) > 13)              throw Exception(2);
  342.     if (CNP[0] == '0' || CNP[0] == '3' || CNP[0] == '4' || CNP[0] == '7' || CNP[0] == '8' || CNP[0] == '9') throw Exception(4);
  343.     if (CNP[3] > '1')                  throw Exception(5);
  344.     if (CNP[3] == '1' && CNP[4] > '2') throw Exception(6);
  345.     if (CNP[3] == '0' && CNP[4] == '0') throw Exception(7);
  346.     if (CNP[5] > '3')                  throw Exception(8);
  347.     if (CNP[5] == '3' && CNP[6] > '1') throw Exception(9);
  348.     if (CNP[5] == '0' && CNP[6] == '0')throw Exception(10);
  349.     for (int i = 0; i < 13; i++)if (CNP[i] <'0' || CNP[i] > '9') throw Exception(99999);
  350.  
  351. }
  352. void VerificareNume(string &nume) {
  353.     while (nume[0] == ' ' || nume[0] == '-')
  354.     {
  355.         while (nume[0] == ' ') nume.erase(0, 1);
  356.         while (nume[0] == '-') nume.erase(0, 1);
  357.     }
  358.  
  359.     if (nume == "")throw Exception("nume invalid");
  360.     for (int i = 0, n = nume.size(); i < n; i++)
  361.         if (nume[i] == ' ' || nume[i] == '-') continue;
  362.         else if (nume[i] >= 'a' && nume[i] <= 'z') continue;
  363.         else if (nume[i] >= 'A' && nume[i] <= 'Z') continue;
  364.         else throw Exception("nume invalid");
  365. }
  366. void AdaugaPersoana() {
  367.     string nume;
  368.     char CNP[20];
  369.     Persoana *q;
  370.     struct adresa adresa;
  371.     getchar();
  372. CitireNume:
  373.  
  374.     try {
  375.         cout << "Nume persoana( valoarea 0 pentru a reveni la meniu): "; getline(cin, nume);
  376.         VerificareNume(nume);
  377.     }
  378.     catch (exception &e) {
  379.         if (nume == "0") return;
  380.         cerr << e.what();
  381.         goto CitireNume;
  382.     }
  383.  
  384.  
  385. CitireCNP:
  386.     try
  387.     {
  388.         cout << "CNP( valoarea 0 pentru a reveni la meniu):  "; cin >> CNP;
  389.         VerificareCNP(CNP);
  390.         for (it = listaPersone.begin(); it != listaPersone.end(); it++)
  391.             if ((*it)->get_CNP() == CNP)
  392.                 throw Exception(3);
  393.  
  394.     }
  395.     catch (exception &e)
  396.     {
  397.         if (strcmp(CNP, "0") == 0) return;
  398.         cerr << e.what();
  399.         goto CitireCNP;
  400.     }
  401.  
  402.  
  403.     getchar();
  404.     cout << "Localitatea: "; getline(cin, adresa.localitate);
  405.     cout << "Judet: "; getline(cin, adresa.judet);
  406.     cout << "Cod Postal: "; getline(cin, adresa.codPostal);
  407.     cout << "Strada: "; getline(cin, adresa.strada);
  408.     cout << "Numar casa: "; cin >> adresa.nr;
  409.     q = new Persoana(nume, CNP, adresa);
  410.     if (listaPersone.empty())
  411.         listaPersone.push_back(q);
  412.     else {
  413.         it = listaPersone.begin();
  414.         while (it != listaPersone.end() && (*it)->get_nume() < nume)
  415.             advance(it, 1);
  416.         listaPersone.emplace(it, q);
  417.     }
  418.     int nrCadouri;
  419.     cout << "Numar de cadouri: "; cin >> nrCadouri;
  420.     for (int i = 0; i < nrCadouri; i++) {
  421.         system("cls");
  422.         cout << "Cadoul " << i + 1 << endl;
  423.         q->CitireCadou();
  424.     }
  425. }
  426. void CitireCadou(char *CNP) {
  427.     try { if (listaPersone.empty()) throw Exception("lista_goala"); }
  428.     catch (exception &e) { cerr << e.what(); }
  429.     char CNP2[20];
  430.     if (CNP == NULL) {
  431.     CitireCNP:
  432.         try
  433.         {
  434.             cout << "CNP( valoarea 0 pentru a reveni la meniu): "; cin >> CNP2;
  435.             VerificareCNP(CNP2);
  436.             for (it = listaPersone.begin(); it != listaPersone.end(); it++)
  437.                 if (strcmp((*it)->get_CNP(), CNP2) == 0) break;
  438.             if (strcmp((*it)->get_CNP(), CNP2)) throw Exception("CNP_not_found");
  439.  
  440.         }
  441.         catch (exception &e)
  442.         {
  443.             if (strcmp(CNP2, "0") == 0) return;
  444.             cerr << e.what();
  445.             goto CitireCNP;
  446.         }
  447.         CNP = CNP2;
  448.     }
  449.  
  450.     int nrCadouri;
  451.     (*it)->Afisare(true);
  452.     cout << "Numar de cadouri: "; cin >> nrCadouri;
  453.     for (int i = 0; i < nrCadouri; i++) {
  454.         system("cls");
  455.         cout << "Cadoul " << i + 1 << endl;
  456.         (*it)->CitireCadou();
  457.     }
  458. }
  459. void CautarePersoana() {
  460.     char CNP[20];
  461. CitireCNP:
  462.     try
  463.     {
  464.         cout << "CNP( valoarea 0 pentru a reveni la meniu): "; cin >> CNP;
  465.         VerificareCNP(CNP);
  466.         for (it = listaPersone.begin(); it != listaPersone.end(); it++)
  467.             if (strcmp((*it)->get_CNP(), CNP) == 0) break;
  468.         if (it == listaPersone.end()) throw Exception("CNP_not_found");
  469.  
  470.     }
  471.     catch (exception &e)
  472.     {
  473.         if (strcmp(CNP, "0") == 0) return;
  474.         cerr << e.what();
  475.         goto CitireCNP;
  476.     }
  477.     (*it)->Afisare(true);
  478.     (*it)->AfisareCheltuitTotal();
  479. }
  480. void CautareTipCadou() {
  481.     string tip;
  482.     bool gasitPersoana = 0;
  483.     getchar();
  484.     cout << "Introduceti tipul: ";  getline(cin, tip);
  485.     for (it = listaPersone.begin(); it != listaPersone.end(); it++) {
  486.         if ((*it)->CautCadou(tip)) {
  487.             (*it)->Afisare(false);
  488.             cout << "--------------------------------------------------------------------------";
  489.             gasitPersoana = 1;
  490.         }
  491.     }
  492.     if (gasitPersoana == false)cout << "Nu s-a gasit nici-o persoana care sa aiba un cadou de tipul " << tip << endl;
  493. }
  494. void DublareCadou() throw() {
  495.     char CNP[20];
  496. CitireCNP:
  497.     try
  498.     {
  499.         cout << "CNP( valoarea 0 pentru a reveni la meniu):  "; cin >> CNP;
  500.         VerificareCNP(CNP);
  501.         for (it = listaPersone.begin(); it != listaPersone.end(); it++)
  502.             if (strcmp((*it)->get_CNP(), CNP) == 0) break;
  503.         if (it == listaPersone.end()) throw Exception("CNP_not_found");
  504.     }
  505.     catch (exception &e)
  506.     {
  507.         if (strcmp(CNP, "0") == 0) return;
  508.         cerr << e.what();
  509.         goto CitireCNP;
  510.     }
  511.     (*it)->DublareCadou();
  512. }
  513. void StergereDinlista() {
  514.     bool option;
  515.    
  516.     cout << "Persoana = 0, Cadou = 1"; cin >> option;
  517.     char CNP[20];
  518.     CitireCNP:
  519.     try
  520.     {
  521.         cout << "CNP( valoarea 0 pentru a reveni la meniu):  "; cin >> CNP;
  522.         VerificareCNP(CNP);
  523.         for (it = listaPersone.begin(); it != listaPersone.end(); it++)
  524.             if (strcmp((*it)->get_CNP(), CNP) == 0) break;
  525.         if (it == listaPersone.end()) throw Exception("CNP_not_found");
  526.     }
  527.     catch (exception &e)
  528.     {
  529.         if (strcmp(CNP, "0") == 0) return;
  530.         cerr << e.what();
  531.         goto CitireCNP;
  532.     }
  533.     if (option) (*it)->StergeCadou();
  534.     else {
  535.         (*it)->ClearList();
  536.         listaPersone.erase(it, it);
  537.     }
  538.    
  539. }
  540.  
  541. int main() {
  542.  
  543.     int opt;
  544.     do {
  545.         cout << "\n\n";
  546.         cout << "1. Adaugare persoana si lista de cadouri\n";
  547.         cout << "2. Afisare  persoane si lista cadouri sub forma tabelara\n";
  548.         cout << "3. Adaugare cadou pentru o persoana oarecare\n";
  549.         cout << "4. Cautarea unei persoane, afisarea listei de cadouri si pretul total\n";
  550.         cout << "5. Cautarea dupa un tip de cadou si afisarea persoaneler\n";
  551.         cout << "6. Dublare cadou pentru o persoana\n";
  552.         cout << "0. Iesire \n";
  553.         cout << "Dati optiunea dvs: ";
  554.         cin >> opt;
  555.         system("cls");
  556.         switch (opt) {
  557.         case 1:
  558.             AdaugaPersoana();
  559.             break;
  560.         case 2:
  561.             try { if (listaPersone.empty()) throw Exception("lista_goala"); }
  562.             catch (exception &e) { cerr << e.what(); }
  563.  
  564.             for (it = listaPersone.begin(); it != listaPersone.end(); it++)     (*it)->Afisare(true);
  565.  
  566.             break;
  567.         case 3:
  568.             CitireCadou(NULL); break;
  569.         case 4:
  570.             CautarePersoana(); break;
  571.         case 5:
  572.             CautareTipCadou(); break;
  573.         case 6:
  574.             DublareCadou(); break;
  575.         case 0:
  576.             return 0;
  577.  
  578.         }
  579.     } while (1);
  580.     return 0;
  581. }
  582.  
Advertisement
Add Comment
Please, Sign In to add comment