Advertisement
StefiIOE

Truck

May 28th, 2020
905
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.62 KB | None | 0 0
  1. #include<iostream>
  2. #include<cstring>
  3.  
  4. using namespace std;
  5. class Exception {
  6. public:
  7.     void showMessage() {
  8.         cout<<"Incorrect registration!"<<endl;
  9.     }
  10. };
  11. class AlkoholException {
  12. public:
  13.     void showMessage() {
  14.         cout<<"Incorrect alcohol percentage!"<<endl;
  15.     }
  16. };
  17. class ImaMasa {
  18. public:
  19.     virtual double vratiMasa()=0;
  20.     virtual void pecati () = 0 ;
  21.  
  22. };
  23.  
  24. class PaketPijalok {
  25. protected:
  26.     double volumenEden;
  27.     int kolicina;
  28.     static double density;
  29.     static double mass;
  30. public:
  31.     PaketPijalok(double volumenEden=0.0 , int kolicina=0) {
  32.         this->volumenEden=volumenEden;
  33.         this->kolicina=kolicina;
  34.     }
  35.     PaketPijalok(const PaketPijalok &p) {
  36.         this->volumenEden=p.volumenEden;
  37.         this->kolicina=p.kolicina;
  38.     }
  39.     double vratiMasa() {
  40.         return kolicina*(volumenEden *density+mass);
  41.     }
  42.     void pecati() {
  43.         cout<<"quantity "<<kolicina;
  44.     }
  45.     int getKolicina() {
  46.         return this->kolicina;
  47.     }
  48. };
  49.  
  50. double PaketPijalok::density=0.8;
  51. double PaketPijalok::mass=0.2;
  52.  
  53. class PaketSok :public ImaMasa,PaketPijalok {
  54. private:
  55.     bool daliGaziran;
  56. public:
  57.     PaketSok(double volumenEden=0.0 , int kolicina=0,bool daliGaziran=true)
  58.         :PaketPijalok(volumenEden,kolicina) {
  59.         this->daliGaziran=daliGaziran;
  60.     }
  61.     PaketSok(const PaketSok &p) {
  62.         this->daliGaziran=p.daliGaziran;
  63.     }
  64.     double vratiMasa() {
  65.         if(!daliGaziran) {
  66.             return PaketPijalok::vratiMasa()+(0.1 * kolicina);
  67.         } else return PaketPijalok::vratiMasa();
  68.     }
  69.     void pecati() {
  70.         cout<<"PaketSok"<<endl;
  71.         cout<<"quantity "<<getKolicina()<<", each of "<<volumenEden*density<<" l(dm3)"<<endl;
  72.     }
  73.  
  74. };
  75. class PaketVino : public ImaMasa,PaketPijalok {
  76. private:
  77.     double ProcentAlkohol;
  78. public:
  79.     PaketVino(double volumenEden=0.0 , int kolicina=0,double ProcentAlkohol=0.0)
  80.         :PaketPijalok(volumenEden,kolicina) {
  81.         this->ProcentAlkohol=ProcentAlkohol;
  82.         try {
  83.             if(ProcentAlkohol<0 || ProcentAlkohol>1) {
  84.                 throw AlkoholException();
  85.             }
  86.  
  87.         } catch (AlkoholException &e) {
  88.             this->ProcentAlkohol=0;
  89.             e.showMessage();
  90.         }
  91.     }
  92.  
  93.     PaketVino(const PaketVino &p) {
  94.         this->ProcentAlkohol=p.ProcentAlkohol;
  95.     }
  96.     double getProcentAlokohol() {
  97.         return this->ProcentAlkohol;
  98.     }
  99.  
  100.  
  101.     double vratiMasa() {
  102.         double sum=(0.9+ProcentAlkohol);
  103.         return  PaketPijalok::vratiMasa()*sum;
  104.     }
  105.  
  106.     void pecati() {
  107.         cout<<"PaketVino"<<endl;
  108.         cout<<"quantity "<<getKolicina()<<", "<<ProcentAlkohol*100<<"% alcohol each of "<<volumenEden*density<<" l(dm3)"<<endl;
  109.     }
  110. };
  111. class Kamion {
  112. private:
  113.     char registration [25];
  114.     char driver[20];
  115.     int n;
  116.     ImaMasa**im;
  117. public:
  118.     Kamion (char*registration = "", char *driver="") {
  119.         if(!(isalpha (registration[0])&& isalpha(registration[6])&&isalpha(registration[7]))) {
  120.             throw Exception();
  121.         } else {
  122.             strcpy(this->registration,registration);
  123.             strcpy(this->driver,driver);
  124.             im=new ImaMasa*[0];
  125.             n=0;
  126.         }
  127.     }
  128.     void dodadiElement(ImaMasa*m) {
  129.         ImaMasa**tmp=new ImaMasa *[n+1];
  130.         for(int i = 0 ; i < n ; i++) {
  131.             tmp[i]=im[i];
  132.         }
  133.         tmp[n]=m;
  134.         n++;
  135.         delete[]im;
  136.         im=tmp;
  137.  
  138.     }
  139.     double vratiVkupnaMasa() {
  140.         double sum=0.0;
  141.         for(int i = 0 ; i < n ; i++) {
  142.             sum+=im[i]->vratiMasa();
  143.         }
  144.         return sum;
  145.     }
  146.     void pecati() {
  147.         cout<<"Truck with registration "<<registration<<" and driver "<<driver<<" transports: "<<endl;
  148.         for(int i=0; i<n; i++) {
  149.             im[i]->pecati();
  150.         }
  151.     }
  152.     Kamion pretovar(char *reg, char *ime) {
  153.         strcpy(registration,reg);
  154.         strcpy(driver,ime);
  155.         double maxMasa=0;
  156.         int index=-1;
  157.         for(int i=0; i<n; i++) {
  158.             if(im[i]->vratiMasa() > maxMasa) {
  159.                 maxMasa=im[i]->vratiMasa();
  160.                 index=i;
  161.             }
  162.         }
  163.         ImaMasa *p = im[index];
  164.         ImaMasa **tmp=new ImaMasa*[n-1];
  165.         int j=-1;
  166.         for(int i=0; i<n; i++) {
  167.             if(im[i] != p ) {
  168.                 j++;
  169.                 tmp[j]=im[i];
  170.             }
  171.  
  172.         }
  173.         delete [] im;
  174.         im=tmp;
  175.         this->n--;
  176.  
  177.         return *this;
  178.     }
  179.  
  180.  
  181.  
  182. };
  183. int main() {
  184.     char ime[20], reg[9];
  185.     double vol;
  186.     int kol;
  187.     bool g;
  188.     double proc;
  189.  
  190.  
  191.     try {
  192.         cin>>reg;
  193.         cin>>ime;
  194.         Kamion A(reg, ime);
  195.  
  196.  
  197.         ImaMasa **d = new ImaMasa*[5];
  198.         cin>>vol>>kol;
  199.         cin>>g;
  200.         d[0] = new PaketSok(vol, kol, g);
  201.         cin>>vol>>kol;
  202.         cin>>proc;
  203.         d[1] = new PaketVino(vol, kol, proc);
  204.         cin>>vol>>kol;
  205.         cin>>proc;
  206.         d[2] = new PaketVino(vol, kol, proc);
  207.         cin>>vol>>kol;
  208.         cin>>g;
  209.         d[3] = new PaketSok(vol, kol, g);
  210.         cin>>vol>>kol;
  211.         cin>>proc;
  212.         d[4] = new PaketVino(vol, kol, proc);
  213.  
  214.         A.dodadiElement(d[0]);
  215.         A.dodadiElement(d[1]);
  216.         A.dodadiElement(d[2]);
  217.         A.dodadiElement(d[3]);
  218.         A.dodadiElement(d[4]);
  219.         A.pecati();
  220.         cout<<"Total mass: "<<A.vratiVkupnaMasa()<<endl;
  221.         cin>>reg;
  222.         cin>>ime;
  223.         Kamion B = A.pretovar(reg, ime);
  224.         B.pecati();
  225.         cout<<"Total mass: "<<B.vratiVkupnaMasa()<<endl;
  226.  
  227.     } catch (Exception &e) {
  228.         e.showMessage();
  229.     }
  230.     return 0;
  231. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement