Advertisement
StefiIOE

Transactions

May 28th, 2020
2,161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.32 KB | None | 0 0
  1. #include<iostream>
  2. #include<cstring>
  3.  
  4. using namespace std;
  5.  
  6. class NotSupportedCurrencyException
  7. {
  8.     private:
  9.     char valuta[4];
  10. public:
  11.     NotSupportedCurrencyException(char*valuta=""){strcpy(this->valuta,valuta);}
  12.     void showMessage()
  13.     {
  14.     cout<<valuta<<" is not a supported currency"<<endl;
  15.     }
  16. };
  17.  
  18. class InvalidDateException{
  19.     private:
  20.     int den;
  21.     int mesec;
  22.     int godina;
  23.    
  24. public:
  25.     InvalidDateException(int den , int mesec , int godina){
  26.         this->den=den;
  27.         this->mesec=mesec;
  28.         this->godina=godina;
  29.     }
  30.     void showMessage(){cout<<"Invalid Date "<<den<<"/"<<mesec<<"/"<<godina<<endl;}
  31. };
  32. class Transaction{
  33. protected:
  34.     int day;
  35.     int month;
  36.     int year;
  37.     double amount;
  38.     static double EUR;
  39.     static double USD;
  40.     public:
  41.     Transaction(int day=0 , int month = 0 , int year = 0 , double amount=0.0)
  42.         {
  43.         if(day<1||day>31 || month<1 ||month>12){
  44.          throw InvalidDateException(day, month , year);
  45.         }
  46.         this->day=day;
  47.         this->month=month;
  48.         this->year=year;
  49.         this->amount=amount;
  50.        
  51.     }
  52.     Transaction(const Transaction &t)
  53.     {    
  54.         this->day=t.day;
  55.         this->month=t.month;
  56.         this->year=t.year;
  57.         this->amount=t.amount;        
  58.     }
  59.    
  60.     static void setEUR(double EUR){Transaction::EUR=EUR;}
  61.     static void setUSD(double USD){Transaction::USD=USD;}
  62.     //static double
  63.     virtual void print()=0;
  64.     virtual double toDenars()=0;
  65.    
  66.        
  67.    
  68.  
  69. };
  70. double Transaction::EUR=61.0;
  71. double Transaction::USD=50.0;
  72.  
  73. class DenarsTransaction : public Transaction{
  74. public:
  75.     DenarsTransaction(int day=0 , int month = 0 , int year = 0 , double amount=0.0)
  76.         :Transaction(day,month,year,amount){}
  77.    
  78.     DenarsTransaction(const DenarsTransaction &t) :Transaction (t){};
  79.     void print()
  80.     {
  81.     cout<<day<<"/"<<month<<"/"<<year<<" "<<amount<<" MKD"<<endl;
  82.        
  83.     }
  84.     double toDenars()
  85.     {
  86.     return this->amount;
  87.     }
  88.  
  89. };
  90. class ForeignCurrencyTransaction : public Transaction{
  91. char value[4];
  92.     public:
  93.     ForeignCurrencyTransaction(int day=0 , int month = 0 , int year = 0 , double amount=0.0, char *value="")
  94.         :Transaction(day,month,year,amount)
  95.         {
  96.        
  97.             if(strcmp(value,"EUR")==0 || strcmp(value,"USD")==0){
  98.                 strcpy(this->value,value);
  99.             }
  100.             else
  101.             {
  102.             throw NotSupportedCurrencyException(value);
  103.             }
  104.         }
  105.     ForeignCurrencyTransaction(const ForeignCurrencyTransaction &f): Transaction(f)
  106.     {
  107.     strcpy(this->value,f.value);
  108.     }
  109.     void print()
  110.     {
  111.     cout<<day<<"/"<<month<<"/"<<year<<" "<<amount<<" "<<value<<endl;
  112.        
  113.     }
  114.     double toDenars()
  115.     {
  116.         if(strcmp(value,"EUR")==0)
  117.         {
  118.         return amount *EUR;
  119.         }
  120.         else
  121.         {
  122.         return amount *USD;
  123.         }
  124.     }
  125.    
  126.    
  127. };
  128. class Account{
  129. private:
  130.     Transaction**t;
  131.     int n ;
  132.     char account [15];
  133.     double balance;
  134.     public:
  135.     Account(char *account ="", double balance=0.0)
  136.     {
  137.         strcpy(this->account,account);
  138.         this->balance=balance;
  139.         n = 0;
  140.         t=new Transaction *[0];
  141.        
  142.     }
  143.     Account &operator +=(Transaction *tran)
  144.     {
  145.     Transaction**tmp=new Transaction*[n+1];
  146.         for(int i = 0 ; i < n ; i ++)
  147.         {
  148.         tmp[i]=t[i];
  149.         }
  150.         tmp[n]=tran;
  151.         n++;
  152.         delete[]t;
  153.         t=tmp;
  154.         return *this;        
  155.     }
  156.     void reportInDenars()
  157.     {
  158.         double sum=this->balance;
  159.        
  160.         for(int i = 0 ; i < n ; i ++)
  161.         {
  162.         sum+=t[i]->toDenars();
  163.         }
  164.         cout<<"The user with account number: "<<account<<" has account balance of: "<<sum<<" MKD"<<endl;
  165.     }
  166.    
  167.     void printTranscations()
  168.     {
  169.         for(int i = 0 ; i < n ; i ++)
  170.         {
  171.         t[i]->print();
  172.         }
  173.     }
  174. };
  175.  
  176. int main () {
  177.    
  178.     Account s ("300047024112789",1500);
  179.    
  180.     int n, den, mesec, godina, tip;
  181.     double iznos;
  182.     char valuta [3];
  183.    
  184.     cin>>n;
  185.     cout<<"===TRANSACTION ADDITION AND EXCEPTION HANDLING==="<<endl;
  186.     for (int i=0;i<n;i++){
  187.         cin>>tip>>den>>mesec>>godina>>iznos;
  188.        // cout<<iznos<<endl;
  189.         try{
  190.         if (tip==2){
  191.             cin>>valuta;
  192.             Transaction * t = new ForeignCurrencyTransaction(den,mesec,godina,iznos,valuta);
  193.             s+=t;
  194.             //delete t;
  195.         }
  196.         else {
  197.             Transaction * t = new DenarsTransaction(den,mesec,godina,iznos);
  198.             s+=t;
  199.             //delete t;
  200.         }
  201.            
  202.     }
  203.         catch
  204.        
  205.             (InvalidDateException &e)
  206.             {
  207.             e.showMessage();
  208.             }
  209.        
  210.         catch (NotSupportedCurrencyException &e)
  211.         {
  212.             e.showMessage();
  213.         }
  214.     }
  215.     cout<<"===TRANSACTIONS LIST==="<<endl;
  216.     s.printTranscations();
  217.     cout<<"===TRANSACTIONS REPORT IN DENARS==="<<endl;
  218.     s.reportInDenars();
  219.    
  220.    
  221.     cout<<"\n===CHANGE OF THE COURSE OF THE EUR AND USD===\n"<<endl;
  222.    
  223.        
  224.     double newEUR, newUSD;
  225.     cin>>newEUR>>newUSD;
  226.    // cout<<"test"<<newEUR<<newUSD<<endl;
  227.     Transaction::setEUR(newEUR);
  228.     Transaction::setUSD(newUSD);
  229.     cout<<"===TRANSACTIONS REPORT IN DENARS==="<<endl;
  230.     s.reportInDenars();
  231.    
  232.    
  233.    
  234.    
  235.     return 0;
  236. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement