metalni

OOP Labs 9 Transakcii

Jun 2nd, 2020
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.96 KB | None | 0 0
  1. #include<iostream>
  2. #include<cstring>
  3.  
  4. using namespace std;
  5.  
  6. class InvalidDateException{
  7.     private:
  8.         int day;
  9.         int month;
  10.         int year;
  11.     public:
  12.         InvalidDateException(){
  13.             this->day = 0;
  14.             this->month = 0;
  15.             this->year = 0;
  16.         }
  17.         InvalidDateException(const int day, const int month, const int year){
  18.             this->day = day;
  19.             this->month = month;
  20.             this->year = year;
  21.         }
  22.         const void showMsg(){
  23.             cout << "Invalid Date " << this->day <<  "/" << this->month << "/" << this->year <<endl;
  24.         }
  25. };
  26.  
  27. class NotSupportedCurrencyException{
  28.     private:
  29.         char currency[4];
  30.     public:
  31.         NotSupportedCurrencyException(){
  32.             strcpy(this->currency, "Uknown");
  33.         }
  34.         NotSupportedCurrencyException(const char * currency){
  35.             strcpy(this->currency, currency);
  36.         }
  37.         const void showMsg(){
  38.             cout << this->currency << " is not a supported currency" << endl;
  39.         }
  40. };
  41.  
  42. class Transakcija{
  43.     protected:
  44.         int day;
  45.         int month;
  46.         int year;
  47.         double total;
  48.         static double eur;
  49.         static double usd;
  50.     public:
  51.         Transakcija(){
  52.             this->day = 0;
  53.             this->month = 0;
  54.             this->year = 0;
  55.             this->total = 0.0;
  56.         }
  57.         Transakcija(const int day, const int month, const int year, const double total){
  58.             if(day<1 || day>31 || month<1 || month>12 || year < 1)
  59.                 throw InvalidDateException(day, month, year);
  60.             this->day = day;
  61.             this->month = month;
  62.             this->year = year;
  63.             this->total = total;
  64.         }
  65.         virtual const double voDenari() = 0;
  66.         virtual const double voEvra() = 0;
  67.         virtual const double voDolari() = 0;
  68.         virtual const void pecati() = 0;
  69.         const static void setEUR(double newEUR);
  70.         const static void setUSD(double newUSD);
  71.         const static double getEUR();
  72.         const static double getUSD();
  73. };
  74. double Transakcija::eur = 61.0;
  75. double Transakcija::usd = 50.0;
  76. const double Transakcija::getEUR(){
  77.     return eur;
  78. }
  79. const double Transakcija::getUSD(){
  80.     return usd;
  81. }
  82. const void Transakcija::setEUR(double newEUR){
  83.     eur = newEUR * 1.0;
  84. }
  85. const void Transakcija::setUSD(double newUSD){
  86.     usd = newUSD * 1.0;
  87. }
  88.  
  89. class DenarskaTransakcija : public Transakcija{
  90.     public:
  91.         DenarskaTransakcija(const int day, const int month, const int year, const double total) : Transakcija(day, month, year, total) {}
  92.         const double voDenari(){
  93.             return this->total;
  94.         }
  95.         const double voEvra(){
  96.             return this->total / eur;
  97.         }
  98.         const double voDolari(){
  99.             return this->total / usd;
  100.         }
  101.         const void pecati(){
  102.             cout << this->day << "/" << this->month << "/" << this->year << " " << this->total << " MKD" << endl;
  103.         }
  104.         ~DenarskaTransakcija(){}
  105. };
  106.  
  107. class DeviznaTransakcija : public Transakcija {
  108.     private:
  109.         char currency[4];
  110.     public:
  111.     DeviznaTransakcija(){
  112.         strcpy(this->currency, "Uknown");
  113.     }
  114.         DeviznaTransakcija(const int day, const int month, const int year, const double total, const char * currency) : Transakcija(day, month, year, total){
  115.             if((strcmp(currency, "EUR"))&&(strcmp(currency, "USD")))
  116.                 throw NotSupportedCurrencyException(currency);
  117.             strcpy(this->currency, currency);
  118.         }
  119.         const double voDenari(){
  120.             if(!(strcmp(this->currency, "USD")))
  121.                 return this->total * usd;
  122.             else
  123.                 return this->total * eur;
  124.         }
  125.         const double voEvra(){
  126.             if (!(strcmp(this->currency, "EUR")))
  127.                 return this->total;
  128.             else
  129.                 return (this->total * this->usd) / this->eur;  
  130.         }
  131.         const double voDolari(){
  132.             if(!(strcmp(this->currency, "USD")))
  133.                 return this->total;
  134.             else
  135.                 return (this->total * this->eur) / this->usd;
  136.         }
  137.         const void pecati(){
  138.             cout << this->day << "/" << this->month << "/" << this->year << " " << this->total << " " << this->currency << endl;
  139.         }
  140.         ~DeviznaTransakcija(){}
  141. };
  142.  
  143. class Smetka{
  144.     private:
  145.         Transakcija ** tii;
  146.         int noTransactions;
  147.         char smetka[15];
  148.         double saldo;
  149.         const void copy(const Smetka &orig){
  150.             this->tii = new Transakcija *[orig.noTransactions + 1];
  151.             this->noTransactions = orig.noTransactions;
  152.             strcpy(this->smetka, orig.smetka);
  153.             this->saldo = orig.saldo;
  154.         }
  155.     public:
  156.         Smetka(){
  157.             this->tii = new Transakcija *[0];
  158.             this->noTransactions = 0;
  159.             strcpy(this->smetka, "N/A");
  160.             this->saldo = 0.0;
  161.         }
  162.         Smetka(const char * smetka, const double saldo){
  163.             this->tii = new Transakcija *[0];
  164.             this->noTransactions = 0;
  165.             strcpy(this->smetka, smetka);
  166.             this->saldo = saldo;
  167.         }
  168.         Smetka(const Smetka &orig){
  169.             this->copy(orig);
  170.         }
  171.         Smetka &operator=(const Smetka &orig){
  172.             if(this != &orig){
  173.                 delete [] this->tii;
  174.                 this->copy(orig);
  175.             }
  176.  
  177.             return *this;
  178.         }
  179.         ~Smetka(){
  180.             delete [] this->tii;
  181.         }
  182.         Smetka &operator+=(Transakcija *orig){
  183.             Transakcija **tmp = new Transakcija*[this->noTransactions + 1];
  184.             for(int i=0; i<this->noTransactions; i++)
  185.                 tmp[i] = this->tii[i];
  186.             tmp[this->noTransactions++] = orig;
  187.             delete [] this->tii;
  188.             this->tii = tmp;
  189.  
  190.             return *this;
  191.         }
  192.         const void izvestajVoDenari(){
  193.             double total = 0.0;
  194.             for(int i=0; i<this->noTransactions; i++)
  195.                 total += this->tii[i]->voDenari();
  196.             cout << "Korisnikot so smetka: " << this->smetka << " ima momentalno saldo od " << this->saldo + total << " MKD" << endl;
  197.         }
  198.         const void izvestajVoEvra(){
  199.             double total = 0.0;
  200.             for(int i=0; i<this->noTransactions; i++)
  201.                 total += this->tii[i]->voEvra();
  202.             cout << "Korisnikot so smetka: " << this->smetka << " ima momentalno saldo od " << (this->saldo / Transakcija::getEUR()) + total << " EUR" << endl;
  203.         }
  204.         const void izvestajVoDolari(){
  205.             double total = 0.0;
  206.             for(int i=0; i<this->noTransactions; i++)
  207.                 total += this->tii[i]->voDolari();
  208.             cout << "Korisnikot so smetka: " << this->smetka << " ima momentalno saldo od " << (this->saldo / Transakcija::getUSD()) + total << " USD" << endl;
  209.         }
  210.         const void pecatiTransakcii(){
  211.             for(int i=0; i<this->noTransactions; i++)
  212.                 this->tii[i]->pecati();
  213.         }
  214. };
  215.  
  216.  
  217. int main () {
  218.    
  219.     Smetka s ("300047024112789",1500);
  220.    
  221.     int n, den, mesec, godina, tip;
  222.     double iznos;
  223.     char valuta [3];
  224.    
  225.     cin>>n;
  226.     cout<<"===VNESUVANJE NA TRANSAKCIITE I SPRAVUVANJE SO ISKLUCOCI==="<<endl;
  227.     for (int i=0;i<n;i++){
  228.         cin>>tip>>den>>mesec>>godina>>iznos;       
  229.         if (tip==2){
  230.             cin>>valuta;
  231.             try{
  232.                 Transakcija * t = new DeviznaTransakcija(den,mesec,godina,iznos,valuta);
  233.                 s+=t;
  234.             }
  235.             catch (InvalidDateException &dateExc){
  236.                 dateExc.showMsg();
  237.             }
  238.             catch (NotSupportedCurrencyException &cExc){
  239.                 cExc.showMsg();
  240.             }
  241.         }
  242.         else {
  243.             try{
  244.                 Transakcija * t = new DenarskaTransakcija(den,mesec,godina,iznos);
  245.                 s+=t;
  246.             }
  247.             catch (InvalidDateException &dateExc){
  248.                 dateExc.showMsg();
  249.             }
  250.             catch (NotSupportedCurrencyException &cExc){
  251.                 cExc.showMsg();
  252.             }
  253.         }
  254.            
  255.     }
  256.     cout<<"===PECHATENJE NA SITE TRANSAKCII==="<<endl;
  257.     s.pecatiTransakcii();
  258.     cout<<"===IZVESHTAJ ZA SOSTOJBATA NA SMETKATA VO DENARI==="<<endl;
  259.     s.izvestajVoDenari();
  260.     cout<<"===IZVESHTAJ ZA SOSTOJBATA NA SMETKATA VO EVRA==="<<endl;
  261.     s.izvestajVoEvra();
  262.     cout<<"===IZVESHTAJ ZA SOSTOJBATA NA SMETKATA VO DOLARI==="<<endl;
  263.     s.izvestajVoDolari();
  264.    
  265.     cout<<"\n===PROMENA NA KURSOT NA EVROTO I DOLAROT===\n"<<endl;
  266.    
  267.        
  268.     double newEUR, newUSD;
  269.     cin>>newEUR>>newUSD;
  270.     Transakcija::setEUR(newEUR);
  271.     Transakcija::setUSD(newUSD);
  272.     cout<<"===IZVESHTAJ ZA SOSTOJBATA NA SMETKATA VO DENARI==="<<endl;
  273.     s.izvestajVoDenari();
  274.     cout<<"===IZVESHTAJ ZA SOSTOJBATA NA SMETKATA VO EVRA==="<<endl;
  275.     s.izvestajVoEvra();
  276.     cout<<"===IZVESHTAJ ZA SOSTOJBATA NA SMETKATA VO DOLARI==="<<endl;
  277.     s.izvestajVoDolari();
  278.    
  279.    
  280.    
  281.     return 0;
  282. }
Add Comment
Please, Sign In to add comment