metalni

OOP Labs 9 Kamion

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